View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.gui.preferences;
25  
26  import java.awt.Component;
27  import java.awt.GridBagConstraints;
28  import java.awt.GridBagLayout;
29  import java.awt.Insets;
30  
31  import javax.swing.JCheckBox;
32  import javax.swing.JPanel;
33  import javax.swing.JPasswordField;
34  import javax.swing.JTextField;
35  
36  import com.mindtree.techworks.insight.preferences.model.Preference;
37  import com.mindtree.techworks.insight.preferences.model.PreferenceAttribute;
38  import com.mindtree.techworks.insight.preferences.model.PreferenceAttributeType;
39  
40  /**
41   * The <code>AbstractPreferencesUIPanel</code> abstract class is the base GUI
42   * panel for rendering Insight Preference instances. Specific sub-types render
43   * the respective Preference details. All subtypes need to provide a no-args
44   * constructor to permit instantiation using Reflection. This panel uses a
45   * GrigBagLayout. Derived types may add widgets using
46   * 
47   * @see #addComponent(Component, int, int, int, int, int, int)
48   * @see com.mindtree.techworks.insight.preferences.model.Preference
49   * @author Regunath B
50   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
51   */
52  public abstract class AbstractPreferencesUIPanel extends JPanel {
53  
54  	/**
55  	 * Used for object serialization
56  	 */
57  	private static final long serialVersionUID = -1986410111937810144L;
58  
59  	/**
60  	 * Constant for 'true' and 'false' boolean value
61  	 */
62  	protected static final String TRUE = "true";
63  
64  	/**
65  	 * Constant for 'true' and 'false' boolean value
66  	 */
67  	protected static final String FALSE = "false";
68  
69  	/**
70  	 * The Preference rendered by this panel
71  	 */
72  	protected Preference preference;
73  
74  	/**
75  	 * The PreferencesFrame for this panel
76  	 */
77  	protected PreferencesFrame parent;
78  
79  	/**
80  	 * Layout definition.
81  	 */
82  	private GridBagLayout gl;
83  
84  	/**
85  	 * Layout constants
86  	 */
87  	private GridBagConstraints gc;
88  
89  	/**
90  	 * Initializes this class with the Preference specified. Also sets up the
91  	 * display layout.
92  	 * 
93  	 * @param preferenceToLoad
94  	 *            the Preference to be displayed by this panel
95  	 * @param parentComponent
96  	 *            The parent container of this component
97  	 */
98  	public void initialize (Preference preferenceToLoad,
99  			PreferencesFrame parentComponent) {
100 
101 		this.preference = preferenceToLoad;
102 		this.parent = parentComponent;
103 		this.gl = new GridBagLayout ();
104 		this.gc = new GridBagConstraints ();
105 		setLayout (gl);
106 		initializeDisplay ();
107 	}
108 
109 	/**
110 	 * Abstract method to initialize the display contents. This method is called
111 	 * after initialize() has been invoked.
112 	 * 
113 	 * @see #initialize(Preference, PreferencesFrame)
114 	 */
115 	protected abstract void initializeDisplay ();
116 
117 	/**
118 	 * Abstract method that informs this panel to set the edited preference
119 	 * values to the Preference that this panel was initialized with.
120 	 * 
121 	 * @see #initialize(Preference, PreferencesFrame)
122 	 */
123 	protected abstract void setPreferenceValues ();
124 
125 	/**
126 	 * Adds the specified component to this panel using the specified
127 	 * constraints.
128 	 * 
129 	 * @param c
130 	 *            The component to add
131 	 * @param gridx
132 	 *            Horizontal location on the grid
133 	 * @param gridy
134 	 *            Vertical location on the grid
135 	 * @param weightx
136 	 *            The horizontal weight
137 	 * @param weighty
138 	 *            The vertical weight
139 	 * @param width
140 	 *            The width of the component
141 	 * @param height
142 	 *            The height of the component
143 	 */
144 	protected final void addComponent (Component c, int gridx, int gridy,
145 			int weightx, int weighty, int width, int height) {
146 
147 		gc.fill = GridBagConstraints.BOTH;
148 		gc.anchor = GridBagConstraints.NORTHWEST;
149 		gc.insets = new Insets (5, 5, 5, 5);
150 		gc.gridx = gridx;
151 		gc.gridy = gridy;
152 		gc.weightx = weightx;
153 		gc.weighty = weighty;
154 		gc.gridwidth = width;
155 		gc.gridheight = height;
156 		gl.setConstraints (c, gc);
157 		add (c);
158 	}
159 
160 	/**
161 	 * Helper method to created and add a PreferenceAttribute of type TEXT and
162 	 * containing the specified valued
163 	 * 
164 	 * @param childPreference
165 	 *            the Preference to add the attribute to
166 	 * @param id
167 	 *            id of the attribute
168 	 * @param name
169 	 *            name of the attribute
170 	 * @param textField
171 	 *            the JTextField to pick up the value from
172 	 */
173 	protected void createAndAddTextAttribute (Preference childPreference,
174 			String id, String name, JTextField textField) {
175 
176 		PreferenceAttribute attribute = new PreferenceAttribute (
177 				PreferenceAttributeType.TEXT, id, textField.getText (), false, true,
178 				true, childPreference);
179 		attribute.setName (name);
180 		childPreference.addPreferenceAttribute (attribute);
181 	}
182 
183 	/**
184 	 * Helper method to created and add a password PreferenceAttribute of type
185 	 * TEXT and containing the specified valued
186 	 * 
187 	 * @param childPreference
188 	 *            the Preference to add the attribute to
189 	 * @param id
190 	 *            id of the attribute
191 	 * @param name
192 	 *            name of the attribute
193 	 * @param passwordField
194 	 *            the JPasswordField to pick up the value from
195 	 */
196 	protected void createAndAddPasswordAttribute (Preference childPreference,
197 			String id, String name, JPasswordField passwordField) {
198 
199 		PreferenceAttribute attribute = new PreferenceAttribute (
200 				PreferenceAttributeType.TEXT, id, new String (passwordField
201 						.getPassword ()), true, true, true, childPreference);
202 		attribute.setName (name);
203 		childPreference.addPreferenceAttribute (attribute);
204 	}
205 
206 	/**
207 	 * Helper method to created and add a PreferenceAttribute of type CHECK and
208 	 * containing the specified valued
209 	 * 
210 	 * @param childPreference
211 	 *            the Preference to add the attribute to
212 	 * @param id
213 	 *            id of the attribute
214 	 * @param name
215 	 *            name of the attribute
216 	 * @param checkBox
217 	 *            the JCheckBox to pick up the value from
218 	 */
219 	protected void createAndAddBooleanAttribute (Preference childPreference,
220 			String id, String name, JCheckBox checkBox) {
221 
222 		PreferenceAttribute authenticationAttribute = new PreferenceAttribute (
223 				PreferenceAttributeType.CHECK, id,
224 				checkBox.isSelected () ? TRUE : FALSE, false, true, true,
225 				childPreference);
226 		authenticationAttribute.setName (name);
227 		childPreference.addPreferenceAttribute (authenticationAttribute);
228 	}
229 
230 }