diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/MetaDataEditorController.java b/simpleexample2/fxui/src/main/java/simpleex/ui/MetaDataEditorController.java index 17bd9464f638a587896b0e54092132d38f0a60ca..bbd168a6873ab63a8eb58d25a37cb6c4fd29cc57 100644 --- a/simpleexample2/fxui/src/main/java/simpleex/ui/MetaDataEditorController.java +++ b/simpleexample2/fxui/src/main/java/simpleex/ui/MetaDataEditorController.java @@ -5,143 +5,224 @@ import java.util.Collection; import java.util.Iterator; import javafx.beans.property.SimpleStringProperty; +import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; +import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.event.EventType; import javafx.fxml.FXML; +import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; -import javafx.scene.control.TableColumn.CellDataFeatures; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; -import javafx.util.Callback; import javafx.util.Pair; import simpleex.core.LatLong; import simpleex.core.MetaData; import simpleex.ui.tags.TagsBar; - +/* + * Controller class for the location metadata editor + */ public class MetaDataEditorController { - + /* + * Generic metadata event + */ public static EventType<MetaDataEvent> OPTIONS_ALL = new EventType<>("OPTIONS_ALL"); + + /* + * Specific metadata event dispatched when the metadata is saved + */ public static EventType<MetaDataEvent> METADATA_SAVED = new EventType<>(OPTIONS_ALL, "METADATA_SAVED"); private LatLong latlong; @FXML private BorderPane rootContainer; - - @FXML - private Button saveButton; - - @FXML - private Button cancelButton; + + @FXML + private VBox centerVBox; - @FXML - private VBox centerVBox; + @FXML + private Button saveButton; - @FXML - private TextField nameInput; + @FXML + private Button cancelButton; + + @FXML + private TextField nameInput; - @FXML - private TextArea descriptionInput; + @FXML + private TextArea descriptionInput; - @FXML - private TableView<Pair<String,String>> propertiesTableView; + @FXML + private TableView<Pair<String,String>> propertiesTableView; - @FXML - private TableColumn<Pair<String, String>, String> propertyNamesColumn; + @FXML + private TableColumn<Pair<String, String>, String> propertyNamesColumn; - @FXML - private TableColumn<Pair<String, String>, String> propertyValuesColumn; + @FXML + private TableColumn<Pair<String, String>, String> propertyValuesColumn; - @FXML - private TagsBar tagsBar; - - @FXML - private Label coordinatesLabel; + @FXML + private TextField newValueInput; + + @FXML + private TextField newKeyInput; + + @FXML + private Button buttonAddUpdate; - @FXML - private void initialize() { - propertyNamesColumn.setCellValueFactory( - cellData -> new SimpleStringProperty( - ((Pair<String, String>) (cellData.getValue())).getKey() - )); - - propertyValuesColumn.setCellValueFactory( - cellData -> new SimpleStringProperty( - ((Pair<String, String>) (cellData.getValue())).getValue() - )); - } + @FXML + private Button buttonDelete; + + @FXML + private TagsBar tagsBar; + @FXML + private Label coordinatesLabel; + + @FXML + private void initialize() { + propertyNamesColumn.setCellValueFactory( + cellData -> new SimpleStringProperty( + ((Pair<String, String>) (cellData.getValue())).getKey() + )); - @FXML - void onCancel(ActionEvent event) { - closeDialog(event); - } - - @FXML - void onLocationName(ActionEvent event) { - + propertyValuesColumn.setCellValueFactory( + cellData -> new SimpleStringProperty( + ((Pair<String, String>) (cellData.getValue())).getValue() + )); + propertiesTableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Pair<String, String>>() { + + @Override + public void changed(ObservableValue<? extends Pair<String, String>> observable, Pair<String, String> oldValue, Pair<String, String> newValue) { + if(newValue == null) { + buttonAddUpdate.setDisable(false); + buttonDelete.setDisable(true); + } else { + buttonAddUpdate.setDisable(false); + buttonDelete.setDisable(false); + newKeyInput.setText(newValue.getKey()); + newValueInput.setText(newValue.getValue()); + } + } + }); + } + + @FXML + void onCancel(ActionEvent event) { + closeDialog(event); + } + + @FXML + void onLocationName(ActionEvent event) { + + } + + void closeDialog(ActionEvent event) { + Stage stage = (Stage) ((Button) event.getSource()).getScene().getWindow(); + stage.close(); + } + + @FXML + void onSave(ActionEvent event) { + latlong.getMetaData().setProperty(MetaData.NAME_PROPERTY, nameInput.getText()); + latlong.getMetaData().setProperty(MetaData.DESCRIPTION_PROPERTY, descriptionInput.getText()); + + ObservableList<Pair<String, String>> props = propertiesTableView.getItems(); + final Iterator<String> propertyNames = latlong.getMetaData().propertyNames(); + if (propertyNames.hasNext()) { + while(propertyNames.hasNext()) { + final String propName = propertyNames.next(); + if((propName==MetaData.NAME_PROPERTY)||(propName==MetaData.DESCRIPTION_PROPERTY)) { + continue; + } else { + latlong.getMetaData().removeProperty(propName); + } + } + } + for (Pair<String, String> pair : props) { + latlong.getMetaData().setProperty(pair.getKey(), pair.getValue()); } - void closeDialog(ActionEvent event) { - Stage stage = (Stage) ((Button) event.getSource()).getScene().getWindow(); - stage.close(); - } - - @FXML - void onSave(ActionEvent event) { - latlong.getMetaData().setProperty(MetaData.NAME_PROPERTY, nameInput.getText()); - latlong.getMetaData().setProperty(MetaData.DESCRIPTION_PROPERTY, descriptionInput.getText()); - Event.fireEvent(((Button) event.getSource()).getScene().getWindow(), - new MetaDataEvent(METADATA_SAVED)); - closeDialog(event); - - } - - void setLatLong(LatLong latLong) { - this.latlong = latLong; - updateUi(); + final ObservableList<Node> tags = tagsBar.getTags(); + latlong.getMetaData().setTags(); + for (Node node : tags) { + latlong.getMetaData().addTags(((TagsBar.Tag)node).getTag()); } + + Event.fireEvent(((Button) event.getSource()).getScene().getWindow(), + new MetaDataEvent(METADATA_SAVED)); + closeDialog(event); + } + + void setLatLong(LatLong latLong) { + this.latlong = latLong; + updateUi(); + } private void updateUi() { nameInput.setText(latlong.getMetaData().getProperty(MetaData.NAME_PROPERTY)); descriptionInput.setText(latlong.getMetaData().getProperty(MetaData.DESCRIPTION_PROPERTY)); coordinatesLabel.setText(latlong.toString()); - MetaData metaData = latlong.getMetaData(); - final Iterator<String> propertyNames = metaData.propertyNames(); - Collection<Pair<String, String>> locationProperties = new ArrayList<Pair<String,String>>(); - if( propertyNames.hasNext()) { - while(propertyNames.hasNext()) { - final String propertyName = propertyNames.next(); - if ((propertyName == MetaData.NAME_PROPERTY) || - (propertyName == MetaData.DESCRIPTION_PROPERTY)) { - continue; - } else { - Pair<String,String> p = new Pair<String, String>(propertyName, metaData.getProperty(propertyName)); - locationProperties.add(p); - } - } - propertiesTableView.getItems().addAll(locationProperties); - } - - final Iterator<String> tags = metaData.tags(); - if(tags.hasNext()) { - while(tags.hasNext()) { - final String tag = tags.next(); - tagsBar.addTag(tag); - } - } - + updatePropertiesTable(); + updateTags(); } - + @FXML + public void onAddUpdateProperty(ActionEvent event) { + final String newKey = newKeyInput.getText(); + final String newValue = newValueInput.getText(); + if(!newKey.isBlank() && (newKey != MetaData.NAME_PROPERTY) + && (newKey != MetaData.DESCRIPTION_PROPERTY)) { + latlong.getMetaData().setProperty(newKey, newValue); + updatePropertiesTable(); + } + } + + private void updatePropertiesTable() { + MetaData metaData = latlong.getMetaData(); + final Iterator<String> propertyNames = metaData.propertyNames(); + Collection<Pair<String, String>> locationProperties = new ArrayList<Pair<String,String>>(); + if( propertyNames.hasNext()) { + while(propertyNames.hasNext()) { + final String propertyName = propertyNames.next(); + if ((propertyName == MetaData.NAME_PROPERTY) || + (propertyName == MetaData.DESCRIPTION_PROPERTY)) { + continue; + } else { + Pair<String,String> p = new Pair<String, String>(propertyName, metaData.getProperty(propertyName)); + locationProperties.add(p); + } + } + propertiesTableView.getItems().clear(); + propertiesTableView.getItems().addAll(locationProperties); + } + } + + private void updateTags() { + tagsBar.clearAllTags(); + MetaData metaData = latlong.getMetaData(); + final Iterator<String> tags = metaData.tags(); + if(tags.hasNext()) { + while(tags.hasNext()) { + final String tag = tags.next(); + tagsBar.addTag(tag); + } + } + } + + @FXML + public void onDeleteProperty(ActionEvent event) { + int selectedIndex = propertiesTableView.getSelectionModel().getSelectedIndex(); + propertiesTableView.getItems().remove(selectedIndex); + } } diff --git a/simpleexample2/fxui/src/main/resources/simpleex/ui/MetaDataEditor.fxml b/simpleexample2/fxui/src/main/resources/simpleex/ui/MetaDataEditor.fxml index adaae662aa305bc22842555f72d57a897edc4863..3bc0351c73dfbf51b414ca641bb41c09634a544a 100644 --- a/simpleexample2/fxui/src/main/resources/simpleex/ui/MetaDataEditor.fxml +++ b/simpleexample2/fxui/src/main/resources/simpleex/ui/MetaDataEditor.fxml @@ -36,7 +36,8 @@ </children> </HBox> <Label text="Description:" /> - <TextArea fx:id="descriptionInput" prefHeight="68.0" prefWidth="580.0" promptText="enter a description for this location" /> + <TextArea fx:id="descriptionInput" prefHeight="68.0" prefWidth="580.0" + promptText="enter a description for this location" wrapText="true"/> <Label text="Custom properties:" /> <TableView fx:id="propertiesTableView" prefHeight="120.0" prefWidth="580.0"> <columns> @@ -44,6 +45,14 @@ <TableColumn fx:id="propertyValuesColumn" prefWidth="400.0" text="Value" /> </columns> </TableView> + <HBox prefWidth="580" spacing="3.0"> + <children> + <TextField fx:id="newKeyInput" promptText="Property name"/> + <TextField fx:id="newValueInput" promptText="Property value"/> + <Button fx:id="buttonAddUpdate" onAction="#onAddUpdateProperty" text="Add / Update"/> + <Button fx:id="buttonDelete" onAction="#onDeleteProperty" text="Delete" disable="true"/> + </children> + </HBox> <Label text="Tags:" /> <TagsBar fx:id="tagsBar" /> </children>