diff --git a/README.md b/README.md index 862217fd..5b4ab78a 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,5 @@ http://codemetropolis.github.io/CodeMetropolis/ 1. navigate to `sources` folder 1. `mvn clean package` 1. The current distribution will be aviable under `source/distro`. + +## Demo \ No newline at end of file diff --git a/sources/gui/src/main/java/codemetropolis/toolchain/gui/CodeMetropolisGUI.java b/sources/gui/src/main/java/codemetropolis/toolchain/gui/CodeMetropolisGUI.java index 7ef2ae8a..f331f813 100644 --- a/sources/gui/src/main/java/codemetropolis/toolchain/gui/CodeMetropolisGUI.java +++ b/sources/gui/src/main/java/codemetropolis/toolchain/gui/CodeMetropolisGUI.java @@ -6,8 +6,13 @@ import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; import java.io.File; import java.io.PipedOutputStream; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Paths; import java.util.Random; import javax.swing.ImageIcon; @@ -58,6 +63,9 @@ public class CodeMetropolisGUI extends JFrame { private CMSpinner scaleSpinner; private CMComboBox layoutSelector; + private static final Color colorGreen = Color.decode(Translations.t("color_green")); + private static final Color colorRed = Color.decode(Translations.t("color_red")); + /** * Instantiates the CodeMetropolis GUI. * @@ -96,6 +104,52 @@ public void initFields() { } } + public boolean validatePath(String path) { + if (path.isEmpty()) + return false; + try { + return Files.exists(Paths.get(path)); + } catch (InvalidPathException | NullPointerException ex) { + return false; + } + } + + /** + * Function for checking the mapping path field, if the file is correct, the field turns green, otherwise it turns red. + * + * @return Return with focuslistener. + */ + public FocusListener getFocusListener() { + FocusListener focusListener = new FocusListener() { + public void focusGained(FocusEvent e) { + try { + mappingPath = (CMTextField) e.getSource(); + if(!(validatePath(mappingPath.getText()) && (mappingPath.getText().endsWith(".xml"))) || mappingPath.getText().isEmpty()) { + mappingPath.setBackground(colorRed); + } else { + mappingPath.setBackground(colorGreen); + } + } catch (ClassCastException ignored) { + GuiUtils.showError(Translations.t("gui_err_unexpected_err")); + } + } + + public void focusLost(FocusEvent e) { + try { + mappingPath = (CMTextField) e.getSource(); + if(!(validatePath(mappingPath.getText()) && (mappingPath.getText().endsWith(".xml"))) || mappingPath.getText().isEmpty()) { + mappingPath.setBackground(colorRed); + } else { + mappingPath.setBackground(colorGreen); + } + } catch (ClassCastException ignored) { + GuiUtils.showError(Translations.t("gui_err_unexpected_err")); + } + } + }; + return focusListener; + } + /** * Creates the base panel for the CodeMetropolis GUI. * @@ -202,6 +256,7 @@ private final void addMappingOptions(JPanel panel) { mappingPath = new CMTextField(145, 555, 235, 30); CMButton mappingBrowse = new CMButton(Translations.t("gui_b_browse"), 385, 555, 100, 30); mappingBrowse.addActionListener(new BrowseListener(mappingPath, JFileChooser.FILES_ONLY, XML_FILTER)); + mappingPath.addFocusListener(getFocusListener()); CMLabel scaleLabel = new CMLabel(Translations.t("gui_l_scale"), 15, 590, 120, 30); scaleSpinner = new CMSpinner(145, 590, 120, 30); diff --git a/sources/gui/src/main/java/codemetropolis/toolchain/gui/components/listeners/BrowseListener.java b/sources/gui/src/main/java/codemetropolis/toolchain/gui/components/listeners/BrowseListener.java index 9e4776d7..49cce755 100644 --- a/sources/gui/src/main/java/codemetropolis/toolchain/gui/components/listeners/BrowseListener.java +++ b/sources/gui/src/main/java/codemetropolis/toolchain/gui/components/listeners/BrowseListener.java @@ -40,6 +40,7 @@ public BrowseListener(CMTextField fileNameTextField, int fileSelectionMode, File public void actionPerformed(ActionEvent event) { if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { fileNameTextField.setText(fileChooser.getSelectedFile().getAbsolutePath()); + fileNameTextField.requestFocus(); } } diff --git a/sources/gui/src/main/resources/translations.properties b/sources/gui/src/main/resources/translations.properties index f56865a2..b7f61c03 100644 --- a/sources/gui/src/main/resources/translations.properties +++ b/sources/gui/src/main/resources/translations.properties @@ -61,3 +61,7 @@ gui_err_sm_run_failed = Failed to run SourceMeter! gui_err_unexpected_err = Unexpected error occured! gui_err_unhandled_metric_source = Unhandled metric source! gui_err_world_gen_failed = World generation failed! Check logs for details! + +# Colors +color_green = 0xA5FFB4 +color_red = 0xFF9C9C