OptionsDialog.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package view;
  2. import java.awt.GridLayout;
  3. import java.awt.Toolkit;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.ArrayList;
  7. import javax.swing.JButton;
  8. import javax.swing.JComboBox;
  9. import javax.swing.JDialog;
  10. import javax.swing.JLabel;
  11. import javax.swing.JTextField;
  12. /**
  13. * the "preferences" dialog
  14. * @author kolja
  15. *
  16. */
  17. public class OptionsDialog extends JDialog {
  18. private static final long serialVersionUID = 1L;
  19. private ArrayList< ActionListener > listeners;
  20. private JComboBox<String> display;
  21. private JButton speichern;
  22. private JComboBox<String> run;
  23. private JComboBox<String> collapse;
  24. private JTextField fSize;
  25. private int tempFSize = 12;
  26. private static class Options
  27. { // Speichert die Optionen wie sie zuletzt waren (um zu prüfen ob was geändert wurde)
  28. public int layoutDisplaySelection = 0;
  29. public int runStepsSelection = 0;
  30. public int autoCollapse = 0;
  31. public int fontSize = 12;
  32. }
  33. private Options options;
  34. OptionsDialog()
  35. {
  36. setTitle( "Preferences" );
  37. setLayout( new GridLayout( 5, 2 ) );
  38. add( new JLabel( "Display layouts:" ) );
  39. String[] displayValues = { "All", "Only current" };
  40. display = new JComboBox<String>( displayValues );
  41. add( display );
  42. // Run Option
  43. add( new JLabel( "Run steps:" ) );
  44. String[] runValues = { "All", "Only expanded" };
  45. run = new JComboBox<String>( runValues );
  46. add( run );
  47. // Automatically collapse option
  48. add( new JLabel( "Automatically collapse pseudocode:" ) );
  49. String[] collapseValues = { "No", "Yes" };
  50. collapse = new JComboBox<String>( collapseValues );
  51. add( collapse );
  52. // Font size Option
  53. add( new JLabel( "Font size:" ) );
  54. fSize = new JTextField( "12" );
  55. add( fSize );
  56. fSize.getDocument().addDocumentListener( new NumberDocumentListener( new NumberDocumentListener.Action() {
  57. @Override
  58. public void action(int val) {
  59. tempFSize = val;
  60. }
  61. }, fSize ) );
  62. add( new JLabel() );
  63. // save
  64. speichern = new JButton( "Ok" );
  65. speichern.addActionListener( new ActionListener() {
  66. @Override
  67. public void actionPerformed(ActionEvent e) {
  68. try {
  69. // prüfe auf änderungen
  70. boolean change = options.layoutDisplaySelection != display.getSelectedIndex() ||
  71. options.runStepsSelection != run.getSelectedIndex() ||
  72. tempFSize != options.fontSize ||
  73. options.autoCollapse != collapse.getSelectedIndex();
  74. options.layoutDisplaySelection = display.getSelectedIndex();
  75. options.runStepsSelection = run.getSelectedIndex();
  76. options.fontSize = tempFSize;
  77. options.autoCollapse = collapse.getSelectedIndex();
  78. if( change )
  79. {
  80. for( ActionListener l : listeners )
  81. { // benachrichtige die Listener über die Änderungen
  82. l.actionPerformed( new ActionEvent(this, 0, "Options changed" ) );
  83. }
  84. }
  85. setVisible( false );
  86. } catch( Exception e1 )
  87. {
  88. e1.printStackTrace();
  89. }
  90. }
  91. });
  92. add( speichern );
  93. setSize( 250, 150 );
  94. listeners = new ArrayList<ActionListener>();
  95. options = new Options();
  96. setLocation( Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2 );
  97. }
  98. public int getAutoCollapseOption()
  99. {
  100. return options.autoCollapse;
  101. }
  102. public int getLayerDisplayOption()
  103. {
  104. return options.layoutDisplaySelection;
  105. }
  106. public int getRunStepsOption()
  107. {
  108. return options.runStepsSelection;
  109. }
  110. public int getFontSize()
  111. {
  112. return options.fontSize;
  113. }
  114. public void addActionListener( ActionListener al )
  115. {
  116. listeners.add( al );
  117. }
  118. }