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: * OpenAction.java
043: *
044: * Created on September 24, 2004, 8:41 PM
045: */
046:
047: package org.netbeans.modules.java.navigation.actions;
048:
049: import java.awt.Toolkit;
050: import javax.lang.model.element.Element;
051: import org.netbeans.api.java.source.ElementHandle;
052: import org.netbeans.api.java.source.ui.ElementOpen;
053: import org.openide.awt.*;
054: import org.openide.awt.StatusDisplayer;
055: import org.openide.cookies.*;
056: import org.openide.filesystems.FileObject;
057: import org.openide.nodes.*;
058: import org.openide.util.*;
059:
060: import javax.swing.*;
061: import java.awt.event.*;
062:
063: /**
064: * An action that opens editor and jumps to the element given in constructor.
065: * Similar to editor's go to declaration action.
066: *
067: * @author tim, Dafe Simonek
068: */
069: public final class OpenAction extends AbstractAction {
070:
071: private ElementHandle<? extends Element> elementHandle;
072: private FileObject fileObject;
073: private String displayName;
074:
075: public OpenAction(ElementHandle<? extends Element> elementHandle,
076: FileObject fileObject) {
077: this (elementHandle, fileObject, null);
078: }
079:
080: public OpenAction(ElementHandle<? extends Element> elementHandle,
081: FileObject fileObject, String displayName) {
082: this .elementHandle = elementHandle;
083: this .fileObject = fileObject;
084: this .displayName = displayName;
085: putValue(Action.NAME, NbBundle.getMessage(OpenAction.class,
086: "LBL_Goto")); //NOI18N
087: }
088:
089: public void actionPerformed(ActionEvent ev) {
090: if (null == fileObject) {
091: Toolkit.getDefaultToolkit().beep();
092: if (null != displayName) {
093: StatusDisplayer.getDefault().setStatusText(
094: NbBundle.getMessage(OpenAction.class,
095: "MSG_NoSource", displayName)); //NOI18N
096: }
097: } else {
098: ElementOpen.open(fileObject, elementHandle);
099: }
100: // find source
101: // boolean isSourceAvailable = false;
102: // repo.beginTrans(false);
103: // try {
104: // if (!elem.isValid()) {
105: // return;
106: // }
107: // ClassDefinition declaringClass = elem instanceof ClassDefinition ?
108: // (ClassDefinition)elem : ((Feature)elem).getDeclaringClass();
109: // isSourceAvailable = JUtils.getSourceForBinary(declaringClass) != null;
110: // } finally {
111: // repo.endTrans();
112: // }
113: //
114: // if (isSourceAvailable) {
115: // openElement(elem);
116: // } else {
117: // // no source attached
118: // StatusDisplayer.getDefault().setStatusText(
119: // NbBundle.getMessage(OpenAction.class, "MSG_NoSource", elem) //NOI18N
120: // );
121: // }
122: }
123:
124: public boolean isEnabled() {
125: return true;
126: }
127:
128: // following hard-to-read code is copied from editor/JavaKit class. fuj.
129: //
130: // /** Opens source code of given element in editor and goes to element
131: // * Works only if element really has source
132: // */
133: // private static boolean openElement(final Element element) {
134: // repo.beginTrans(false);
135: // try {
136: // Resource resource = element.isValid() ? element.getResource() : null;
137: // if (resource != null) {
138: // JavaModel.setClassPath(resource);
139: // DataObject dob = jmm.getDataObject(resource);
140: // if (dob != null) {
141: // final EditorCookie.Observable ec = (EditorCookie.Observable)dob.getCookie(EditorCookie.Observable.class);
142: // if (ec != null) {
143: // StatusDisplayer.getDefault().setStatusText(
144: // NbBundle.getMessage(OpenAction.class, "MSG_OpeningElement", // NOI18N
145: // element instanceof NamedElement ? ((NamedElement)element).getName() : "")
146: // );
147: // SwingUtilities.invokeLater(new Runnable() {
148: // public void run() {
149: // JEditorPane[] panes = ec.getOpenedPanes();
150: // if (panes != null && panes.length > 0) {
151: // // editor already opened, so just select
152: // selectElementInPane(panes[0], element, false);
153: // } else {
154: // // editor not yet
155: // ec.addPropertyChangeListener(new PropertyChangeListener() {
156: // public void propertyChange(PropertyChangeEvent evt) {
157: // if (EditorCookie.Observable.PROP_OPENED_PANES.equals(evt.getPropertyName())) {
158: // final JEditorPane[] panes = ec.getOpenedPanes();
159: // if (panes != null && panes.length > 0) {
160: // selectElementInPane(panes[0], element, true);
161: // }
162: // ec.removePropertyChangeListener(this);
163: // }
164: // }
165: // });
166: // ec.open();
167: // }
168: // }
169: //
170: // });
171: // return true;
172: // }
173: // }
174: // }
175: // } finally {
176: // repo.endTrans(false);
177: // }
178: // return false;
179: // }
180: //
181: // /** Jumps to element in given editor pane. When delayProcessing is
182: // * specified, waits for real visible open before jump
183: // */
184: // private static void selectElementInPane(final JEditorPane pane, final Element element, boolean delayProcessing) {
185: // //final Cursor editCursor = pane.getCursor();
186: // //pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
187: // if (delayProcessing) {
188: // // [dafe] I don't know why, but editor guys are waiting for focus
189: // // in delay processing, so I will do the same
190: // pane.addFocusListener(new FocusAdapter() {
191: // public void focusGained(FocusEvent e) {
192: // RequestProcessor.getDefault().post(new Runnable() {
193: // public void run() {
194: // jumpToElement(pane, element);
195: // }
196: // });
197: // pane.removeFocusListener(this);
198: // }
199: // });
200: // } else {
201: // // immediate processing
202: // RequestProcessor.getDefault().post(new Runnable() {
203: // public void run() {
204: // jumpToElement(pane, element);
205: // }
206: // });
207: // // try to activate outer TopComponent
208: // Container temp = pane;
209: // // #81238 - don't throw exception when no outer TC exists
210: // while (temp != null && !(temp instanceof TopComponent)) {
211: // temp = temp.getParent();
212: // }
213: // if (temp != null) {
214: // ((TopComponent) temp).requestActive();
215: // }
216: // }
217: // }
218: //
219: // /** Jumps to element on given editor pane. Call only outside AWT thread!
220: // */
221: // private static void jumpToElement (JEditorPane pane, Element element) {
222: // int caretPos = pane.getCaretPosition();
223: // Container parent = pane.getParent();
224: // Point viewPos = parent instanceof JViewport ? ((JViewport)parent).getViewPosition() : null;
225: // PositionBounds bounds = null;
226: // // get elem position first
227: // repo.beginTrans(false);
228: // try {
229: // if (element.isValid()) {
230: // JavaModel.setClassPath(element.getResource());
231: // bounds = jmm.getElementPosition(element);
232: // }
233: // } finally {
234: // repo.endTrans(false);
235: // }
236: // // and actually jump if user not moving already
237: // if (bounds != null && pane.getCaretPosition() == caretPos &&
238: // (viewPos == null || viewPos.equals(((JViewport)parent).getViewPosition()))) {
239: // pane.setCaretPosition(bounds.getBegin().getOffset());
240: // }
241: // StatusDisplayer.getDefault().setStatusText(""); // NOI18N
242: // }
243:
244: }
|