KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > base > app > IpanemaActionBuilder


1 package com.nightlabs.ipanema.base.app;
2
3 import java.beans.PropertyChangeEvent JavaDoc;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.eclipse.jface.action.GroupMarker;
11 import org.eclipse.jface.action.IAction;
12 import org.eclipse.jface.action.IContributionItem;
13 import org.eclipse.jface.action.ICoolBarManager;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.action.MenuManager;
16 import org.eclipse.jface.action.Separator;
17 import org.eclipse.ui.ISharedImages;
18 import org.eclipse.ui.IWorkbenchActionConstants;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.actions.ActionFactory;
21 import org.eclipse.ui.actions.ContributionItemFactory;
22 import org.eclipse.ui.application.IActionBarConfigurer;
23 import org.eclipse.ui.application.WorkbenchAdvisor;
24
25 import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
26
27 import com.nightlabs.config.Config;
28 import com.nightlabs.config.ConfigException;
29 import com.nightlabs.ipanema.base.IpanemaBasePlugin;
30 import com.nightlabs.rcp.action.NewFileRegistry;
31 import com.nightlabs.rcp.action.OpenFileAction;
32 import com.nightlabs.rcp.action.ReOpenFileAction;
33 import com.nightlabs.rcp.config.RecentFileConfig;
34
35 /**
36  * Creates the Menu
37  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
38  *
39  */

