Skip to content
Draft
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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +63,9 @@ public class CodeMetropolisGUI extends JFrame {
private CMSpinner scaleSpinner;
private CMComboBox<LayoutAlgorithm> 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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
4 changes: 4 additions & 0 deletions sources/gui/src/main/resources/translations.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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