1 | /***************************************************************************
|
---|
2 | * Copyright (C) 2009 by Tomasz Stelmach *
|
---|
3 | * http://www.stelmach-online.net/ *
|
---|
4 | * *
|
---|
5 | * This program is free software; you can redistribute it and/or modify *
|
---|
6 | * it under the terms of the GNU General Public License as published by *
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or *
|
---|
8 | * (at your option) any later version. *
|
---|
9 | * *
|
---|
10 | * This program is distributed in the hope that it will be useful, *
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
---|
13 | * GNU General Public License for more details. *
|
---|
14 | * *
|
---|
15 | * You should have received a copy of the GNU General Public License *
|
---|
16 | * along with this program; if not, write to the *
|
---|
17 | * Free Software Foundation, Inc., *
|
---|
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
---|
19 | ***************************************************************************/
|
---|
20 |
|
---|
21 | package org.openstreetmap.josm.plugins.piclayer;
|
---|
22 |
|
---|
23 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
24 |
|
---|
25 | import java.util.ArrayList;
|
---|
26 | import java.util.List;
|
---|
27 |
|
---|
28 | import javax.swing.JOptionPane;
|
---|
29 | import javax.swing.UIManager;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
33 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
34 | import org.openstreetmap.josm.gui.IconToggleButton;
|
---|
35 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
36 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
37 | import org.openstreetmap.josm.gui.MapView;
|
---|
38 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
39 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
40 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
41 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
42 | import org.openstreetmap.josm.plugins.piclayer.actions.SavePictureCalibrationAction;
|
---|
43 | import org.openstreetmap.josm.plugins.piclayer.actions.newlayer.NewLayerFromClipboardAction;
|
---|
44 | import org.openstreetmap.josm.plugins.piclayer.actions.newlayer.NewLayerFromFileAction;
|
---|
45 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.MovePictureAction;
|
---|
46 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.RotatePictureAction;
|
---|
47 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.ScaleXPictureAction;
|
---|
48 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.ScaleXYPictureAction;
|
---|
49 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.ScaleYPictureAction;
|
---|
50 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.ShearPictureAction;
|
---|
51 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.MovePointAction;
|
---|
52 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.RemovePointAction;
|
---|
53 | import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.TransformPointAction;
|
---|
54 | import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerAbstract;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Main Plugin class.
|
---|
58 | */
|
---|
59 | public class PicLayerPlugin extends Plugin implements LayerChangeListener {
|
---|
60 |
|
---|
61 | public static List<IconToggleButton> buttonList = null;
|
---|
62 |
|
---|
63 | // Plugin menu
|
---|
64 | JosmAction newLayerFromFileAction = new NewLayerFromFileAction();
|
---|
65 | JosmAction newLayerFromClipboardAction = new NewLayerFromClipboardAction();
|
---|
66 | /**
|
---|
67 | * Constructor...
|
---|
68 | */
|
---|
69 | public PicLayerPlugin(PluginInformation info) {
|
---|
70 | super(info);
|
---|
71 |
|
---|
72 | // Create menu entry
|
---|
73 |
|
---|
74 | // Add menu items
|
---|
75 | MainMenu.add(Main.main.menu.imagerySubMenu, newLayerFromFileAction);
|
---|
76 | MainMenu.add(Main.main.menu.imagerySubMenu, newLayerFromClipboardAction);
|
---|
77 | newLayerFromFileAction.setEnabled(false);
|
---|
78 | newLayerFromClipboardAction.setEnabled(false);
|
---|
79 | // Listen to layers
|
---|
80 | MapView.addLayerChangeListener(this);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Called when the map is created. Creates the toolbar buttons.
|
---|
85 | */
|
---|
86 | @Override
|
---|
87 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
88 | if(newFrame != null) {
|
---|
89 | // Create plugin map modes
|
---|
90 | MovePictureAction movePictureAction = new MovePictureAction(newFrame);
|
---|
91 | MovePointAction movePointAction = new MovePointAction(newFrame);
|
---|
92 | TransformPointAction transformPointAction = new TransformPointAction(newFrame);
|
---|
93 | RemovePointAction removePointAction = new RemovePointAction(newFrame);
|
---|
94 |
|
---|
95 | RotatePictureAction rotatePictureAction = new RotatePictureAction(newFrame);
|
---|
96 | ScaleXYPictureAction scaleXYPictureAction = new ScaleXYPictureAction(newFrame);
|
---|
97 | ScaleXPictureAction scaleXPictureAction = new ScaleXPictureAction(newFrame);
|
---|
98 | ScaleYPictureAction scaleYPictureAction = new ScaleYPictureAction(newFrame);
|
---|
99 | ShearPictureAction shearPictureAction = new ShearPictureAction(newFrame);
|
---|
100 | // Create plugin buttons and add them to the toolbar
|
---|
101 |
|
---|
102 | buttonList = new ArrayList<IconToggleButton>(7);
|
---|
103 | buttonList.add(picLayerActionButtonFactory(movePictureAction));
|
---|
104 | buttonList.add(picLayerActionButtonFactory(movePointAction));
|
---|
105 | buttonList.add(picLayerActionButtonFactory(transformPointAction));
|
---|
106 | buttonList.add(picLayerActionButtonFactory(removePointAction));
|
---|
107 | buttonList.add(picLayerActionButtonFactory(rotatePictureAction));
|
---|
108 | buttonList.add(picLayerActionButtonFactory(scaleXYPictureAction));
|
---|
109 | buttonList.add(picLayerActionButtonFactory(scaleXPictureAction));
|
---|
110 | buttonList.add(picLayerActionButtonFactory(scaleYPictureAction));
|
---|
111 | buttonList.add(picLayerActionButtonFactory(shearPictureAction));
|
---|
112 |
|
---|
113 | for(IconToggleButton btn : buttonList) {
|
---|
114 | newFrame.addMapMode(btn);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private IconToggleButton picLayerActionButtonFactory(MapMode action) {
|
---|
120 | IconToggleButton button = new IconToggleButton(action);
|
---|
121 | button.setAutoHideDisabledButton(true);
|
---|
122 | return button;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * The toolbar buttons shall be active and visible only when the PicLayer is active.
|
---|
127 | */
|
---|
128 | @Override
|
---|
129 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
130 | boolean oldPic = oldLayer instanceof PicLayerAbstract;
|
---|
131 | boolean newPic = newLayer instanceof PicLayerAbstract;
|
---|
132 |
|
---|
133 | if (oldPic) {
|
---|
134 | ((PicLayerAbstract)oldLayer).setDrawPoints(false);
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (newPic) {
|
---|
138 | ((PicLayerAbstract)newLayer).setDrawPoints(true);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * The menu is enabled once another layer is first created. This is needed
|
---|
144 | * because the picture must be positioned based on the current mapview (so
|
---|
145 | * one must exist first). User should not be able to load a picture too early.
|
---|
146 | */
|
---|
147 | @Override
|
---|
148 | public void layerAdded(Layer arg0) {
|
---|
149 | newLayerFromFileAction.setEnabled(true);
|
---|
150 | newLayerFromClipboardAction.setEnabled(true);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * When all layers are gone - the menu is gone too.
|
---|
155 | */
|
---|
156 | @Override
|
---|
157 | public void layerRemoved(Layer arg0) {
|
---|
158 | if (arg0 instanceof PicLayerAbstract && ((PicLayerAbstract) arg0).getTransformer().isModified()) {
|
---|
159 | if (JOptionPane.showConfirmDialog(Main.parent, tr("Do you want to save current calibration of layer {0}?",
|
---|
160 | ((PicLayerAbstract)arg0).getPicLayerName()),
|
---|
161 | UIManager.getString("OptionPane.titleText"),
|
---|
162 | JOptionPane.YES_NO_OPTION) == 0)
|
---|
163 | new SavePictureCalibrationAction((PicLayerAbstract) arg0).actionPerformed(null);
|
---|
164 | }
|
---|
165 | // Why should I do all these checks now?
|
---|
166 | boolean enable = Main.map != null && Main.map.mapView != null && Main.map.mapView.getAllLayers() != null && Main.map.mapView.getAllLayers().size() != 0;
|
---|
167 | newLayerFromFileAction.setEnabled(enable);
|
---|
168 | newLayerFromClipboardAction.setEnabled(enable);
|
---|
169 | }
|
---|
170 | };
|
---|