~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to java/hints/src/org/netbeans/modules/java/hints/introduce/IntroduceVariablePanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
 
27
 */
 
28
package org.netbeans.modules.java.hints.introduce;
 
29
 
 
30
import java.beans.PropertyChangeEvent;
 
31
import java.beans.PropertyChangeListener;
 
32
import java.util.Collections;
 
33
import java.util.EnumSet;
 
34
import java.util.Set;
 
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;
 
41
 
 
42
/**
 
43
 *
 
44
 * @author Jan Lahoda
 
45
 */
 
46
public class IntroduceVariablePanel extends javax.swing.JPanel {
 
47
    
 
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;
 
52
    
 
53
    private boolean introduceConstant;
 
54
    
 
55
    private JButton btnOk;
 
56
    
 
57
    public IntroduceVariablePanel(int numDuplicates, String defaultName, boolean introduceConstant, JButton btnOk) {
 
58
        this.btnOk = btnOk;
 
59
        
 
60
        initComponents();
 
61
        
 
62
        this.introduceConstant = introduceConstant;
 
63
        
 
64
        lblAccess.setVisible( introduceConstant );
 
65
        accessPublic.setVisible( introduceConstant );
 
66
        accessProtected.setVisible( introduceConstant );
 
67
        accessDefault.setVisible( introduceConstant );
 
68
        accessPrivate.setVisible( introduceConstant );
 
69
        
 
70
        Preferences pref = getPreferences( introduceConstant );
 
71
        if( numDuplicates == 1 ) {
 
72
            replaceAll.setEnabled( false );
 
73
            replaceAll.setSelected( false );
 
74
        } else {
 
75
            replaceAll.setEnabled( true );
 
76
            replaceAll.setText( replaceAll.getText() + " (" + numDuplicates + ")" );
 
77
            replaceAll.setSelected( pref.getBoolean("replaceAll", true) ); //NOI18N
 
78
        }
 
79
        
 
80
        declareFinal.setEnabled(!introduceConstant);
 
81
        declareFinal.setSelected( introduceConstant ? true : pref.getBoolean("declareFinal", true) ); //NOI18N
 
82
        
 
83
        if( !introduceConstant ) {
 
84
            int accessModifier = pref.getInt( "accessModifier", ACCESS_PUBLIC ); //NOI18N
 
85
            switch( accessModifier ) {
 
86
            case ACCESS_PUBLIC:
 
87
                accessPublic.setSelected( true );
 
88
                break;
 
89
            case ACCESS_PROTECTED:
 
90
                accessProtected.setSelected( true );
 
91
                break;
 
92
            case ACCESS_DEFAULT:
 
93
                accessDefault.setSelected( true );
 
94
                break;
 
95
            case ACCESS_PRIVATE:
 
96
                accessPrivate.setSelected( true );
 
97
                break;
 
98
            }
 
99
        }
 
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());
 
105
        }
 
106
    }
 
107
    
 
108
    private Preferences getPreferences( boolean introduceConstant ) {
 
109
        return NbPreferences.forModule( IntroduceVariablePanel.class ).node( introduceConstant ? "introduceConstant" : "introduceVariable" ); //NOI18N
 
110
    }
 
111
 
 
112
    private JLabel createErrorLabel() {
 
113
        ErrorLabel.Validator validator = new ErrorLabel.Validator() {
 
114
 
 
115
            public String validate(String text) {
 
116
                if( null == text 
 
117
                    || text.length() == 0 ) return "";
 
118
                if (!Utilities.isJavaIdentifier(text))
 
119
                    return getDefaultErrorMessage( text );
 
120
                return null;
 
121
            }
 
122
        };
 
123
        
 
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());
 
128
            }
 
129
        });
 
130
        return errorLabel;
 
131
    }
 
132
    
 
133
    String getDefaultErrorMessage( String inputText ) {
 
134
        return "'" + inputText +"' is not a valid identifier";
 
135
    }
 
136
    
 
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.
 
141
     */
 
142
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
 
