KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > syncview > OpenInEditorAction


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.versioning.system.cvss.ui.syncview;
21
22 import org.openide.util.NbBundle;
23 import org.openide.nodes.Node;
24 import org.openide.cookies.EditorCookie;
25 import org.openide.cookies.EditCookie;
26 import org.openide.cookies.OpenCookie;
27 import org.openide.cookies.ViewCookie;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataObjectNotFoundException;
32 import org.netbeans.modules.versioning.system.cvss.util.Utils;
33
34 import javax.swing.*;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.io.File JavaDoc;
37
38 /**
39  * Opens the file under {@link SyncFileNode} in editor.
40  *
41  * @author Maros Sandor
42  */

43 public class OpenInEditorAction extends AbstractAction {
44
45     public OpenInEditorAction() {
46         super(NbBundle.getBundle(OpenInEditorAction.class).getString("CTL_Synchronize_Popup_OpenInEditor"));
47         setEnabled(isActionEnabled());
48     }
49
50     private boolean isActionEnabled() {
51         File JavaDoc [] files = Utils.getCurrentContext(null).getFiles();
52         for (File JavaDoc file : files) {
53             if (file.canRead()) return true;
54         }
55         return false;
56     }
57
58     public void actionPerformed(ActionEvent JavaDoc e) {
59         File JavaDoc [] files = Utils.getCurrentContext(null).getFiles();
60         for (File JavaDoc file : files) {
61             FileObject fo = FileUtil.toFileObject(file);
62             if (fo != null) {
63                 try {
64                     openDataObjectByCookie(DataObject.find(fo));
65                 } catch (DataObjectNotFoundException ex) {
66                     // ignore this error and try to open other files too
67
}
68             }
69         }
70     }
71     
72     private final boolean openDataObjectByCookie(DataObject dataObject) {
73         Node.Cookie cookie;
74         Class JavaDoc cookieClass;
75         if ((((cookie = dataObject.getCookie(cookieClass = EditorCookie.Observable.class)) != null
76                             || (cookie = dataObject.getCookie(cookieClass = EditorCookie.class)) != null))
77                 || (cookie = dataObject.getCookie(cookieClass = OpenCookie.class)) != null
78                 || (cookie = dataObject.getCookie(cookieClass = EditCookie.class)) != null
79                 || (cookie = dataObject.getCookie(cookieClass = ViewCookie.class)) != null) {
80             return openByCookie(cookie, cookieClass);
81         }
82         return false;
83     }
84     
85     private boolean openByCookie(Node.Cookie cookie, Class JavaDoc cookieClass) {
86         if ((cookieClass == EditorCookie.class)
87                 || (cookieClass == EditorCookie.Observable.class)) {
88             ((EditorCookie) cookie).open();
89         } else if (cookieClass == OpenCookie.class) {
90             ((OpenCookie) cookie).open();
91         } else if (cookieClass == EditCookie.class) {
92             ((EditCookie) cookie).edit();
93         } else if (cookieClass == ViewCookie.class) {
94             ((ViewCookie) cookie).view();
95         } else {
96             throw new IllegalArgumentException JavaDoc("Reopen #58766: " + cookieClass); // NOI18N
97
}
98         return true;
99     }
100 }
101
Popular Tags