40 public class IpanemaActionBuilder
41 {
42     public static final String JavaDoc RECENT_FILES_MENU_ID = "recentFilesMenu";
43     
44     private IWorkbenchWindow window;
45     
46     // File-Menu
47
private IMenuManager newMenu;
48     private IMenuManager recentFilesMenu;
49 // private ActionFactory.IWorkbenchAction newAction;
50
// private ActionFactory.IWorkbenchAction closeAction;
51
// private ActionFactory.IWorkbenchAction closeAllAction;
52
private ActionFactory.IWorkbenchAction saveAction;
53     private ActionFactory.IWorkbenchAction saveAsAction;
54 // private ActionFactory.IWorkbenchAction printAction;
55
private ActionFactory.IWorkbenchAction importAction;
56     private ActionFactory.IWorkbenchAction exportAction;
57 // private ActionFactory.IWorkbenchAction propertiesAction;
58
private ActionFactory.IWorkbenchAction quitAction;
59     private OpenFileAction openAction;
60     
61     // Help-Menu
62
// private ActionFactory.IWorkbenchAction introAction;
63
private ActionFactory.IWorkbenchAction helpAction;
64 // private ActionFactory.IWorkbenchAction updateAction;
65
private ActionFactory.IWorkbenchAction aboutAction;
66     
67     // Window-Menu
68
private IContributionItem openPerspectiveMenu;
69     private IContributionItem showViewMenu;
70     private ActionFactory.IWorkbenchAction preferencesAction;
71             
72     public IpanemaActionBuilder(IWorkbenchWindow window) {
73         this.window = window;
74         try {
75             fileHistory = (RecentFileConfig) Config.sharedInstance().createConfigModule(RecentFileConfig.class);
76         } catch (ConfigException e) {
77             // TODO Auto-generated catch block
78
e.printStackTrace();
79         }
80     }
81     
82     public void fillActionBars(IActionBarConfigurer configurer, int flags)
83     {
84         if ((flags & WorkbenchAdvisor.FILL_PROXY) == 0) {
85             makeActions();
86         }
87         if ((flags & WorkbenchAdvisor.FILL_MENU_BAR) != 0) {
88             fillMenuBar(configurer.getMenuManager());
89         }
90         if ((flags & WorkbenchAdvisor.FILL_COOL_BAR) != 0) {
91             fillCoolBar(configurer.getCoolBarManager());
92         }
93     }
94     
95     private void makeActions()
96     {
97 // ISharedImages images = window.getWorkbench().getSharedImages();
98

99         // File-Menu
100
newMenu = new MenuManager("New", ActionFactory.NEW.getId());
101 // newMenu.add((ActionFactory.NEW.create(window)));
102
newMenu.add(new GroupMarker(ActionFactory.NEW.getId()));
103         
104 // newAction = ActionFactory.NEW.create(window);
105
openAction = new OpenFileAction();
106         openAction.addPropertyChangeListener(historyFileListener);
107         recentFilesMenu = new MenuManager(IpanemaBasePlugin.getResourceString("menu.openrecentfiles.text"), RECENT_FILES_MENU_ID);
108         recentFilesMenu.add(new GroupMarker(IWorkbenchActionConstants.HISTORY_GROUP));
109 // closeAction = ActionFactory.CLOSE.create(window);
110
// closeAllAction = ActionFactory.CLOSE_ALL.create(window);
111
saveAction = ActionFactory.SAVE.create(window);
112         saveAsAction = ActionFactory.SAVE_AS.create(window);
113 // printAction = ActionFactory.PRINT.create(window);
114
importAction = ActionFactory.IMPORT.create(window);
115         exportAction = ActionFactory.EXPORT.create(window);
116 // propertiesAction = ActionFactory.PROPERTIES.create(window);
117
quitAction = ActionFactory.QUIT.create(window);
118         
119         // Window-Menu
120
openPerspectiveMenu = ContributionItemFactory.PERSPECTIVES_SHORTLIST.create(window);
121         showViewMenu = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
122         preferencesAction = ActionFactory.PREFERENCES.create(window);
123                 
124         // Help-Menu
125
// introAction = ActionFactory.INTRO.create(window);
126
helpAction = ActionFactory.HELP_CONTENTS.create(window);
127         aboutAction = ActionFactory.ABOUT.create(window);
128     }
129
130     public void fillMenuBar(IMenuManager menuBar)
131     {
132       // File-Menu
133
IMenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE); //$NON-NLS-2$
134
menuBar.add(fileMenu);
135
136         fileMenu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
137         fileMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
138         fileMenu.add(newMenu);
139         createNewEntries(newMenu);
140     fileMenu.add(openAction);
141
142     fileMenu.add(recentFilesMenu);
143         historyFileMenuManager = recentFilesMenu;
144         createHistoryEntries(historyFileMenuManager);
145         
146         fileMenu.add(new Separator());
147 // fileMenu.add(closeAction);
148
// fileMenu.add(closeAllAction);
149

150     fileMenu.add(new GroupMarker(IWorkbenchActionConstants.SAVE_GROUP));
151         fileMenu.add(saveAction);
152         fileMenu.add(saveAsAction);
153         fileMenu.add(new Separator());
154         
155 // fileMenu.add(printAction);
156
fileMenu.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT));
157         fileMenu.add(importAction);
158         fileMenu.add(exportAction);
159         fileMenu.add(new Separator());
160                 
161         fileMenu.add(quitAction);
162     fileMenu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
163         
164     menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
165     
166     // Window-Menu
167
IMenuManager windowMenu = new MenuManager("&Window", IWorkbenchActionConstants.M_WINDOW);
168         menuBar.add(windowMenu);
169         
170         // Perspective-SubMenu
171
MenuManager openPerspectiveMenuMgr = new MenuManager("Open Perspective", "openPerspective");
172         openPerspectiveMenuMgr.add(openPerspectiveMenu);
173         windowMenu.add(openPerspectiveMenuMgr);
174         
175         // View-SubMenu
176
MenuManager showViewMenuMgr = new MenuManager("Show View", "showView");
177         showViewMenuMgr.add(showViewMenu);
178         windowMenu.add(showViewMenuMgr);
179         windowMenu.add(new Separator());
180         
181         windowMenu.add(preferencesAction);
182         
183         // Help-Menu
184
IMenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP); //$NON-NLS-2$
185
menuBar.add(helpMenu);
186 // helpMenu.add(introAction);
187
helpMenu.add(helpAction);
188         helpMenu.add(new Separator());
189         helpMenu.add(aboutAction);
190     }
191
192     public void fillCoolBar(ICoolBarManager coolBar)
193     {
194
195     }
196     
197     public void dispose()
198     {
199         aboutAction.dispose();
200 // closeAction.dispose();
201
// closeAllAction.dispose();
202
exportAction.dispose();
203         helpAction.dispose();
204         importAction.dispose();
205 // introAction.dispose();
206
// newAction.dispose();
207

208         preferencesAction.dispose();
209 // printAction.dispose();
210
// propertiesAction.dispose();
211
quitAction.dispose();
212         saveAction.dispose();
213         saveAsAction.dispose();
214     }
215     
216     protected RecentFileConfig fileHistory;
217     protected IMenuManager historyFileMenuManager;
218     protected int historyEntries = 0;
219     protected int maxHistoryLength = 0;
220     protected String JavaDoc firstHistoryID = null;
221     protected String JavaDoc lastHistoryID = null;
222         
223     protected PropertyChangeListener JavaDoc historyFileListener = new PropertyChangeListener JavaDoc() {
224         public void propertyChange(PropertyChangeEvent JavaDoc arg0) {
225             if (arg0.getPropertyName().equals(OpenFileAction.HISTORY_FILE_ADDED)) {
226                 String JavaDoc fileName = (String JavaDoc) arg0.getNewValue();
227                 addHistoryFile(historyFileMenuManager, fileName, false);
228             }
229         }
230     };
231         
232     protected void addHistoryFile(IMenuManager menuMan, String JavaDoc fileName, boolean append)
233     {
234         ReOpenFileAction action = new ReOpenFileAction(fileName);
235         if (firstHistoryID == null) {
236             firstHistoryID = action.getId();
237             menuMan.add(action);
238         }
239         else
240         {
241             if (!append) {
242                 menuMan.insertBefore(firstHistoryID, action);
243                 firstHistoryID = action.getId();
244             }
245             else
246                 menuMan.add(action);
247         }
248         
249         historyEntries++;
250         
251         if (maxHistoryLength == historyEntries)
252             lastHistoryID = action.getId();
253         
254         if (maxHistoryLength < historyEntries)
255         {
256             menuMan.remove(lastHistoryID);
257             if (!fileHistory.getRecentFileNames().contains(fileName))
258                 fileHistory.getRecentFileNames().add(fileName);
259             
260             for (int i=0; i<fileHistory.getRecentFileNames().size()-maxHistoryLength; i++) {
261                 fileHistory.getRecentFileNames().remove(i);
262             }
263         }
264     }
265     
266     /**
267      * creates the MenuEntries of all previous opened files
268      * @param menuMan The IMenuManager to which the entries should be added
269      */

