1 package com.nightlabs.ipanema.base.app; 2 3 import java.beans.PropertyChangeEvent ; 4 import java.beans.PropertyChangeListener ; 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 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 40 public class IpanemaActionBuilder 41 { 42 public static final String RECENT_FILES_MENU_ID = "recentFilesMenu"; 43 44 private IWorkbenchWindow window; 45 46 private IMenuManager newMenu; 48 private IMenuManager recentFilesMenu; 49 private ActionFactory.IWorkbenchAction saveAction; 53 private ActionFactory.IWorkbenchAction saveAsAction; 54 private ActionFactory.IWorkbenchAction importAction; 56 private ActionFactory.IWorkbenchAction exportAction; 57 private ActionFactory.IWorkbenchAction quitAction; 59 private OpenFileAction openAction; 60 61 private ActionFactory.IWorkbenchAction helpAction; 64 private ActionFactory.IWorkbenchAction aboutAction; 66 67 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 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 99 newMenu = new MenuManager("New", ActionFactory.NEW.getId()); 101 newMenu.add(new GroupMarker(ActionFactory.NEW.getId())); 103 104 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 saveAction = ActionFactory.SAVE.create(window); 112 saveAsAction = ActionFactory.SAVE_AS.create(window); 113 importAction = ActionFactory.IMPORT.create(window); 115 exportAction = ActionFactory.EXPORT.create(window); 116 quitAction = ActionFactory.QUIT.create(window); 118 119 openPerspectiveMenu = ContributionItemFactory.PERSPECTIVES_SHORTLIST.create(window); 121 showViewMenu = ContributionItemFactory.VIEWS_SHORTLIST.create(window); 122 preferencesAction = ActionFactory.PREFERENCES.create(window); 123 124 helpAction = ActionFactory.HELP_CONTENTS.create(window); 127 aboutAction = ActionFactory.ABOUT.create(window); 128 } 129 130 public void fillMenuBar(IMenuManager menuBar) 131 { 132 IMenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE); 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 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(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 IMenuManager windowMenu = new MenuManager("&Window", IWorkbenchActionConstants.M_WINDOW); 168 menuBar.add(windowMenu); 169 170 MenuManager openPerspectiveMenuMgr = new MenuManager("Open Perspective", "openPerspective"); 172 openPerspectiveMenuMgr.add(openPerspectiveMenu); 173 windowMenu.add(openPerspectiveMenuMgr); 174 175 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 IMenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP); menuBar.add(helpMenu); 186 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 exportAction.dispose(); 203 helpAction.dispose(); 204 importAction.dispose(); 205 208 preferencesAction.dispose(); 209 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 firstHistoryID = null; 221 protected String lastHistoryID = null; 222 223 protected PropertyChangeListener historyFileListener = new PropertyChangeListener () { 224 public void propertyChange(PropertyChangeEvent arg0) { 225 if (arg0.getPropertyName().equals(OpenFileAction.HISTORY_FILE_ADDED)) { 226 String fileName = (String ) arg0.getNewValue(); 227 addHistoryFile(historyFileMenuManager, fileName, false); 228 } 229 } 230 }; 231 232 protected void addHistoryFile(IMenuManager menuMan, String 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 270 protected void createHistoryEntries(IMenuManager menuMan) 271 { 272 if (fileHistory != null) { 273 List fileNames = fileHistory.getRecentFileNames(); 274 maxHistoryLength = fileHistory.getMaxHistoryLength(); 275 if (fileNames.size() != 0) { 276 for (int i=fileNames.size()-1; i!=0; i--) { 277 String fileName = (String ) 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 categoryID2Action = newFileRegistry.getCategory2Actions(); 288 List defaultActions = new ArrayList (); 289 for (Iterator it = categoryID2Action.keySet().iterator(); it.hasNext(); ) 290 { 291 String categoryID = (String ) 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 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 itDefault = defaultActions.iterator(); itDefault.hasNext(); ) { 306 menuMan.add((IAction)itDefault.next()); 307 } 308 } 309 310 } 311 | Popular Tags |