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.apisupport.refactoring;
043:
044: import javax.swing.text.Position;
045: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
046: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
047: import org.openide.cookies.EditorCookie;
048: import org.openide.filesystems.FileObject;
049: import org.openide.loaders.DataObject;
050: import org.openide.loaders.DataObjectNotFoundException;
051: import org.openide.text.CloneableEditorSupport;
052: import org.openide.text.PositionBounds;
053: import org.openide.util.Lookup;
054:
055: /**
056: *
057: * @author Milos Kleint
058: */
059: public abstract class AbstractRefactoringElement extends
060: SimpleRefactoringElementImplementation implements
061: RefactoringElementImplementation {
062:
063: private int status = RefactoringElementImplementation.NORMAL;
064:
065: protected String name;
066: protected FileObject parentFile;
067: protected boolean enabled = true;
068:
069: public AbstractRefactoringElement() {
070: }
071:
072: public boolean isEnabled() {
073: return enabled;
074: }
075:
076: public String getText() {
077: return getDisplayText();
078: }
079:
080: public void setEnabled(boolean enabled) {
081: this .enabled = enabled;
082: }
083:
084: public FileObject getParentFile() {
085: return parentFile;
086: }
087:
088: /** start and end positions of text (must be 2-element array); default [0, 0] */
089: protected int[] location() {
090: return new int[] { 0, 0 };
091: }
092:
093: private int[] loc; // cached
094:
095: public PositionBounds getPosition() {
096: try {
097: DataObject dobj = DataObject.find(getParentFile());
098: if (dobj != null) {
099: EditorCookie.Observable obs = (EditorCookie.Observable) dobj
100: .getCookie(EditorCookie.Observable.class);
101: if (obs != null
102: && obs instanceof CloneableEditorSupport) {
103: CloneableEditorSupport supp = (CloneableEditorSupport) obs;
104:
105: if (loc == null) {
106: loc = location();
107: }
108: PositionBounds bounds = new PositionBounds(supp
109: .createPositionRef(loc[0],
110: Position.Bias.Forward), supp
111: .createPositionRef(
112: Math.max(loc[0], loc[1]),
113: Position.Bias.Forward));
114:
115: return bounds;
116: }
117: }
118: } catch (DataObjectNotFoundException ex) {
119: ex.printStackTrace();
120: }
121: return null;
122: }
123:
124: public int getStatus() {
125: return status;
126: }
127:
128: public void setStatus(int status) {
129: this .status = status;
130: }
131:
132: public void performChange() {
133: }
134:
135: public void undoChange() {
136: }
137:
138: public Lookup getLookup() {
139: return Lookup.EMPTY;
140: }
141:
142: }
|