270     protected void createHistoryEntries(IMenuManager menuMan)
271     {
272         if (fileHistory != null) {
273             List JavaDoc fileNames = fileHistory.getRecentFileNames();
274             maxHistoryLength = fileHistory.getMaxHistoryLength();
275             if (fileNames.size() != 0) {
276                 for (int i=fileNames.size()-1; i!=0; i--) {
277                     String JavaDoc fileName = (String JavaDoc) fileNames.get(i);
278                     addHistoryFile(menuMan, fileName, true);
279                 }
280             }
281         }
282     }
283         
284     protected void createNewEntries(IMenuManager menuMan)
285     {
286         NewFileRegistry newFileRegistry = NewFileRegistry.getSharedInstance();
287         Map JavaDoc categoryID2Action = newFileRegistry.getCategory2Actions();
288         List JavaDoc defaultActions = new ArrayList JavaDoc();
289         for (Iterator JavaDoc it = categoryID2Action.keySet().iterator(); it.hasNext(); )
290         {
291             String JavaDoc categoryID = (String JavaDoc) it.next();
292             IAction action = (IAction) categoryID2Action.get(categoryID);
293             if (categoryID.equals(NewFileRegistry.DEFAULT_CATEGORY)) {
294                 defaultActions.add(action);
295             }
296             else {
297                 String JavaDoc categoryName = newFileRegistry.getCategoryName(categoryID);
298                 if (categoryName != null && !categoryName.equals("")) {
299                     IMenuManager categoryMenu = new MenuManager(categoryName);
300                     categoryMenu.add(action);
301                     menuMan.add(categoryMenu);
302                 }
303             }
304         }
305         for (Iterator JavaDoc itDefault = defaultActions.iterator(); itDefault.hasNext(); ) {
306             menuMan.add((IAction)itDefault.next());
307         }
308     }
309     
310 }
311
Popular Tags