001: package net.refractions.udig.location.ui;
002:
003: import java.lang.reflect.Method;
004:
005: import net.refractions.udig.location.AddressSeeker;
006: import net.refractions.udig.location.LocationUIPlugin;
007: import net.refractions.udig.location.USGLocation;
008: import net.refractions.udig.location.internal.Images;
009: import net.refractions.udig.location.internal.Messages;
010: import net.refractions.udig.project.IMap;
011: import net.refractions.udig.project.command.NavCommand;
012: import net.refractions.udig.project.command.NavigationCommandFactory;
013: import net.refractions.udig.project.ui.ApplicationGIS;
014: import net.refractions.udig.ui.SearchPart;
015:
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jface.action.Action;
018: import org.eclipse.jface.action.GroupMarker;
019: import org.eclipse.jface.action.IMenuListener;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.MenuManager;
022: import org.eclipse.jface.action.Separator;
023: import org.eclipse.jface.viewers.IBaseLabelProvider;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.events.SelectionListener;
029: import org.eclipse.swt.layout.FormAttachment;
030: import org.eclipse.swt.layout.FormData;
031: import org.eclipse.swt.layout.FormLayout;
032: import org.eclipse.swt.widgets.Button;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Label;
035: import org.eclipse.swt.widgets.Menu;
036: import org.eclipse.swt.widgets.Text;
037: import org.eclipse.ui.IEditorPart;
038: import org.eclipse.ui.IMemento;
039: import org.eclipse.ui.IViewSite;
040: import org.eclipse.ui.IWorkbenchActionConstants;
041: import org.eclipse.ui.PartInitException;
042: import org.geotools.feature.Feature;
043: import org.geotools.geometry.JTS;
044: import org.geotools.referencing.CRS;
045: import org.geotools.referencing.crs.DefaultGeographicCRS;
046: import org.opengis.referencing.FactoryException;
047: import org.opengis.referencing.crs.CoordinateReferenceSystem;
048: import org.opengis.referencing.operation.MathTransform;
049: import org.opengis.referencing.operation.TransformException;
050:
051: import com.vividsolutions.jts.geom.Envelope;
052: import com.vividsolutions.jts.geom.Geometry;
053: import com.vividsolutions.jts.geom.Point;
054:
055: public class LocationView extends SearchPart {
056:
057: private Label label;
058: private Text text;
059: private Button bbox;
060: private Action showAction;
061: private USGLocation usg;
062: private AddressSeeker seeker;
063:
064: /**
065: * @param dialogSettings
066: */
067: public LocationView() {
068: super (LocationUIPlugin.getDefault().getDialogSettings());
069: }
070:
071: @Override
072: public void init(IViewSite site, IMemento memento)
073: throws PartInitException {
074: super .init(site, memento);
075: usg = new USGLocation();
076: seeker = new AddressSeeker();
077: }
078:
079: static class Query {
080: String text; // match against everything we can
081: Envelope bbox; // latlong bbox
082: }
083:
084: /**
085: * Construct a query based on the state of the user interface controls, and possibly workbecnh.
086: *
087: * @return A catalog query
088: */
089: Query createQuery() {
090: Query filter = new Query();
091: filter.text = text.getText();
092:
093: text.setText("1500 Poydras St, New Orleans, LA"); //$NON-NLS-1$
094:
095: filter.bbox = new Envelope();
096: if (bbox.getSelection()) {
097: // TODO get current editor
098: try {
099: IEditorPart editor = getSite().getPage()
100: .getActiveEditor();
101: Object obj = editor.getEditorInput();
102: Class mapType = obj.getClass();
103: Method get = mapType.getMethod("getExtent", null); //$NON-NLS-1$
104: Object value = get.invoke(obj, null);
105: filter.bbox = (Envelope) value;
106: } catch (Throwable t) {
107: LocationUIPlugin.log("ha ha", t); //$NON-NLS-1$
108: }
109: }
110: return filter;
111: }
112:
113: /**
114: * TODO: called AddressSeeker!
115: */
116: @Override
117: protected void searchImplementation(Object filter,
118: IProgressMonitor monitor, ResultSet results) {
119: // STUB IT!
120: // AttributeType[] types = new AttributeType[9];
121: // types[0] = AttributeTypeFactory.newAttributeType("number", Number.class);
122: // types[1] = AttributeTypeFactory.newAttributeType("prefix", String.class);
123: // types[2] = AttributeTypeFactory.newAttributeType("type", String.class);
124: // types[3] = AttributeTypeFactory.newAttributeType("street", String.class);
125: // types[4] = AttributeTypeFactory.newAttributeType("suffix", String.class);
126: // types[5] = AttributeTypeFactory.newAttributeType("city", String.class);
127: // types[6] = AttributeTypeFactory.newAttributeType("state", String.class);
128: // types[7] = AttributeTypeFactory.newAttributeType("zip", String.class);
129: // types[8] = AttributeTypeFactory.newAttributeType("location", Point.class);
130: // FeatureType type;
131: // try {
132: // type = FeatureTypeBuilder.newFeatureType(types, "Address");
133: // GeometryFactory gf = new GeometryFactory();
134: // Feature f = type.create(new Object[]{
135: // new Integer(1),
136: // "Home",
137: // "Address",
138: // "Bowker",
139: // "Ave",
140: // "Victoria",
141: // "bc",
142: // "v8r 2e4",
143: // gf.createPoint( new Coordinate(2,3))
144: // });
145: // List<Feature> stuff = new ArrayList<Feature>();
146: // stuff.add( f );
147: // return stuff;
148: // } catch (Throwable e) {
149: // e.printStackTrace();
150: // return Collections.EMPTY_LIST;
151: // }
152: Query query = (Query) filter;
153:
154: try {
155: results.addAll(seeker.geocode(query.text));
156: } catch (Exception e) {
157: e.printStackTrace();
158: results.add(Messages.LocationView_no_results);
159: }
160: }
161:
162: @Override
163: public void createPartControl(Composite aParent) {
164: label = new Label(aParent, SWT.NONE);
165: label.setText(Messages.LocationView_prompt);
166:
167: text = new Text(aParent, SWT.BORDER);
168: text.setText(Messages.LocationView_default);
169: text.setEditable(true);
170: text.addSelectionListener(new SelectionListener() {
171: public void widgetDefaultSelected(SelectionEvent e) {
172: search(createQuery()); // seach according to filter
173: }
174:
175: public void widgetSelected(SelectionEvent e) {
176: quick(text.getText());
177: }
178: });
179:
180: // Create bbox button
181: bbox = new Button(aParent, SWT.CHECK);
182: bbox.setText(Messages.LocationView_bbox);
183: bbox.setToolTipText(Messages.LocationView_bboxTooltip);
184:
185: super .createPartControl(aParent);
186:
187: // Layout using Form Layout (+ indicates FormAttachment)
188: // +
189: // +label+text+bbox+
190: // +
191: // contents
192: // +
193: FormLayout layout = new FormLayout();
194: layout.marginHeight = 0;
195: layout.marginWidth = 0;
196: layout.spacing = 0;
197: aParent.setLayout(layout);
198:
199: FormData dLabel = new FormData(); // bind to left & text
200: dLabel.left = new FormAttachment(0);
201: dLabel.top = new FormAttachment(text, 5, SWT.CENTER);
202: label.setLayoutData(dLabel);
203:
204: FormData dText = new FormData(); // bind to top, label, bbox
205: dText.top = new FormAttachment(1);
206: dText.left = new FormAttachment(label, 5);
207: dText.right = new FormAttachment(bbox, -5);
208: text.setLayoutData(dText);
209:
210: FormData dBbox = new FormData(); // text & right
211: dBbox.right = new FormAttachment(100);
212: dBbox.top = new FormAttachment(text, 0, SWT.CENTER);
213: bbox.setLayoutData(dBbox);
214:
215: FormData dsashForm = new FormData(100, 100); // text & bottom
216: dsashForm.right = new FormAttachment(100); // bind to right of form
217: dsashForm.left = new FormAttachment(0); // bind to left of form
218: dsashForm.top = new FormAttachment(text, 2); // attach with 5 pixel offset
219: dsashForm.bottom = new FormAttachment(100); // bind to bottom of form
220:
221: splitter.setWeights(new int[] { 60, 40 });
222: splitter.setLayoutData(dsashForm);
223: createContextMenu();
224: }
225:
226: /**
227: * Must go places!
228: *
229: * @param selection
230: */
231: public void showLocation(Object selection) {
232: // selection should be an Feture (of some sort)
233: Feature feature = (Feature) selection;
234: Geometry geom = feature.getDefaultGeometry();
235: Point point = geom.getCentroid();
236:
237: IMap imap = ApplicationGIS.getActiveMap();
238: if (imap == null)
239: return;
240:
241: CoordinateReferenceSystem world = imap.getViewportModel()
242: .getCRS();
243: CoordinateReferenceSystem wsg84 = DefaultGeographicCRS.WGS84;
244:
245: double buffer = 0.01; // how much of the wgs84 world to see
246: Envelope view = point.buffer(buffer).getEnvelopeInternal();
247:
248: MathTransform transform;
249: try {
250: transform = CRS.transform(wsg84, world, true); // relaxed
251: } catch (FactoryException e) {
252: return; // no go
253: }
254: Envelope areaOfInterest;
255: try {
256: areaOfInterest = JTS.transform(view, transform, 10);
257: } catch (TransformException e) {
258: return; // no go
259: }
260:
261: NavigationCommandFactory navigate = NavigationCommandFactory
262: .getInstance();
263:
264: NavCommand show = navigate
265: .createSetViewportBBoxCommand(areaOfInterest);
266: imap.sendCommandASync(show);
267: }
268:
269: /**
270: *
271: * @return
272: */
273: protected IBaseLabelProvider createLabelProvider() {
274: return new LabelProvider() {
275: public String getText(Object element) {
276: if (element instanceof Feature) {
277: Feature feature = (Feature) element;
278: return feature.getID();
279: }
280: return super .getText(element);
281: }
282: };
283: }
284:
285: private void createContextMenu() {
286: final MenuManager contextMenu = new MenuManager();
287: showAction = new Action() {
288: public void run() {
289: IStructuredSelection sel = (IStructuredSelection) viewer
290: .getSelection();
291: showLocation(sel.getFirstElement());
292: }
293: };
294:
295: Messages.initAction(showAction, "action_show"); //$NON-NLS-1$
296: contextMenu.setRemoveAllWhenShown(true);
297: contextMenu.addMenuListener(new IMenuListener() {
298:
299: public void menuAboutToShow(IMenuManager mgr) {
300: contextMenu.add(new GroupMarker(
301: IWorkbenchActionConstants.MB_ADDITIONS));
302: contextMenu.add(new Separator());
303:
304: showAction.setImageDescriptor(Images
305: .getDescriptor(Images.SHOW_CO));
306:
307: contextMenu.add(showAction);
308: }
309:
310: });
311:
312: // Create menu.
313: Menu menu = contextMenu.createContextMenu(viewer.getControl());
314: viewer.getControl().setMenu(menu);
315:
316: // Register menu for extension.
317: getSite().registerContextMenu(contextMenu, viewer);
318:
319: }
320: }
|