View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  /*
19   * Project: KernelConfigurator
20   * Created on 26-feb-2004
21   *
22   * Copyright (c)2003 Grid Systems
23   */
24  package com.gridsystems.config.modules.jvm;
25  
26  import java.awt.GridBagConstraints;
27  import java.awt.GridBagLayout;
28  
29  import javax.swing.JCheckBox;
30  import javax.swing.JComponent;
31  import javax.swing.JPanel;
32  import javax.swing.JTextField;
33  
34  import com.gridsystems.config.Configurator;
35  import com.gridsystems.config.ConfiguratorModel;
36  import com.gridsystems.config.app.UI;
37  import com.gridsystems.config.tools.ErrorSupport;
38  import com.gridsystems.config.tools.PortVerifier;
39  import com.gridsystems.config.tools.swing.BasePanel;
40  import com.gridsystems.config.tools.swing.SwingTools;
41  
42  /**
43   * Swing View for the JVM Configurator.
44   *
45   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
46   * @version 1.0
47   */
48  public class JVMSwingView extends BasePanel {
49    /**
50     * Large text fields length.
51     */
52    private static final int LARGE_FIELD_LENGTH = 12;
53  
54    /**
55     * Normal text fields length.
56     */
57    private static final int NORMAL_FIELD_LENGTH = 6;
58  
59    /**
60     * Proxy activation flag.
61     */
62    JCheckBox cbxProxyEnabled = new JCheckBox();
63  
64    /**
65     * Proxy host address flag.
66     */
67    JTextField edProxyHost = new JTextField(LARGE_FIELD_LENGTH);
68  
69    /**
70     * Proxy port address flag.
71     */
72    JTextField edProxyPort = new JTextField(NORMAL_FIELD_LENGTH);
73  
74    /**
75     * Minimum heap size field.
76     */
77    JTextField edMinHeap = new JTextField(NORMAL_FIELD_LENGTH);
78  
79    /**
80     * Maximum heap size field.
81     */
82    JTextField edMaxHeap = new JTextField(NORMAL_FIELD_LENGTH);
83  
84    /**
85     * Thread stack size field.
86     */
87    JTextField edStackSize = new JTextField(NORMAL_FIELD_LENGTH);
88  
89    /**
90     * Remote debug enabled flag.
91     */
92    JCheckBox cbxDebugEnabled = new JCheckBox();
93  
94    /**
95     * Remote debug port field.
96     */
97    JTextField edDebugPort = new JTextField(NORMAL_FIELD_LENGTH);
98  
99    /**
100    * Remote debug start mode flag.
101    */
102   JCheckBox cbxSuspend = new JCheckBox();
103 
104   /**
105    * One-time initialization flag.
106    */
107   boolean initialized = false;
108 
109   /**
110    * Default constructor.
111    *
112    * @param config The configurator this view is associated to
113    */
114   public JVMSwingView(Configurator config) {
115     super(config);
116     setBundle(JVMConfigurator.BUNDLE);
117 
118     setIcon(SwingTools.loadIcon(JVMConfigurator.class, "icon.gif"));
119     setSmallIcon(SwingTools.loadIcon(JVMConfigurator.class, "small.gif"));
120 
121     init();
122   }
123 
124   /**
125    * View initialization code.
126    */
127   private void init() {
128     if (initialized) {
129       return;
130     }
131     initialized = true;
132 
133     ErrorSupport support = new ErrorSupport(this.getConfigurator());
134 
135     this.setLayout(new GridBagLayout());
136     JPanel pnlMemory = createMemoryPanel(support);
137 
138     GridBagConstraints gc = new GridBagConstraints();
139     gc.fill = UI.isExpertMode() ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL;
140     gc.gridx = GridBagConstraints.REMAINDER;
141     gc.anchor = GridBagConstraints.NORTH;
142     gc.weightx = 1.0;
143     gc.weighty = 1.0;
144 
145     this.add(pnlMemory, gc);
146 
147     if (UI.isExpertMode()) {
148       JPanel pnlProxy  = createProxyPanel(support);
149       JPanel pnlDebug  = createDebugPanel(support);
150       this.add(pnlProxy, gc);
151       this.add(pnlDebug, gc);
152     }
153   }
154 
155   /**
156    * Transfers data from the internal data structure to the GUI fields.
157    *
158    * @param model The model
159    */
160   public void getValues(ConfiguratorModel model) {
161     JVMConfigModel data = (JVMConfigModel)model;
162     edMinHeap.setText(data.getMinHeap());
163     edMaxHeap.setText(data.getMaxHeap());
164     edStackSize.setText(data.getStackSize());
165 
166     if (UI.isExpertMode()) {
167       boolean flag = data.getUseProxy().equals("true");
168       cbxProxyEnabled.setSelected(flag);
169       edProxyHost.setText(data.getProxyHost());
170       edProxyPort.setText(data.getProxyPort());
171       edProxyHost.setEnabled(flag);
172       edProxyPort.setEnabled(flag);
173 
174       flag = data.getDebugEnabled().equals("true");
175       cbxDebugEnabled.setSelected(flag);
176       edDebugPort.setText(data.getDebugPort());
177       edDebugPort.setEnabled(flag);
178       cbxSuspend.setEnabled(flag);
179 
180       flag = data.getDebugSuspend().equals("true");
181       cbxSuspend.setSelected(flag);
182     }
183 
184     setModified(this, false);
185   }
186 
187   /**
188    * Transfers data from the GUI fields to the internal data structure.
189    *
190    * @param model The model
191    */
192   public void setValues(ConfiguratorModel model) {
193     JVMConfigModel data = (JVMConfigModel)model;
194     data.setValue("heap.min", edMinHeap.getText());
195     data.setValue("heap.max", edMaxHeap.getText());
196     data.setValue("stack.size", edStackSize.getText());
197 
198     if (UI.isExpertMode()) {
199       data.setValue("proxy.use", cbxProxyEnabled.isSelected() ? "true" : "false");
200       data.setValue("proxy.host", edProxyHost.getText());
201       data.setValue("proxy.port", edProxyPort.getText());
202 
203       data.setValue("debug.enable", cbxDebugEnabled.isSelected() ? "true" : "false");
204       data.setValue("debug.port", edDebugPort.getText());
205       data.setValue("debug.suspend", cbxSuspend.isSelected() ? "true" : "false");
206     }
207 
208     setModified(this, false);
209   }
210 
211   /**
212    * Creates a panel containing the "Memory Settings" related fields.
213    *
214    * @param support The ErrorSupport instance
215    * @return The "Memory Settings" fields container
216    */
217   private JPanel createMemoryPanel(ErrorSupport support) {
218     String s = getString("pnlMemory.title");
219     JPanel panel = SwingTools.getGroupPanel(s);
220 
221     SwingTools.addField(panel, getString("pnlMemory.lblMinHeap"), edMinHeap, 0);
222     SwingTools.addField(panel, getString("pnlMemory.lblStackSize"), edStackSize, 0);
223     SwingTools.addField(panel, getString("pnlMemory.lblMaxHeap"), edMaxHeap, 1);
224 
225     support.addError("badMemSize1", "MinHeap: " + getString("errors.badMemSize"));
226     support.addError("badMemSize2", "MaxHeap: " + getString("errors.badMemSize"));
227     support.addError("badMemSize3", "StackSize: " + getString("errors.badMemSize"));
228 
229     edMinHeap.setInputVerifier(new MemSizeVerifier(support, "badMemSize1"));
230     edMaxHeap.setInputVerifier(new MemSizeVerifier(support, "badMemSize2"));
231     edStackSize.setInputVerifier(new MemSizeVerifier(support, "badMemSize3", true));
232 
233     watch(edMinHeap);
234     watch(edMaxHeap);
235     watch(edStackSize);
236 
237     return panel;
238   }
239 
240   /**
241    * Creates a panel containing the "Proxy Settings" related fields.
242    *
243    * @param support The ErrorSupport instance
244    * @return The "Proxy Settings" fields container
245    */
246   private JPanel createProxyPanel(ErrorSupport support) {
247     String title = getString("pnlProxy.title");
248     JPanel panel = SwingTools.getGroupPanel(title, new GridBagLayout());
249 
250     cbxProxyEnabled.setText(getString("pnlProxy.cbxProxyEnabled"));
251 
252     GridBagConstraints gc = new GridBagConstraints();
253     gc.gridwidth = 2;
254     gc.weightx = 1.0;
255     gc.anchor = GridBagConstraints.LINE_START;
256     gc.fill = GridBagConstraints.BOTH;
257     panel.add(cbxProxyEnabled, gc);
258 
259     SwingTools.addField(panel, getString("pnlProxy.lblProxyHost"), edProxyHost, 1);
260     SwingTools.addField(panel, getString("pnlProxy.lblProxyPort"), edProxyPort, 2);
261 
262     this.watch(edProxyHost);
263     this.watch(edProxyPort);
264 
265     support.addError("badProxyPort", getString("errors.badProxyPort"));
266     edProxyPort.setInputVerifier(new PortVerifier(support, "badProxyPort"));
267 
268     JComponent[] guarded = { edProxyHost, edProxyPort };
269     setGuarded(cbxProxyEnabled, guarded);
270     return panel;
271   }
272 
273   /**
274    * Creates a panel containing the "Debug Mode" related fields.
275    *
276    * @param support The ErrorSupport instance
277    * @return The "Debug Mode" fields container
278    */
279   private JPanel createDebugPanel(ErrorSupport support) {
280     String title = getString("pnlDebug.title");
281     JPanel panel = SwingTools.getGroupPanel(title);
282 
283     cbxDebugEnabled.setText(getString("pnlDebug.cbxDebugEnabled"));
284     GridBagConstraints gc = new GridBagConstraints();
285     gc.gridwidth = 2;
286     gc.weightx = 1.0;
287     gc.anchor = GridBagConstraints.LINE_START;
288     gc.fill = GridBagConstraints.BOTH;
289     panel.add(cbxDebugEnabled, gc);
290 
291     SwingTools.addField(panel, getString("pnlDebug.lblDebugPort"), edDebugPort, 1);
292     SwingTools.addField(panel, getString("pnlDebug.cbxSuspend"), cbxSuspend, 2);
293 
294     JComponent[] guarded = { edDebugPort, cbxSuspend };
295 
296     setGuarded(cbxDebugEnabled, guarded);
297     setGuarded(cbxSuspend, null);
298     watch(edDebugPort);
299 
300     support.addError("badDebugPort", getString("errors.badDebugPort"));
301     edDebugPort.setInputVerifier(new PortVerifier(support, "badDebugPort"));
302 
303     return panel;
304   }
305 
306 }