KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > forte > KelpInstaller


1 package org.enhydra.kelp.forte;
2
3 import java.io.*;
4
5 import org.openide.TopManager;
6 import org.openide.filesystems.FileUtil;
7 import org.openide.loaders.*;
8 import org.openide.modules.ModuleInstall;
9 import org.openide.util.NbBundle;
10 import org.openide.util.Utilities;
11
12 import org.enhydra.kelp.forte.actions.AppWizardAction;
13 import org.enhydra.kelp.forte.actions.XMLCAction;
14 import org.enhydra.kelp.forte.actions.DeploymentAction;
15 import org.enhydra.kelp.forte.actions.DODSAction;
16
17 //mn 10.06.2002
18
import java.io.File JavaDoc;
19 /** Manages a module's lifecycle.
20  *
21  * @author rees0234
22  */

23 public class KelpInstaller extends ModuleInstall {
24
25     // By default, do nothing but call restored ().
26

27     public void installed () {
28     // Install some actions:
29
try {
30         createAction (AppWizardAction.class, DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "File"),
31               "New", true, false, false, false, false);
32         InstanceDataObject.create (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "File"),
33                        "AppWizardAction", AppWizardAction.class);
34
35             //Dusan 20.11.2002.
36
createAction (DODSAction.class, DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "Tools"),
37               "", true, false, true, false, false);
38         InstanceDataObject.create (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "Tools"),
39                        "DODSAction", DODSAction.class);
40
41         createAction (XMLCAction.class, DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "Tools"),
42               "DODSAction", true, false, true, false, false);
43         InstanceDataObject.create (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "Tools"),
44                        "XMLCAction", XMLCAction.class);
45         createAction (DeploymentAction.class, DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "Tools"),
46               "XMLCAction", true, false, false, false, true);
47         InstanceDataObject.create (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "Tools"),
48                        "DeploymentAction", DeploymentAction.class);
49
50     } catch (IOException ioe) {
51         if (Boolean.getBoolean ("netbeans.debug.exceptions"))
52         ioe.printStackTrace ();
53     }
54     restored ();
55     }
56
57
58     public void uninstalled () {
59     // Remove your actions:
60
try {
61         removeAction (AppWizardAction.class,
62               DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "File"),
63               false);
64         InstanceDataObject.remove (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "File"),
65                        "AppWizardAction", AppWizardAction.class);
66
67             //Dusan 20.11.2002.
68
removeAction (DODSAction.class,
69               DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "Tools"),
70               false);
71         InstanceDataObject.remove (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "Tools"),
72                        "DODSAction", XMLCAction.class);
73
74             removeAction (XMLCAction.class,
75               DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "Tools"),
76               false);
77         InstanceDataObject.remove (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "Tools"),
78                        "XMLCAction", XMLCAction.class);
79             removeAction (DeploymentAction.class,
80               DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().menus (), "Tools"),
81               false);
82         InstanceDataObject.remove (DataFolder.create (TopManager.getDefault ().getPlaces ().folders ().actions (), "Tools"),
83                        "DeploymentAction", DeploymentAction.class);
84
85     } catch (IOException ioe) {
86         if (Boolean.getBoolean ("netbeans.debug.exceptions"))
87         ioe.printStackTrace ();
88     }
89     }
90
91     // mn 10.06.2002
92
public boolean closing() {
93         uninstalled();
94
95         char sep = File.separatorChar;
96
97         File JavaDoc f = new File JavaDoc(System.getProperty("netbeans.user")+sep+"system"+sep+"Modules"+sep+"org-enhydra-kelp-forte.xml");
98         f.delete();
99
100         return true;
101     }
102
103     // By default, call restored ().
104
// If you have changed e.g. actions since a previous release, you can
105
// specify how to update the module installation, e.g.:
106
/*
107     public void updated (int release, String specVersion) {
108     // removeAction's for the old actions
109     // createAction's for the new actions
110     }
111     */

