001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.refactoring.java.ui;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.awt.event.ItemEvent;
046: import javax.swing.JPanel;
047: import javax.swing.SwingUtilities;
048: import javax.swing.event.ChangeListener;
049: import javax.swing.event.DocumentEvent;
050: import javax.swing.event.DocumentListener;
051: import org.netbeans.modules.refactoring.java.api.InnerToOuterRefactoring;
052: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
053:
054: /** UI panel for collecting refactoring parameters.
055: *
056: * @author Martin Matula
057: * @author Jan Becicka
058: */
059: public class InnerToOuterPanel extends JPanel implements
060: CustomRefactoringPanel {
061: // refactoring this panel provides parameters for
062: private final InnerToOuterRefactoring refactoring;
063: private final ChangeListener parent;
064: private boolean disableDeclareFields;
065:
066: /** Creates new form InnerToOuterPanel
067: * @param refactoring The refactoring this panel provides parameters for.
068: */
069: public InnerToOuterPanel(InnerToOuterRefactoring refactoring,
070: final ChangeListener parent, boolean disableDeclareFields) {
071: this .refactoring = refactoring;
072: this .parent = parent;
073: initComponents();
074: setPreferredSize(new Dimension(300, 130));
075: this .disableDeclareFields = disableDeclareFields;
076: }
077:
078: public Component getComponent() {
079: return this ;
080: }
081:
082: /** Initialization of the panel (called by the parent window).
083: */
084: public void initialize() {
085: SwingUtilities.invokeLater(new Runnable() {
086: public void run() {
087: classNameField.setText(refactoring.getClassName());
088: if (disableDeclareFields) {
089: fieldCheckBox.setEnabled(false);
090: } else if (refactoring.getReferenceName() != null) {
091: fieldNameField.setText(refactoring
092: .getReferenceName());
093: fieldCheckBox.setSelected(true);
094: }
095: DocumentListener dl = new DocumentListener() {
096: public void changedUpdate(DocumentEvent event) {
097: parent.stateChanged(null);
098: }
099:
100: public void insertUpdate(DocumentEvent event) {
101: parent.stateChanged(null);
102: }
103:
104: public void removeUpdate(DocumentEvent event) {
105: parent.stateChanged(null);
106: }
107: };
108: classNameField.getDocument().addDocumentListener(dl);
109: fieldNameField.getDocument().addDocumentListener(dl);
110: classNameField.selectAll();
111: classNameField.requestFocusInWindow();
112: }
113: });
114: }
115:
116: // --- GETTERS FOR REFACTORING PARAMETERS ----------------------------------
117:
118: /** Getter used by the refactoring UI to get value
119: * of target type.
120: * @return Target type.
121: */
122: public String getClassName() {
123: return classNameField.getText();
124: }
125:
126: public String getReferenceName() {
127: if (fieldCheckBox.isSelected()) {
128: return fieldNameField.getText();
129: } else {
130: return null;
131: }
132: }
133:
134: public void requestFocus() {
135: super .requestFocus();
136: classNameField.requestFocus();
137: }
138:
139: // --- GENERATED CODE ------------------------------------------------------
140:
141: /** This method is called from within the constructor to
142: * initialize the form.
143: * WARNING: Do NOT modify this code. The content of this method is
144: * always regenerated by the Form Editor.
145: */
146: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
147: private void initComponents() {
148:
149: dataPanel = new javax.swing.JPanel();
150: classNameLabel = new javax.swing.JLabel();
151: classNameField = new javax.swing.JTextField();
152: fieldPanel = new javax.swing.JPanel();
153: fieldCheckBox = new javax.swing.JCheckBox();
154: fieldNamePanel = new javax.swing.JPanel();
155: fieldNameLabel = new javax.swing.JLabel();
156: fieldNameField = new javax.swing.JTextField();
157:
158: setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12,
159: 11, 11));
160: setLayout(new java.awt.BorderLayout());
161:
162: dataPanel.setBorder(javax.swing.BorderFactory
163: .createEmptyBorder(1, 1, 1, 1));
164: dataPanel.setLayout(new java.awt.BorderLayout(12, 0));
165:
166: classNameLabel.setLabelFor(classNameField);
167: org.openide.awt.Mnemonics.setLocalizedText(classNameLabel,
168: org.openide.util.NbBundle.getBundle(
169: InnerToOuterPanel.class).getString(
170: "LBL_InnerToOuter_ClassName")); // NOI18N
171: dataPanel.add(classNameLabel, java.awt.BorderLayout.WEST);
172: dataPanel.add(classNameField, java.awt.BorderLayout.CENTER);
173: classNameField.getAccessibleContext().setAccessibleName(
174: org.openide.util.NbBundle.getMessage(
175: InnerToOuterPanel.class, "ACSD_nameField")); // NOI18N
176: classNameField.getAccessibleContext().setAccessibleDescription(
177: org.openide.util.NbBundle.getMessage(
178: InnerToOuterPanel.class, "ACSD_nameField")); // NOI18N
179:
180: fieldPanel.setBorder(javax.swing.BorderFactory
181: .createEmptyBorder(12, 0, 0, 0));
182: fieldPanel.setLayout(new java.awt.BorderLayout());
183:
184: org.openide.awt.Mnemonics.setLocalizedText(fieldCheckBox,
185: org.openide.util.NbBundle.getMessage(
186: InnerToOuterPanel.class,
187: "LBL_InnerToOuter_DeclareField")); // NOI18N
188: fieldCheckBox.setBorder(javax.swing.BorderFactory
189: .createEmptyBorder(4, 0, 4, 0));
190: fieldCheckBox
191: .addItemListener(new java.awt.event.ItemListener() {
192: public void itemStateChanged(
193: java.awt.event.ItemEvent evt) {
194: fieldCheckBoxItemStateChanged(evt);
195: }
196: });
197: fieldPanel.add(fieldCheckBox, java.awt.BorderLayout.NORTH);
198: fieldCheckBox.getAccessibleContext().setAccessibleName(
199: org.openide.util.NbBundle.getMessage(
200: InnerToOuterPanel.class,
201: "ACSD_DeclareFieldName")); // NOI18N
202: fieldCheckBox.getAccessibleContext().setAccessibleDescription(
203: org.openide.util.NbBundle.getMessage(
204: InnerToOuterPanel.class,
205: "ACSD_DeclareFieldDescription")); // NOI18N
206:
207: fieldNamePanel.setBorder(javax.swing.BorderFactory
208: .createEmptyBorder(4, 32, 0, 0));
209: fieldNamePanel.setLayout(new java.awt.BorderLayout(12, 0));
210:
211: fieldNameLabel.setLabelFor(fieldNameField);
212: org.openide.awt.Mnemonics.setLocalizedText(fieldNameLabel,
213: org.openide.util.NbBundle.getMessage(
214: InnerToOuterPanel.class,
215: "LBL_InnerToOuter_FieldName")); // NOI18N
216: fieldNameLabel.setEnabled(false);
217: fieldNamePanel.add(fieldNameLabel, java.awt.BorderLayout.WEST);
218:
219: fieldNameField.setEnabled(false);
220: fieldNamePanel
221: .add(fieldNameField, java.awt.BorderLayout.CENTER);
222:
223: fieldPanel.add(fieldNamePanel, java.awt.BorderLayout.SOUTH);
224:
225: dataPanel.add(fieldPanel, java.awt.BorderLayout.SOUTH);
226:
227: add(dataPanel, java.awt.BorderLayout.NORTH);
228: }// </editor-fold>//GEN-END:initComponents
229:
230: private void fieldCheckBoxItemStateChanged(
231: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_fieldCheckBoxItemStateChanged
232: boolean enable = evt.getStateChange() == ItemEvent.SELECTED;
233: fieldNameField.setEnabled(enable);
234: fieldNameLabel.setEnabled(enable);
235: parent.stateChanged(null);
236: }//GEN-LAST:event_fieldCheckBoxItemStateChanged
237:
238: // Variables declaration - do not modify//GEN-BEGIN:variables
239: private javax.swing.JTextField classNameField;
240: private javax.swing.JLabel classNameLabel;
241: private javax.swing.JPanel dataPanel;
242: private javax.swing.JCheckBox fieldCheckBox;
243: private javax.swing.JTextField fieldNameField;
244: private javax.swing.JLabel fieldNameLabel;
245: private javax.swing.JPanel fieldNamePanel;
246: private javax.swing.JPanel fieldPanel;
247: // End of variables declaration//GEN-END:variables
248:
249: }
|