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;
016:
017: import java.util.List;
018:
019: import net.refractions.udig.project.AdaptableFeature;
020: import net.refractions.udig.project.EditManagerEvent;
021: import net.refractions.udig.project.IEditManagerListener;
022: import net.refractions.udig.project.ILayer;
023: import net.refractions.udig.project.IMap;
024: import net.refractions.udig.project.ui.ApplicationGIS;
025: import net.refractions.udig.project.ui.tool.IToolContext;
026: import net.refractions.udig.project.ui.tool.selection.provider.AbstractMapEditorSelectionProvider;
027: import net.refractions.udig.tools.edit.support.EditBlackboard;
028: import net.refractions.udig.tools.edit.support.EditBlackboardEvent;
029: import net.refractions.udig.tools.edit.support.EditBlackboardListener;
030:
031: import org.eclipse.jface.viewers.StructuredSelection;
032: import org.eclipse.ui.IEditorPart;
033:
034: /**
035: * The selection provided by this provider is first the vertices of the edit geom, if there are any selected.
036: *
037: * If not then it is the EditFeature in the edit manager.
038: *
039: * @author Jesse
040: * @since 1.1.0
041: */
042: public class EditSelectionProvider extends
043: AbstractMapEditorSelectionProvider {
044:
045: private EditBlackboardListener listener = new EditBlackboardListener() {
046:
047: public void changed(EditBlackboardEvent e) {
048: EditBlackboard old = EditBlackboardUtil.getEditBlackboard(
049: context, map.getEditManager().getSelectedLayer());
050:
051: if (e.getEditBlackboard() != old) {
052: e.getEditBlackboard().getListeners().remove(this );
053: return;
054: }
055:
056: if (e.getType() == EditBlackboardEvent.EventType.SELECTION) {
057: selection();
058: }
059: }
060:
061: public void batchChange(List<EditBlackboardEvent> e) {
062: EditBlackboardEvent lastSelecion = null;
063: for (EditBlackboardEvent event : e) {
064:
065: if (event.getType() == EditBlackboardEvent.EventType.SELECTION) {
066: lastSelecion = event;
067: }
068: }
069:
070: if (lastSelecion != null)
071: changed(lastSelecion);
072: }
073:
074: };
075: private IMap map;
076: private final IEditManagerListener editManagerListener = new IEditManagerListener() {
077:
078: public void changed(EditManagerEvent event) {
079: if (event.getSource() != map.getEditManager()) {
080: event.getSource().removeListener(this );
081: return;
082: }
083: switch (event.getType()) {
084: case EditManagerEvent.EDIT_FEATURE:
085: AdaptableFeature selectedFeature = (AdaptableFeature) event
086: .getNewValue();
087: if (selectedFeature == null)
088: selection = new StructuredSelection();
089: else {
090: selection = new StructuredSelection(selectedFeature);
091: }
092: notifyListeners();
093: break;
094: case EditManagerEvent.SELECTED_LAYER:
095: if (event.getNewValue() == event.getOldValue())
096: return;
097:
098: EditBlackboard old = EditBlackboardUtil
099: .getEditBlackboard(context, (ILayer) event
100: .getOldValue());
101: old.getListeners().remove(listener);
102:
103: selection();
104: break;
105: default:
106: break;
107: }
108: }
109:
110: };
111:
112: private IToolContext context;
113:
114: public void setActiveMap(IMap map, IEditorPart editor) {
115: this .map = map;
116: context = ApplicationGIS.createContext(map);
117: selection();
118:
119: map.getEditManager().addListener(editManagerListener);
120: EditBlackboard editBlackboard = EditBlackboardUtil
121: .getEditBlackboard(context, map.getEditManager()
122: .getSelectedLayer());
123: editBlackboard.getListeners().add(listener);
124: }
125:
126: /**
127: *
128: */
129: private void selection() {
130: if (!selectVertices())
131: selectEditFeature();
132: }
133:
134: /**
135: * Sets the selection to be a selection of vertices and returns true if there are any, otherwise
136: * return false.
137: */
138: private boolean selectVertices() {
139: EditBlackboard editBlackboard = EditBlackboardUtil
140: .getEditBlackboard(context, map.getEditManager()
141: .getSelectedLayer());
142:
143: if (editBlackboard == null
144: || editBlackboard.getSelection().isEmpty())
145: return false;
146: selection = new StructuredSelection(editBlackboard
147: .getSelection().toArray());
148:
149: notifyListeners();
150: return true;
151: }
152:
153: private void selectEditFeature() {
154:
155: AdaptableFeature selectedFeature = (AdaptableFeature) map
156: .getEditManager().getEditFeature();
157: if (selectedFeature == null)
158: selection = new StructuredSelection();
159: else {
160: selection = new StructuredSelection(selectedFeature);
161: }
162:
163: notifyListeners();
164:
165: }
166:
167: }
|