summaryrefslogtreecommitdiff
path: root/sources/prefs
diff options
context:
space:
mode:
Diffstat (limited to 'sources/prefs')
-rw-r--r--sources/prefs/editors/VSColorChooser.java36
-rw-r--r--sources/prefs/editors/VSEditor.java488
-rw-r--r--sources/prefs/editors/VSEditorFrame.java225
-rw-r--r--sources/prefs/editors/VSProcessEditor.java154
-rw-r--r--sources/prefs/editors/VSProtocolEditor.java335
-rw-r--r--sources/prefs/editors/VSSimulationEditor.java60
6 files changed, 1298 insertions, 0 deletions
diff --git a/sources/prefs/editors/VSColorChooser.java b/sources/prefs/editors/VSColorChooser.java
new file mode 100644
index 0000000..8b15eaa
--- /dev/null
+++ b/sources/prefs/editors/VSColorChooser.java
@@ -0,0 +1,36 @@
+package prefs.editors;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.event.*;
+import javax.swing.colorchooser.*;
+
+import prefs.VSPrefs;
+
+public class VSColorChooser extends JPanel implements ChangeListener {
+ protected JColorChooser colorChooser;
+ private Color color;
+ private JTextField valField;
+ private VSPrefs prefs;
+
+ public VSColorChooser(VSPrefs prefs, JTextField valField) {
+ super(new BorderLayout());
+ this.prefs = prefs;
+ this.color = valField.getBackground();
+ this.valField = valField;
+
+ colorChooser = new JColorChooser(Color.yellow);
+ colorChooser.setColor(color);
+ colorChooser.getSelectionModel().addChangeListener(this);
+ colorChooser.setBorder(BorderFactory.createTitledBorder(
+ prefs.getString("lang.colorchooser2")));
+ add(colorChooser, BorderLayout.CENTER);
+ }
+
+ public void stateChanged(ChangeEvent e) {
+ Color newColor = colorChooser.getColor();
+ valField.setBackground(newColor);
+ valField.repaint();
+ }
+}
diff --git a/sources/prefs/editors/VSEditor.java b/sources/prefs/editors/VSEditor.java
new file mode 100644
index 0000000..df77433
--- /dev/null
+++ b/sources/prefs/editors/VSEditor.java
@@ -0,0 +1,488 @@
+package prefs.editors;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.border.*;
+import javax.swing.filechooser.*;
+import java.util.*;
+import java.io.File;
+
+import simulator.*;
+import utils.*;
+import prefs.VSPrefs;
+
+abstract class VSEditor implements ActionListener {
+ protected int prefsCategory;
+ private HashMap<String,JComboBox> integerFields;
+ private HashMap<String,JTextField> colorFields;
+ private HashMap<String,JTextField> floatFields;
+ private HashMap<String,JTextField> longFields;
+ private HashMap<String,JCheckBox> booleanFields;
+ private HashMap<String,JTextField> stringFields;
+ private Vector<String> colorKeys;
+ private Vector<String> floatKeys;
+ private Vector<String> integerKeys;
+ private Vector<String> longKeys;
+ private Vector<String> booleanKeys;
+ private Vector<String> stringKeys;
+ protected JPanel buttonPanel;
+ protected JPanel editPanel;
+ protected VSPrefs prefs;
+ protected VSPrefs prefsToEdit;
+ public static final int ALL_PREFERENCES = 0;
+ public static final int SIMULATION_PREFERENCES = 1;
+ protected GridBagConstraints editPanelConstraints;
+ protected int editPanelRow;
+ protected Insets insetsTopSpaceing = new Insets(15, 0, 0, 0);
+ protected Insets insets = new Insets(0, 0, 0, 0);
+
+ public VSEditor(VSPrefs prefs, VSPrefs prefsToEdit) {
+ init(prefs, prefsToEdit, SIMULATION_PREFERENCES);
+ }
+
+ public VSEditor(VSPrefs prefs, VSPrefs prefsToEdit, int prefsCategory) {
+ init(prefs, prefsToEdit, prefsCategory);
+ }
+
+ private void init(VSPrefs prefs, VSPrefs prefsToEdit, int prefsCategory) {
+ this.prefs = prefs;
+ this.prefsToEdit = prefsToEdit;
+ this.prefsCategory = prefsCategory;
+
+ final String keyStartsWith = "sim.";
+ boolean reversed;
+
+ switch (prefsCategory) {
+ case SIMULATION_PREFERENCES:
+ reversed = false;
+ break;
+ default:
+ reversed = true;
+ }
+
+ colorKeys = setToSortedVector(prefsToEdit.getColorKeySet(), keyStartsWith, reversed);
+ floatKeys = setToSortedVector(prefsToEdit.getFloatKeySet(), keyStartsWith, reversed);
+ integerKeys = setToSortedVector(prefsToEdit.getIntegerKeySet(), keyStartsWith, reversed);
+ longKeys = setToSortedVector(prefsToEdit.getLongKeySet(), keyStartsWith, reversed);
+ booleanKeys = setToSortedVector(prefsToEdit.getBooleanKeySet(), keyStartsWith, reversed);
+ stringKeys = setToSortedVector(prefsToEdit.getStringKeySet(), keyStartsWith, reversed);
+
+ colorFields = new HashMap<String,JTextField>();
+ floatFields = new HashMap<String,JTextField>();
+ integerFields = new HashMap<String,JComboBox>();
+ longFields = new HashMap<String,JTextField>();
+ booleanFields = new HashMap<String,JCheckBox>();
+ stringFields = new HashMap<String,JTextField>();
+
+ //this.editPanel = createEditPanel();
+ //this.buttonPanel = createButtonPanel();
+ this.editPanel = createEditPanel();
+ this.buttonPanel = createButtonPanel();
+ }
+
+ private Vector<String> setToSortedVector(Set<String> set, String startsWith, boolean reversed) {
+ Vector<String> vector = new Vector<String>();
+
+ if (reversed) {
+ for (String elem : set)
+ if (!elem.startsWith(startsWith) && !elem.endsWith("!") && !elem.startsWith("keyevent"))
+ vector.add(elem);
+ } else {
+ for (String elem : set)
+ if (elem.startsWith(startsWith) && !elem.endsWith("!") && !elem.startsWith("keyevent"))
+ vector.add(elem);
+ }
+
+ Collections.sort(vector);
+
+ return vector;
+ }
+
+ private JPanel createButtonPanel() {
+ JPanel buttonPanel = new JPanel();
+ buttonPanel.setBackground(Color.WHITE);
+
+ JButton saveButton = new JButton(
+ prefs.getString("lang.ok"));
+ saveButton.setMnemonic(prefs.getInteger("keyevent.ok"));
+ saveButton.addActionListener(this);
+ buttonPanel.add(saveButton);
+
+ JButton resetButton = new JButton(
+ prefs.getString("lang.reset"));
+ resetButton.setMnemonic(prefs.getInteger("keyevent.reset"));
+ resetButton.addActionListener(this);
+ buttonPanel.add(resetButton);
+
+ return buttonPanel;
+ }
+
+ abstract protected void addToEditPanelFront(JPanel editPanel);
+
+ abstract protected void addToEditPanelLast(JPanel editPanel);
+
+ private JPanel createUnitPanel(Component comp, String key) {
+ JPanel unitPanel = new JPanel(new GridBagLayout());
+ unitPanel.setBackground(Color.WHITE);
+
+ String unitText = prefs.getUnit(key);
+ if (unitText == null)
+ unitText = "";
+
+ JLabel unitLabel = new JLabel(" " + unitText);
+
+ unitPanel.setLayout(new BoxLayout(unitPanel, BoxLayout.X_AXIS));
+ unitPanel.add(comp);
+ unitPanel.add(unitLabel);
+
+ return unitPanel;
+ }
+
+ private JPanel createEditPanel() {
+ JPanel editPanel = new JPanel(new GridBagLayout());
+ editPanel.setBackground(Color.WHITE);
+
+ editPanelConstraints = new GridBagConstraints();
+ editPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
+ editPanelConstraints.ipady = 20;
+ editPanelConstraints.ipadx = 20;
+ editPanelRow = 0;
+
+ addToEditPanelFront(editPanel);
+
+ for (String key : integerKeys) {
+ String fullKey = VSPrefs.INTEGER_PREFIX + key;
+ String descr = prefsToEdit.getDescription(fullKey);
+
+ JLabel keyLabel = null;
+ if (descr == null)
+ keyLabel = new JLabel(fullKey);
+ else
+ keyLabel = new JLabel(descr);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(keyLabel, editPanelConstraints);
+ editPanelConstraints.insets = insets;
+
+ Integer integer = prefsToEdit.getInteger(key);
+ Integer initialSelection[] = { integer };
+ JComboBox valComboBox = new JComboBox(initialSelection);
+ VSPrefs.SettingRestriction settingRestriction = prefsToEdit.getRestriction(fullKey);
+
+ int minValue, maxValue;
+
+ if (settingRestriction != null) {
+ VSPrefs.IntegerSettingRestriction integerSettingRestriction =
+ (VSPrefs.IntegerSettingRestriction) settingRestriction;
+
+ minValue = integerSettingRestriction.getMinValue();
+ maxValue = integerSettingRestriction.getMaxValue();
+
+ } else {
+ minValue = 0;
+ maxValue = 100;
+ }
+
+ for (int i = minValue; i <= maxValue; ++i)
+ valComboBox.addItem(new Integer(i));
+
+ valComboBox.repaint();
+
+ JPanel pane = new JPanel(new BorderLayout());
+ pane.setBackground(Color.WHITE);
+ pane.add(createUnitPanel(valComboBox, fullKey), BorderLayout.WEST);
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(pane, editPanelConstraints);
+ integerFields.put(key, valComboBox);
+ }
+
+ final String activated = prefs.getString("lang.activated");
+ for (String key : booleanKeys) {
+ String fullKey = VSPrefs.BOOLEAN_PREFIX + key;
+ String descr = prefsToEdit.getDescription(fullKey);
+
+ JLabel keyLabel = null;
+ if (descr == null)
+ keyLabel = new JLabel(fullKey);
+ else
+ keyLabel = new JLabel(descr);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(keyLabel, editPanelConstraints);
+
+ JCheckBox valField = new JCheckBox(activated, prefsToEdit.getBoolean(key));
+ valField.setBackground(Color.WHITE);
+
+ JPanel pane = new JPanel(new BorderLayout());
+ pane.setBackground(Color.WHITE);
+ pane.add(createUnitPanel(valField, fullKey), BorderLayout.WEST);
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(pane, editPanelConstraints);
+ booleanFields.put(key, valField);
+ }
+
+ for (String key : longKeys) {
+ String fullKey = VSPrefs.LONG_PREFIX + key;
+ String descr = prefsToEdit.getDescription(fullKey);
+
+ JLabel keyLabel = null;
+ if (descr == null)
+ keyLabel = new JLabel(fullKey);
+ else
+ keyLabel = new JLabel(descr);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(keyLabel, editPanelConstraints);
+
+ JTextField valField = new JTextField(15);
+ valField.addKeyListener(new java.awt.event.KeyAdapter() {
+ public void keyTyped(java.awt.event.KeyEvent e) {
+ JTextField valField = (JTextField)e.getSource();
+ if (valField.getText().length() >= valField.getColumns() + 10)
+ e.consume();
+ }
+ });
+ valField.setText(""+prefsToEdit.getLong(key));
+
+ JPanel pane = new JPanel(new BorderLayout());
+ pane.setBackground(Color.WHITE);
+ pane.add(createUnitPanel(valField, fullKey), BorderLayout.WEST);
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(pane, editPanelConstraints);
+ longFields.put(key, valField);
+ }
+
+
+ for (String key : floatKeys) {
+ String fullKey = VSPrefs.FLOAT_PREFIX + key;
+ String descr = prefsToEdit.getDescription(fullKey);
+
+ JLabel keyLabel = null;
+ if (descr == null)
+ keyLabel = new JLabel(fullKey);
+ else
+ keyLabel = new JLabel(descr);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(keyLabel, editPanelConstraints);
+
+ JTextField valField = new JTextField(15);
+ valField.addKeyListener(new java.awt.event.KeyAdapter() {
+ public void keyTyped(java.awt.event.KeyEvent e) {
+ JTextField valField = (JTextField)e.getSource();
+ if (valField.getText().length() >= valField.getColumns() + 10)
+ e.consume();
+ }
+ });
+ valField.setText(""+prefsToEdit.getFloat(key));
+
+ JPanel pane = new JPanel(new BorderLayout());
+ pane.setBackground(Color.WHITE);
+ pane.add(createUnitPanel(valField, fullKey), BorderLayout.WEST);
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(pane, editPanelConstraints);
+ floatFields.put(key, valField);
+ }
+
+
+ for (String key : colorKeys) {
+ String fullKey = VSPrefs.COLOR_PREFIX + key;
+ String descr = prefsToEdit.getDescription(fullKey);
+
+ JLabel keyLabel = null;
+ if (descr == null)
+ keyLabel = new JLabel(fullKey);
+ else
+ keyLabel = new JLabel(descr);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(keyLabel, editPanelConstraints);
+
+ final JTextField valField = new JTextField(15);
+ Color color = prefsToEdit.getColor(key);
+ valField.setBackground(color);
+ valField.setEditable(false);
+ valField.addMouseListener(new MouseListener() {
+ public void mouseExited(MouseEvent e) { }
+ public void mouseReleased(MouseEvent e) { }
+ public void mouseEntered(MouseEvent e) { }
+ public void mousePressed(MouseEvent e) { }
+ public void mouseClicked(MouseEvent e) {
+ JFrame parentFrame = getFrame();
+ JFrame frame = new VSFrame(
+ prefs.getString("name") + " - " +
+ prefs.getString(
+ "lang.colorchooser"),parentFrame);
+ frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+
+ JComponent colorChooserPane = new VSColorChooser(prefs, valField);
+ colorChooserPane.setOpaque(true);
+
+ frame.setContentPane(colorChooserPane);
+ frame.pack();
+ frame.setVisible(true);
+ }
+ });
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(valField, editPanelConstraints);
+ colorFields.put(key, valField);
+ }
+
+ for (String key : stringKeys) {
+ String fullKey = VSPrefs.STRING_PREFIX + key;
+ String descr = prefsToEdit.getDescription(fullKey);
+
+ JLabel keyLabel = null;
+ if (descr == null)
+ keyLabel = new JLabel(fullKey);
+ else
+ keyLabel = new JLabel(descr);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(keyLabel, editPanelConstraints);
+
+ JTextField valField = new JTextField(15);
+ valField.addKeyListener(new java.awt.event.KeyAdapter() {
+ public void keyTyped(java.awt.event.KeyEvent e) {
+ JTextField valField = (JTextField)e.getSource();
+ if (valField.getText().length() >= valField.getColumns() + 10)
+ e.consume();
+ }
+ });
+ valField.setText(prefsToEdit.getString(key));
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(createUnitPanel(valField, fullKey), editPanelConstraints);
+ stringFields.put(key, valField);
+ }
+
+ addToEditPanelLast(editPanel);
+
+ return editPanel;
+ }
+
+ protected void resetEditPanel() {
+ for (String key : integerKeys) {
+ JComboBox valComboBox = integerFields.get(key);
+ valComboBox.setSelectedIndex(0);
+ }
+
+ for (String key : booleanKeys) {
+ JCheckBox valField = booleanFields.get(key);
+ valField.setSelected(prefsToEdit.getBoolean(key));
+ }
+
+ for (String key : floatKeys) {
+ JTextField valField = floatFields.get(key);
+ valField.setText(""+prefsToEdit.getFloat(key));
+ }
+
+ for (String key : longKeys) {
+ JTextField valField = longFields.get(key);
+ valField.setText(""+prefsToEdit.getLong(key));
+ }
+
+ for (String key : colorKeys) {
+ JTextField valField = colorFields.get(key);
+ valField.setBackground(prefsToEdit.getColor(key));
+ }
+
+ for (String key : stringKeys) {
+ JTextField valField = stringFields.get(key);
+ valField.setText(prefsToEdit.getString(key));
+ }
+ }
+
+ protected void savePrefs() {
+ for (String key : integerKeys) {
+ JComboBox valComboBox = integerFields.get(key);
+ prefsToEdit.setInteger(key, (Integer) valComboBox.getSelectedItem());
+ }
+
+ for (String key : booleanKeys) {
+ JCheckBox valField = booleanFields.get(key);
+ prefsToEdit.setBoolean(key, valField.isSelected());
+ }
+
+ for (String key : floatKeys) {
+ JTextField valField = floatFields.get(key);
+
+ try {
+ Float val = Float.valueOf(valField.getText());
+ prefsToEdit.setFloat(key, val);
+
+ } catch (NumberFormatException e) {
+ valField.setText("0.0");
+ }
+ }
+
+ for (String key : longKeys) {
+ JTextField valField = longFields.get(key);
+
+ try {
+ Long val = Long.valueOf(valField.getText());
+ prefsToEdit.setLong(key, val);
+
+ } catch (NumberFormatException e) {
+ valField.setText("0");
+ }
+ }
+
+ for (String key : colorKeys) {
+ JTextField valField = colorFields.get(key);
+ prefsToEdit.setColor(key, valField.getBackground());
+ }
+
+ for (String key : stringKeys) {
+ JTextField valField = stringFields.get(key);
+ prefsToEdit.setString(key, valField.getText());
+ }
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ String actionCommand = e.getActionCommand();
+
+ if (actionCommand.equals(prefs.getString("lang.ok"))) {
+ savePrefs();
+
+ } else if (actionCommand.equals(prefs.getString("lang.save"))) {
+ savePrefs();
+ prefs.saveFile();
+
+ } else if (actionCommand.equals(prefs.getString("lang.reset"))) {
+ resetEditPanel();
+
+ } else if (actionCommand.equals(prefs.getString("lang.default"))) {
+ prefs.fillWithDefaults();
+ resetEditPanel();
+ }
+ }
+
+ public JPanel getEditPanel() {
+ return editPanel;
+ }
+
+ public JPanel getButtonPanel() {
+ return buttonPanel;
+ }
+
+ abstract protected JFrame getFrame();
+}
diff --git a/sources/prefs/editors/VSEditorFrame.java b/sources/prefs/editors/VSEditorFrame.java
new file mode 100644
index 0000000..5cae7a0
--- /dev/null
+++ b/sources/prefs/editors/VSEditorFrame.java
@@ -0,0 +1,225 @@
+package prefs.editors;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.border.*;
+import javax.swing.filechooser.*;
+import java.util.*;
+import java.io.File;
+
+import prefs.*;
+import simulator.*;
+import utils.*;
+
+public abstract class VSEditorFrame extends VSEditor {
+ protected VSInfoArea infoArea;
+ protected VSFrame frame;
+
+ public VSEditorFrame(VSPrefs prefs, Component relativeTo, VSPrefs prefsToEdit, String title) {
+ super(prefs, prefsToEdit);
+ frame = new VSFrame(title, relativeTo);
+ init();
+ }
+
+ public VSEditorFrame(VSPrefs prefs, Component relativeTo, VSPrefs prefsToEdit, String title, int prefsCategory) {
+ super(prefs, prefsToEdit, prefsCategory);
+ frame = new VSFrame(title, relativeTo);
+ init();
+ }
+
+ private void init() {
+ frame.setJMenuBar(createJMenuBar());
+ frame.setContentPane(createContentPane());
+ frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+ frame.setSize(prefs.getInteger("window.prefs.xsize"),
+ prefs.getInteger("window.prefs.ysize"));
+ frame.setResizable(false);
+ frame.setVisible(true);
+
+ /*
+ frame.addWindowListener(new WindowAdapter() {
+ public void windowClosing(WindowEvent we) {
+ Window window = we.getWindow();
+ }
+ });
+ */
+ }
+
+ private Container createContentPane() {
+ Container contentPane = frame.getContentPane();
+
+ infoArea = new VSInfoArea();
+ JPanel editPanel = super.editPanel;
+ JPanel buttonPanel = createButtonPanel();
+
+ JScrollPane scrollPane = new JScrollPane(editPanel);
+ contentPane.add(infoArea, BorderLayout.NORTH);
+ contentPane.add(scrollPane, BorderLayout.CENTER);
+ contentPane.add(buttonPanel, BorderLayout.SOUTH);
+
+ return contentPane;
+ }
+
+ protected void addToEditPanelFront(JPanel editPanel) { }
+
+ protected void addToEditPanelLast(JPanel editPanel) { }
+
+ private JPanel createButtonPanel() {
+ JPanel buttonPanel = super.buttonPanel;
+
+ JButton cancelButton = new JButton(
+ prefs.getString("lang.cancel"));
+ cancelButton.setMnemonic(prefs.getInteger("keyevent.cancel"));
+ cancelButton.addActionListener(this);
+ buttonPanel.add(cancelButton);
+
+ return buttonPanel;
+ }
+
+ private JMenuBar createJMenuBar() {
+ /* File menu */
+ JMenu menuFile = new JMenu(
+ prefs.getString("lang.file"));
+ menuFile.setMnemonic(prefs.getInteger("keyevent.file"));
+ JMenuItem menuItem;
+
+ menuItem = new JMenuItem(
+ prefs.getString("lang.save"));
+ menuItem.setAccelerator(KeyStroke.getKeyStroke(
+ prefs.getInteger("keyevent.save"),
+ ActionEvent.ALT_MASK));
+ menuItem.addActionListener(this);
+ menuFile.add(menuItem);
+
+ if (!(this instanceof VSSimulationEditor))
+ menuItem.setEnabled(false);
+
+ menuItem = new JMenuItem(
+ prefs.getString("lang.saveas"));
+ menuItem.setAccelerator(KeyStroke.getKeyStroke(
+ prefs.getInteger("keyevent.saveas"),
+ ActionEvent.ALT_MASK));
+ menuItem.addActionListener(this);
+ menuFile.add(menuItem);
+
+ if (!(this instanceof VSSimulationEditor))
+ menuItem.setEnabled(false);
+
+ menuItem = new JMenuItem(
+ prefs.getString("lang.open"));
+ menuItem.setAccelerator(KeyStroke.getKeyStroke(
+ prefs.getInteger("keyevent.open"),
+ ActionEvent.ALT_MASK));
+ menuItem.addActionListener(this);
+ menuFile.add(menuItem);
+
+ if (!(this instanceof VSSimulationEditor))
+ menuItem.setEnabled(false);
+
+ menuFile.addSeparator();
+
+ menuItem = new JMenuItem(
+ prefs.getString("lang.close"));
+ menuItem.setAccelerator(KeyStroke.getKeyStroke(
+ prefs.getInteger("keyevent.close"),
+ ActionEvent.ALT_MASK));
+ menuItem.addActionListener(this);
+ menuFile.add(menuItem);
+
+ /* Edit menu */
+ JMenu menuEdit = new JMenu(
+ prefs.getString("lang.edit"));
+ menuEdit.setMnemonic(prefs.getInteger("keyevent.edit"));
+
+ menuItem = new JMenuItem(
+ prefs.getString("lang.default"));
+ menuItem.setAccelerator(KeyStroke.getKeyStroke(
+ prefs.getInteger("keyevent.default"),
+ ActionEvent.ALT_MASK));
+ menuItem.addActionListener(this);
+ menuEdit.add(menuItem);
+
+ if (!(this instanceof VSSimulationEditor))
+ menuItem.setEnabled(false);
+
+ menuEdit.addSeparator();
+
+ menuItem = new JMenuItem(prefs.getString("lang.prefs"));
+ /*
+ if (super.prefsCategory == ALL_PREFERENCES) {
+ menuItem.setAccelerator(KeyStroke.getKeyStroke(
+ prefs.getInteger("keyevent.prefs"),
+ ActionEvent.ALT_MASK));
+ menuItem.addActionListener(this);
+ }
+ */
+ menuItem.setEnabled(false);
+ menuEdit.add(menuItem);
+
+ menuItem = new JMenuItem(prefs.getString("lang.prefs.ext"));
+ if (super.prefsCategory == SIMULATION_PREFERENCES) {
+ menuItem.setAccelerator(KeyStroke.getKeyStroke(
+ prefs.getInteger("keyevent.prefs.ext"),
+ ActionEvent.ALT_MASK));
+ menuItem.addActionListener(this);
+ } else {
+ menuItem.setEnabled(false);
+ }
+ menuEdit.add(menuItem);
+
+ JMenuBar mainMenuBar = new JMenuBar();
+ mainMenuBar.add(menuFile);
+ mainMenuBar.add(menuEdit);
+
+ return mainMenuBar;
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ String actionCommand = e.getActionCommand();
+
+ if (actionCommand.equals(prefs.getString("lang.close"))) {
+ frame.dispose();
+
+ } else if (actionCommand.equals(prefs.getString("lang.saveas"))) {
+ JFileChooser fileChooser = new JFileChooser();
+ int ret = fileChooser.showSaveDialog(frame);
+
+ if (ret == JFileChooser.APPROVE_OPTION) {
+ File file = fileChooser.getSelectedFile();
+ savePrefs();
+ prefs.saveFile(file.getName());
+ }
+
+ } else if (actionCommand.equals(prefs.getString("lang.open"))) {
+ JFileChooser fileChooser = new JFileChooser();
+ fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
+ int ret = fileChooser.showOpenDialog(frame);
+
+ if (ret == JFileChooser.APPROVE_OPTION) {
+ File file = fileChooser.getSelectedFile();
+ prefsToEdit = prefs = VSDefaultPrefs.init(file.getName());
+ resetEditPanel();
+ }
+
+ } else if (actionCommand.equals(prefs.getString("lang.cancel"))) {
+ frame.dispose();
+
+ } else if (actionCommand.equals(prefs.getString("lang.prefs"))) {
+ newVSEditorInstance(prefs, prefs, VSEditor.SIMULATION_PREFERENCES);
+
+ } else if (actionCommand.equals(prefs.getString("lang.prefs.ext"))) {
+ newVSEditorInstance(prefs, prefs, VSEditor.ALL_PREFERENCES);
+
+ } else {
+ /* More action in the super class!!! */
+ super.actionPerformed(e);
+ }
+ }
+
+ public void newVSEditorInstance(VSPrefs prefs, VSPrefs prefsToEdit, int prefsCategory) { };
+
+ protected VSFrame getFrame() {
+ return frame;
+ }
+}
diff --git a/sources/prefs/editors/VSProcessEditor.java b/sources/prefs/editors/VSProcessEditor.java
new file mode 100644
index 0000000..8392bdb
--- /dev/null
+++ b/sources/prefs/editors/VSProcessEditor.java
@@ -0,0 +1,154 @@
+package prefs.editors;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.table.*;
+import javax.swing.border.*;
+import javax.swing.filechooser.*;
+import java.util.*;
+import java.io.File;
+
+import simulator.*;
+import utils.*;
+import core.*;
+import protocols.*;
+import prefs.VSPrefs;
+
+public class VSProcessEditor extends VSEditorFrame {
+ private VSProcess process;
+
+ public VSProcessEditor(VSPrefs prefs, Component relativeTo, VSProcess process) {
+ super(prefs, relativeTo, process, prefs.getString("name") + " - "
+ + prefs.getString("lang.prefs.process"));
+
+ this.process = process;
+
+ init();
+ }
+
+ public VSProcessEditor(VSPrefs prefs, Component relativeTo, VSProcess process, int prefsCategory) {
+ super(prefs, relativeTo, process, prefs.getString("name") + " - "
+ + prefs.getString("lang.prefs.process"
+ + (prefsCategory == ALL_PREFERENCES ? ".ext" : "")),
+ prefsCategory);
+
+ this.process = process;
+
+ init();
+ }
+
+ private void init() {
+ super.infoArea.setText(prefs.getString("lang.prefs.process.info!"));
+ getFrame().disposeWithParent();
+ createButtonPanel();
+ }
+
+ protected void addToEditPanelFront(JPanel editPanel) {
+ super.addToEditPanelFront(editPanel);
+
+ if (prefsCategory != SIMULATION_PREFERENCES)
+ return;
+
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(new JLabel(prefs.getString("lang.protocol.editor")), editPanelConstraints);
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ JPanel protocolSelectorPanel = createProtocolSelector();
+ editPanel.add(protocolSelectorPanel, editPanelConstraints);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ }
+
+ private JPanel createProtocolSelector() {
+ JPanel panel = new JPanel(new GridBagLayout());
+ panel.setBorder(BorderFactory.createLineBorder(Color.black));
+ Vector<String> registeredProtocols = VSRegisteredProtocols.getProtocolNames();
+
+ GridBagConstraints constraints = new GridBagConstraints();
+ constraints.fill = GridBagConstraints.HORIZONTAL;
+ constraints.gridx = 0;
+ constraints.gridy = 0;
+ constraints.insets = new Insets(5, 0, 5, 0);
+ constraints.ipadx = 10;
+ constraints.ipady = 10;
+ final JComboBox comboBox = new JComboBox(registeredProtocols);
+ comboBox.setBackground(Color.WHITE);
+ panel.add(comboBox, constraints);
+ constraints.gridy = 1;
+ JButton button = new JButton(prefs.getString("lang.edit"));
+ button.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent ae) {
+ if (ae.getActionCommand().equals(prefs.getString("lang.edit"))) {
+ String protocolName = (String) comboBox.getSelectedItem();
+ String protocolClassname = VSRegisteredProtocols.getProtocolClassname(protocolName);
+ VSProtocol protocol = null;
+ if (process.objectExists(protocolClassname)) {
+ Object object = process.getObject(protocolClassname);
+ if (object instanceof VSProtocol)
+ protocol = (VSProtocol) process.getObject(protocolClassname);
+ else
+ return;
+ } else {
+ protocol = VSRegisteredProtocols.getProtocolInstanceByName(protocolName, process);
+ process.setObject(protocolClassname, protocol);
+ }
+ new VSProtocolEditor(prefs, frame, protocol);
+ }
+ }
+ });
+
+ panel.add(button, constraints);
+
+ return panel;
+ }
+
+ protected void resetEditPanel() {
+ super.resetEditPanel();
+ }
+
+ protected void savePrefs() {
+ super.savePrefs();
+ }
+
+ private JPanel createButtonPanel() {
+ JPanel buttonPanel = super.buttonPanel;
+
+ JButton cancelButton = new JButton(
+ prefs.getString("lang.takeover"));
+ cancelButton.setMnemonic(prefs.getInteger("keyevent.takeover"));
+ cancelButton.addActionListener(this);
+ buttonPanel.add(cancelButton);
+
+ return buttonPanel;
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ String actionCommand = e.getActionCommand();
+
+ if (actionCommand.equals(prefs.getString("lang.ok"))) {
+ savePrefs();
+ process.updateFromVSPrefs();
+ frame.dispose();
+
+ } else if (actionCommand.equals(prefs.getString("lang.takeover"))) {
+ savePrefs();
+ process.updateFromVSPrefs();
+
+ } else {
+ super.actionPerformed(e);
+ }
+ }
+
+ public void newVSEditorInstance(VSPrefs prefs, VSPrefs prefsToEdit, int prefsCategory) {
+ if (prefsToEdit instanceof VSProcess) {
+ VSProcess process = (VSProcess) prefsToEdit;
+ new VSProcessEditor(prefs, frame, process, prefsCategory);
+
+ } else {
+ new VSProcessEditor(prefs, frame, process, prefsCategory);
+ }
+ }
+}
diff --git a/sources/prefs/editors/VSProtocolEditor.java b/sources/prefs/editors/VSProtocolEditor.java
new file mode 100644
index 0000000..1cf5059
--- /dev/null
+++ b/sources/prefs/editors/VSProtocolEditor.java
@@ -0,0 +1,335 @@
+package prefs.editors;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.io.File;
+import java.util.*;
+import javax.swing.*;
+import javax.swing.border.*;
+import javax.swing.event.*;
+import javax.swing.filechooser.*;
+
+import protocols.*;
+import utils.*;
+import core.*;
+import prefs.VSPrefs;
+
+public class VSProtocolEditor extends VSEditorFrame {
+ private JCheckBox clientCheckBox;
+ private JCheckBox serverCheckBox;
+ private JComboBox clientComboBox;
+ private VSProtocol protocol;
+ private VSTaskManager taskManager;
+ private JPanel clientTaskManagerEditorPanel;
+ private JButton takeOverButton;
+ private JButton deleteButton;
+ private JTextField textField;
+
+ public VSProtocolEditor(VSPrefs prefs, Component relativeTo, VSProtocol protocol) {
+ super(prefs, relativeTo, protocol, prefs.getString("name") + " - "
+ + protocol.getProtocolName() + " " + prefs.getString("lang.editor"), ALL_PREFERENCES);
+
+ this.protocol = protocol;
+ this.taskManager = protocol.getProcess().getSimulationPanel().getTaskManager();
+
+ init();
+ }
+
+ private void init() {
+ super.getFrame().disposeWithParent();
+ super.infoArea.setText(prefs.getString("lang.prefs.protocol.info!"));
+ initTaskManagerEditor(clientTaskManagerEditorPanel);
+ initClientServerCheckboxes();
+ createButtonPanel();
+ }
+
+ private JPanel createButtonPanel() {
+ JPanel buttonPanel = super.buttonPanel;
+
+ JButton cancelButton = new JButton(
+ prefs.getString("lang.takeover"));
+ cancelButton.setMnemonic(prefs.getInteger("keyevent.takeover"));
+ cancelButton.addActionListener(this);
+ buttonPanel.add(cancelButton);
+
+ return buttonPanel;
+ }
+
+ protected void addToEditPanelFront(JPanel editPanel) {
+ super.addToEditPanelFront(editPanel);;
+
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(new JLabel(prefs.getString("lang.protocol.tasks.activation")), editPanelConstraints);
+
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(createClientServerCheckboxes(), editPanelConstraints);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(new JLabel(prefs.getString("lang.protocol.tasks.client")), editPanelConstraints);
+ clientComboBox = new JComboBox();
+ clientTaskManagerEditorPanel = new JPanel(new GridBagLayout());
+ editPanelConstraints.insets = insets;
+ editPanelConstraints.gridy = editPanelRow++;
+ editPanel.add(clientTaskManagerEditorPanel, editPanelConstraints);
+
+ editPanelConstraints.insets = insetsTopSpaceing;
+ editPanelConstraints.gridy = editPanelRow++;
+ }
+
+ private JPanel createClientServerCheckboxes() {
+ final String activated = prefs.getString("lang.activate");
+ final String client = prefs.getString("lang.protocol.client") + " " + activated;
+ final String server = prefs.getString("lang.protocol.server") + " " + activated;
+
+ final JPanel panel = new JPanel(new GridBagLayout());
+ panel.setBorder(BorderFactory.createLineBorder(Color.black));
+
+ int row = 0;
+ GridBagConstraints constraints = new GridBagConstraints();
+ constraints.fill = GridBagConstraints.HORIZONTAL;
+ constraints.ipady = 10;
+ constraints.ipadx = 10;
+ constraints.insets = new Insets(5, 0, 5, 0);
+ constraints.gridy = row++;
+ constraints.gridx = 0;
+ panel.add(new JLabel(client), constraints);
+
+ constraints.gridx = 1;
+ clientCheckBox = new JCheckBox();
+ clientCheckBox.addChangeListener(new ChangeListener() {
+ public void stateChanged(ChangeEvent ce) {
+ if (takeOverButton != null && textField != null) {
+ AbstractButton abstractButton = (AbstractButton) ce.getSource();
+ ButtonModel buttonModel = abstractButton.getModel();
+ takeOverButton.setEnabled(buttonModel.isSelected());
+ textField.setEnabled(buttonModel.isSelected());
+ if (!buttonModel.isSelected()) {
+ clientComboBox.setEnabled(false);
+ deleteButton.setEnabled(false);
+ } else if (clientComboBox.getItemCount() > 0) {
+ clientComboBox.setEnabled(true);
+ deleteButton.setEnabled(true);
+ }
+ }
+ }
+ });
+
+ panel.add(clientCheckBox, constraints);
+
+ constraints.gridy = row++;
+ constraints.gridx = 0;
+ panel.add(new JLabel(server), constraints);
+
+ constraints.gridx = 1;
+ serverCheckBox = new JCheckBox();
+ panel.add(serverCheckBox, constraints);
+
+ return panel;
+ }
+
+ private void initClientServerCheckboxes() {
+ final String protocolName = protocol.getProtocolName();
+ final VSProcess process = protocol.getProcess();
+
+ String protocolKey = "sim."+protocolName.toLowerCase()+".client.enabled!";
+ clientCheckBox.setSelected(process.getBoolean(protocolKey));
+ protocolKey = "sim."+protocolName.toLowerCase()+".server.enabled!";
+ serverCheckBox.setSelected(process.getBoolean(protocolKey));
+
+ }
+
+ private void initTaskManagerEditor(JPanel panel) {
+ clientComboBox = new JComboBox();
+ deleteButton = new JButton(prefs.getString("lang.remove"));
+ takeOverButton = new JButton(prefs.getString("lang.takeover"));
+ textField = new JTextField();
+
+ panel.setBorder(BorderFactory.createLineBorder(Color.black));
+
+ int row = 0;
+ GridBagConstraints constraints = new GridBagConstraints();
+ constraints.fill = GridBagConstraints.HORIZONTAL;
+ constraints.ipady = 10;
+ constraints.ipadx = 10;
+ constraints.insets = new Insets(5, 5, 5, 5);
+
+ textField.setText("0000");
+ textField.setColumns(10);
+ constraints.gridy = row++;
+ constraints.gridx = 0;
+ panel.add(textField, constraints);
+
+ Insets insetsBackup = constraints.insets;
+ constraints.insets = new Insets(0, 0, 0, 0);
+ constraints.gridx = 1;
+ panel.add(new JLabel("ms"), constraints);
+
+ constraints.insets = insetsBackup;
+ constraints.gridx = 2;
+ panel.add(takeOverButton, constraints);
+
+ constraints.gridy = row++;
+ constraints.gridx = 0;
+ resetTaskManager();
+ clientComboBox.setBackground(Color.WHITE);
+ panel.add(clientComboBox, constraints);
+
+ constraints.gridx = 2;
+ panel.add(deleteButton, constraints);
+
+ ActionListener actionListener = new ActionListener() {
+ private boolean isRed;
+ public void actionPerformed(ActionEvent ae) {
+ if (ae.getActionCommand().equals(prefs.getString("lang.takeover"))) {
+ String textValue = textField.getText();
+ try {
+ Long longValue = Long.valueOf(textValue);
+
+ if (longValue.longValue() < 0) {
+ textField.setBackground(Color.RED);
+ isRed = true;
+ return;
+ }
+
+ clientComboBox.addItem(VSTools.getTimeString(longValue.longValue()));
+ clientComboBox.setSelectedIndex(clientComboBox.getItemCount()-1);
+ clientComboBox.setEnabled(true);
+ sortComboBox(clientComboBox);
+ deleteButton.setEnabled(true);
+
+ if (isRed) {
+ textField.setBackground(Color.WHITE);
+ isRed = false;
+ }
+ } catch (NumberFormatException e) {
+ textField.setBackground(Color.RED);
+ isRed = true;
+ }
+
+ } else if (ae.getActionCommand().equals(prefs.getString("lang.remove"))) {
+ Object[] objects = clientComboBox.getSelectedObjects();
+ for (Object object : objects)
+ clientComboBox.removeItem(object);
+ if (clientComboBox.getItemCount() == 0) {
+ clientComboBox.setEnabled(false);
+ deleteButton.setEnabled(false);
+ }
+ }
+ }
+ };
+
+ takeOverButton.addActionListener(actionListener);
+ deleteButton.addActionListener(actionListener);
+
+ clientComboBox.setEnabled(false);
+ deleteButton.setEnabled(false);
+ takeOverButton.setEnabled(false);
+ textField.setEnabled(false);
+ }
+
+ protected void resetEditPanel() {
+ super.resetEditPanel();
+
+ resetTaskManager();
+
+ final VSProcess process = protocol.getProcess();
+ final String protocolName = protocol.getProtocolName();
+ String protocolKey = "sim."+protocolName.toLowerCase()+".client.enabled!";
+ clientCheckBox.setSelected(process.getBoolean(protocolKey));
+ protocolKey = "sim."+protocolName.toLowerCase()+".server.enabled!";
+ serverCheckBox.setSelected(process.getBoolean(protocolKey));
+
+ takeOverButton.setEnabled(clientCheckBox.isSelected());
+ textField.setEnabled(clientCheckBox.isSelected());
+ if (!clientCheckBox.isSelected()) {
+ clientComboBox.setEnabled(false);
+ deleteButton.setEnabled(false);
+ }
+ }
+
+ protected void savePrefs() {
+ super.savePrefs();
+ saveTasks();
+ }
+
+ private void resetTaskManager() {
+ clientComboBox.removeAllItems();
+ LinkedList<VSTask> protocolVSTasks = taskManager.getProtocolTasks(protocol);
+
+ for (VSTask task : protocolVSTasks)
+ clientComboBox.addItem(VSTools.getTimeString(task.getTaskTime()));
+ }
+
+ private void saveTasks() {
+ LinkedList<VSTask> tasks = new LinkedList<VSTask>();
+ int numItems;
+
+ numItems = clientComboBox.getItemCount();
+ for (int i = 0; i < numItems; ++i) {
+ long taskTime = VSTools.getStringTime((String) clientComboBox.getItemAt(i));
+ VSTask task = new VSTask(taskTime, protocol.getProcess(), protocol);
+ task.isProgrammed(true);
+ tasks.addLast(task);
+ }
+
+ taskManager.modifyProtocolTasks(protocol, tasks);
+
+ final VSProcess process = protocol.getProcess();
+ final String protocolName = protocol.getProtocolName();
+ String protocolKey = "sim."+protocolName.toLowerCase()+".client.enabled!";
+ process.setBoolean(protocolKey, clientCheckBox.isSelected());
+ protocol.isClient(clientCheckBox.isSelected());
+
+ protocolKey = "sim."+protocolName.toLowerCase()+".server.enabled!";
+ process.setBoolean(protocolKey, serverCheckBox.isSelected());
+ protocol.isServer(serverCheckBox.isSelected());
+
+ Object protocolsObj = null;
+ if (process.objectExists("protocols.registered")) {
+ protocolsObj = process.getObject("protocols.registered");
+ } else {
+ protocolsObj = new Vector<VSProtocol>();
+ process.setObject("protocols.registered", protocolsObj);
+ }
+
+ if (protocolsObj instanceof Vector) {
+ Vector<VSProtocol> protocols = (Vector<VSProtocol>) protocolsObj;
+ if (!protocols.contains(protocol))
+ protocols.add(protocol);
+ }
+ }
+
+ private void sortComboBox(JComboBox comboBox) {
+ Object selected = comboBox.getSelectedItem();
+ int numItems = comboBox.getItemCount();
+ Vector<String> vector = new Vector<String>();
+
+ for (int i = 0; i < numItems; ++i) {
+ String value = (String) comboBox.getItemAt(i);
+ vector.add(value);
+ }
+
+ Collections.sort(vector);
+ comboBox.removeAllItems();
+ for (String value : vector)
+ comboBox.addItem(value);
+ comboBox.setSelectedItem(selected);
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ String actionCommand = e.getActionCommand();
+
+ if (actionCommand.equals(prefs.getString("lang.ok"))) {
+ savePrefs();
+ frame.dispose();
+
+ } else if (actionCommand.equals(prefs.getString("lang.takeover"))) {
+ savePrefs();
+
+ } else {
+ super.actionPerformed(e);
+ }
+ }
+}
diff --git a/sources/prefs/editors/VSSimulationEditor.java b/sources/prefs/editors/VSSimulationEditor.java
new file mode 100644
index 0000000..0aa6256
--- /dev/null
+++ b/sources/prefs/editors/VSSimulationEditor.java
@@ -0,0 +1,60 @@
+package prefs.editors;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.border.*;
+import javax.swing.filechooser.*;
+import java.util.*;
+import java.io.File;
+
+import simulator.*;
+import utils.*;
+import prefs.VSPrefs;
+
+public class VSSimulationEditor extends VSEditorFrame {
+ private boolean startNewVSSimulation;
+
+ public VSSimulationEditor(VSPrefs prefs, Component relativeTo) {
+ super(prefs, relativeTo, prefs, prefs.getString("name")
+ + " - " + prefs.getString("lang.prefs"));
+
+ startNewVSSimulation = true;
+ init();
+ }
+
+ public VSSimulationEditor(VSPrefs prefs, Component relativeTo, int prefsCategory) {
+ super(prefs, relativeTo, prefs, prefs.getString("name")
+ + " - " + prefs.getString("lang.prefs"
+ + (prefsCategory == ALL_PREFERENCES ? ".ext" : "")),
+ prefsCategory);
+
+ startNewVSSimulation = false;
+ init();
+ }
+
+ private void init() {
+ super.infoArea.setText(prefs.getString("lang.prefs.info!"));
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ String actionCommand = e.getActionCommand();
+
+ if (actionCommand.equals(prefs.getString("lang.ok"))) {
+ super.actionPerformed(e);
+ prefsToEdit.saveFile();
+
+ frame.dispose();
+
+ if (startNewVSSimulation)
+ new VSSimulation(prefs, getFrame());
+
+ } else {
+ super.actionPerformed(e);
+ }
+ }
+
+ public void newVSEditorInstance(VSPrefs prefs, VSPrefs prefsToEdit, int prefsCategory) {
+ new VSSimulationEditor(prefs, getFrame(), prefsCategory);
+ }
+}