001: /**
002: *
003: */package net.refractions.udig.project.ui.internal;
004:
005: import net.refractions.udig.project.ui.ApplicationGIS;
006: import net.refractions.udig.project.ui.internal.tool.display.PlaceholderToolbarContributionItem;
007: import net.refractions.udig.project.ui.tool.IToolManager;
008: import net.refractions.udig.project.ui.tool.ToolConstants;
009:
010: import org.eclipse.jface.action.IContributionItem;
011: import org.eclipse.jface.action.ICoolBarManager;
012: import org.eclipse.jface.action.IMenuManager;
013: import org.eclipse.jface.action.SubCoolBarManager;
014: import org.eclipse.jface.action.ToolBarContributionItem;
015: import org.eclipse.jface.action.ToolBarManager;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.ui.IActionBars;
018: import org.eclipse.ui.IEditorPart;
019: import org.eclipse.ui.IWorkbenchPage;
020: import org.eclipse.ui.part.EditorActionBarContributor;
021:
022: /**
023: * Adds {@link net.refractions.udig.project.ui.tool.Tool}s to the actions bars.
024: * @author jeichar
025: */
026: public class MapEditorActionBarContributor extends
027: EditorActionBarContributor {
028: private SubCoolBarManager subManager;
029:
030: @Override
031: public void init(IActionBars bars, IWorkbenchPage page) {
032: super .init(bars, page);
033: }
034:
035: @Override
036: public void contributeToCoolBar(ICoolBarManager coolBarManager) {
037: if (coolBarManager instanceof SubCoolBarManager) {
038: subManager = (SubCoolBarManager) coolBarManager;
039: } else
040: subManager = new SubCoolBarManager(coolBarManager);
041:
042: /*
043: * Vitalus:
044: * For correct toolbar management by Eclipse platform we MUST
045: * provide an ID for the toolbar contribution item. It causes saving/restoring
046: * workbench configuration by Eclipse with already created toolbar contribution
047: * items right after starting, so the solution is just to create fresh set of contribution items when
048: * the toolbar is created - placeholders are needed for correct toolbars management by the Eclipse
049: * with the same IDs.
050: *
051: * If not to do these steps it causes problems during resetting perspective because
052: * of some "features" of Eclispe JFace implementation.
053: */
054: subManager.setVisible(true);
055:
056: /*
057: * Contribute action tools to the toolbar.
058: */
059: ToolBarManager actionToolBarManager = new ToolBarManager(
060: SWT.FLAT);
061: ApplicationGIS.getToolManager().contributeActionTools(
062: actionToolBarManager, getActionBars());
063: if (actionToolBarManager.getItems().length > 0) {
064: IContributionItem item = subManager
065: .find(ToolConstants.ACTION_TOOLBAR_ID);
066: if (item != null) {
067: subManager.remove(ToolConstants.ACTION_TOOLBAR_ID);
068: }
069: ToolBarContributionItem toolBarContributionItem = new ToolBarContributionItem(
070: actionToolBarManager,
071: ToolConstants.ACTION_TOOLBAR_ID);
072: subManager.add(toolBarContributionItem);
073: }
074:
075: /*
076: * Contribute modal tools to the toolbar.
077: */
078: ToolBarManager modalToolBarManager = new ToolBarManager(
079: SWT.FLAT);
080: ApplicationGIS.getToolManager().contributeModalTools(
081: modalToolBarManager, getActionBars());
082: if (modalToolBarManager.getItems().length > 0) {
083: IContributionItem item = subManager
084: .find(ToolConstants.MODAL_TOOLBAR_ID);
085: if (item != null) {
086: subManager.remove(ToolConstants.MODAL_TOOLBAR_ID);
087: }
088: ToolBarContributionItem toolBarContributionItem = new ToolBarContributionItem(
089: modalToolBarManager, ToolConstants.MODAL_TOOLBAR_ID);
090: subManager.add(toolBarContributionItem);
091: }
092:
093: super .contributeToCoolBar(coolBarManager);
094: }
095:
096: @Override
097: public void contributeToMenu(IMenuManager menuManager) {
098: ApplicationGIS.getToolManager().contributeToMenu(menuManager);
099: super .contributeToMenu(menuManager);
100: }
101:
102: @Override
103: public void dispose() {
104: subManager.setVisible(false);
105:
106: PlaceholderToolbarContributionItem actionToolbarPlaceholder = new PlaceholderToolbarContributionItem(
107: ToolConstants.ACTION_TOOLBAR_ID);
108: subManager.add(actionToolbarPlaceholder);
109: PlaceholderToolbarContributionItem modalToolbarPlaceholder = new PlaceholderToolbarContributionItem(
110: ToolConstants.MODAL_TOOLBAR_ID);
111: subManager.add(modalToolbarPlaceholder);
112:
113: getActionBars().updateActionBars();
114: super .dispose();
115: }
116:
117: @Override
118: public void setActiveEditor(IEditorPart targetEditor) {
119: super .setActiveEditor(targetEditor);
120: IToolManager toolManager = ApplicationGIS.getToolManager();
121: toolManager.contributeGlobalActions(targetEditor,
122: getActionBars());
123: toolManager.registerActionsWithPart(targetEditor);
124: getActionBars().updateActionBars();
125: }
126: }
|