Commit 53e001a2 authored by Ludwig Dickmanns's avatar Ludwig Dickmanns

Auto-Layout: Configuration.

* ConfigurationGridPane: Two-columned GridPane to configure, load and
save Properties.
* ElkConfigurationGridPane: Two-columned GridPane to configure, load and
save Properties for ElkAutoLayouter.

Issue-Ref: 3872
Issue-Url: https://af3-developer.fortiss.org/issues/3872Signed-off-by: Ludwig Dickmanns's avatarLudwig Dickmanns <dickmanns@fortiss.org>
parent 67cbbd46
/*-------------------------------------------------------------------------+
| Copyright 2020 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.ui.layout.auto.elk.configuration;
import static javafx.scene.control.Alert.AlertType.INFORMATION;
import static javafx.scene.control.Alert.AlertType.WARNING;
import static org.fortiss.tooling.base.ui.layout.auto.elk.configuration.ConfigurationUtils.readPropertiesFromFile;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.function.Predicate;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
/**
* Two-columned {@link GridPane} to configure, load and save {@link Properties}.
*
* @author dickmanns
*/
public abstract class ConfigurationGridPane {
/** String for load {@link Button}. */
protected static final String LOAD_BUTTON = "Load";
/** String for save {@link Button}. */
protected static final String SAVE_BUTTON = "Save";
/** {@link Predicate} to check whether the text of a {@link TextField} is an int. */
protected static final Predicate<TextField> IS_INT = ConfigurationUtils::isTextFieldTextInt;
/** {@link Predicate} to check whether the text of a {@link TextField} is a double. */
protected static final Predicate<TextField> IS_DOUBLE =
ConfigurationUtils::isTextFieldTextDouble;
/** Constructor. */
public ConfigurationGridPane() {
setupButtons();
setupGridPane();
createGridPane();
}
/** The {@link GridPane} for the configuration fo the properties. */
protected GridPane gridPane;
/** The Map for all options, which are configured with a {@link TextField}s. */
protected Map<String, TextField> keyTextFieldMap;
/** Button to load {@link Properties} from a {@link File}. */
protected final Button loadButton = new Button(LOAD_BUTTON);
/** Button to save {@link Properties} to a {@link File}. */
protected final Button saveButton = new Button(SAVE_BUTTON);
/** Returns the name of the {@link ConfigurationGridPane}. */
protected abstract String getName();
/** Returns the preferred column width for the key column. */
protected abstract int getKeyColumnWidth();
/** Returns the preferred column width for the value column. */
public abstract int getValuesColumnWidth();
/** Returns <code>true</code> if the {@link ConfigurationGridPane} is filled correctly. */
public abstract boolean isFilledCorrectly();
/**
* Use {@link #isFilledCorrectly()} before usage.
*
* @return The configured {@link Properties}.
*/
public abstract Properties createProperties();
/**
* Creates the {@link ConfigurationGridPane}. Here {@link Label}s for keys and entry field for
* the
* values are added to the {@link GridPane}.
*/
protected abstract void createGridPane();
/** Initializes the {@link GridPane}. */
private final void setupGridPane() {
gridPane = new GridPane();
ColumnConstraints keyColumnCC = new ColumnConstraints(getKeyColumnWidth());
ColumnConstraints valueColumnCC = new ColumnConstraints(getValuesColumnWidth());
gridPane.getColumnConstraints().addAll(keyColumnCC, valueColumnCC);
}
/** Fills the {@link GridPane} from {@link Properties}. */
public abstract void readValuesFromProperties(Properties properties);
/** Returns a {@link Label} with {@link ConfigurationGridPane#getName()}. */
public final Label getLabel() {
return new Label(getName());
}
/** Returns node containing the gridPane. */
public Node getNode() {
return gridPane;
}
/** Returns keyTextFieldMap. */
public Map<String, TextField> getKeyTextFieldMap() {
return keyTextFieldMap;
}
/** Returns loadButton. */
public Button getLoadButton() {
return loadButton;
}
/** Returns saveButton. */
public Button getSaveButton() {
return saveButton;
}
/** Initializes the {@link Button}s. */
protected final void setupButtons() {
loadButton.setOnAction(event -> {
Properties properties = readPropertiesFromFile();
readValuesFromProperties(properties);
Alert alert = new Alert(INFORMATION);
alert.setContentText("Inputs loaded successfully!");
alert.show();
});
saveButton.setOnAction(event -> {
Alert alert;
if(isFilledCorrectly()) {
FileChooser fc = new FileChooser();
fc.setInitialDirectory(new File(System.getProperty("user.dir")).getParentFile());
fc.setTitle("Save properties configuration - " + getName());
ExtensionFilter prop = new ExtensionFilter("Properties", "*.properties", "*.prop");
ExtensionFilter all = new ExtensionFilter("All files", "*");
fc.getExtensionFilters().addAll(prop, all);
Properties properties = createProperties();
File file = fc.showSaveDialog(null);
if(file != null) {
try {
FileWriter fw = new FileWriter(file);
properties.store(fw, null);
alert = new Alert(INFORMATION);
alert.setContentText("Inputs stored successfully!");
alert.show();
} catch(IOException e) {
alert = new Alert(WARNING);
alert.setContentText("Error while storing the inputs!");
alert.show();
e.printStackTrace();
}
} else {
alert = new Alert(INFORMATION);
alert.setContentText("Storing of inputs was canceled!");
alert.show();
}
} else {
alert = new Alert(WARNING);
alert.setContentText("Inputs are not set correctly!");
alert.show();
}
});
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment