001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.enablement;
016:
017: import net.refractions.udig.project.IEditManager;
018: import net.refractions.udig.project.ILayer;
019: import net.refractions.udig.project.command.UndoableMapCommand;
020: import net.refractions.udig.project.internal.EditManager;
021: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
022: import net.refractions.udig.tool.edit.internal.Messages;
023: import net.refractions.udig.tools.edit.EditPlugin;
024: import net.refractions.udig.tools.edit.EditToolHandler;
025: import net.refractions.udig.tools.edit.EnablementBehaviour;
026: import net.refractions.udig.tools.edit.EventType;
027: import net.refractions.udig.ui.PlatformGIS;
028:
029: import org.eclipse.jface.dialogs.MessageDialog;
030: import org.eclipse.swt.widgets.Shell;
031: import org.eclipse.ui.PlatformUI;
032: import org.geotools.data.FeatureStore;
033:
034: /**
035: * This class detects and warns the user if the current tool can or can't edit the current layer.
036: *
037: * @author Jesse
038: * @since 1.0.0
039: */
040: public class ValidToolDetectionActivator implements EnablementBehaviour {
041:
042: private final Class[] legalClasses;
043: private ILayer lastLayer;
044:
045: public ValidToolDetectionActivator(Class[] classes) {
046: Class[] c = new Class[0];
047: if (classes != null) {
048: c = new Class[classes.length];
049: System.arraycopy(classes, 0, c, 0, c.length);
050: }
051: this .legalClasses = c;
052: }
053:
054: @SuppressWarnings("unchecked")
055: private String detectCompatibility(EditToolHandler handler,
056: IEditManager editManager) {
057:
058: ILayer selectedLayer = editManager.getSelectedLayer();
059: if (((EditManager) editManager).isEditLayerLocked()) {
060: selectedLayer = editManager.getEditLayer();
061: }
062: if (!selectedLayer.hasResource(FeatureStore.class)) {
063: return Messages.ValidToolDetectionActivator_warning1;
064: } else if (!selectedLayer.isApplicable(EditPlugin.ID)) {
065: return openQuestion(handler,
066: Messages.ValidToolDetectionActivator_question,
067: selectedLayer);
068: } else {
069: Class geomType = selectedLayer.getSchema()
070: .getDefaultGeometry().getType();
071: boolean acceptable = false;
072: for (Class type : legalClasses) {
073: if (geomType.isAssignableFrom(type)) {
074: acceptable = true;
075: break;
076: }
077: }
078: if (!acceptable) {
079: return Messages.ValidToolDetectionActivator_warning2;
080: }
081: }
082:
083: return null;
084: }
085:
086: private String openQuestion(final EditToolHandler handler,
087: final String string, final ILayer layer) {
088: final String[] warning = new String[1];
089: final Shell shell = PlatformUI.getWorkbench()
090: .getActiveWorkbenchWindow().getShell();
091: PlatformGIS.syncInDisplayThread(shell.getDisplay(),
092: new Runnable() {
093: public void run() {
094: boolean decision = MessageDialog
095: .openQuestion(
096: shell,
097: Messages.ValidToolDetectionActivator_questionTitle,
098: string);
099:
100: if (decision) {
101: UndoableMapCommand command = handler
102: .getContext()
103: .getBasicCommandFactory()
104: .createSetApplicabilityCommand(
105: layer, EditPlugin.ID, true);
106: handler.getContext().sendASyncCommand(
107: command);
108: } else {
109: warning[0] = Messages.ValidToolDetectionActivator_warning2;
110: }
111: }
112: });
113: return warning[0];
114: }
115:
116: public String isEnabled(EditToolHandler handler, MapMouseEvent e,
117: EventType eventType) {
118: if ((eventType != EventType.MOVED && eventType != EventType.ENTERED)
119: || lastLayer == handler.getEditLayer())
120: return null;
121:
122: IEditManager editManager = handler.getContext()
123: .getEditManager();
124: lastLayer = handler.getEditLayer();
125: return detectCompatibility(handler, editManager);
126: }
127:
128: }
|