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:
042: package org.netbeans.modules.tasklist.impl;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import javax.swing.AbstractAction;
047: import org.netbeans.spi.tasklist.Task;
048: import org.openide.cookies.EditCookie;
049: import org.openide.cookies.LineCookie;
050: import org.openide.cookies.OpenCookie;
051: import org.openide.cookies.ViewCookie;
052: import org.openide.filesystems.FileObject;
053: import org.openide.loaders.DataObject;
054: import org.openide.loaders.DataObjectNotFoundException;
055: import org.openide.text.Line;
056: import org.openide.util.NbBundle;
057:
058: /**
059: *
060: * @author S. Aubrecht
061: */
062: public class OpenTaskAction extends AbstractAction {
063:
064: private Task task;
065:
066: /** Creates a new instance of OpenTaskAction */
067: public OpenTaskAction(Task task) {
068: super (NbBundle.getMessage(OpenTaskAction.class,
069: "LBL_ShowSource")); //NOI18N
070: assert null != task;
071: this .task = task;
072: setEnabled(canOpenTask());
073: }
074:
075: public void actionPerformed(ActionEvent e) {
076: if (!canOpenTask())
077: return;
078:
079: ActionListener al = Accessor.getActionListener(task);
080: if (null != al) {
081: al.actionPerformed(e);
082: return;
083: }
084:
085: FileObject fileObject = Accessor.getResource(task);
086: int line = Accessor.getLine(task) - 1;
087:
088: /* Find a DataObject for the FileObject: */
089: final DataObject dataObject;
090: try {
091: dataObject = DataObject.find(fileObject);
092: } catch (DataObjectNotFoundException donfE) {
093: return;
094: }
095:
096: LineCookie lineCookie = (LineCookie) dataObject
097: .getCookie(LineCookie.class);
098: if (null != lineCookie && openAt(lineCookie, line)) {
099: return;
100: }
101:
102: EditCookie editCookie = (EditCookie) dataObject
103: .getCookie(EditCookie.class);
104: if (null != editCookie) {
105: editCookie.edit();
106: return;
107: }
108:
109: OpenCookie openCookie = (OpenCookie) dataObject
110: .getCookie(OpenCookie.class);
111: if (null != openCookie) {
112: openCookie.open();
113: return;
114: }
115:
116: ViewCookie viewCookie = (ViewCookie) dataObject
117: .getCookie(ViewCookie.class);
118: if (null != viewCookie) {
119: viewCookie.view();
120: return;
121: }
122: }
123:
124: private boolean openAt(LineCookie lineCookie, int lineNo) {
125: Line.Set lines = lineCookie.getLineSet();
126: try {
127: Line line = lines.getCurrent(lineNo);
128: if (null == line)
129: line = lines.getCurrent(0);
130: if (null != line) {
131: line.show(Line.SHOW_TOFRONT);
132: return true;
133: }
134: } catch (IndexOutOfBoundsException e) {
135: //probably the document has been modified but not saved yet
136: }
137: return false;
138: }
139:
140: private boolean canOpenTask() {
141: if (null != Accessor.getActionListener(task))
142: return true;
143:
144: FileObject fo = Accessor.getResource(task);
145: if (null == fo)
146: return false;
147:
148: DataObject dob = null;
149: try {
150: dob = DataObject.find(fo);
151: } catch (DataObjectNotFoundException donfE) {
152: return false;
153: }
154: if (Accessor.getLine(task) > 0) {
155: return null != dob.getCookie(LineCookie.class);
156: }
157:
158: return null != dob.getCookie(OpenCookie.class)
159: || null != dob.getCookie(EditCookie.class)
160: || null != dob.getCookie(ViewCookie.class);
161: }
162: }
|