01: package com.vividsolutions.jump.workbench.ui.plugin;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05:
06: import javax.swing.JOptionPane;
07:
08: import com.vividsolutions.jump.coordsys.CoordinateSystem;
09: import com.vividsolutions.jump.coordsys.CoordinateSystemRegistry;
10: import com.vividsolutions.jump.coordsys.Reprojector;
11: import com.vividsolutions.jump.workbench.model.Layer;
12: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
13: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
14: import com.vividsolutions.jump.workbench.ui.EditTransaction;
15:
16: /**
17: * Implements a {@link com.vividsolutions.jump.workbench.plugin.PlugIn
18: * PlugIn} that allows the user to change coordinate systems.
19: *
20: */
21: public class ChangeCoordinateSystemPlugIn extends AbstractPlugIn {
22: public boolean execute(PlugInContext context) throws Exception {
23: //Don't make this plug-in undoable -- it's a lot of data to store in memory [Jon Aquino]
24: context.getLayerManager().getUndoableEditReceiver()
25: .reportIrreversibleChange();
26:
27: CoordinateSystem destination = (CoordinateSystem) JOptionPane
28: .showInputDialog(context.getWorkbenchFrame(),
29: "Coordinate system for task:", getName(),
30: JOptionPane.PLAIN_MESSAGE, null, new ArrayList(
31: CoordinateSystemRegistry.instance(
32: context.getWorkbenchContext()
33: .getBlackboard())
34: .getCoordinateSystems())
35: .toArray(), context.getLayerManager()
36: .getCoordinateSystem());
37:
38: if (destination == null) {
39: return false;
40: }
41:
42: if (context.getLayerManager().getCoordinateSystem() == destination) {
43: return true;
44: }
45:
46: if (Reprojector.instance().wouldChangeValues(
47: context.getLayerManager().getCoordinateSystem(),
48: destination)) {
49: //Two-phase commit [Jon Aquino]
50: ArrayList transactions = new ArrayList();
51:
52: for (Iterator i = context.getLayerManager().iterator(); i
53: .hasNext();) {
54: Layer layer = (Layer) i.next();
55: EditTransaction transaction = new EditTransaction(layer
56: .getFeatureCollectionWrapper().getFeatures(),
57: getName(), layer,
58: isRollingBackInvalidEdits(context), false,
59: context.getLayerViewPanel());
60:
61: for (int j = 0; j < transaction.size(); j++) {
62: Reprojector.instance()
63: .reproject(
64: transaction.getGeometry(j),
65: context.getLayerManager()
66: .getCoordinateSystem(),
67: destination);
68: }
69:
70: transactions.add(transaction);
71: }
72:
73: EditTransaction.commit(transactions);
74: }
75:
76: for (Iterator i = context.getLayerManager().iterator(); i
77: .hasNext();) {
78: Layer layer = (Layer) i.next();
79: layer.getFeatureCollectionWrapper().getFeatureSchema()
80: .setCoordinateSystem(destination);
81: }
82:
83: context.getLayerManager().setCoordinateSystem(destination);
84: if (context.getLayerViewPanel() != null) {
85: context.getLayerViewPanel().getViewport()
86: .zoomToFullExtent();
87: }
88:
89: return true;
90: }
91: }
|