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: package org.netbeans.modules.bpel.refactoring;
042:
043: import javax.swing.Icon;
044: import javax.swing.text.Position.Bias;
045:
046: import org.openide.filesystems.FileObject;
047: import org.openide.loaders.DataObject;
048: import org.openide.loaders.DataObjectNotFoundException;
049: import org.openide.text.PositionRef;
050: import org.openide.text.CloneableEditorSupport;
051: import org.openide.text.PositionBounds;
052: import org.openide.util.Lookup;
053: import org.openide.util.lookup.Lookups;
054:
055: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
056: import org.netbeans.modules.refactoring.spi.ui.TreeElement;
057: import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
058:
059: import org.netbeans.modules.xml.xam.Component;
060: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
061: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
062: import org.netbeans.modules.xml.xam.dom.DocumentModelAccess;
063:
064: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
065: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
066:
067: import org.netbeans.modules.bpel.editors.api.utils.RefactorUtil;
068: import org.netbeans.modules.bpel.editors.api.utils.Util;
069:
070: /**
071: * @author Vladimir Yaroslavskiy
072: * @version 2007.03.16
073: */
074: final class Element extends SimpleRefactoringElementImplementation
075: implements TreeElement {
076: Element(Component component) {
077: myComponent = component;
078: }
079:
080: public Lookup getLookup() {
081: return Lookups.singleton(myComponent);
082: }
083:
084: public FileObject getParentFile() {
085: return (FileObject) myComponent.getModel().getModelSource()
086: .getLookup().lookup(FileObject.class);
087: }
088:
089: public TreeElement getParent(boolean isLogical) {
090: if (myComponent.getParent() != null) {
091: return TreeElementFactory.getTreeElement(myComponent
092: .getParent());
093: }
094: return TreeElementFactory.getTreeElement(getParentFile());
095: }
096:
097: public String getText() {
098: return RefactorUtil.getName(myComponent);
099: }
100:
101: public String getText(boolean isLogical) {
102: return getText();
103: }
104:
105: public String getDisplayText() {
106: return RefactorUtil.getHtmlName(myComponent);
107: }
108:
109: public Icon getIcon() {
110: return RefactorUtil.getIcon(myComponent);
111: }
112:
113: public PositionBounds getPosition() {
114: if (!(myComponent.getModel() instanceof AbstractDocumentModel)) {
115: return null;
116: }
117: DocumentModelAccess access = ((AbstractDocumentModel) myComponent
118: .getModel()).getAccess();
119: String text = access
120: .getXmlFragmentInclusive(((DocumentComponent) myComponent)
121: .getPeer());
122: int startPos = ((DocumentComponent) myComponent).findPosition();
123: int endPos = startPos + text.length();
124: DataObject data = null;
125:
126: try {
127: data = DataObject.find((FileObject) myComponent.getModel()
128: .getModelSource().getLookup().lookup(
129: FileObject.class));
130: } catch (DataObjectNotFoundException e) {
131: return null;
132: }
133: CloneableEditorSupport editor = SharedUtils
134: .findCloneableEditorSupport(data);
135:
136: if (editor == null) {
137: return null;
138: }
139: PositionRef start = editor.createPositionRef(startPos,
140: Bias.Forward);
141: PositionRef end = editor
142: .createPositionRef(endPos, Bias.Forward);
143:
144: return new PositionBounds(start, end);
145: }
146:
147: public void openInEditor() {
148: Util.goToSource(myComponent);
149: }
150:
151: public Object getUserObject() {
152: return myComponent;
153: }
154:
155: void setTransactionObject(XMLRefactoringTransaction transaction) {
156: myTransaction = transaction;
157: }
158:
159: protected String getNewFileContent() {
160: if (myComponent.getModel() instanceof AbstractDocumentModel
161: && myTransaction != null) {
162: return myTransaction.refactorForPreview(myComponent
163: .getModel());
164: }
165: return null;
166: }
167:
168: public void performChange() {
169: }
170:
171: private Component myComponent;
172: private XMLRefactoringTransaction myTransaction;
173: }
|