Skip to content

Commit

Permalink
Soluzione ese2
Browse files Browse the repository at this point in the history
  • Loading branch information
JIMMY authored and JIMMY committed Mar 21, 2018
1 parent 636fa0b commit 875a911
Show file tree
Hide file tree
Showing 21 changed files with 390 additions and 92 deletions.
1 change: 0 additions & 1 deletion Lab2_Alien/bin/.gitignore

This file was deleted.

22 changes: 0 additions & 22 deletions Lab2_Alien/src/it/polito/tdp/alien/Alien.fxml

This file was deleted.

55 changes: 0 additions & 55 deletions Lab2_Alien/src/it/polito/tdp/alien/AlienController.java

This file was deleted.

5 changes: 0 additions & 5 deletions Lab2_Alien/src/it/polito/tdp/alien/Test.java

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion Lab2_Alien/.project → Lab2_Alien_soluzione_Ese2/.project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lab2_Alien</name>
<name>Lab2_Alien_soluzione_Ese2</name>
<comment></comment>
<projects>
</projects>
Expand Down
69 changes: 69 additions & 0 deletions Lab2_Alien_soluzione_Ese2/bin/it/polito/tdp/alien/Alien.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.polito.tdp.alien.AlienController">
<top>
<Label text="Lab 1 - Alien Translator" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="10.0" />
</BorderPane.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
</top>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<center>
<VBox alignment="CENTER" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER_LEFT" spacing="10.0">
<children>
<Label text="Insert Text: " />
<TextField fx:id="lblTesto" promptText="Insert the text here.." />
<Button fx:id="btnTranslate" mnemonicParsing="false" onAction="#doTranslate" text="Translate" />
<ImageView fitHeight="82.0" fitWidth="150.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../rsc/Alien-512.png" />
</image>
<HBox.margin>
<Insets left="15.0" />
</HBox.margin>
</ImageView>
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets bottom="15.0" />
</VBox.margin>
</HBox>
<TextArea fx:id="txtResult" promptText="Welcome to the AlienDictionary v2016...">
<VBox.margin>
<Insets />
</VBox.margin></TextArea>
</children>
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</VBox>
</center>
<bottom>
<Button fx:id="btnReset" mnemonicParsing="false" onAction="#doReset" text="Reset" BorderPane.alignment="CENTER_RIGHT">
<BorderPane.margin>
<Insets bottom="10.0" right="15.0" top="20.0" />
</BorderPane.margin></Button>
</bottom>
</BorderPane>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes
69 changes: 69 additions & 0 deletions Lab2_Alien_soluzione_Ese2/src/it/polito/tdp/alien/Alien.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.polito.tdp.alien.AlienController">
<top>
<Label text="Lab 1 - Alien Translator" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="10.0" />
</BorderPane.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
</top>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<center>
<VBox alignment="CENTER" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER_LEFT" spacing="10.0">
<children>
<Label text="Insert Text: " />
<TextField fx:id="lblTesto" promptText="Insert the text here.." />
<Button fx:id="btnTranslate" mnemonicParsing="false" onAction="#doTranslate" text="Translate" />
<ImageView fitHeight="82.0" fitWidth="150.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../rsc/Alien-512.png" />
</image>
<HBox.margin>
<Insets left="15.0" />
</HBox.margin>
</ImageView>
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets bottom="15.0" />
</VBox.margin>
</HBox>
<TextArea fx:id="txtResult" promptText="Welcome to the AlienDictionary v2016...">
<VBox.margin>
<Insets />
</VBox.margin></TextArea>
</children>
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</VBox>
</center>
<bottom>
<Button fx:id="btnReset" mnemonicParsing="false" onAction="#doReset" text="Reset" BorderPane.alignment="CENTER_RIGHT">
<BorderPane.margin>
<Insets bottom="10.0" right="15.0" top="20.0" />
</BorderPane.margin></Button>
</bottom>
</BorderPane>
117 changes: 117 additions & 0 deletions Lab2_Alien_soluzione_Ese2/src/it/polito/tdp/alien/AlienController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package it.polito.tdp.alien;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class AlienController {

private AlienDictionary alienDictionary = new AlienDictionary();

@FXML
private ResourceBundle resources;

@FXML
private URL location;

@FXML
private TextField lblTesto;

@FXML
private Button btnTranslate;

@FXML
private TextArea txtResult;

@FXML
private Button btnReset;

@FXML
void doReset(ActionEvent event) {
lblTesto.clear();
txtResult.clear();
alienDictionary.resetDictionary();
}

@FXML
void doTranslate(ActionEvent event) {

txtResult.clear();
String riga = lblTesto.getText().toLowerCase();

// Controllo sull'input
if (riga == null || riga.length() == 0) {
txtResult.setText("Inserire una o due parole.");
return;
}

StringTokenizer st = new StringTokenizer(riga, " ");

// Controllo su String Tokenizer (superfluo)
if (!st.hasMoreElements()) {
txtResult.setText("Inserire una o due parole.");
return;
}

// Estraggo la prima parola
String alienWord = st.nextToken();

if (st.hasMoreTokens()) {
// Devo inserire nel dizionario

// Estraggo la seconda parola
String translation = st.nextToken();

if (!alienWord.matches("[a-zA-Z]*") || !translation.matches("[a-zA-Z]*")) {
txtResult.setText("Inserire solo caratteri alfabetici.");
return;
}

// Aggiungo la parola aliena e traduzione nel dizionario
alienDictionary.addWord(alienWord, translation);

txtResult.setText("La parola: " + alienWord + " traduzione: " + translation + " è stata inserita.");

} else {

// Controllo che non ci siano caratteri non ammessi
if (!alienWord.matches("[a-zA-Z?]*")) {
txtResult.setText("Inserire solo caratteri alfabetici.");
return;
}

String translation;

if (alienWord.matches("[a-zA-Z?]*") && !alienWord.matches("[a-zA-Z]*")) {

// Traduzione con WildCard
translation = alienDictionary.translateWordWildCard(alienWord);

} else {

// Traduzione classica
translation = alienDictionary.translateWord(alienWord);
}

if (translation != null) {
txtResult.setText(translation);
} else {
txtResult.setText("La parola cercata non esiste nel dizionario.");
}
}
}

@FXML
void initialize() {
assert lblTesto != null : "fx:id=\"lblTesto\" was not injected: check your FXML file 'Alien.fxml'.";
assert btnTranslate != null : "fx:id=\"btnTranslate\" was not injected: check your FXML file 'Alien.fxml'.";
assert txtResult != null : "fx:id=\"txtResult\" was not injected: check your FXML file 'Alien.fxml'.";
assert btnReset != null : "fx:id=\"btnReset\" was not injected: check your FXML file 'Alien.fxml'.";
}
}
Loading

0 comments on commit 875a911

Please sign in to comment.