Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLUZIONE LAB 02 #1

Open
wants to merge 3 commits into
base: soluzione-ese1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lab2_Alien/.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_mio</name>
<comment></comment>
<projects>
</projects>
Expand Down
44 changes: 43 additions & 1 deletion Lab2_Alien/src/it/polito/tdp/alien/Alien.fxml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?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 prefHeight="343.0" prefWidth="513.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.polito.tdp.alien.AlienController">
<BorderPane prefHeight="438.0" prefWidth="513.0" xmlns="http://javafx.com/javafx/8.0.141" 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>
Expand All @@ -19,4 +26,39 @@
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<center>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
<children>
<Label text="Alien Text" />
<TextField fx:id="txtWord" />
<Button fx:id="btnTranslate" mnemonicParsing="false" onAction="#doTranslate" text="Translate" />
<ImageView fitHeight="100.0" fitWidth="103.0">
<image>
<Image url="@../../../../../rsc/Alien-512.png" />
</image>
<HBox.margin>
<Insets bottom="5.0" />
</HBox.margin>
</ImageView>
</children>
<opaqueInsets>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</opaqueInsets>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
<HBox prefHeight="241.0" prefWidth="493.0">
<children>
<TextArea fx:id="txtResult" editable="false" prefHeight="259.0" prefWidth="493.0" promptText="Inserisci una parola aliena oppure una parola aliena e la rispettiva traduzione per aggiungerla al dizionario" />
</children>
</HBox>
</children>
</VBox>
</center>
<bottom>
<Button fx:id="btnReset" mnemonicParsing="false" onAction="#doReset" text="Clear Text" BorderPane.alignment="BOTTOM_RIGHT" />
</bottom>
</BorderPane>
162 changes: 107 additions & 55 deletions Lab2_Alien/src/it/polito/tdp/alien/AlienController.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,107 @@
package it.polito.tdp.alien;

/**
* Sample Skeleton for 'Alien.fxml' Controller Class
*/



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

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 {

@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TextField txtWord;
@FXML
private TextArea txtResult;
@FXML
private Button btnTranslate;
@FXML
private Button btnReset;


@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert txtWord != null : "fx:id=\"txtWord\" 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 btnTranslate != null : "fx:id=\"bntTranslate\" was not injected: check your FXML file 'Alien.fxml'.";
assert btnReset != null : "fx:id=\"btnReset\" was not injected: check your FXML file 'Alien.fxml'.";

}


@FXML
void doTranslate(ActionEvent event) {

}


@FXML
void doReset(ActionEvent event) {

}

}
/**
* Sample Skeleton for 'Alien.fxml' Controller Class
*/

package it.polito.tdp.alien;

import java.net.URL;
import java.util.ResourceBundle;
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 {

AlienDictionary dizionario = new AlienDictionary();

@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;

@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;

@FXML // fx:id="txtWord"
private TextField txtWord; // Value injected by FXMLLoader

@FXML // fx:id="btnTranslate"
private Button btnTranslate; // Value injected by FXMLLoader

@FXML // fx:id="txtResult"
private TextArea txtResult; // Value injected by FXMLLoader

@FXML // fx:id="btnReset"
private Button btnReset; // Value injected by FXMLLoader

@FXML
void doReset(ActionEvent event) {

txtResult.clear();

}

@FXML
void doTranslate(ActionEvent event) {
/*
String s = txtWord.getText().toLowerCase().trim();
String v[] = s.split(" ");

if(v.length == 2) {
dizionario.addWord(v[0], v[1]);
txtResult.appendText("La traduzione � stata aggiunta \n");

}

if(v.length == 1) {
if(dizionario.translateWord(v[0]) == null) {
txtResult.appendText("Parola non torvata !!! \n");}
else {
txtResult.appendText("La traduzione � " +dizionario.translateWord(v[0])+"\n");
}

}
if(v.length == 0 || v.length>2) {
txtResult.appendText("Formato errato \n");}



txtWord.clear(); */

//con wordEnhanced
String s = txtWord.getText().toLowerCase().trim();
String v[] = s.split(" ");

if(v.length == 2) {
dizionario.addWord(v[0], v[1]);
txtResult.appendText("La traduzione � stata aggiunta \n");

}

if(v.length == 1) {
if(dizionario.translateWord(v[0]) == null) {
txtResult.appendText("Parola non torvata !!! \n");}
else {
txtResult.appendText("La traduzione � " +dizionario.translateWord(v[0])+"\n");
}

}
if(v.length == 0 || v.length>2) {
txtResult.appendText("Formato errato \n");}



txtWord.clear();


}

@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert txtWord != null : "fx:id=\"txtWord\" 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'.";

}
}
68 changes: 68 additions & 0 deletions Lab2_Alien/src/it/polito/tdp/alien/AlienDictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package it.polito.tdp.alien;

import java.util.*;

public class AlienDictionary {

//TreeMap<String, Word> mappaTraduzioni;
TreeMap<String, WordEnhanced> mt;

public AlienDictionary() {

//mappaTraduzioni = new TreeMap<String, Word>();
mt = new TreeMap<String, WordEnhanced>();
}

public void addWord(String alienWord, String translation) {

/*Word w = new Word(alienWord, translation);

//se contiene la parola alieno la sostituisce con nuova traduzione
if(mappaTraduzioni.containsKey(alienWord)) {
mappaTraduzioni.replace(alienWord, w);}
else { //altrimenti aggiunge la nuova parola
mappaTraduzioni.put(alienWord, w);}

*/

//soluzione con wordEnhanced

WordEnhanced w = new WordEnhanced(alienWord);

if(mt.containsKey(alienWord)) {
mt.get(alienWord).addTraduzioni(translation);
}
else {
w.addTraduzioni(translation);
mt.put(alienWord, w);
}

}

public String translateWord(String alienWord) {
//restituisce la traduzione di una parola alieno
/*
if(mappaTraduzioni.containsKey(alienWord))
return mappaTraduzioni.get(alienWord).toString();

return null;*/


//con wordEnhanced
String risultato = "";

if(!mt.containsKey(alienWord)) {
return null;
}

if(mt.containsKey(alienWord)) {
for(String a : mt.get(alienWord).traduzioni) {
risultato = risultato +a +", ";
}
}

return risultato;

}

}
9 changes: 5 additions & 4 deletions Lab2_Alien/src/it/polito/tdp/alien/Main.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package it.polito.tdp.alien;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;



public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
try {

BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Alien.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
e.printStackTrace();
}
}

Expand Down
46 changes: 46 additions & 0 deletions Lab2_Alien/src/it/polito/tdp/alien/Word.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package it.polito.tdp.alien;

public class Word {

private String alienWord;
private String translation;

public Word(String alienWord, String translation) {
//super();
this.alienWord = alienWord;
this.translation = translation;
}

public String getAlienWord() {
return alienWord;
}

public void setAlienWord(String alienWord) {
this.alienWord = alienWord;
}

public String getTranslation() {
return translation;
}

public void setTranslation(String translation) {
this.translation = translation;
}

@Override
public String toString() {
return " AlienWord = " + alienWord + ", Translation = " + translation;
}

public boolean equals(String s) { //se la parola aliena � gia presente
if(alienWord.equals(s)) { // ritorna false e la sostituisce
//this.alienWord = s;
return false;
}

return true;

}


}
Loading