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 = 700;
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 = 110;
60  
61  	/** The content panel. */
62  	private final JPanel contentPanel;
63  
64  	/** The field for the number of trials. */
65  	private final JTextField numOfTrials = new JTextField();
66  
67  	/** The field for the number of parallel threads. */
68  	private final JTextField numOfParallelThreads = new JTextField();
69  
70  	/** The p3j configuration file. */
71  	private final P3JConfigFile p3jConfiguration;
72  
73  	/** The execution mode button group. */
74  	private final ButtonGroup execModeButtonGroup = new ButtonGroup();
75  
76  	/** The apply button. */
77  	private final JButton apply = new JButton("Apply");
78  	{
79  		apply.addActionListener(new ApplyAction(this));
80  	}
81  
82  	/** The cancel button. */
83  	private final JButton cancel = new JButton("Cancel");
84  	{
85  		cancel.addActionListener(new ActionListener() {
86  			@Override
87  			public void actionPerformed(ActionEvent e) {
88  				setVisible(false);
89  			}
90  		});
91  	}
92  
93  	/** The 'reset to defaults'-button. */
94  	private final JButton resetToDefaults = new JButton("Reset Defaults");
95  	{
96  		resetToDefaults.addActionListener(new ActionListener() {
97  			@Override
98  			public void actionPerformed(ActionEvent e) {
99  
100 				numOfTrials.setText("" + Misc.DEFAULT_NUM_TRIALS);
101 				numOfParallelThreads.setText("" + Misc.DEFAULT_NUM_PARALLEL_THREADS);
102 
103 				ExecutionMode defaultMode = Misc.DEFAULT_EXEC_MODE;
104 				Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
105 				    .getElements();
106 				while (execModeButtons.hasMoreElements()) {
107 					AbstractButton execModeButton = execModeButtons.nextElement();
108 					if (execModeButton.getText().equals(defaultMode.toString())) {
109 						execModeButton.setSelected(true);
110 						break;
111 					}
112 				}
113 				contentPanel.repaint();
114 			}
115 		});
116 	}
117 
118 	/** The buttons. */
119 	private final List<JButton> buttons = new ArrayList<JButton>();
120 	{
121 		buttons.add(resetToDefaults);
122 		buttons.add(cancel);
123 		buttons.add(apply);
124 	}
125 
126 	/**
127 	 * Instantiates a new execution preferences dialog.
128 	 * 
129 	 * @param owner
130 	 *          the owner
131 	 */
132 	public ExecutionPreferencesDialog(Frame owner) {
133 		super(owner, "Edit Execution Preferences", true);
134 		p3jConfiguration = P3J.getInstance().getConfigFile();
135 		setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
136 		GUI.centerOnScreen(this);
137 
138 		PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
139 		    FORM_KEY_WIDTH, buttons, 2);
140 		numOfTrials.setText("" + p3jConfiguration.get(Misc.PREF_NUM_TRIALS));
141 		pspf.app(Misc.PREF_NUM_TRIALS + ":", numOfTrials);
142 
143 		numOfParallelThreads.setText(""
144 		    + p3jConfiguration.get(Misc.PREF_NUM_PARALLEL_THREADS));
145 		pspf.app(Misc.PREF_NUM_PARALLEL_THREADS + ":", numOfParallelThreads);
146 
147 		pspf.app(Misc.PREF_EXECUTION_MODE + ":", createExecutionModePanel());
148 		contentPanel = pspf.constructPanel();
149 		this.getContentPane().add(contentPanel);
150 	}
151 
152 	/**
153 	 * Create the panel to select an execution mode.
154 	 * 
155 	 * @return the execution mode panel
156 	 */
157 	private JPanel createExecutionModePanel() {
158 		JPanel execModePanel = new JPanel();
159 		ExecutionMode currentMode = (ExecutionMode) p3jConfiguration
160 		    .get(Misc.PREF_EXECUTION_MODE);
161 
162 		for (ExecutionMode mode : ExecutionMode.values()) {
163 			JRadioButton button = new JRadioButton(mode.toString());
164 			execModePanel.add(button);
165 			execModeButtonGroup.add(button);
166 			if (mode == currentMode) {
167 				button.setSelected(true);
168 			}
169 		}
170 		return execModePanel;
171 	}
172 
173 	/**
174 	 * The application of the new preferences.
175 	 */
176 	class ApplyAction implements ActionListener {
177 
178 		/** The owner. */
179 		private Component owner;
180 
181 		/**
182 		 * Instantiates a new apply action executioner.
183 		 * 
184 		 * @param own
185 		 *          the owner
186 		 */
187 		ApplyAction(Component own) {
188 			owner = own;
189 		}
190 
191 		/*
192 		 * (non-Javadoc)
193 		 * 
194 		 * @see
195 		 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
196 		 */
197 		@Override
198 		public void actionPerformed(ActionEvent e) {
199 			try {
200 				Integer numTrials = Integer.parseInt(numOfTrials.getText());
201 				Integer numParallelThreads = Integer.parseInt(numOfParallelThreads
202 				    .getText());
203 
204 				Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
205 				    .getElements();
206 				ExecutionMode execMode = Misc.DEFAULT_EXEC_MODE;
207 				while (execModeButtons.hasMoreElements()) {
208 					AbstractButton execModeButton = execModeButtons.nextElement();
209 					if (execModeButton.isSelected()) {
210 						execMode = ExecutionMode.forString(execModeButton.getText());
211 						break;
212 					}
213 				}
214 
215 				p3jConfiguration.put(Misc.PREF_NUM_TRIALS, numTrials);
216 				p3jConfiguration
217 				    .put(Misc.PREF_NUM_PARALLEL_THREADS, numParallelThreads);
218 				p3jConfiguration.put(Misc.PREF_EXECUTION_MODE, execMode);
219 				setVisible(false);
220 			} catch (Exception ex) {
221 				GUI.printErrorMessage(owner, "Error applying new settings.",
222 				    "The new settings cannot be applied:" + ex.getMessage(), ex);
223 			}
224 		}
225 	}
226 }