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-2007 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: /*
043: * WhereUsedElement.java
044: *
045: * Created on December 30, 2006, 4:41 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.refactoring;
052:
053: import java.awt.event.ActionEvent;
054: import javax.swing.Action;
055: import javax.swing.text.Document;
056: import javax.swing.text.Position;
057: import javax.swing.text.Position.Bias;
058: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
059: import org.netbeans.modules.refactoring.spi.ui.UI;
060: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
061: import org.netbeans.modules.xml.refactoring.spi.UIHelper;
062: import org.netbeans.modules.xml.refactoring.spi.AnalysisUtilities;
063: import org.netbeans.modules.xml.xam.Component;
064: import org.netbeans.modules.xml.xam.Model;
065: import org.netbeans.modules.xml.xam.Referenceable;
066: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
067: import org.netbeans.modules.xml.xam.dom.DocumentModel;
068: import org.openide.filesystems.FileObject;
069: import org.openide.loaders.DataObject;
070: import org.openide.loaders.DataObjectNotFoundException;
071: import org.openide.nodes.Node;
072: import org.openide.text.CloneableEditorSupport;
073: import org.openide.text.PositionBounds;
074: import org.openide.text.PositionRef;
075: import org.openide.util.Lookup;
076: import org.openide.util.lookup.Lookups;
077:
078: /**
079: *
080: * @author Sonali
081: */
082: public class FauxRefactoringElement extends
083: SimpleRefactoringElementImplementation {
084:
085: Node node;
086: FileObject targetFobj;
087: Referenceable ref;
088: String label, name, type;
089:
090: public FauxRefactoringElement(Referenceable ref,
091: String refactoringType) {
092: this .ref = ref;
093: Component targetComponent = ref instanceof Component ? (Component) ref
094: : ref instanceof DocumentModel ? ((DocumentModel) ref)
095: .getRootComponent() : null;
096: assert targetComponent != null : "target is not Component or DocumentModel";
097: targetFobj = SharedUtils.getFileObject(targetComponent);
098: type = refactoringType;
099: //node = AnalysisUtilities.getDisplayNode(ref);
100: name = SharedUtils.getName(ref);
101: label = " <b>" + refactoringType + " " + name + "</b>";
102: }
103:
104: public String getText() {
105: return SharedUtils.getName(ref);
106: }
107:
108: public String getDisplayText() {
109: return label;
110:
111: }
112:
113: public void performChange() {
114: }
115:
116: public Lookup getLookup() {
117: return Lookups.singleton(this );
118: }
119:
120: public FileObject getParentFile() {
121: return targetFobj;
122: }
123:
124: public PositionBounds getPosition() {
125: Model mod = SharedUtils.getModel(ref);
126: Document doc = ((AbstractDocumentModel) mod).getBaseDocument();
127: Position start = doc.getStartPosition();
128: Position end = doc.getEndPosition();
129: DataObject dob = null;
130: try {
131: FileObject source = mod.getModelSource().getLookup()
132: .lookup(FileObject.class);
133:
134: dob = DataObject.find(source);
135: } catch (DataObjectNotFoundException ex) {
136: ex.printStackTrace();
137: }
138:
139: CloneableEditorSupport ces = SharedUtils
140: .findCloneableEditorSupport(dob);
141: PositionRef ref1 = ces.createPositionRef(start.getOffset(),
142: Bias.Forward);
143: PositionRef ref2 = ces.createPositionRef(end.getOffset(),
144: Bias.Forward);
145: PositionBounds bounds = new PositionBounds(ref1, ref2);
146: return bounds;
147: }
148:
149: public void showPreview() {
150: UI.setComponentForRefactoringPreview(null);
151: }
152:
153: public void openInEditor() {
154: //System.out.println("openInEditor::called");
155: }
156:
157: public void setEnabled(boolean enabled) {
158:
159: }
160:
161: public String getRefactoringType() {
162: return type;
163: }
164:
165: public Referenceable getTarget() {
166: return ref;
167:
168: }
169:
170: }
|