KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > actions > OpenAction


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  * OpenAction.java
21  *
22  * Created on September 24, 2004, 8:41 PM
23  */

24
25 package org.netbeans.modules.java.navigation.actions;
26
27 import java.awt.Container JavaDoc;
28 import java.awt.Point JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.lang.model.element.Element;
33 import javax.swing.JEditorPane JavaDoc;
34 import org.netbeans.api.java.source.ElementHandle;
35 import org.netbeans.api.java.source.UiUtils;
36 import org.openide.awt.*;
37 import org.openide.awt.StatusDisplayer;
38 import org.openide.cookies.*;
39 import org.openide.cookies.EditorCookie;
40 import org.openide.filesystems.FileObject;
41 import org.openide.loaders.DataObject;
42 import org.openide.nodes.*;
43 import org.openide.text.PositionBounds;
44 import org.openide.util.*;
45
46 import javax.swing.*;
47 import java.awt.event.*;
48 import org.openide.windows.TopComponent;
49
50 /**
51  * An action that opens editor and jumps to the element given in constructor.
52  * Similar to editor's go to declaration action.
53  *
54  * @author tim, Dafe Simonek
55  */

56 public final class OpenAction extends AbstractAction {
57     
58     private ElementHandle<? extends Element> elementHandle;
59     private FileObject fileObject;
60       
61     public OpenAction( ElementHandle<? extends Element> elementHandle, FileObject fileObject ) {
62         this.elementHandle = elementHandle;
63         this.fileObject = fileObject;
64         putValue ( Action.NAME, NbBundle.getMessage ( OpenAction.class, "LBL_Goto" ) ); //NOI18N
65
}
66     
67     public void actionPerformed (ActionEvent ev) {
68         UiUtils.open(fileObject, elementHandle);
69         // find source
70
// boolean isSourceAvailable = false;
71
// repo.beginTrans(false);
72
// try {
73
// if (!elem.isValid()) {
74
// return;
75
// }
76
// ClassDefinition declaringClass = elem instanceof ClassDefinition ?
77
// (ClassDefinition)elem : ((Feature)elem).getDeclaringClass();
78
// isSourceAvailable = JUtils.getSourceForBinary(declaringClass) != null;
79
// } finally {
80
// repo.endTrans();
81
// }
82
//
83
// if (isSourceAvailable) {
84
// openElement(elem);
85
// } else {
86
// // no source attached
87
// StatusDisplayer.getDefault().setStatusText(
88
// NbBundle.getMessage(OpenAction.class, "MSG_NoSource", elem) //NOI18N
89
// );
90
// }
91
}
92
93     public boolean isEnabled () {
94           return true;
95     }
96
97     
98     // following hard-to-read code is copied from editor/JavaKit class. fuj.
99
//
100
// /** Opens source code of given element in editor and goes to element
101
// * Works only if element really has source
102
// */
103
// private static boolean openElement(final Element element) {
104
// repo.beginTrans(false);
105
// try {
106
// Resource resource = element.isValid() ? element.getResource() : null;
107
// if (resource != null) {
108
// JavaModel.setClassPath(resource);
109
// DataObject dob = jmm.getDataObject(resource);
110
// if (dob != null) {
111
// final EditorCookie.Observable ec = (EditorCookie.Observable)dob.getCookie(EditorCookie.Observable.class);
112
// if (ec != null) {
113
// StatusDisplayer.getDefault().setStatusText(
114
// NbBundle.getMessage(OpenAction.class, "MSG_OpeningElement", // NOI18N
115
// element instanceof NamedElement ? ((NamedElement)element).getName() : "")
116
// );
117
// SwingUtilities.invokeLater(new Runnable() {
118
// public void run() {
119
// JEditorPane[] panes = ec.getOpenedPanes();
120
// if (panes != null && panes.length > 0) {
121
// // editor already opened, so just select
122
// selectElementInPane(panes[0], element, false);
123
// } else {
124
// // editor not yet
125
// ec.addPropertyChangeListener(new PropertyChangeListener() {
126
// public void propertyChange(PropertyChangeEvent evt) {
127
// if (EditorCookie.Observable.PROP_OPENED_PANES.equals(evt.getPropertyName())) {
128
// final JEditorPane[] panes = ec.getOpenedPanes();
129
// if (panes != null && panes.length > 0) {
130
// selectElementInPane(panes[0], element, true);
131
// }
132
// ec.removePropertyChangeListener(this);
133
// }
134
// }
135
// });
136
// ec.open();
137
// }
138
// }
139
//
140
// });
141
// return true;
142
// }
143
// }
144
// }
145
// } finally {
146
// repo.endTrans(false);
147
// }
148
// return false;
149
// }
150
//
151
// /** Jumps to element in given editor pane. When delayProcessing is
152
// * specified, waits for real visible open before jump
153
// */
154
// private static void selectElementInPane(final JEditorPane pane, final Element element, boolean delayProcessing) {
155
// //final Cursor editCursor = pane.getCursor();
156
// //pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
157
// if (delayProcessing) {
158
// // [dafe] I don't know why, but editor guys are waiting for focus
159
// // in delay processing, so I will do the same
160
// pane.addFocusListener(new FocusAdapter() {
161
// public void focusGained(FocusEvent e) {
162
// RequestProcessor.getDefault().post(new Runnable() {
163
// public void run() {
164
// jumpToElement(pane, element);
165
// }
166
// });
167
// pane.removeFocusListener(this);
168
// }
169
// });
170
// } else {
171
// // immediate processing
172
// RequestProcessor.getDefault().post(new Runnable() {
173
// public void run() {
174
// jumpToElement(pane, element);
175
// }
176
// });
177
// // try to activate outer TopComponent
178
// Container temp = pane;
179
// // #81238 - don't throw exception when no outer TC exists
180
// while (temp != null && !(temp instanceof TopComponent)) {
181
// temp = temp.getParent();
182
// }
183
// if (temp != null) {
184
// ((TopComponent) temp).requestActive();
185
// }
186
// }
187
// }
188
//
189
// /** Jumps to element on given editor pane. Call only outside AWT thread!
190
// */
191
// private static void jumpToElement (JEditorPane pane, Element element) {
192
// int caretPos = pane.getCaretPosition();
193
// Container parent = pane.getParent();
194
// Point viewPos = parent instanceof JViewport ? ((JViewport)parent).getViewPosition() : null;
195
// PositionBounds bounds = null;
196
// // get elem position first
197
// repo.beginTrans(false);
198
// try {
199
// if (element.isValid()) {
200
// JavaModel.setClassPath(element.getResource());
201
// bounds = jmm.getElementPosition(element);
202
// }
203
// } finally {
204
// repo.endTrans(false);
205
// }
206
// // and actually jump if user not moving already
207
// if (bounds != null && pane.getCaretPosition() == caretPos &&
208
// (viewPos == null || viewPos.equals(((JViewport)parent).getViewPosition()))) {
209
// pane.setCaretPosition(bounds.getBegin().getOffset());
210
// }
211
// StatusDisplayer.getDefault().setStatusText(""); // NOI18N
212
// }
213

214     
215 }
216
Popular Tags