KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > impl > ToolbarActionsProvider


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.editor.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.api.editor.mimelookup.MimeLookup;
26 import org.netbeans.api.editor.mimelookup.MimePath;
27 import org.netbeans.spi.editor.mimelookup.Class2LayerFolder;
28 import org.netbeans.spi.editor.mimelookup.InstanceProvider;
29 import org.openide.filesystems.FileObject;
30 import org.openide.loaders.DataObject;
31
32 /**
33  *
34  * @author Vita Stejskal
35  */

36 public final class ToolbarActionsProvider extends ActionsList implements Class2LayerFolder, InstanceProvider {
37
38     private static final String JavaDoc TOOLBAR_ACTIONS_FOLDER_NAME = "Toolbars/Default"; //NOI18N
39
private static final String JavaDoc TEXT_BASE_PATH = "Editors/text/base/"; //NOI18N
40

41     public static List JavaDoc getToolbarItems(String JavaDoc mimeType) {
42         MimePath mimePath = MimePath.parse(mimeType);
43         ActionsList provider;
44         if (mimeType.equals("text/base")) { //NOI18N
45
provider = MimeLookup.getLookup(mimePath).lookup(LegacyToolbarActionsProvider.class);
46         } else {
47             provider = MimeLookup.getLookup(mimePath).lookup(ToolbarActionsProvider.class);
48         }
49         return provider == null ? Collections.emptyList() : provider.getAllInstances();
50     }
51     
52     public ToolbarActionsProvider() {
53         super(null);
54     }
55
56     private ToolbarActionsProvider(List JavaDoc keys) {
57         super(keys, true);
58     }
59     
60     public Class JavaDoc getClazz(){
61         return ToolbarActionsProvider.class;
62     }
63
64     public String JavaDoc getLayerFolderName(){
65         return TOOLBAR_ACTIONS_FOLDER_NAME;
66     }
67
68     public InstanceProvider getInstanceProvider() {
69         return new ToolbarActionsProvider();
70     }
71     
72     public Object JavaDoc createInstance(List JavaDoc fileObjectList) {
73         return new ToolbarActionsProvider(fileObjectList);
74     }
75     
76     // XXX: This is here to help NbEditorToolbar to deal with legacy code
77
// that registered toolbar actions in text/base. The artificial text/base
78
// mime type is deprecated and should not be used anymore.
79
public static final class LegacyToolbarActionsProvider extends ActionsList implements Class2LayerFolder, InstanceProvider {
80
81         public LegacyToolbarActionsProvider() {
82             super(null);
83         }
84
85         private LegacyToolbarActionsProvider(List JavaDoc keys) {
86             super(keys);
87         }
88
89         public Class JavaDoc getClazz(){
90             return LegacyToolbarActionsProvider.class;
91         }
92
93         public String JavaDoc getLayerFolderName(){
94             return TOOLBAR_ACTIONS_FOLDER_NAME;
95         }
96
97         public InstanceProvider getInstanceProvider() {
98             return new LegacyToolbarActionsProvider();
99         }
100
101         public Object JavaDoc createInstance(List JavaDoc fileObjectList) {
102             ArrayList JavaDoc<FileObject> textBaseFilesList = new ArrayList JavaDoc<FileObject>();
103
104             for(Object JavaDoc o : fileObjectList) {
105                 FileObject fileObject = null;
106
107                 if (o instanceof DataObject) {
108                     fileObject = ((DataObject) o).getPrimaryFile();
109                 } else if (o instanceof FileObject) {
110                     fileObject = (FileObject) o;
111                 } else {
112                     continue;
113                 }
114
115                 String JavaDoc fullPath = fileObject.getPath();
116                 int idx = fullPath.lastIndexOf(TOOLBAR_ACTIONS_FOLDER_NAME);
117                 assert idx != -1 : "Expecting files with '" + TOOLBAR_ACTIONS_FOLDER_NAME + "' in the path: " + fullPath; //NOI18N
118

119                 String JavaDoc path = fullPath.substring(0, idx);
120                 if (TEXT_BASE_PATH.equals(path)) {
121                     textBaseFilesList.add(fileObject);
122                 }
123             }
124
125             return new LegacyToolbarActionsProvider(textBaseFilesList);
126         }
127     } // End of LegacyToolbarActionsProvider class
128
}
129
Popular Tags