View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.dialogs;
17  
18  import java.awt.Component;
19  import java.awt.Frame;
20  import java.awt.event.ActionEvent;
21  import java.awt.event.ActionListener;
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.List;
25  
26  import javax.swing.AbstractButton;
27  import javax.swing.ButtonGroup;
28  import javax.swing.JButton;
29  import javax.swing.JDialog;
30  import javax.swing.JPanel;
31  import javax.swing.JRadioButton;
32  import javax.swing.JTextField;
33  
34  import p3j.gui.P3J;
35  import p3j.gui.misc.P3JConfigFile;
36  import p3j.gui.panels.PropertiesShowPanelFactory;
37  import p3j.misc.Misc;
38  import p3j.misc.gui.GUI;
39  import p3j.simulation.ExecutionMode;
40  
41  /**
42   * Simple dialog to show the execution preferences.
43   * 
44   * @author Christina Bohk
45   * @author Roland Ewald
46   */
47  public class ExecutionPreferencesDialog extends JDialog {
48  
49    /** The Constant serialVersionUID. */
50    private static final long serialVersionUID = 3246519924977747183L;
51  
52    /** Width of the dialog. */
53    public static final int DIALOG_WIDTH = 600;
54  
55    /** Height of the dialog. */
56    public static final int DIALOG_HEIGHT = 200;
57  
58    /** The width of the key column in the form. */
59    private static final int FORM_KEY_WIDTH = 200;
60  
61    /** The width of the value column in the form. */
62    private static final int FORM_VALUE_WIDTH = 100;
63  
64    /** The content panel. */
65    private final JPanel contentPanel;
66  
67    /** The field for the number of trials. */
68    private final JTextField numOfTrials = new JTextField();
69  
70    /** The field for the number of parallel threads. */
71    private final JTextField numOfParallelThreads = new JTextField();
72  
73    /** The p3j configuration file. */
74    private final P3JConfigFile p3jConfiguration;
75  
76    /** The execution mode button group. */
77    private final ButtonGroup execModeButtonGroup = new ButtonGroup();
78  
79    /** The apply button. */
80    private final JButton apply = new JButton("Apply");
81    {
82      apply.addActionListener(new ApplyAction(this));
83    }
84  
85    /** The cancel button. */
86    private final JButton cancel = new JButton("Cancel");
87    {
88      cancel.addActionListener(new ActionListener() {
89        @Override
90        public void actionPerformed(ActionEvent e) {
91          setVisible(false);
92        }
93      });
94    }
95  
96    /** The 'reset to defaults'-button. */
97    private final JButton resetToDefaults = new JButton("Reset Defaults");
98    {
99      resetToDefaults.addActionListener(new ActionListener() {
100       @Override
101       public void actionPerformed(ActionEvent e) {
102 
103         numOfTrials.setText("" + Misc.DEFAULT_NUM_TRIALS);
104         numOfParallelThreads.setText("" + Misc.DEFAULT_NUM_PARALLEL_THREADS);
105 
106         ExecutionMode defaultMode = Misc.DEFAULT_EXEC_MODE;
107         Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
108             .getElements();
109         while (execModeButtons.hasMoreElements()) {
110           AbstractButton execModeButton = execModeButtons.nextElement();
111           if (execModeButton.getText().equals(defaultMode.toString())) {
112             execModeButton.setSelected(true);
113             break;
114           }
115         }
116         contentPanel.repaint();
117       }
118     });
119   }
120 
121   /** The buttons. */
122   private final List<JButton> buttons = new ArrayList<JButton>();
123   {
124     buttons.add(resetToDefaults);
125     buttons.add(cancel);
126     buttons.add(apply);
127   }
128 
129   /**
130    * Instantiates a new execution preferences dialog.
131    * 
132    * @param owner
133    *          the owner
134    */
135   public ExecutionPreferencesDialog(Frame owner) {
136     super(owner, "Edit Execution Preferences", true);
137     p3jConfiguration = P3J.getInstance().getConfigFile();
138     setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
139     GUI.centerOnScreen(this);
140 
141     PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
142         FORM_KEY_WIDTH, FORM_VALUE_WIDTH, buttons, 2);
143     numOfTrials.setText("" + p3jConfiguration.get(Misc.PREF_NUM_TRIALS));
144     pspf.app(Misc.PREF_NUM_TRIALS + ":", numOfTrials);
145 
146     numOfParallelThreads.setText(""
147         + p3jConfiguration.get(Misc.PREF_NUM_PARALLEL_THREADS));
148     pspf.app(Misc.PREF_NUM_PARALLEL_THREADS + ":", numOfParallelThreads);
149 
150     pspf.app(Misc.PREF_EXECUTION_MODE + ":", createExecutionModePanel());
151     contentPanel = pspf.constructPanel();
152     this.getContentPane().add(contentPanel);
153   }
154 
155   /**
156    * Create the panel to select an execution mode.
157    * 
158    * @return the execution mode panel
159    */
160   private JPanel createExecutionModePanel() {
161     JPanel execModePanel = new JPanel();
162     ExecutionMode currentMode = (ExecutionMode) p3jConfiguration
163         .get(Misc.PREF_EXECUTION_MODE);
164 
165     for (ExecutionMode mode : ExecutionMode.values()) {
166       JRadioButton button = new JRadioButton(mode.toString());
167       execModePanel.add(button);
168       execModeButtonGroup.add(button);
169       if (mode == currentMode) {
170         button.setSelected(true);
171       }
172     }
173     return execModePanel;
174   }
175 
176   /**
177    * The application of the new preferences.
178    */
179   class ApplyAction implements ActionListener {
180 
181     /** The owner. */
182     private Component owner;
183 
184     /**
185      * Instantiates a new apply action executioner.
186      * 
187      * @param own
188      *          the owner
189      */
190     ApplyAction(Component own) {
191       owner = own;
192     }
193 
194     /*
195      * (non-Javadoc)
196      * 
197      * @see
198      * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
199      */
200     @Override
201     public void actionPerformed(ActionEvent e) {
202       try {
203         Integer numTrials = Integer.parseInt(numOfTrials.getText());
204         Integer numParallelThreads = Integer.parseInt(numOfParallelThreads
205             .getText());
206 
207         Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
208             .getElements();
209         ExecutionMode execMode = Misc.DEFAULT_EXEC_MODE;
210         while (execModeButtons.hasMoreElements()) {
211           AbstractButton execModeButton = execModeButtons.nextElement();
212           if (execModeButton.isSelected()) {
213             execMode = ExecutionMode.forString(execModeButton.getText());
214             break;
215           }
216         }
217 
218         // Increase number of trials in case they are not a multiple of the
219         // number of threads
220         if (numTrials % numParallelThreads != 0)
221           numTrials += (numParallelThreads - numTrials % numParallelThreads);
222 
223         p3jConfiguration.put(Misc.PREF_NUM_TRIALS, numTrials);
224         p3jConfiguration
225             .put(Misc.PREF_NUM_PARALLEL_THREADS, numParallelThreads);
226         p3jConfiguration.put(Misc.PREF_EXECUTION_MODE, execMode);
227         setVisible(false);
228       } catch (Exception ex) {
229         GUI.printErrorMessage(owner, "Error applying new settings.",
230             "The new settings cannot be applied:" + ex.getMessage(), ex);
231       }
232     }
233   }
234 }