143
    private void initComponents() {
 
144
 
 
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();
 
156
 
 
157
        lblName.setLabelFor(name);
 
158
        org.openide.awt.Mnemonics.setLocalizedText(lblName, org.openide.util.NbBundle.getBundle(IntroduceVariablePanel.class).getString("LBL_Name")); // NOI18N
 
159
 
 
160
        name.setColumns(20);
 
161
 
 
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));
 
165
 
 
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));
 
169
 
 
170
        org.openide.awt.Mnemonics.setLocalizedText(lblAccess, org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "LBL_Access")); // NOI18N
 
171
 
 
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));
 
177
 
 
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));
 
182
 
 
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));
 
187
 
 
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));
 
192
 
 
193
        org.openide.awt.Mnemonics.setLocalizedText(errorLabel, "jLabel1");
 
194
 
 
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()
 
200
                .addContainerGap()
 
201
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
202
                    .add(errorLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
 
203
                    .add(replaceAll)
 
204
                    .add(declareFinal)
 
205
                    .add(layout.createSequentialGroup()
 
206
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
207
                            .add(lblAccess)
 
208
                            .add(lblName))
 
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()
 
213
                                .add(accessPublic)
 
214
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
215
                                .add(accessProtected)
 
216
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
217
                                .add(accessDefault)
 
218
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
219
                                .add(accessPrivate)))))
 
220
                .addContainerGap())
 
221
        );
 
222
        layout.setVerticalGroup(
 
223
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
224
            .add(layout.createSequentialGroup()
 
225
                .addContainerGap()
 
226
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
 
227
                    .add(lblName)
 
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)
 
231
                    .add(lblAccess)
 
232
                    .add(accessPublic)
 
233
                    .add(accessProtected)
 
234
                    .add(accessDefault)
 
235
                    .add(accessPrivate))
 
236
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
237
                .add(declareFinal)
 
238
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
239
                .add(replaceAll)
 
240
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 32, Short.MAX_VALUE)
 
241
                .add(errorLabel)
 
242
                .addContainerGap())
 
243
        );
 
244
 
 
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
 
252
 
 
253
        getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IntroduceVariablePanel.class, "AD_IntrVar_Dialog")); // NOI18N
 
254
    }// </editor-fold>//GEN-END:initComponents
 
255
    
 
256
    
 
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
 
270
    
 
271
    private Set<Modifier> testAccess;
 
272
    
 
273
    public String getVariableName() {
 
274
        return name.getText();
 
275
    }
 
276
    
 
277
    public boolean isReplaceAll() {
 
278
        boolean ret = replaceAll.isSelected();
 
279
        getPreferences( introduceConstant ).putBoolean( "replaceAll", ret ); //NOI18N
 
280
        return ret;
 
281
    }
 
282
    
 
283
    public boolean isDeclareFinal() {
 
284
        boolean ret = declareFinal.isSelected();
 
285
        getPreferences( introduceConstant ).putBoolean( "declareFinal", ret ); //NOI18N
 
286
        return ret;
 
287
    }
 
288
    
 
289
    public Set<Modifier> getAccess() {
 
290
        if (testAccess != null) return testAccess;
 
291
        
 
292
        Set<Modifier> set;
 
293
        int val;
 
294
        if( accessPublic.isSelected() ) {
 
295
            val = ACCESS_PUBLIC;
 
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();
 
303
        } else {
 
304
            val = ACCESS_PRIVATE;
 
305
            set = EnumSet.of(Modifier.PRIVATE);
 
306
        }
 
307
        getPreferences( introduceConstant ).putInt( "accessModifier", val ); //NOI18N
 
308
        return set;
 
309
    }
 
310
    
 
311
    //for tests only:
 
312
    void setVariableName(String name) {
 
313
        this.name.setText(name);
 
314
    }
 
315
    
 
316
    void setDeclareFinal(boolean declareFinal) {
 
317
        this.declareFinal.setSelected(declareFinal);
 
318
    }
 
319
 
 
320
    void setReplaceAll(boolean replaceAll) {
 
321
        this.replaceAll.setSelected(replaceAll);
 
322
    }
 
323
 
 
324
    void setAccess(Set<Modifier> access) {
 
325
        testAccess = access;
 
326
    }
 
327
}