package view; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JTextField; /** * the "preferences" dialog * @author kolja * */ public class OptionsDialog extends JDialog { private static final long serialVersionUID = 1L; private ArrayList< ActionListener > listeners; private JComboBox display; private JButton speichern; private JComboBox run; private JComboBox collapse; private JTextField fSize; private int tempFSize = 12; private static class Options { // Speichert die Optionen wie sie zuletzt waren (um zu prüfen ob was geändert wurde) public int layoutDisplaySelection = 0; public int runStepsSelection = 0; public int autoCollapse = 0; public int fontSize = 12; } private Options options; OptionsDialog() { setTitle( "Preferences" ); setLayout( new GridLayout( 5, 2 ) ); add( new JLabel( "Display layouts:" ) ); String[] displayValues = { "All", "Only current" }; display = new JComboBox( displayValues ); add( display ); // Run Option add( new JLabel( "Run steps:" ) ); String[] runValues = { "All", "Only expanded" }; run = new JComboBox( runValues ); add( run ); // Automatically collapse option add( new JLabel( "Automatically collapse pseudocode:" ) ); String[] collapseValues = { "No", "Yes" }; collapse = new JComboBox( collapseValues ); add( collapse ); // Font size Option add( new JLabel( "Font size:" ) ); fSize = new JTextField( "12" ); add( fSize ); fSize.getDocument().addDocumentListener( new NumberDocumentListener( new NumberDocumentListener.Action() { @Override public void action(int val) { tempFSize = val; } }, fSize ) ); add( new JLabel() ); // save speichern = new JButton( "Ok" ); speichern.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { // prüfe auf änderungen boolean change = options.layoutDisplaySelection != display.getSelectedIndex() || options.runStepsSelection != run.getSelectedIndex() || tempFSize != options.fontSize || options.autoCollapse != collapse.getSelectedIndex(); options.layoutDisplaySelection = display.getSelectedIndex(); options.runStepsSelection = run.getSelectedIndex(); options.fontSize = tempFSize; options.autoCollapse = collapse.getSelectedIndex(); if( change ) { for( ActionListener l : listeners ) { // benachrichtige die Listener über die Änderungen l.actionPerformed( new ActionEvent(this, 0, "Options changed" ) ); } } setVisible( false ); } catch( Exception e1 ) { e1.printStackTrace(); } } }); add( speichern ); setSize( 250, 150 ); listeners = new ArrayList(); options = new Options(); setLocation( Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2 ); } public int getAutoCollapseOption() { return options.autoCollapse; } public int getLayerDisplayOption() { return options.layoutDisplaySelection; } public int getRunStepsOption() { return options.runStepsSelection; } public int getFontSize() { return options.fontSize; } public void addActionListener( ActionListener al ) { listeners.add( al ); } }