KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > status > 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.subversion.ui.status;
21
22 import org.netbeans.modules.subversion.util.*;
23 import org.openide.util.NbBundle;
24 import org.openide.nodes.Node;
25 import org.openide.cookies.*;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataObjectNotFoundException;
30
31 import javax.swing.*;
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.io.File JavaDoc;
34
35 /**
36  * Opens the file under {@link SyncFileNode} in editor.
37  *
38  * @author Maros Sandor
39  */

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