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:
042: package org.netbeans.modules.xml.refactoring.ui;
043:
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.text.MessageFormat;
047: import java.util.ArrayList;
048: import java.util.Collection;
049: import java.util.HashMap;
050: import java.util.Map;
051: import javax.swing.Icon;
052: import javax.swing.ImageIcon;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.modules.refactoring.api.RefactoringElement;
055: import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
056: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
057: import org.netbeans.modules.refactoring.spi.ui.RefactoringCustomUI;
058: import org.netbeans.modules.refactoring.spi.ui.TreeElement;
059: import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
060: import org.netbeans.modules.xml.nbprefuse.AnalysisViewer;
061: import org.netbeans.modules.xml.nbprefuse.View;
062: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
063: import org.netbeans.modules.refactoring.api.Problem;
064: import org.netbeans.modules.refactoring.api.WhereUsedQuery;
065: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
066: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
067: import org.netbeans.modules.xml.refactoring.ui.GraphHelper;
068: import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
069: import org.netbeans.modules.xml.xam.Nameable;
070: import org.netbeans.modules.xml.xam.NamedReferenceable;
071: import org.netbeans.modules.xml.xam.Referenceable;
072: import org.openide.util.HelpCtx;
073: import org.openide.util.NbBundle;
074: import org.openide.util.Utilities;
075: import org.openide.util.lookup.Lookups;
076: import prefuse.data.Graph;
077:
078: /**
079: *
080: * @author Jeri Lockhart
081: */
082: public class DeleteRefactoringUI implements
083: org.netbeans.modules.refactoring.spi.ui.RefactoringUI,
084: RefactoringCustomUI {
085:
086: private WhereUsedQuery query;
087: private WhereUsedView view;
088: private CustomRefactoringPanel panel;
089: private AbstractRefactoring refactoring;
090: private NamedReferenceable target;
091:
092: /** Creates a new instance of WhereUsedQueryUI */
093: public DeleteRefactoringUI(NamedReferenceable target) {
094: this .target = target;
095: refactoring = new SafeDeleteRefactoring(Lookups
096: .singleton(target));
097: XMLRefactoringTransaction transaction = new XMLRefactoringTransaction(
098: (Referenceable) target, refactoring);
099: refactoring.getContext().add(transaction);
100: }
101:
102: ////////////////////////////////////////////////////////////////////////////
103: /** Start Implementation of RefactoringUI
104: */
105: ////////////////////////////////////////////////////////////////////////////
106: /**
107: * Returns refactoring-specific panel containing input fields for
108: * refactoring parameters. This method is called by ParametersPanel
109: * which is responsible for displaying refactoring parameters dialog.
110: * Name of the panel returned from this method will be used as the dialog
111: * name. This panel can use setPreviewEnabled method of the passed
112: * ParametersPanel to enable/disable Preview button of the refactoring
113: * parameters dialog.
114: *
115: * @param parent ParametersPanel that the returned panel will be displayed in.
116: * @return Refactoring-specific parameters panel.
117: */
118: public CustomRefactoringPanel getPanel(ChangeListener parent) {
119: if (panel == null)
120: panel = new SafeDeletePanel(target);
121: return panel;
122: }
123:
124: /**
125: * Implementation of this method should set the refactoring parameters entered
126: * by user into the refactoring-specific parameters panel (returned from getPanel
127: * method) into the underlying refactoring object.
128: *
129: * @return Chain of problems returned from the underlying refactoring object
130: * when trying to set its parameters.
131: */
132: public Problem setParameters() {
133: return refactoring.checkParameters();
134: }
135:
136: /**
137: * Indicates whether this class represents a real refactoring that changes
138: * code or whether it is just a query (e.g. all usages for a class).
139: *
140: * @return <code>true</code> if the class represents only a query,
141: * <code>false</code> if the class represents a real refactoring.
142: */
143: public boolean isQuery() {
144: return false;
145: }
146:
147: public boolean hasParameters() {
148: return true;
149: }
150:
151: /**
152: * Returns underlying refactoring object.
153: *
154: * @return Underlying refactoring object.
155: */
156: public AbstractRefactoring getRefactoring() {
157: return refactoring;
158: }
159:
160: public HelpCtx getHelpCtx() {
161: return new HelpCtx(DeleteRefactoringUI.class);
162: }
163:
164: /**
165: * Returns description of the refactoring.
166: *
167: * @return Refactoring description.
168: */
169: public String getDescription() {
170: return new MessageFormat(NbBundle.getMessage(
171: DeleteRefactoringUI.class, "LBL_Delete"))
172: .format(new Object[] { target.getName() });
173:
174: }
175:
176: public Problem checkParameters() {
177: return refactoring.fastCheckParameters();
178: }
179:
180: public View getView() {
181: return view;
182: }
183:
184: public void setView(View view) {
185: this .view = WhereUsedView.class.cast(view);
186: }
187:
188: /**
189: * Returns name of the refactoring.
190: *
191: * @return Refactoring name.
192: */
193: public String getName() {
194: return new MessageFormat(NbBundle.getMessage(
195: DeleteRefactoringUI.class, "LBL_Delete"))
196: .format(new Object[] { target.getName() });
197: }
198:
199: ////////////////////////////////////////////////////////////////////////////
200: /** End Implementation of RefactoringUI
201: */
202: ////////////////////////////////////////////////////////////////////////////
203: public NamedReferenceable getTarget() {
204: return target;
205: }
206:
207: public Component getCustomComponent(
208: Collection<RefactoringElement> elements) {
209: WhereUsedView view = new WhereUsedView(target);
210: GraphHelper gh = new GraphHelper((target));
211:
212: ArrayList<TreeElement> nodes = new ArrayList<TreeElement>();
213: for (RefactoringElement element : elements) {
214: TreeElement previewNode = TreeElementFactory
215: .getTreeElement(element);
216: if (previewNode != null)
217: nodes.add(previewNode);
218: }
219:
220: Graph graph = gh.loadGraph(nodes);
221: view.setGraph(graph);
222: AnalysisViewer analysisViewer = new AnalysisViewer();
223: analysisViewer.setCurrentView(view);
224: analysisViewer.getPanel().setMinimumSize(new Dimension(10, 10));
225: analysisViewer.getPanel().setPreferredSize(
226: new Dimension(10, 10));
227: view.showView(analysisViewer);
228:
229: return analysisViewer.getPanel();
230: }
231:
232: public Icon getCustomIcon() {
233: return new ImageIcon(
234: Utilities
235: .loadImage("org/netbeans/modules/xml/refactoring/resources/"
236: + "graphical_view_refactoring.png"));
237: }
238:
239: public String getCustomToolTip() {
240: return NbBundle.getMessage(WhereUsedQueryUI.class,
241: "LBL_ShowGraph");
242: }
243: }
|