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.mercurial.ui.status;
043:
044: import org.netbeans.modules.mercurial.util.*;
045: import org.openide.util.NbBundle;
046: import org.openide.nodes.Node;
047: import org.openide.cookies.*;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileUtil;
050: import org.openide.loaders.DataObject;
051: import org.openide.loaders.DataObjectNotFoundException;
052:
053: import javax.swing.*;
054: import java.awt.event.ActionEvent;
055: import java.io.File;
056:
057: /**
058: * Opens the file under {@link SyncFileNode} in editor.
059: *
060: * @author Maros Sandor
061: */
062: public class OpenInEditorAction extends AbstractAction {
063:
064: public OpenInEditorAction() {
065: super (NbBundle.getBundle(OpenInEditorAction.class).getString(
066: "CTL_Synchronize_Popup_OpenInEditor")); // NOI18N
067: setEnabled(isActionEnabled());
068: }
069:
070: private boolean isActionEnabled() {
071: for (File file : HgUtils.getCurrentContext(null).getRootFiles()) {
072: if (file.canRead())
073: return true;
074: }
075: return false;
076: }
077:
078: public void actionPerformed(ActionEvent e) {
079: for (File file : HgUtils.getCurrentContext(null).getRootFiles()) {
080: FileObject fo = FileUtil.toFileObject(file);
081: if (fo != null) {
082: try {
083: openDataObjectByCookie(DataObject.find(fo));
084: } catch (DataObjectNotFoundException ex) {
085: // ignore this error and try to open other files too
086: }
087: }
088: }
089: }
090:
091: private final boolean openDataObjectByCookie(DataObject dataObject) {
092: Node.Cookie cookie;
093:
094: if ((cookie = dataObject
095: .getCookie(EditorCookie.Observable.class)) != null)
096: return openByCookie(cookie, EditorCookie.Observable.class);
097: if ((cookie = dataObject.getCookie(EditorCookie.class)) != null)
098: return openByCookie(cookie, EditorCookie.class);
099: if ((cookie = dataObject.getCookie(OpenCookie.class)) != null)
100: return openByCookie(cookie, OpenCookie.class);
101: if ((cookie = dataObject.getCookie(EditCookie.class)) != null)
102: return openByCookie(cookie, EditCookie.class);
103: if ((cookie = dataObject.getCookie(ViewCookie.class)) != null)
104: return openByCookie(cookie, ViewCookie.class);
105:
106: return false;
107: }
108:
109: private boolean openByCookie(Node.Cookie cookie, Class cookieClass) {
110: if ((cookieClass == EditorCookie.class)
111: || (cookieClass == EditorCookie.Observable.class)) {
112: ((EditorCookie) cookie).open();
113: } else if (cookieClass == OpenCookie.class) {
114: ((OpenCookie) cookie).open();
115: } else if (cookieClass == EditCookie.class) {
116: ((EditCookie) cookie).edit();
117: } else if (cookieClass == ViewCookie.class) {
118: ((ViewCookie) cookie).view();
119: } else {
120: throw new IllegalArgumentException("Reopen #58766: "
121: + cookieClass); // NOI18N
122: }
123: return true;
124: }
125: }
|