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.BorderLayout;
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.event.ItemEvent;
047: import java.awt.event.ItemListener;
048: import java.io.IOException;
049: import java.util.Collection;
050: import javax.swing.JCheckBox;
051: import javax.swing.JPanel;
052: import javax.swing.SwingUtilities;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.api.java.source.CancellableTask;
055: import org.netbeans.api.java.source.CompilationController;
056: import org.netbeans.api.java.source.JavaSource;
057: import org.netbeans.api.java.source.JavaSource.Phase;
058: import org.netbeans.api.java.source.TreePathHandle;
059: import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
060: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
061: import org.openide.filesystems.FileObject;
062: import org.openide.util.NbBundle;
063: import org.netbeans.modules.refactoring.java.RefactoringModule;
064: import org.openide.awt.Mnemonics;
065: import org.openide.util.NbBundle;
066:
067: /**
068: * Subclass of CustomRefactoringPanel representing the
069: * Safe Delete refactoring UI
070: * @author Bharath Ravikumar
071: */
072: public class SafeDeletePanel extends JPanel implements
073: CustomRefactoringPanel {
074:
075: private final transient Collection elements;
076: private final transient SafeDeleteRefactoring refactoring;
077: private boolean regulardelete;
078: private ChangeListener parent;
079:
080: /**
081: * Creates new form RenamePanelName
082: * @param refactoring The SafeDelete refactoring used by this panel
083: * @param selectedElements A Collection of selected elements
084: */
085: public SafeDeletePanel(SafeDeleteRefactoring refactoring,
086: Collection selectedElements, boolean regulardelete,
087: ChangeListener parent) {
088: setName(NbBundle.getMessage(SafeDeletePanel.class,
089: "LBL_SafeDel")); // NOI18N
090: this .elements = selectedElements;
091: this .refactoring = refactoring;
092: this .regulardelete = regulardelete;
093: this .parent = parent;
094: initComponents();
095: }
096:
097: private boolean initialized = false;
098: private String methodDeclaringClass = null;
099:
100: String getMethodDeclaringClass() {
101: return methodDeclaringClass;
102: }
103:
104: /**
105: * Initialization method. Creates appropriate labels in the panel.
106: */
107: public void initialize() {
108: //This is needed since the checkBox is gets disabled on a
109: //repeated invocation of SafeDelete follwing removal of references
110: //to the element
111: searchInComments.setEnabled(true);
112:
113: if (initialized)
114: return;
115:
116: final String labelText;
117:
118: Collection<? extends FileObject> files = refactoring
119: .getRefactoringSource().lookupAll(FileObject.class);
120: final Collection<? extends TreePathHandle> handles = refactoring
121: .getRefactoringSource().lookupAll(TreePathHandle.class);
122:
123: if (files.size() > 1 && files.size() == handles.size()) {
124: //delete multiple files
125: if (regulardelete) {
126: labelText = NbBundle.getMessage(SafeDeletePanel.class,
127: "LBL_SafeDel_RegularDelete", handles.size());
128: } else {
129: labelText = NbBundle.getMessage(SafeDeletePanel.class,
130: "LBL_SafeDel_Classes", handles.size());
131: }
132: } else if (handles.size() > 1) {
133: labelText = NbBundle.getMessage(SafeDeletePanel.class,
134: "LBL_SafeDel_Classes", handles.size());
135: } else if (handles.size() == 1) {
136: JavaSource s = JavaSource.forFileObject(handles.iterator()
137: .next().getFileObject());
138: final String[] name = new String[1];
139: try {
140: s.runUserActionTask(
141: new CancellableTask<CompilationController>() {
142: public void cancel() {
143: }
144:
145: public void run(
146: CompilationController parameter)
147: throws Exception {
148: parameter.toPhase(Phase.RESOLVED);
149: name[0] = handles.iterator().next()
150: .resolveElement(parameter)
151: .getSimpleName().toString();
152: }
153: }, true);
154: } catch (IOException ioe) {
155: throw (RuntimeException) new RuntimeException()
156: .initCause(ioe);
157: }
158: if (regulardelete) {
159: labelText = NbBundle.getMessage(SafeDeletePanel.class,
160: "LBL_SafeDel_RegularDeleteElement", name[0]);
161: } else {
162: labelText = NbBundle.getMessage(SafeDeletePanel.class,
163: "LBL_SafeDel_Element", name[0]);
164: }
165: } else {
166: labelText = "";
167: }
168:
169: SwingUtilities.invokeLater(new Runnable() {
170: public void run() {
171: if (regulardelete) {
172: safeDelete = new JCheckBox();
173: Mnemonics.setLocalizedText(safeDelete, NbBundle
174: .getMessage(SafeDeletePanel.class,
175: "LBL_SafeDelCheckBox"));
176: safeDelete.setMargin(new java.awt.Insets(2, 14, 2,
177: 2));
178: searchInComments.setEnabled(false);
179: safeDelete.addItemListener(new ItemListener() {
180: public void itemStateChanged(ItemEvent evt) {
181: searchInComments.setEnabled(safeDelete
182: .isSelected());
183: parent.stateChanged(null);
184: }
185: });
186:
187: checkBoxes.add(safeDelete, BorderLayout.CENTER);
188: }
189: label.setText(labelText);
190: validate();
191: }
192: });
193: initialized = true;
194: }
195:
196: @Override
197: public void requestFocus() {
198: super .requestFocus();
199: }
200:
201: boolean isRegularDelete() {
202: if (safeDelete != null) {
203: return !safeDelete.isSelected();
204: }
205: return false;
206: }
207:
208: /** This method is called from within the constructor to
209: * initialize the form.
210: * WARNING: Do NOT modify this code. The content of this method is
211: * always regenerated by the Form Editor.
212: */
213: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
214: private void initComponents() {
215:
216: buttonGroup = new javax.swing.ButtonGroup();
217: checkBoxes = new javax.swing.JPanel();
218: label = new javax.swing.JLabel();
219: searchInComments = new javax.swing.JCheckBox();
220:
221: setLayout(new java.awt.BorderLayout());
222:
223: checkBoxes.setLayout(new java.awt.BorderLayout());
224:
225: label.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,
226: 0, 8, 0));
227: checkBoxes.add(label, java.awt.BorderLayout.NORTH);
228:
229: searchInComments
230: .setSelected(((Boolean) RefactoringModule.getOption(
231: "searchInComments.whereUsed", Boolean.FALSE))
232: .booleanValue());
233: java.util.ResourceBundle bundle = java.util.ResourceBundle
234: .getBundle("org/netbeans/modules/refactoring/java/ui/Bundle"); // NOI18N
235: org.openide.awt.Mnemonics.setLocalizedText(searchInComments,
236: bundle.getString("LBL_SafeDelInComents")); // NOI18N
237: searchInComments.setMargin(new java.awt.Insets(2, 14, 2, 2));
238: searchInComments
239: .addItemListener(new java.awt.event.ItemListener() {
240: public void itemStateChanged(
241: java.awt.event.ItemEvent evt) {
242: searchInCommentsItemStateChanged(evt);
243: }
244: });
245: checkBoxes.add(searchInComments, java.awt.BorderLayout.SOUTH);
246: searchInComments.getAccessibleContext()
247: .setAccessibleDescription(searchInComments.getText());
248:
249: add(checkBoxes, java.awt.BorderLayout.NORTH);
250: }// </editor-fold>//GEN-END:initComponents
251:
252: private void searchInCommentsItemStateChanged(
253: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_searchInCommentsItemStateChanged
254: // used for change default value for deleteInComments check-box.
255: // The value is persisted and then used as default in next IDE run.
256: Boolean b = evt.getStateChange() == ItemEvent.SELECTED ? Boolean.TRUE
257: : Boolean.FALSE;
258: RefactoringModule.setOption("searchInComments.whereUsed", b); // NOI18N
259: refactoring.setCheckInComments(b.booleanValue());
260: }//GEN-LAST:event_searchInCommentsItemStateChanged
261:
262: // Variables declaration - do not modify//GEN-BEGIN:variables
263: private javax.swing.ButtonGroup buttonGroup;
264: private javax.swing.JPanel checkBoxes;
265: private javax.swing.JLabel label;
266: private javax.swing.JCheckBox searchInComments;
267: // End of variables declaration//GEN-END:variables
268: private javax.swing.JCheckBox safeDelete;
269:
270: @Override
271: public Dimension getPreferredSize() {
272: Dimension orig = super .getPreferredSize();
273: return new Dimension(orig.width + 30, orig.height + 30);
274: }
275:
276: /**
277: * Indicates whether the element usage must be checked in comments
278: * before deleting each element.
279: * @return Returns the isSelected() attribute of the
280: * underlying check box that controls search in comments
281: */
282: public boolean isSearchInComments() {
283: return searchInComments.isSelected();
284: }
285:
286: public Component getComponent() {
287: return this;
288: }
289: }
|