Go to the documentation of this file.00001
00007 package org.best_of_robotics.model.datatypes.presentation;
00008
00009 import java.util.ArrayList;
00010 import java.util.Collection;
00011
00012 import org.eclipse.emf.common.ui.viewer.IViewerProvider;
00013
00014 import org.eclipse.emf.edit.domain.EditingDomain;
00015 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
00016
00017 import org.eclipse.emf.edit.ui.action.ControlAction;
00018 import org.eclipse.emf.edit.ui.action.CreateChildAction;
00019 import org.eclipse.emf.edit.ui.action.CreateSiblingAction;
00020 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor;
00021 import org.eclipse.emf.edit.ui.action.LoadResourceAction;
00022 import org.eclipse.emf.edit.ui.action.ValidateAction;
00023
00024 import org.eclipse.jface.action.Action;
00025 import org.eclipse.jface.action.ActionContributionItem;
00026 import org.eclipse.jface.action.IAction;
00027 import org.eclipse.jface.action.IContributionItem;
00028 import org.eclipse.jface.action.IContributionManager;
00029 import org.eclipse.jface.action.IMenuListener;
00030 import org.eclipse.jface.action.IMenuManager;
00031 import org.eclipse.jface.action.IToolBarManager;
00032 import org.eclipse.jface.action.MenuManager;
00033 import org.eclipse.jface.action.Separator;
00034 import org.eclipse.jface.action.SubContributionItem;
00035
00036 import org.eclipse.jface.viewers.ISelection;
00037 import org.eclipse.jface.viewers.ISelectionChangedListener;
00038 import org.eclipse.jface.viewers.ISelectionProvider;
00039 import org.eclipse.jface.viewers.IStructuredSelection;
00040 import org.eclipse.jface.viewers.SelectionChangedEvent;
00041 import org.eclipse.jface.viewers.Viewer;
00042
00043 import org.eclipse.ui.IEditorPart;
00044 import org.eclipse.ui.PartInitException;
00045
00052 public class DatatypesActionBarContributor
00053 extends EditingDomainActionBarContributor
00054 implements ISelectionChangedListener {
00061 protected IEditorPart activeEditorPart;
00062
00069 protected ISelectionProvider selectionProvider;
00070
00077 protected IAction showPropertiesViewAction =
00078 new Action(DataTypesEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) {
00079 @Override
00080 public void run() {
00081 try {
00082 getPage().showView("org.eclipse.ui.views.PropertySheet");
00083 }
00084 catch (PartInitException exception) {
00085 DataTypesEditorPlugin.INSTANCE.log(exception);
00086 }
00087 }
00088 };
00089
00097 protected IAction refreshViewerAction =
00098 new Action(DataTypesEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) {
00099 @Override
00100 public boolean isEnabled() {
00101 return activeEditorPart instanceof IViewerProvider;
00102 }
00103
00104 @Override
00105 public void run() {
00106 if (activeEditorPart instanceof IViewerProvider) {
00107 Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer();
00108 if (viewer != null) {
00109 viewer.refresh();
00110 }
00111 }
00112 }
00113 };
00114
00122 protected Collection<IAction> createChildActions;
00123
00130 protected IMenuManager createChildMenuManager;
00131
00139 protected Collection<IAction> createSiblingActions;
00140
00147 protected IMenuManager createSiblingMenuManager;
00148
00155 public DatatypesActionBarContributor() {
00156 super(ADDITIONS_LAST_STYLE);
00157 loadResourceAction = new LoadResourceAction();
00158 validateAction = new ValidateAction();
00159 controlAction = new ControlAction();
00160 }
00161
00168 @Override
00169 public void contributeToToolBar(IToolBarManager toolBarManager) {
00170 toolBarManager.add(new Separator("datatypes-settings"));
00171 toolBarManager.add(new Separator("datatypes-additions"));
00172 }
00173
00181 @Override
00182 public void contributeToMenu(IMenuManager menuManager) {
00183 super.contributeToMenu(menuManager);
00184
00185 IMenuManager submenuManager = new MenuManager(DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesEditor_menu"), "org.best_of_robotics.model.datatypesMenuID");
00186 menuManager.insertAfter("additions", submenuManager);
00187 submenuManager.add(new Separator("settings"));
00188 submenuManager.add(new Separator("actions"));
00189 submenuManager.add(new Separator("additions"));
00190 submenuManager.add(new Separator("additions-end"));
00191
00192
00193
00194 createChildMenuManager = new MenuManager(DataTypesEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
00195 submenuManager.insertBefore("additions", createChildMenuManager);
00196
00197
00198
00199 createSiblingMenuManager = new MenuManager(DataTypesEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
00200 submenuManager.insertBefore("additions", createSiblingMenuManager);
00201
00202
00203
00204 submenuManager.addMenuListener
00205 (new IMenuListener() {
00206 public void menuAboutToShow(IMenuManager menuManager) {
00207 menuManager.updateAll(true);
00208 }
00209 });
00210
00211 addGlobalActions(submenuManager);
00212 }
00213
00220 @Override
00221 public void setActiveEditor(IEditorPart part) {
00222 super.setActiveEditor(part);
00223 activeEditorPart = part;
00224
00225
00226
00227 if (selectionProvider != null) {
00228 selectionProvider.removeSelectionChangedListener(this);
00229 }
00230 if (part == null) {
00231 selectionProvider = null;
00232 }
00233 else {
00234 selectionProvider = part.getSite().getSelectionProvider();
00235 selectionProvider.addSelectionChangedListener(this);
00236
00237
00238
00239 if (selectionProvider.getSelection() != null) {
00240 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection()));
00241 }
00242 }
00243 }
00244
00253 public void selectionChanged(SelectionChangedEvent event) {
00254
00255
00256 if (createChildMenuManager != null) {
00257 depopulateManager(createChildMenuManager, createChildActions);
00258 }
00259 if (createSiblingMenuManager != null) {
00260 depopulateManager(createSiblingMenuManager, createSiblingActions);
00261 }
00262
00263
00264
00265 Collection<?> newChildDescriptors = null;
00266 Collection<?> newSiblingDescriptors = null;
00267
00268 ISelection selection = event.getSelection();
00269 if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) {
00270 Object object = ((IStructuredSelection)selection).getFirstElement();
00271
00272 EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain();
00273
00274 newChildDescriptors = domain.getNewChildDescriptors(object, null);
00275 newSiblingDescriptors = domain.getNewChildDescriptors(null, object);
00276 }
00277
00278
00279
00280 createChildActions = generateCreateChildActions(newChildDescriptors, selection);
00281 createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection);
00282
00283 if (createChildMenuManager != null) {
00284 populateManager(createChildMenuManager, createChildActions, null);
00285 createChildMenuManager.update(true);
00286 }
00287 if (createSiblingMenuManager != null) {
00288 populateManager(createSiblingMenuManager, createSiblingActions, null);
00289 createSiblingMenuManager.update(true);
00290 }
00291 }
00292
00300 protected Collection<IAction> generateCreateChildActions(Collection<?> descriptors, ISelection selection) {
00301 Collection<IAction> actions = new ArrayList<IAction>();
00302 if (descriptors != null) {
00303 for (Object descriptor : descriptors) {
00304 actions.add(new CreateChildAction(activeEditorPart, selection, descriptor));
00305 }
00306 }
00307 return actions;
00308 }
00309
00317 protected Collection<IAction> generateCreateSiblingActions(Collection<?> descriptors, ISelection selection) {
00318 Collection<IAction> actions = new ArrayList<IAction>();
00319 if (descriptors != null) {
00320 for (Object descriptor : descriptors) {
00321 actions.add(new CreateSiblingAction(activeEditorPart, selection, descriptor));
00322 }
00323 }
00324 return actions;
00325 }
00326
00336 protected void populateManager(IContributionManager manager, Collection<? extends IAction> actions, String contributionID) {
00337 if (actions != null) {
00338 for (IAction action : actions) {
00339 if (contributionID != null) {
00340 manager.insertBefore(contributionID, action);
00341 }
00342 else {
00343 manager.add(action);
00344 }
00345 }
00346 }
00347 }
00348
00356 protected void depopulateManager(IContributionManager manager, Collection<? extends IAction> actions) {
00357 if (actions != null) {
00358 IContributionItem[] items = manager.getItems();
00359 for (int i = 0; i < items.length; i++) {
00360
00361
00362 IContributionItem contributionItem = items[i];
00363 while (contributionItem instanceof SubContributionItem) {
00364 contributionItem = ((SubContributionItem)contributionItem).getInnerItem();
00365 }
00366
00367
00368
00369 if (contributionItem instanceof ActionContributionItem) {
00370 IAction action = ((ActionContributionItem)contributionItem).getAction();
00371 if (actions.contains(action)) {
00372 manager.remove(contributionItem);
00373 }
00374 }
00375 }
00376 }
00377 }
00378
00385 @Override
00386 public void menuAboutToShow(IMenuManager menuManager) {
00387 super.menuAboutToShow(menuManager);
00388 MenuManager submenuManager = null;
00389
00390 submenuManager = new MenuManager(DataTypesEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
00391 populateManager(submenuManager, createChildActions, null);
00392 menuManager.insertBefore("edit", submenuManager);
00393
00394 submenuManager = new MenuManager(DataTypesEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
00395 populateManager(submenuManager, createSiblingActions, null);
00396 menuManager.insertBefore("edit", submenuManager);
00397 }
00398
00405 @Override
00406 protected void addGlobalActions(IMenuManager menuManager) {
00407 menuManager.insertAfter("additions-end", new Separator("ui-actions"));
00408 menuManager.insertAfter("ui-actions", showPropertiesViewAction);
00409
00410 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
00411 menuManager.insertAfter("ui-actions", refreshViewerAction);
00412
00413 super.addGlobalActions(menuManager);
00414 }
00415
00422 @Override
00423 protected boolean removeAllReferencesOnDelete() {
00424 return true;
00425 }
00426
00427 }