112
113     /** Create an action instance.
114     * Goes to some effort to put it into the right place.
115     * You can customize this logic according to your needs.
116     * @param actionClass the class of the action to install
117     * @param folder the folder to install it into
118     * @param relativeTo another item it should be installed next to (identifying portion of file basename)
119     * @param after if true, install after that item; else install before it
120     * @param isToolbar if true, use toolbar separators, else menu separators
121     * @param skipSeparator if true, install on the other side of any separator which may be there
122     * @param separatorBefore if true, make sure a separator is installed before this item
123     * @param separatorAfter if true, make sure a separator is installed after this item
124     * @throws IOException if the creation failed
125     */

126     private void createAction (Class JavaDoc actionClass, DataFolder folder, String JavaDoc relativeTo, boolean after, boolean isToolbar,
127                                boolean skipSeparator, boolean separatorBefore, boolean separatorAfter) throws IOException {
128         String JavaDoc actionShortName = Utilities.getShortClassName (actionClass);
129         String JavaDoc actionName = actionClass.getName ();
130
131         if (InstanceDataObject.find (folder, actionShortName, actionName) != null) return;
132
133         DataObject[] children = folder.getChildren ();
134         int indexToUse = -1;
135         for (int i = 0; i < children.length; i++) {
136             if (children[i] instanceof InstanceDataObject &&
137                     children[i].getPrimaryFile ().getName ().indexOf (relativeTo) != -1) {
138                 indexToUse = i;
139                 break;
140             }
141         }
142         InstanceDataObject actionInstance = InstanceDataObject.create (folder, actionShortName, actionName);
143
144         if (indexToUse != -1) {
145             if (after) {
146                 indexToUse += 1;
147                 if (skipSeparator) {
148                     if ((indexToUse < children.length) &&
149                             (children[indexToUse].getPrimaryFile ().getName ().indexOf ("Separator") != -1)) {
150                         indexToUse += 1;
151                     }
152                 }
153             } else {
154                 if (skipSeparator) {
155                     if ((indexToUse > 0) &&
156                             (children[indexToUse - 1].getPrimaryFile ().getName ().indexOf ("Separator") != -1)) {
157                         indexToUse -= 1;
158                     }
159                 }
160             }
161
162             InstanceDataObject beforeSeparator = separatorBefore ?
163                                                  InstanceDataObject.create (folder, "Separator1-" + actionShortName, sepClass (isToolbar)) :
164                                                  null;
165             InstanceDataObject afterSeparator = separatorAfter ?
166                                                 InstanceDataObject.create (folder, "Separator2-" + actionShortName, sepClass (isToolbar)) :
167                                                 null;
168
169             int itemsAdded = 0;
170             if (separatorBefore) itemsAdded ++;
171             if (separatorAfter) itemsAdded ++;
172             int currentIndex = indexToUse;
173
174             DataObject[] newOrder = new DataObject [children.length + 1 + itemsAdded];
175             System.arraycopy (children, 0, newOrder, 0, indexToUse);
176
177             if (separatorBefore) newOrder[currentIndex++] = beforeSeparator;
178             newOrder[currentIndex++] = actionInstance;
179             if (separatorAfter) newOrder[currentIndex++] = afterSeparator;
180
181             System.arraycopy (children, indexToUse, newOrder, indexToUse + 1 + itemsAdded, children.length - indexToUse);
182             folder.setOrder (newOrder);
183         }
184     }
185
186     /** Remove a previously-installed action.
187     * @param actionClass the class of action to uninstall
188     * @param folder the folder it had been installed to
189     * @param isToolbar if true, use toolbar separators, else menu separators
190     * @throws IOException if the removal of the action failed
191     */

192     private void removeAction (Class JavaDoc actionClass, DataFolder folder, boolean isToolbar) throws IOException {
193         String JavaDoc actionShortName = Utilities.getShortClassName (actionClass);
194         if (! InstanceDataObject.remove (folder, actionShortName, actionClass.getName ()))
195             throw new IOException ("Note: removal of " + actionShortName + " from " +
196                                    folder.getPrimaryFile ().getPackageName ('/') + " failed");
197         InstanceDataObject.remove (folder, "Separator1-" + actionShortName, sepClass (isToolbar));
198         InstanceDataObject.remove (folder, "Separator2-" + actionShortName, sepClass (isToolbar));
199     }
200
201     private String JavaDoc sepClass (boolean isToolbar) {
202         return isToolbar ? "javax.swing.JToolBar$Separator" : "javax.swing.JSeparator";
203     }
204
205 }
206
Popular Tags