KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > dataobject > LanguagesDataNode


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.languages.dataobject;
21
22 import org.openide.ErrorManager;
23 import org.openide.cookies.InstanceCookie;
24 import org.openide.filesystems.FileObject;
25 import org.openide.filesystems.Repository;
26 import org.openide.loaders.DataFolder;
27 import org.openide.loaders.DataNode;
28 import org.openide.loaders.DataObject;
29 import org.openide.nodes.Children;
30
31 import javax.swing.*;
32 import java.io.IOException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37
38
39 public class LanguagesDataNode extends DataNode {
40
41     public LanguagesDataNode(LanguagesDataObject obj) {
42         super(obj, Children.LEAF);
43         String JavaDoc mimeType = obj.getPrimaryFile ().getMIMEType ();
44         FileObject fo = Repository.getDefault ().getDefaultFileSystem ().
45             findResource ("Editors/" + mimeType + "/language.nbs");
46         String JavaDoc icon = (String JavaDoc) fo.getAttribute ("icon");
47         if (icon == null)
48             icon = "org/netbeans/modules/languages/resources/defaultIcon.jpg";
49         setIconBaseWithExtension (icon);
50     }
51
52 // /** Creates a property sheet. */
53
// protected Sheet createSheet() {
54
// Sheet s = super.createSheet();
55
// Sheet.Set ss = s.get(Sheet.PROPERTIES);
56
// if (ss == null) {
57
// ss = Sheet.createPropertiesSet();
58
// s.put(ss);
59
// }
60
// // TODO add some relevant properties: ss.put(...)
61
// return s;
62
// }
63

64     private Map JavaDoc<String JavaDoc,Action[]> mimeTypeToActions = new HashMap JavaDoc<String JavaDoc,Action[]> ();
65     
66     /** Get actions for this data object.
67     * @see DataLoader#getActions
68     * @return array of actions or <code>null</code>
69     */

70     public Action[] getActions (boolean context) {
71         String JavaDoc mimeType = getDataObject ().getPrimaryFile ().getMIMEType ();
72         if (!mimeTypeToActions.containsKey (mimeType)) {
73             List JavaDoc<Action> actions = new ArrayList JavaDoc<Action> ();
74             try {
75                 FileObject fo = Repository.getDefault ().getDefaultFileSystem ().
76                     findResource ("Loaders/" + mimeType + "/Actions");
77                 if (fo != null) {
78                     DataFolder df = DataFolder.findFolder (fo);
79                     DataObject[] dob = df.getChildren ();
80                     int i, k = dob.length;
81                     for (i = 0; i < k; i++) {
82                         InstanceCookie ic = (InstanceCookie) dob [i].getCookie
83                             (InstanceCookie.class);
84                         Class JavaDoc clazz = ic.instanceClass ();
85                         if (JSeparator.class.isAssignableFrom (clazz))
86                             actions.add (null);
87                         else
88                             actions.add ((Action) ic.instanceCreate ());
89                     }
90                 }
91             } catch (ClassNotFoundException JavaDoc ex) {
92                 ErrorManager.getDefault ().notify (ex);
93             } catch (IOException JavaDoc ex) {
94                 ErrorManager.getDefault ().notify (ex);
95             }
96             if (!actions.isEmpty ())
97                 mimeTypeToActions.put (mimeType, actions.toArray (new Action [actions.size ()]));
98             else
99                 mimeTypeToActions.put (mimeType, super.getActions (context));
100         }
101         return (Action[]) mimeTypeToActions.get (mimeType);
102     }
103 }
104
105
106
107
108
Popular Tags