001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui.internal.properties;
018:
019: import net.refractions.udig.project.command.factory.NavigationCommandFactory;
020: import net.refractions.udig.project.internal.Map;
021: import net.refractions.udig.project.ui.internal.MapEditor;
022: import net.refractions.udig.project.ui.internal.Messages;
023: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
024:
025: import org.eclipse.jface.viewers.CellEditor;
026: import org.eclipse.jface.viewers.ILabelProvider;
027: import org.eclipse.jface.viewers.LabelProvider;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.events.KeyAdapter;
030: import org.eclipse.swt.events.KeyEvent;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Control;
036: import org.eclipse.swt.widgets.Event;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Listener;
039: import org.eclipse.ui.PlatformUI;
040: import org.eclipse.ui.views.properties.PropertyDescriptor;
041: import org.geotools.geometry.jts.JTS;
042:
043: import com.vividsolutions.jts.geom.Envelope;
044: import com.vividsolutions.jts.geom.Geometry;
045:
046: /**
047: * @author jones
048: * @since 0.3
049: */
050: public class GeometryPropertyDescriptor extends PropertyDescriptor {
051:
052: /**
053: * Construct <code>GeometryPropertyDescriptor</code>.
054: *
055: * @param id
056: * @param displayName
057: */
058: public GeometryPropertyDescriptor(Object id, String displayName) {
059: super (id, displayName);
060: }
061:
062: /**
063: * @see org.eclipse.ui.views.properties.PropertyDescriptor#createPropertyEditor(org.eclipse.swt.widgets.Composite)
064: */
065: public CellEditor createPropertyEditor(Composite parent) {
066:
067: return new CellEditor(parent, SWT.NONE) {
068:
069: Geometry geometry;
070: private Button button;
071: private Label label;
072:
073: protected Control createControl(Composite comp) {
074: Composite parent = new Composite(comp, SWT.NONE);
075: parent.setLayout(new GridLayout(2, false));
076: label = new Label(parent, SWT.READ_ONLY);
077: if (geometry != null)
078: label.setText(getLabelProvider().getText(geometry));
079: GridData data = new GridData(SWT.FILL, SWT.FILL, true,
080: true);
081: data.verticalSpan = 2;
082: label.setLayoutData(data);
083: button = new Button(parent, SWT.PUSH | SWT.FLAT);
084: button
085: .setText(Messages.GeometryPropertyDescriptor_viewButton_text);
086: data = new GridData(SWT.RIGHT, SWT.TOP, false, false);
087: data.verticalSpan = 1;
088: button.setLayoutData(data);
089: button.addListener(SWT.MouseUp, new Listener() {
090:
091: public void handleEvent(Event event) {
092: MapEditor editor = (MapEditor) PlatformUI
093: .getWorkbench()
094: .getActiveWorkbenchWindow()
095: .getActivePage().getActiveEditor();
096: Map map = editor.getMap();
097: Envelope env = geometry.getEnvelopeInternal();
098: try {
099: env = JTS.transform(env, map
100: .getEditManager().getEditLayer()
101: .layerToMapTransform());
102: } catch (Exception e1) {
103: ProjectUIPlugin.log(null, e1);
104: }
105: map.sendCommandASync(NavigationCommandFactory
106: .getInstance()
107: .createSetViewportBBoxCommand(env));
108: PlatformUI.getWorkbench()
109: .getActiveWorkbenchWindow()
110: .getActivePage().activate(editor);
111: }
112:
113: });
114: button.addKeyListener(new KeyAdapter() {
115: public void keyReleased(KeyEvent e) {
116: if (e.character == '\u001b') { // Escape
117: fireCancelEditor();
118: }
119: }
120: });
121: return parent;
122: }
123:
124: protected Object doGetValue() {
125: return geometry;
126: }
127:
128: protected void doSetFocus() {
129: button.setFocus();
130: }
131:
132: protected void doSetValue(Object value) {
133: geometry = (Geometry) value;
134: label.setText(getLabelProvider().getText(geometry));
135: }
136:
137: };
138: }
139:
140: /**
141: * @see org.eclipse.ui.views.properties.PropertyDescriptor#getLabelProvider()
142: */
143: public ILabelProvider getLabelProvider() {
144: return new LabelProvider() {
145: /**
146: * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
147: */
148: public String getText(Object element) {
149: Geometry geom = (Geometry) element;
150: return geom.getGeometryType();
151: }
152: };
153: }
154:
155: }
|