2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* Portions Copyrighted 2007 Sun Microsystems, Inc.
28
package org.netbeans.modules.java.hints.introduce;
30
import java.beans.PropertyChangeEvent;
31
import java.beans.PropertyChangeListener;
32
import java.util.Collections;
33
import java.util.EnumSet;
35
import java.util.prefs.Preferences;
36
import javax.lang.model.element.Modifier;
37
import javax.swing.JButton;
38
import javax.swing.JLabel;
39
import org.openide.util.NbPreferences;
40
import org.openide.util.Utilities;
46
public class IntroduceVariablePanel extends javax.swing.JPanel {
48
private static final int ACCESS_PUBLIC = 1;
49
private static final int ACCESS_PROTECTED = 2;
50
private static final int ACCESS_DEFAULT = 3;
51
private static final int ACCESS_PRIVATE = 4;
53
private boolean introduceConstant;
55
private JButton btnOk;
57
public IntroduceVariablePanel(int numDuplicates, String defaultName, boolean introduceConstant, JButton btnOk) {
62
this.introduceConstant = introduceConstant;
64
lblAccess.setVisible( introduceConstant );
65
accessPublic.setVisible( introduceConstant );
66
accessProtected.setVisible( introduceConstant );
67
accessDefault.setVisible( introduceConstant );
68
accessPrivate.setVisible( introduceConstant );
70
Preferences pref = getPreferences( introduceConstant );
71
if( numDuplicates == 1 ) {
72
replaceAll.setEnabled( false );
73
replaceAll.setSelected( false );
75
replaceAll.setEnabled( true );
76
replaceAll.setText( replaceAll.getText() + " (" + numDuplicates + ")" );
77
replaceAll.setSelected( pref.getBoolean("replaceAll", true) ); //NOI18N
80
declareFinal.setEnabled(!introduceConstant);
81
declareFinal.setSelected( introduceConstant ? true : pref.getBoolean("declareFinal", true) ); //NOI18N
83
if( !introduceConstant ) {
84
int accessModifier = pref.getInt( "accessModifier", ACCESS_PUBLIC ); //NOI18N
85
switch( accessModifier ) {
87
accessPublic.setSelected( true );
89
case ACCESS_PROTECTED:
90
accessProtected.setSelected( true );
93
accessDefault.setSelected( true );
96
accessPrivate.setSelected( true );
100
name.setText(defaultName);
101
if ( name != null && defaultName.trim().length() > 0 ) {
102
this.name.setCaretPosition(defaultName.length());
103
this.name.setSelectionStart(0);
104
this.name.setSelectionEnd(defaultName.length());
108
private Preferences getPreferences( boolean introduceConstant ) {
109
return NbPreferences.forModule( IntroduceVariablePanel.class ).node( introduceConstant ? "introduceConstant" : "introduceVariable" ); //NOI18N
112
private JLabel createErrorLabel() {
113
ErrorLabel.Validator validator = new ErrorLabel.Validator() {
115
public String validate(String text) {
117
|| text.length() == 0 ) return "";
118
if (!Utilities.isJavaIdentifier(text))
119
return getDefaultErrorMessage( text );
124
final ErrorLabel errorLabel = new ErrorLabel( name.getDocument(), validator );
125
errorLabel.addPropertyChangeListener( ErrorLabel.PROP_IS_VALID, new PropertyChangeListener() {
126
public void propertyChange(PropertyChangeEvent e) {
127
btnOk.setEnabled(errorLabel.isInputTextValid());
133
String getDefaultErrorMessage( String inputText ) {
134
return "'" + inputText +"' is not a valid identifier";
137
/** This method is called from within the constructor to
138
* initialize the form.
139
* WARNING: Do NOT modify this code. The content of this method is
140
* always regenerated by the Form Editor.
142
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
143
private void initComponents() {
145
accessGroup = new javax.swing.ButtonGroup();
146
lblName = new javax.swing.JLabel();
147
name = new javax.swing.JTextField();
148
replaceAll = new javax.swing.JCheckBox();
149
declareFinal = new javax.swing.JCheckBox();
150
lblAccess = new javax.swing.JLabel();
151
accessPublic = new javax.swing.JRadioButton();
152
accessProtected = new javax.swing.JRadioButton();
153
accessDefault = new javax.swing.JRadioButton();
154
accessPrivate = new javax.swing.JRadioButton();
155
errorLabel = createErrorLabel();
157
lblName.setLabelFor(name);
158
org.openide.awt.Mnemonics.setLocalizedText(lblName, org.openide.util.NbBundle.getBundle(IntroduceVariablePanel.class).getString("LBL_Name")); // NOI18N
162
org.openide.awt.Mnemonics.setLocalizedText(replaceAll, org.openide.util.NbBundle.getBundle(IntroduceVariablePanel.class).getString("LBL_ReplaceAll")); // NOI18N
163
replaceAll.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
164
replaceAll.setMargin(new java.awt.Insets(0, 0, 0, 0));
166
org.openide.awt.Mnemonics.setLocalizedText(declareFinal, org.openide.util.NbBundle.getBundle(IntroduceVariablePanel.class).getString("LBL_DeclareFinal")); // NOI18N
167
declareFinal.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
168
declareFinal.setMargin(new java.awt.Insets(0, 0, 0, 0));
170
org.openide.awt.Mnemonics.setLocalizedText(lblAccess, org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "LBL_Access")); // NOI18N
172
accessGroup.add(accessPublic);
173
accessPublic.setSelected(true);
174
org.openide.awt.Mnemonics.setLocalizedText(accessPublic, org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "LBL_public")); // NOI18N
175
accessPublic.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
176
accessPublic.setMargin(new java.awt.Insets(0, 0, 0, 0));
178
accessGroup.add(accessProtected);
179
org.openide.awt.Mnemonics.setLocalizedText(accessProtected, org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "LBL_protected")); // NOI18N
180
accessProtected.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
181
accessProtected.setMargin(new java.awt.Insets(0, 0, 0, 0));
183
accessGroup.add(accessDefault);
184
org.openide.awt.Mnemonics.setLocalizedText(accessDefault, org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "LBL_Default")); // NOI18N
185
accessDefault.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
186
accessDefault.setMargin(new java.awt.Insets(0, 0, 0, 0));
188
accessGroup.add(accessPrivate);
189
org.openide.awt.Mnemonics.setLocalizedText(accessPrivate, org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "LBL_private")); // NOI18N
190
accessPrivate.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
191
accessPrivate.setMargin(new java.awt.Insets(0, 0, 0, 0));
193
org.openide.awt.Mnemonics.setLocalizedText(errorLabel, "jLabel1");
195
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
196
this.setLayout(layout);
197
layout.setHorizontalGroup(
198
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
199
.add(layout.createSequentialGroup()
201
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
202
.add(errorLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
205
.add(layout.createSequentialGroup()
206
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
209
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
210
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
211
.add(name, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
212
.add(layout.createSequentialGroup()
214
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
215
.add(accessProtected)
216
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
218
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
219
.add(accessPrivate)))))
222
layout.setVerticalGroup(
223
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
224
.add(layout.createSequentialGroup()
226
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
228
.add(name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
229
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
230
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
233
.add(accessProtected)
236
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
238
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
240
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 32, Short.MAX_VALUE)
245
name.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_Name")); // NOI18N
246
replaceAll.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_ReplaceAllOccurences")); // NOI18N
247
declareFinal.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_DeclareFinal")); // NOI18N
248
accessPublic.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_Public")); // NOI18N
249
accessProtected.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_Protected")); // NOI18N
250
accessDefault.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_Default")); // NOI18N
251
accessPrivate.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_Private")); // NOI18N
253
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_Dialog")); // NOI18N
254
}// </editor-fold>//GEN-END:initComponents
257
// Variables declaration - do not modify//GEN-BEGIN:variables
258
private javax.swing.JRadioButton accessDefault;
259
private javax.swing.ButtonGroup accessGroup;
260
private javax.swing.JRadioButton accessPrivate;
261
private javax.swing.JRadioButton accessProtected;
262
private javax.swing.JRadioButton accessPublic;
263
private javax.swing.JCheckBox declareFinal;
264
private javax.swing.JLabel errorLabel;
265
private javax.swing.JLabel lblAccess;
266
private javax.swing.JLabel lblName;
267
private javax.swing.JTextField name;
268
private javax.swing.JCheckBox replaceAll;
269
// End of variables declaration//GEN-END:variables
271
private Set<Modifier> testAccess;
273
public String getVariableName() {
274
return name.getText();
277
public boolean isReplaceAll() {
278
boolean ret = replaceAll.isSelected();
279
getPreferences( introduceConstant ).putBoolean( "replaceAll", ret ); //NOI18N
283
public boolean isDeclareFinal() {
284
boolean ret = declareFinal.isSelected();
285
getPreferences( introduceConstant ).putBoolean( "declareFinal", ret ); //NOI18N
289
public Set<Modifier> getAccess() {
290
if (testAccess != null) return testAccess;
294
if( accessPublic.isSelected() ) {
296
set = EnumSet.of(Modifier.PUBLIC);
297
} else if( accessProtected.isSelected() ) {
298
val = ACCESS_PROTECTED;
299
set = EnumSet.of(Modifier.PROTECTED);
300
} else if( accessDefault.isSelected() ) {
301
val = ACCESS_DEFAULT;
302
set = Collections.emptySet();
304
val = ACCESS_PRIVATE;
305
set = EnumSet.of(Modifier.PRIVATE);
307
getPreferences( introduceConstant ).putInt( "accessModifier", val ); //NOI18N
312
void setVariableName(String name) {
313
this.name.setText(name);
316
void setDeclareFinal(boolean declareFinal) {
317
this.declareFinal.setSelected(declareFinal);
320
void setReplaceAll(boolean replaceAll) {
321
this.replaceAll.setSelected(replaceAll);
324
void setAccess(Set<Modifier> access) {