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.schema.refactoring;
043:
044: /*
045: * SchemaRefactoringElement.java
046: *
047: * Created on December 30, 2006, 4:41 PM
048: *
049: * To change this template, choose Tools | Template Manager
050: * and open the template in the editor.
051: */
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.xml.refactoring.XMLRefactoringTransaction;
060: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
061: import org.netbeans.modules.xml.schema.model.SchemaComponent;
062: import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategorizedSchemaNodeFactory;
063: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
064: import org.netbeans.modules.xml.xam.dom.DocumentModelAccess;
065: import org.netbeans.modules.xml.xam.ui.actions.ShowSourceAction;
066: import org.openide.filesystems.FileObject;
067: import org.openide.loaders.DataObject;
068: import org.openide.loaders.DataObjectNotFoundException;
069: import org.openide.nodes.Node;
070: import org.openide.text.CloneableEditorSupport;
071: import org.openide.text.PositionBounds;
072: import org.openide.text.PositionRef;
073: import org.openide.util.Exceptions;
074: import org.openide.util.Lookup;
075: import org.openide.util.lookup.Lookups;
076: import org.openide.util.actions.SystemAction;
077: import org.w3c.dom.Element;
078:
079: /**
080: *
081: * @author Sonali
082: */
083: public class SchemaRefactoringElement extends
084: SimpleRefactoringElementImplementation {
085:
086: private FileObject source;
087: SchemaComponent comp;
088: Node node;
089: XMLRefactoringTransaction transaction;
090:
091: /**
092: * Creates a new instance of SchemaRefactoringElement
093: */
094: public SchemaRefactoringElement(SchemaComponent comp) {
095: this .comp = comp;
096: CategorizedSchemaNodeFactory nodeFactory = new CategorizedSchemaNodeFactory(
097: comp.getModel(), Lookups.singleton(comp));
098: this .node = nodeFactory.createNode(comp);
099:
100: }
101:
102: public Lookup getLookup() {
103: return Lookups.singleton(comp);
104:
105: }
106:
107: public FileObject getParentFile() {
108: FileObject source = (FileObject) comp.getModel()
109: .getModelSource().getLookup().lookup(FileObject.class);
110: assert source != null : "ModelSource should have FileObject in lookup"; //NOI18N
111: return source;
112: }
113:
114: public String getText() {
115: return node.getName();
116: }
117:
118: public String getDisplayText() {
119: return node.getHtmlDisplayName();
120: }
121:
122: public void performChange() {
123: }
124:
125: public PositionBounds getPosition() {
126: if (comp.getModel() instanceof AbstractDocumentModel) {
127: Document doc = ((AbstractDocumentModel) comp.getModel())
128: .getBaseDocument();
129: DocumentModelAccess docAcc = ((AbstractDocumentModel) comp
130: .getModel()).getAccess();
131: Element elem = comp.getPeer();
132: String txt = docAcc.getXmlFragmentInclusive(elem);
133: int startPos = comp.findPosition();
134: int endPos = startPos + txt.length();
135: DataObject dob = null;
136: try {
137: FileObject source = (FileObject) comp.getModel()
138: .getModelSource().getLookup().lookup(
139: FileObject.class);
140: dob = DataObject.find(source);
141: } catch (DataObjectNotFoundException ex) {
142: ex.printStackTrace();
143: }
144: CloneableEditorSupport ces = SharedUtils
145: .findCloneableEditorSupport(dob);
146: if (ces == null)
147: return null;
148:
149: PositionRef ref1 = ces.createPositionRef(startPos,
150: Bias.Forward);
151: PositionRef ref2 = ces.createPositionRef(endPos,
152: Bias.Forward);
153: PositionBounds bounds = new PositionBounds(ref1, ref2);
154:
155: return bounds;
156: } else {
157: return null;
158: }
159:
160: }
161:
162: public void openInEditor() {
163: //System.out.println("SchemaRefactoringElement:: openInEditor called");
164: Action preferredAction = SystemAction
165: .get(ShowSourceAction.class);
166: String command = (String) preferredAction
167: .getValue(Action.ACTION_COMMAND_KEY);
168: ActionEvent ae = new ActionEvent(node, 0, command);
169: preferredAction.actionPerformed(ae);
170:
171: }
172:
173: void addTransactionObject(XMLRefactoringTransaction transaction) {
174: this .transaction = transaction;
175: }
176:
177: protected String getNewFileContent() {
178: if (comp.getModel() instanceof AbstractDocumentModel
179: && transaction != null) {
180: try {
181:
182: String refactoredString = transaction
183: .refactorForPreview(comp.getModel());
184: return refactoredString;
185: } catch (Exception ex) {
186: Exceptions.printStackTrace(ex);
187: return null;
188: }
189: }
190: return null;
191: }
192:
193: }
|