001: package net.refractions.udig.project.ui.internal.tool.display;
002:
003: import net.refractions.udig.project.ILayer;
004: import net.refractions.udig.project.IMap;
005: import net.refractions.udig.project.ui.ApplicationGIS;
006: import net.refractions.udig.project.ui.internal.LayersView;
007: import net.refractions.udig.project.ui.internal.tool.display.ModalToolCategory.CurrentModalToolContribution;
008: import net.refractions.udig.ui.operations.OpFilter;
009:
010: import org.eclipse.jface.action.IAction;
011: import org.eclipse.jface.viewers.ISelection;
012: import org.eclipse.jface.viewers.IStructuredSelection;
013: import org.eclipse.jface.viewers.StructuredSelection;
014: import org.eclipse.ui.ISelectionListener;
015: import org.eclipse.ui.IWorkbenchPart;
016:
017: /*
018: * Not sure if this is the best name for this class.
019: */
020: /**
021: * Monitors events and things going on in uDig and activates or deactivates tools according to the
022: * current context.
023: */
024: public class ToolController implements ISelectionListener {
025:
026: OpFilter enablesFor;
027: ModalItem toolProxy;
028:
029: public ToolController(OpFilter enablesFor, ModalItem toolProxy) {
030: this .enablesFor = enablesFor;
031: this .toolProxy = toolProxy;
032: IMap map = ApplicationGIS.getActiveMap();
033: if (map != null) {
034: ILayer selectedLayer = map.getEditManager()
035: .getSelectedLayer();
036: if (selectedLayer != null)
037: enablesFor
038: .accept(new StructuredSelection(selectedLayer));
039: }
040: }
041:
042: public void selectionChanged(IWorkbenchPart part,
043: ISelection selection) {
044: if (toolProxy.isDisposed())
045: part.getSite().getPage().getWorkbenchWindow()
046: .getSelectionService()
047: .removeSelectionListener(this );
048:
049: if (part instanceof LayersView) {
050:
051: if (selection instanceof IStructuredSelection) {
052: IStructuredSelection structuredSelection = (IStructuredSelection) selection;
053:
054: if (enablesFor.accept(structuredSelection)) {
055: activate();
056: } else {
057: deactivate();
058: }
059: }
060: }
061: }
062:
063: /**
064: * Activate the toolproxy.
065: */
066: private void activate() {
067: toolProxy.setEnabled(true);
068: for (CurrentContributionItem contrib : toolProxy
069: .getContributions()) {
070: if (!(contrib instanceof CurrentModalToolContribution)
071: && contrib instanceof IAction) {
072: IAction action = (IAction) contrib;
073: action.setEnabled(true);
074: }
075:
076: if (contrib instanceof AbstractToolbarContributionItem) {
077: AbstractToolbarContributionItem item = (AbstractToolbarContributionItem) contrib;
078: if (item != null && item.toolItem != null
079: && !item.toolItem.isDisposed())
080: item.toolItem.setEnabled(true);
081: }
082: }
083: }
084:
085: /**
086: * Deactivate the toolProxy.
087: */
088: private void deactivate() {
089: toolProxy.setEnabled(false);
090: for (CurrentContributionItem contrib : toolProxy
091: .getContributions()) {
092: if (!(contrib instanceof CurrentModalToolContribution)
093: && contrib instanceof IAction) {
094: IAction action = (IAction) contrib;
095: action.setEnabled(false);
096: }
097:
098: if (contrib instanceof AbstractToolbarContributionItem) {
099: AbstractToolbarContributionItem item = (AbstractToolbarContributionItem) contrib;
100:
101: boolean enableSelf = false; // If all tools are disabled, disable main button as
102: // well.
103: for (ModalItem tool : item.getTools()) {
104: if (tool.isEnabled) {
105: enableSelf = true;
106: break;
107: }
108: }
109: if (!enableSelf) {
110: if (!item.toolItem.isDisposed())
111: item.toolItem.setEnabled(false);
112: }
113: }
114: }
115:
116: }
117: }
|