KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > refactoring > AbstractRefactoringElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.refactoring;
21
22 import javax.swing.text.Position JavaDoc;
23 import org.netbeans.jmi.javamodel.Element;
24 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
25 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
26 import org.openide.cookies.EditorCookie;
27 import org.openide.filesystems.FileObject;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataObjectNotFoundException;
30 import org.openide.text.CloneableEditorSupport;
31 import org.openide.text.PositionBounds;
32
33 /**
34  *
35  * @author Milos Kleint
36  */

37 public abstract class AbstractRefactoringElement extends SimpleRefactoringElementImpl implements RefactoringElementImplementation {
38     
39     private int status = RefactoringElementImplementation.NORMAL;
40
41     protected String JavaDoc name;
42     protected FileObject parentFile;
43     protected boolean enabled = true;
44
45     public AbstractRefactoringElement() {
46     }
47
48     public boolean isEnabled() {
49         return enabled;
50     }
51
52     public String JavaDoc getText() {
53         return getDisplayText();
54     }
55
56     public void setEnabled(boolean enabled) {
57         this.enabled = enabled;
58     }
59
60     public Element getJavaElement() {
61         return null;
62     }
63
64     public FileObject getParentFile() {
65         return parentFile;
66     }
67     
68     /** start and end positions of text (must be 2-element array); default [0, 0] */
69     protected int[] location() {
70         return new int[] {0, 0};
71     }
72     private int[] loc; // cached
73

74     public PositionBounds getPosition() {
75         try {
76             DataObject dobj = DataObject.find(getParentFile());
77             if (dobj != null) {
78                 EditorCookie.Observable obs = (EditorCookie.Observable)dobj.getCookie(EditorCookie.Observable.class);
79                 if (obs != null && obs instanceof CloneableEditorSupport) {
80                     CloneableEditorSupport supp = (CloneableEditorSupport)obs;
81
82                     if (loc == null) {
83                         loc = location();
84                     }
85                 PositionBounds bounds = new PositionBounds(
86                         supp.createPositionRef(loc[0], Position.Bias.Forward),
87                         supp.createPositionRef(Math.max(loc[0], loc[1]), Position.Bias.Forward)
88                         );
89                 
90                 return bounds;
91             }
92             }
93         } catch (DataObjectNotFoundException ex) {
94             ex.printStackTrace();
95         }
96         return null;
97     }
98
99     public int getStatus() {
100         return status;
101     }
102
103     public void setStatus(int status) {
104         this.status = status;
105     }
106
107     public void performChange() { }
108     
109 }
110
Popular Tags