Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
/* |
14 |
* SettingsPanel.java |
15 |
* |
16 |
* Created on April 24, 2002, 5:40 PM |
17 |
*/ |
18 |
|
19 |
package org.netbeans.modules.java.imptool; |
20 |
import java.awt.BorderLayout; |
21 |
import java.awt.event.ActionEvent; |
22 |
import java.awt.event.ActionListener; |
23 |
import java.beans.*; |
24 |
import java.util.ArrayList; |
25 |
import javax.swing.*; |
26 |
import javax.swing.event.EventListenerList; |
27 |
import org.openide.nodes.*; |
28 |
import org.openide.TopManager; |
29 |
import org.openide.ErrorManager; |
30 |
import org.openide.options.SystemOption; |
31 |
import org.openide.util.NbBundle; |
32 |
import org.openide.util.SharedClassObject; |
33 |
|
34 |
/** A panel that displays the settings for the import management tool. Includes a combobox that |
35 |
* contains some easy-to-use defaults (displays a "custom" item if the defaults have |
36 |
* been changed in Tools | Options) and an Advanced button for manually setting the settings. |
37 |
* <P>The defaults are more or less low-noise, default and high-noise, in terms of raising the |
38 |
* amounts of single imports and fully qualified names to be used. |
39 |
* @author Tim Boudreau |
40 |
* @version 0.1 |
41 |
*/ |
42 |
public class SettingsPanel extends JPanel implements PropertyChangeListener { |
43 |
//defaults for minimal fqn |
44 |
private static final int MIN_FQN = 0; |
45 |
private static final int MIN_SI = 2; |
46 |
//defaults for default fqn - XXX nd to check actual defaults |
47 |
private static final int DEF_FQN = 4; |
48 |
private static final int DEF_SI = 4; |
49 |
//defaults for maximal fqn |
50 |
private static final int MAX_FQN = 100; |
51 |
private static final int MAX_SI = 100; |
52 |
/** Flag to stop ActionPerformed events from being fired. */ |
53 |
private boolean isSetting = true; |
54 |
private ImpToolSettings settingsOption; |
55 |
private JComboBox chooserCombo; |
56 |
private JButton advancedButton; |
57 |
private SettingData customItem = null; |
58 |
private SettingData[] modelData=null; |
59 |
/** Flag variable to indicate when to ignore propertyChangeEvents (e.g. when the |
60 |
* component is the initiator) */ |
61 |
private boolean isChanging; |
62 |
/** Utility field used by event firing mechanism. */ |
63 |
private EventListenerList listenerList = null; |
64 |
|
65 |
/** Creates a new instance of SettingsPanel */ |
66 |
public SettingsPanel() { |
67 |
init(); |
68 |
} |
69 |
|
70 |
/** Create the initial components. */ |
71 |
private void init() { |
72 |
setLayout(new BorderLayout()); |
73 |
JTextArea jta = new JTextArea(NbBundle.getBundle(SettingsPanel.class).getString("LBL_SETTINGCOMBO")); //NOI18N |
74 |
jta.setEditable(false); |
75 |
jta.setFont(javax.swing.UIManager.getFont("Label.font")); |
76 |
jta.setDisabledTextColor(javax.swing.UIManager.getColor("Label.foreground")); |
77 |
jta.setEnabled(false); |
78 |
jta.setOpaque(false); |
79 |
add(jta, BorderLayout.WEST); |
80 |
chooserCombo = new JComboBox(); |
81 |
add(chooserCombo, BorderLayout.CENTER); |
82 |
advancedButton = new JButton(NbBundle.getMessage(SettingsPanel.class, "LBL_ADVANCED")); //NOI18N |
83 |
add (advancedButton, BorderLayout.EAST); |
84 |
ActionListener acl = new ActionListener () { |
85 |
public void actionPerformed(ActionEvent ev) { |
86 |
if (ev.getSource()==chooserCombo) { |
87 |
if (isSetting) return; |
88 |
SettingsPanel.this.processChooserEvent(); |
89 |
} else { |
90 |
SettingsPanel.this.processButtonEvent(); |
91 |
} |
92 |
} |
93 |
}; |
94 |
chooserCombo.addActionListener (acl); |
95 |
advancedButton.addActionListener(acl); |
96 |
initializeChooser(); |
97 |
} |
98 |
|
99 |
/** Initialize the chooser with data reflecting the defaults, and if the current |
100 |
* settings are not a default, a Custom setting item to reflect those settings, |
101 |
* which will be the selected item. */ |
102 |
private void initializeChooser() { |
103 |
chooserCombo.setModel(new DefaultComboBoxModel (createModelData())); |
104 |
updateComponent(); |
105 |
} |
106 |
|
107 |
/** Create the model for the dataModel for the combo box. */ |
108 |
private SettingData[] createModelData () { |
109 |
customItem = new SettingData(); |
110 |
SettingData min = |
111 |
new SettingData (MIN_FQN, MIN_SI, |
112 |
NbBundle.getBundle(SettingsPanel.class).getString("CHOICE_MINFQN") |
113 |
); //NOI18N |
114 |
boolean currIsPreset = customItem.equals (min); |
115 |
SettingData def = |
116 |
new SettingData (DEF_FQN, DEF_SI, |
117 |
NbBundle.getBundle(SettingsPanel.class).getString("CHOICE_DEFFQN") |
118 |
); //NOI18N |
119 |
currIsPreset |= customItem.equals (def); |
120 |
SettingData max = |
121 |
new SettingData (MAX_FQN, MAX_SI, |
122 |
NbBundle.getBundle(SettingsPanel.class).getString("CHOICE_MAXFQN") |
123 |
); //NOI18N |
124 |
//if there are some non-default settings in Options, use them and make default |
125 |
currIsPreset |= customItem.equals (max); |
126 |
if (currIsPreset) { |
127 |
modelData = new SettingData[] { |
128 |
min, def, max |
129 |
}; |
130 |
} else { |
131 |
modelData = new SettingData[] { |
132 |
customItem, min, def, max |
133 |
}; |
134 |
} |
135 |
return modelData; |
136 |
} |
137 |
|
138 |
/** Synchronize the combo-box and tooltip with the current settings. If the |
139 |
* settings have changed, see if the setting is one of the defaults. If not, |
140 |
* create (or select if already created) the Custom settings item and select it. */ |
141 |
public void updateComponent () { |
142 |
int itemIndex=-1; |
143 |
for (int i=0; i < modelData.length; i++) { |
144 |
if (modelData[i].isCurrent()) { |
145 |
itemIndex=i; |
146 |
break; |
147 |
} |
148 |
} |
149 |
isSetting = true; |
150 |
if (itemIndex >=0) { |
151 |
chooserCombo.setSelectedIndex(itemIndex); |
152 |
} else { |
153 |
if (modelData.length == 3) { |
154 |
addCustomItem(); |
155 |
} |
156 |
chooserCombo.setSelectedIndex(0); |
157 |
customItem.update(); |
158 |
} |
159 |
isSetting = false; |
160 |
updateChooserTooltip(); |
161 |
} |
162 |
|
163 |
/** Create the custom item and add it to the combo box. */ |
164 |
private void addCustomItem () { |
165 |
//regen the array w/ added CustomItem |
166 |
modelData = new SettingData[] { |
167 |
modelData[0], modelData[1], modelData[2], customItem |
168 |
}; |
169 |
customItem.update(); |
170 |
chooserCombo.setModel(new DefaultComboBoxModel (createModelData())); |
171 |
} |
172 |
|
173 |
/** Update the tooltip to reflect the current settings. */ |
174 |
private void updateChooserTooltip () { |
175 |
StringBuffer sb = new StringBuffer(); |
176 |
sb.append (NbBundle.getBundle (SettingsPanel.class).getString ("TT_FQN")); //NOI18N |
177 |
sb.append (getSettings().getMaxFqn()); |
178 |
sb.append (" "); //NOI18N |
179 |
sb.append (NbBundle.getBundle (SettingsPanel.class).getString ("TT_SI")); //NOI18N |
180 |
sb.append (getSettings().getSingleImports()); |
181 |
chooserCombo.setToolTipText(sb.toString()); |
182 |
} |
183 |
|
184 |
/** Is the current item one of the defaults? */ |
185 |
|
186 |
/** Called when the component gets a parent. Adds a propertyChangeListener to |
187 |
* the settings node. */ |
188 |
public void addNotify() { |
189 |
getSettings().addPropertyChangeListener(this); |
190 |
super.addNotify(); |
191 |
} |
192 |
|
193 |
/** Called when the component is removed from a window. Remove listeners. */ |
194 |
public void removeNotify() { |
195 |
super.removeNotify(); |
196 |
getSettings().removePropertyChangeListener(this); |
197 |
} |
198 |
|
199 |
/** Returns a SystemOption representing the settings for the import management tool. */ |
200 |
private ImpToolSettings getSettings() { |
201 |
if (settingsOption == null) { |
202 |
settingsOption = (ImpToolSettings) SharedClassObject.findObject (ImpToolSettings.class, true); |
203 |
} |
204 |
return settingsOption; |
205 |
} |
206 |
|
207 |
/** Handle a selection being made in the combo-box. */ |
208 |
private final void processChooserEvent() { |
209 |
SettingData selectedChoice = (SettingData) chooserCombo.getSelectedItem(); |
210 |
ImpToolSettings its = getSettings(); |
211 |
isChanging = true; |
212 |
its.setMaxFqn (selectedChoice.maxfqn); |
213 |
its.setSingleImports (selectedChoice.singleimports); |
214 |
isChanging = false; |
215 |
updateComponent(); |
216 |
fireActionPerformed(); |
217 |
} |
218 |
|
219 |
/** Handle the button being clicked by bringing up a property sheet. */ |
220 |
private synchronized final void processButtonEvent() { |
221 |
//display advanced dialog here |
222 |
Node n=null; |
223 |
try { |
224 |
n = new BeanNode (getSettings()); |
225 |
} catch (IntrospectionException e) { |
226 |
ErrorManager errMan=TopManager.getDefault().getErrorManager(); |
227 |
if (errMan!=null) { |
228 |
errMan.annotate(e, e.getMessage()); |
229 |
} |
230 |
} |
231 |
TopManager.getDefault().getNodeOperation().showProperties(n); |
232 |
} |
233 |
|
234 |
/** Registers ActionListener to receive events. |
235 |
* @param listener The listener to register. |
236 |
*/ |
237 |
public synchronized void addActionListener(ActionListener listener) { |
238 |
if (listenerList == null ) { |
239 |
listenerList = new EventListenerList(); |
240 |
} |
241 |
listenerList.add(ActionListener.class, listener); |
242 |
} |
243 |
|
244 |
/** Removes ActionListener from the list of listeners. |
245 |
* @param listener The listener to remove. |
246 |
*/ |
247 |
public synchronized void removeActionListener(ActionListener listener) { |
248 |
listenerList.remove(ActionListener.class, listener); |
249 |
} |
250 |
|
251 |
/** Notifies all registered listeners about the event. |
252 |
* |
253 |
* @param event The event to be fired |
254 |
*/ |
255 |
private void fireActionPerformed() { |
256 |
if (isSetting) return; |
257 |
if (listenerList == null) return; |
258 |
ActionEvent event = new ActionEvent(this, 0, null); |
259 |
Object[] listeners = listenerList.getListenerList(); |
260 |
for (int i = listeners.length-2; i>=0; i-=2) { |
261 |
if (listeners[i]==ActionListener.class) { |
262 |
((ActionListener)listeners[i+1]).actionPerformed(event); |
263 |
} |
264 |
} |
265 |
} |
266 |
|
267 |
/** Called when a property change is fired on the settings object. Since any change |
268 |
* we perform will result in two propertyChangeEvents, we must be careful not to |
269 |
* prematurely update the components and fire ActionPerformed events. |
270 |
* @param propertyChangeEvent The event in question */ |
271 |
public void propertyChange(PropertyChangeEvent propertyChangeEvent) { |
272 |
//don't update from here during property change because changing |
273 |
//presets creates two property change events. |
274 |
if (!isChanging) { |
275 |
updateComponent(); |
276 |
fireActionPerformed(); |
277 |
} |
278 |
} |
279 |
|
280 |
/** Wrapper class for the defaults. */ |
281 |
private class SettingData extends Object { |
282 |
private String name; |
283 |
int maxfqn = 0; |
284 |
int singleimports = 0; |
285 |
|
286 |
/** Create a new SettingData object which will initialize itself from the current |
287 |
* settings for the IDE. */ |
288 |
private SettingData () { |
289 |
update(); |
290 |
name = NbBundle.getBundle (SettingsPanel.class).getString("CHOICE_CUSTOM"); //NOI18N |
291 |
} |
292 |
|
293 |
private void update () { |
294 |
ImpToolSettings its = getSettings(); |
295 |
this.maxfqn = its.getMaxFqn(); |
296 |
this.singleimports = its.getSingleImports(); |
297 |
} |
298 |
|
299 |
/** Create a SettingData object and initialize it with explicit values. */ |
300 |
private SettingData (int maxfqn, int singleimports, String name) { |
301 |
this.maxfqn = maxfqn; |
302 |
this.singleimports = singleimports; |
303 |
this.name = name; |
304 |
} |
305 |
|
306 |
/** See if this item matches the current settings for the IDE. */ |
307 |
public boolean isCurrent () { |
308 |
ImpToolSettings its = getSettings(); |
309 |
return (its.getMaxFqn() == maxfqn) && |
310 |
(its.getSingleImports() == singleimports); |
311 |
} |
312 |
|
313 |
public String toString () { |
314 |
return name; |
315 |
} |
316 |
|
317 |
/** See if the values represented by this item match another (does not compare the |
318 |
* name property). */ |
319 |
public boolean equals (Object o) { |
320 |
if (o.getClass() != SettingData.class) return false; |
321 |
boolean result = |
322 |
((((SettingData) o).maxfqn == maxfqn) && |
323 |
(((SettingData) o).singleimports == singleimports)); |
324 |
return result; |
325 |
} |
326 |
} |
327 |
|
328 |
} |