001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.ui.internal;
010:
011: import net.refractions.udig.project.ILayer;
012: import net.refractions.udig.project.command.UndoableComposite;
013: import net.refractions.udig.project.internal.Layer;
014: import net.refractions.udig.project.internal.Map;
015: import net.refractions.udig.project.internal.ProjectPlugin;
016: import net.refractions.udig.project.internal.commands.ChangeCRSCommand;
017: import net.refractions.udig.project.ui.internal.commands.SetLayerCRSCommand;
018: import net.refractions.udig.ui.CRSChooser;
019: import net.refractions.udig.ui.Controller;
020:
021: import org.eclipse.jface.dialogs.IDialogConstants;
022: import org.eclipse.jface.dialogs.MessageDialogWithToggle;
023: import org.eclipse.jface.preference.IPreferenceStore;
024: import org.eclipse.swt.graphics.Point;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Control;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.ui.IWorkbench;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.dialogs.PropertyPage;
031: import org.opengis.referencing.crs.CoordinateReferenceSystem;
032:
033: /**
034: * A user interface that allow a user to modify/create and set the current and default
035: * CoordinateReferenceSystems used for maps.
036: * <p>
037: * Responsibilities:
038: * <ul>
039: * <li>Allow creation of user defined CRSs</li>
040: * <li>Allow modification of the current CRS</li>
041: * <li>Allow the CRS of a map to be set</li>
042: * <li>Allow the default CRS of a workbench to be set</li>
043: * </ul>
044: * </p>
045: *
046: * @author jeichar
047: * @since 0.3
048: */
049: public class CRSPropertyPage extends PropertyPage {
050:
051: public static class MapStrategy implements ApplyCRSStrategy {
052:
053: Map map;
054:
055: public MapStrategy(Map map) {
056: this .map = map;
057: }
058:
059: public void applyCoordinateReferenceSystem(
060: CoordinateReferenceSystem crs) {
061: map.sendCommandASync(new ChangeCRSCommand(map, crs));
062: }
063:
064: public CoordinateReferenceSystem getCurrentCoordinateReferenceSystem() {
065: return map.getViewportModel().getCRS();
066: }
067: }
068:
069: public static class LayerStrategy implements ApplyCRSStrategy {
070: private static final String SHOW_DIALOG_KEY = "SHOW_DIALOG_WHEN_UPDATE_LAYER_CRS_KEY"; //$NON-NLS-1$
071: private static final String UPDATE_MAP_CRS_KEY = "UPDATE_MAP_CRS_WHEN_UNKOWN_IS_CHANGED"; //$NON-NLS-1$
072: Layer layer;
073:
074: public LayerStrategy(Layer layer) {
075: this .layer = layer;
076: }
077:
078: public void applyCoordinateReferenceSystem(
079: CoordinateReferenceSystem crs) {
080: UndoableComposite commands = new UndoableComposite();
081: commands.getCommands().add(
082: new SetLayerCRSCommand(layer, crs));
083: if (layer.getCRS() == ILayer.UNKNOWN_CRS
084: && layer.getMap().getViewportModel().getCRS()
085: .equals(ILayer.UNKNOWN_CRS)) {
086: IPreferenceStore store = ProjectUIPlugin.getDefault()
087: .getPreferenceStore();
088: store.setDefault(SHOW_DIALOG_KEY, true);
089: boolean openDialog = store.getBoolean(SHOW_DIALOG_KEY);
090: boolean updateMapCRS;
091: if (openDialog) {
092: Shell shell = PlatformUI.getWorkbench()
093: .getActiveWorkbenchWindow().getShell();
094: MessageDialogWithToggle dialog = MessageDialogWithToggle
095: .openYesNoQuestion(
096: shell,
097: Messages.CRSPropertyPage_title,
098: Messages.CRSPropertyPage_message,
099: Messages.CRSPropertyPage_toggle_message,
100: false, store, SHOW_DIALOG_KEY);
101: int returnCode = dialog.getReturnCode();
102: updateMapCRS = returnCode == IDialogConstants.YES_ID ? true
103: : false;
104: store.setValue(UPDATE_MAP_CRS_KEY, updateMapCRS);
105: } else {
106: updateMapCRS = store.getBoolean(UPDATE_MAP_CRS_KEY);
107: }
108: if (updateMapCRS) {
109: commands.getCommands().add(
110: new ChangeCRSCommand(
111: layer.getMapInternal(), crs));
112: }
113: }
114: layer.getMap().sendCommandASync(commands);
115: }
116:
117: public CoordinateReferenceSystem getCurrentCoordinateReferenceSystem() {
118: return layer.getCRS();
119: }
120: }
121:
122: /**
123: * In order to make CRSPropertyPage more re-usable this strategy is used to apply the CRS to the
124: * object that this page applies to. In addition it retrieves the current CRS from the object.
125: *
126: * @author Jesse
127: * @since 1.1.0
128: */
129: public static interface ApplyCRSStrategy {
130: /**
131: * Called when a new CRS has been chosen. Should set the crs on the object.
132: *
133: * @param crs new CRS. Will not be the same as returned by
134: * {@link #getCurrentCoordinateReferenceSystem()}
135: */
136: public void applyCoordinateReferenceSystem(
137: CoordinateReferenceSystem crs);
138:
139: /**
140: * Returns the current CRS. Should not return null.
141: *
142: * @return current crs of object.
143: */
144: public CoordinateReferenceSystem getCurrentCoordinateReferenceSystem();
145: }
146:
147: CRSChooser chooser = new CRSChooser(new Controller() {
148:
149: public void handleClose() {
150: getControl().getShell().close();
151: }
152:
153: public void handleOk() {
154: CRSPropertyPage.this .performOk();
155: }
156:
157: });
158:
159: private ApplyCRSStrategy strategy;
160:
161: public CRSPropertyPage() {
162: setTitle(Messages.CRSPropertyPage_coordinateSystems_title);
163: }
164:
165: public void setStrategy(ApplyCRSStrategy strategy) {
166: this .strategy = strategy;
167: }
168:
169: /**
170: * @see org.eclipse.jface.preference.IPreferencePage#performOk()
171: */
172: public boolean performOk() {
173: CoordinateReferenceSystem crs = chooser.getCRS();
174: if (crs == null)
175: return false;
176:
177: if (crs.equals(strategy.getCurrentCoordinateReferenceSystem()))
178: return true;
179:
180: strategy.applyCoordinateReferenceSystem(crs);
181: // BasicCommandFactory factory = BasicCommandFactory.getInstance();
182: // MapCommand command = factory.createChangeCRS(map, crs);
183: // map.sendCommandSync(command);
184:
185: return super .performOk();
186: }
187:
188: /**
189: * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
190: */
191: protected void performDefaults() {
192: chooser.clearSearch();
193: chooser.gotoCRS(strategy.getCurrentCoordinateReferenceSystem());
194: }
195:
196: /**
197: * @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore()
198: */
199: public IPreferenceStore getPreferenceStore() {
200: return ProjectPlugin.getPlugin().getPreferenceStore();
201: }
202:
203: /**
204: * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
205: * @param parent
206: */
207: protected Control createContents(Composite parent) {
208: CoordinateReferenceSystem crs = null;
209: Control control;
210:
211: crs = strategy.getCurrentCoordinateReferenceSystem();
212: if (crs == null)
213: control = chooser.createControl(parent);
214: else
215: control = chooser.createControl(parent, crs);
216: return control;
217: }
218:
219: /**
220: * @see org.eclipse.jface.preference.PreferencePage#doComputeSize()
221: */
222: protected Point doComputeSize() {
223: return new Point(200, 300);
224: }
225:
226: /**
227: * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
228: */
229: public void init(IWorkbench workbench) {
230: }
231:
232: }
|