1 |
package skrueger.geotools; |
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
import java.awt.BorderLayout; |
* |
4 |
import java.awt.Dimension; |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
import java.awt.Window; |
* classes based on Java 1.6, focussing (not only) on Java Swing |
6 |
import java.awt.event.ActionEvent; |
* and the Geotools library. |
7 |
import java.util.ArrayList; |
* |
8 |
import java.util.LinkedList; |
* The SCHMITZM project is hosted at: |
9 |
import java.util.Map; |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
import java.util.SortedMap; |
* |
11 |
import java.util.TreeMap; |
* This program is free software; you can redistribute it and/or |
12 |
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
import javax.swing.AbstractAction; |
* as published by the Free Software Foundation; either version 3 |
14 |
import javax.swing.AbstractButton; |
* of the License, or (at your option) any later version. |
15 |
import javax.swing.Action; |
* |
16 |
import javax.swing.BorderFactory; |
* This program is distributed in the hope that it will be useful, |
17 |
import javax.swing.Icon; |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
import javax.swing.ImageIcon; |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
import javax.swing.JButton; |
* GNU General Public License for more details. |
20 |
import javax.swing.JComponent; |
* |
21 |
import javax.swing.JLabel; |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
import javax.swing.JPanel; |
* along with this program; if not, write to the Free Software |
23 |
import javax.swing.JSplitPane; |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
import javax.swing.JToggleButton; |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
import javax.swing.JToolBar; |
* |
26 |
|
* Contributors: |
27 |
import org.apache.log4j.Logger; |
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
* Stefan A. Krüger - additional utility classes |
29 |
import schmitzm.geotools.gui.GeoMapPane; |
******************************************************************************/ |
30 |
import schmitzm.geotools.gui.JMapPane; |
package skrueger.geotools; |
31 |
import schmitzm.geotools.gui.MapContextControlPane; |
|
32 |
import schmitzm.geotools.gui.MapPaneStatusBar; |
import java.awt.Color; |
33 |
import schmitzm.geotools.map.event.JMapPaneEvent; |
import java.awt.Dimension; |
34 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import java.awt.Graphics; |
35 |
import schmitzm.geotools.map.event.MapAreaChangedEvent; |
import java.awt.event.ActionEvent; |
36 |
import schmitzm.geotools.styling.ColorMapManager; |
import java.util.ArrayList; |
37 |
import schmitzm.swing.ButtonGroup; |
import java.util.Locale; |
38 |
import schmitzm.swing.SwingUtil; |
import java.util.SortedMap; |
39 |
|
import java.util.TreeMap; |
40 |
import com.vividsolutions.jts.geom.Envelope; |
|
41 |
|
import javax.swing.AbstractAction; |
42 |
/** |
import javax.swing.AbstractButton; |
43 |
* A toolbar to controll a {@link JMapPane} (Atlas visualization). This contains two types |
import javax.swing.Action; |
44 |
* of buttons. A group of <i>tools</i> for the mouse actions on the map represented |
import javax.swing.BorderFactory; |
45 |
* by {@link JToggleButton JToggleButtons}, where only one tool can be activated |
import javax.swing.Icon; |
46 |
* every time. And some (general) <i>actions</i>, represented by normal |
import javax.swing.ImageIcon; |
47 |
* {@link JButton JButtons}. |
import javax.swing.JButton; |
48 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
import javax.swing.JComponent; |
49 |
*/ |
import javax.swing.JToggleButton; |
50 |
public class MapPaneToolBar extends JToolBar { |
import javax.swing.JToolBar; |
51 |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class.getName()); |
|
52 |
/** Constant for the tool "Panning" (10). */ |
import org.apache.log4j.Logger; |
53 |
public static final int TOOL_PAN = 10; |
|
54 |
/** Constant for the tool "Zoom In" (30). */ |
import schmitzm.geotools.gui.JMapPane; |
55 |
public static final int TOOL_ZOOMIN = 30; |
import schmitzm.geotools.map.event.JMapPaneEvent; |
56 |
/** Constant for the tool "Zoom Out" (40). */ |
import schmitzm.geotools.map.event.JMapPaneListener; |
57 |
public static final int TOOL_ZOOMOUT = 40; |
import schmitzm.geotools.map.event.MapAreaChangedEvent; |
58 |
/** Constant for the tool "Info" (20). */ |
import schmitzm.lang.LangUtil; |
59 |
public static final int TOOL_INFO = 20; |
import schmitzm.lang.ResourceProvider; |
60 |
|
import schmitzm.swing.ButtonGroup; |
61 |
/** Tool currently selected */ |
import schmitzm.swing.SwingUtil; |
62 |
protected int selectedTool = TOOL_ZOOMIN; |
|
63 |
/** Holds the tool buttons of the tool bar. */ |
import com.vividsolutions.jts.geom.Envelope; |
64 |
protected SortedMap<Integer, JToggleButton> toolButtons = null; |
|
65 |
/** Controls that only one tool button is activated. */ |
/** |
66 |
protected ButtonGroup toolButtonGroup = null; |
* A toolbar to control an {@link JMapPane} (Atlas visualization). This contains |
67 |
/** Constant for the action "Zoom back" (100). */ |
* two types of buttons. A group of <i>tools</i> for the mouse actions on the |
68 |
public static final int ACTION_ZOOM_BACK = 100; |
* map represented by {@link JToggleButton JToggleButtons}, where only one tool |
69 |
/** Constant for the action "Zoom forward" (110). */ |
* can be activated every time. And some (general) <i>actions</i>, represented |
70 |
public static final int ACTION_ZOOM_FORWARD = 110; |
* by normal {@link JButton JButtons}. |
71 |
|
* |
72 |
/** Holds the action buttons of the bar. */ |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
73 |
protected SortedMap<Integer, JButton> actionButtons = null; |
* (University of Bonn/Germany) |
74 |
|
* @version 1.2 Stefan Krüger |
75 |
/** Holds the {@link JMapPane} this tool bar controls. */ |
*/ |
76 |
protected JMapPane mapPane = null; |
public class MapPaneToolBar extends JToolBar { |
77 |
|
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
78 |
/** |
.getName()); |
79 |
* A List to remember the last Envelopes that have been watched. Used for |
|
80 |
* the zoomBack- and zoomForwardButtons * |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
81 |
*/ |
.extendPackagePath(MapPaneToolBar.class, |
82 |
protected ArrayList<Envelope> lastZooms = new ArrayList<Envelope>(); |
"resource.locales.mapPaneToolbar"), Locale.ENGLISH); |
83 |
/** Holds the index to the current element in {@link #lastZooms}. */ |
|
84 |
protected int zoomBackIndex = 0; |
public static String R(String key, Object... values) { |
85 |
|
return RESOURCE.getString(key, values); |
86 |
/** Listener to sniff the zoom actions on the map. */ |
} |
87 |
protected JMapPaneListener mapPaneListener = null; |
|
88 |
|
/** Constant for the tool "Panning" (10). */ |
89 |
protected boolean zoomBackForwardButtonInAction; |
public static final int TOOL_PAN = 10; |
90 |
|
/** Constant for the tool "Info" (20). */ |
91 |
/** |
public static final int TOOL_INFO = 20; |
92 |
* Creates a new toolbar. Notice: This toolbar does nothing |
public static final int SEPERATOR0 = 99; |
93 |
* until {@link #setMapPane(JMapPane)} is called! |
|
94 |
*/ |
/** Constant for the tool "Zoom In" (110). */ |
95 |
public MapPaneToolBar() { |
public static final int TOOL_ZOOMIN = 110; |
96 |
this(null); |
/** Constant for the tool "Zoom Out" (120). */ |
97 |
} |
public static final int TOOL_ZOOMOUT = 120; |
98 |
|
/** Constant for the action "Zoom back" (130). */ |
99 |
/** |
public static final int ACTION_ZOOM_BACK = 130; |
100 |
* Creates a new tool bar. |
/** Constant for the action "Zoom forward" (140). */ |
101 |
* @param mapPane {@link JMapPane} the tool bar controls |
public static final int ACTION_ZOOM_FORWARD = 140; |
102 |
*/ |
public static final int SEPERATOR1 = 199; |
103 |
public MapPaneToolBar(JMapPane mapPane) { |
|
104 |
super("Control the map", JToolBar.HORIZONTAL); |
/** |
105 |
this.toolButtons = new TreeMap<Integer,JToggleButton>(); |
* Constant for the tool "Selection Reset" which clears the selection (240). |
106 |
this.toolButtonGroup = new ButtonGroup(); |
*/ |
107 |
this.actionButtons = new TreeMap<Integer,JButton>(); |
public static final int TOOL_SELECTION_CLEAR = 240; |
108 |
// Create a Listener to sniff the zooms on the JMapPane |
|
109 |
this.mapPaneListener = new JMapPaneListener() { |
/** |
110 |
public void performMapPaneEvent(JMapPaneEvent e) { |
* Constant for the tool "Select" which sets the Selection to the selected |
111 |
if ( !(e instanceof MapAreaChangedEvent) ) |
* features (210). |
112 |
return; |
*/ |
113 |
|
public static final int TOOL_SELECTION_SET = 210; |
114 |
if ( zoomBackForwardButtonInAction ) { |
/** |
115 |
zoomBackForwardButtonInAction = false; |
* Constant for the tool "Selection add" which adds the features to the |
116 |
return; |
* Selection (220). |
117 |
} |
*/ |
118 |
|
public static final int TOOL_SELECTION_ADD = 220; |
119 |
Envelope oldMapArea = ((MapAreaChangedEvent)e).getOldMapArea(); |
/** |
120 |
if (lastZooms.size() == 0 && oldMapArea != null ) { |
* Constant for the tool "Selection subtract" which removes the selected |
121 |
lastZooms.add(oldMapArea); |
* features from the selection (230). |
122 |
zoomBackIndex = 1; |
*/ |
123 |
} |
public static final int TOOL_SELECTION_REMOVE = 230; |
124 |
|
|
125 |
final Envelope mapArea = ((MapAreaChangedEvent)e).getNewMapArea(); |
/** Tool currently selected */ |
126 |
if (mapArea == null) |
protected int selectedTool = TOOL_ZOOMIN; |
127 |
return; |
|
128 |
|
/** Holds the tool buttons of the tool bar. */ |
129 |
if (lastZooms.size() > 0 && mapArea.equals(lastZooms.get(lastZooms.size() - 1))) { |
protected SortedMap<Integer, JComponent> toolAndActionButtons = null; |
130 |
// LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch"); |
/** Controls that only one tool button is activated. */ |
131 |
return; |
protected ButtonGroup toolButtonGroup = null; |
132 |
} |
|
133 |
|
// SK: Musste ich ändern damit man Tools und Actions in der Reihenfolge |
134 |
if (lastZooms.size() > 0) |
// mischen kann. |
135 |
while (zoomBackIndex < lastZooms.size()) |
// /** Holds the action buttons of the bar. */ |
136 |
lastZooms.remove(lastZooms.size() - 1); |
// protected SortedMap<Integer, JButton> actionButtons = null; |
137 |
|
|
138 |
lastZooms.add(mapArea); |
/** Holds the {@link JMapPane} this tool bar controls. */ |
139 |
zoomBackIndex = lastZooms.size(); |
protected JMapPane mapPane = null; |
140 |
setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1); |
|
141 |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
/** |
142 |
} |
* A List to remember the last Envelopes that have been watched. Used for |
143 |
}; |
* the zoomBack- and zoomForwardButtons * |
144 |
|
*/ |
145 |
setMapPane(mapPane); |
protected ArrayList<Envelope> lastZooms = new ArrayList<Envelope>(); |
146 |
setFloatable(false); |
/** Holds the index to the current element in {@link #lastZooms}. */ |
147 |
setRollover(true); |
protected int zoomBackIndex = 0; |
148 |
|
|
149 |
init(); |
/** Listener to sniff the zoom actions on the map. */ |
150 |
} |
protected JMapPaneListener mapPaneListener = null; |
151 |
|
|
152 |
/** |
protected boolean zoomBackForwardButtonInAction; |
153 |
* Sets the {@link JMapPane} controlled by this tool bar. |
|
154 |
* @param mapPane {@link JMapPane} to control (if {@code null} this |
/** |
155 |
* tool bar controls NOTHING!) |
* Creates a new toolbar. Notice: This toolbar does nothing until |
156 |
*/ |
* {@link #setMapPane(JMapPane)} is called! |
157 |
public void setMapPane(JMapPane mapPane) { |
*/ |
158 |
// Remove listener from old MapPane |
public MapPaneToolBar() { |
159 |
if ( this.mapPane != null ) |
this(null); |
160 |
this.mapPane.removeMapPaneListener( mapPaneListener ); |
} |
161 |
this.mapPane = mapPane; |
|
162 |
if ( this.mapPane != null && mapPaneListener != null ) |
/** |
163 |
this.mapPane.addMapPaneListener( mapPaneListener ); |
* Creates a new tool bar. |
164 |
} |
* |
165 |
|
* @param mapPane |
166 |
/** |
* {@link JMapPane} the tool bar controls |
167 |
* Calls {@link #initTools()} and {@link #initActions()} and then puts |
*/ |
168 |
* all tool buttons and all actions buttons to the tool bar. |
public MapPaneToolBar(JMapPane mapPane) { |
169 |
*/ |
super("Control the map", JToolBar.HORIZONTAL); |
170 |
protected void init() { |
this.toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
171 |
initTools(); |
this.toolButtonGroup = new ButtonGroup(); |
172 |
initActions(); |
|
173 |
initToolBar(); |
// Create a Listener to listen to the zooms on the JMapPane |
174 |
} |
this.mapPaneListener = new JMapPaneListener() { |
175 |
|
public void performMapPaneEvent(JMapPaneEvent e) { |
176 |
|
if (!(e instanceof MapAreaChangedEvent)) |
177 |
/** |
return; |
178 |
* Creates the tool buttons, adds them to {@link #toolButtons} and finally |
|
179 |
* creates a button group for all tools. So sub-classes which override this |
if (zoomBackForwardButtonInAction) { |
180 |
* method should FIRST add their new tool buttons to {@link #toolButtons} |
zoomBackForwardButtonInAction = false; |
181 |
* before calling {@code super.initTools()}. |
return; |
182 |
*/ |
} |
183 |
protected void initTools() { |
|
184 |
// Panning |
final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e; |
185 |
addTool( new MapPaneToolBarAction( |
Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea(); |
186 |
TOOL_PAN, |
|
187 |
this, |
final Envelope mapArea = mapAreaChangedEvent.getNewMapArea(); |
188 |
"", |
if (mapArea == null || mapArea.equals(oldMapArea) ) { |
189 |
new ImageIcon(MapView.class.getResource("pan.png")) |
// If the MapArea didn't change... we don't want to register it as a zoom action. |
190 |
), false ); |
return; |
191 |
// Info |
} |
192 |
addTool( new MapPaneToolBarAction( |
|
193 |
TOOL_INFO, |
if (lastZooms.size() == 0 && oldMapArea != null) { |
194 |
this, |
lastZooms.add(oldMapArea); |
195 |
"", |
zoomBackIndex = 1; |
196 |
new ImageIcon(MapView.class.getResource("info.png")) |
} |
197 |
), false ); |
if (mapArea == null) |
198 |
// Zoom in |
return; |
199 |
addTool( new MapPaneToolBarAction( |
|
200 |
TOOL_ZOOMIN, |
if (lastZooms.size() > 0 |
201 |
this, |
&& mapArea.equals(lastZooms.get(lastZooms.size() - 1))) { |
202 |
"", |
// LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch"); |
203 |
new ImageIcon(MapView.class.getResource("zoom_in.png")) |
return; |
204 |
), false ); |
} |
205 |
// Zoom out |
|
206 |
addTool( new MapPaneToolBarAction( |
if (lastZooms.size() > 0) |
207 |
TOOL_ZOOMOUT, |
while (zoomBackIndex < lastZooms.size()) |
208 |
this, |
lastZooms.remove(lastZooms.size() - 1); |
209 |
"", |
|
210 |
new ImageIcon(MapView.class.getResource("zoom_out.png")) |
lastZooms.add(mapArea); |
211 |
), false ); |
zoomBackIndex = lastZooms.size(); |
212 |
|
setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1); |
213 |
// set the selected tool enabled |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
214 |
setSelectedTool(selectedTool); |
} |
215 |
|
}; |
216 |
} |
|
217 |
|
setMapPane(mapPane); |
218 |
/** |
setFloatable(false); |
219 |
* Creates the action buttons and adds them to {@link #actionButtons}. |
setRollover(true); |
220 |
*/ |
|
221 |
protected void initActions() { |
init(); |
222 |
// Action button to revert the last zoom |
} |
223 |
addAction( new MapPaneToolBarAction( |
|
224 |
ACTION_ZOOM_BACK, |
/** |
225 |
this, |
* Sets the {@link JMapPane} controlled by this tool bar. |
226 |
"", |
* |
227 |
new ImageIcon(MapView.class.getResource("zoom_back.png")) |
* @param mapPane |
228 |
), false); |
* {@link JMapPane} to control (if {@code null} this tool bar |
229 |
setButtonEnabled( ACTION_ZOOM_BACK, false ); |
* controls NOTHING!) |
230 |
|
*/ |
231 |
// Action button to redo the last zoom |
public void setMapPane(JMapPane mapPane) { |
232 |
addAction( new MapPaneToolBarAction( |
// Remove listener from old MapPane |
233 |
ACTION_ZOOM_FORWARD, |
if (this.mapPane != null) |
234 |
this, |
this.mapPane.removeMapPaneListener(mapPaneListener); |
235 |
"", |
this.mapPane = mapPane; |
236 |
new ImageIcon(MapView.class.getResource("zoom_forward.png")) |
if (this.mapPane != null && mapPaneListener != null) |
237 |
), false); |
this.mapPane.addMapPaneListener(mapPaneListener); |
238 |
setButtonEnabled( ACTION_ZOOM_FORWARD, false ); |
} |
239 |
} |
|
240 |
|
/** |
241 |
/** |
* Calls {@link #initToolsAndActions()} and {@link #initActions()} and then |
242 |
* Clears the GUI of all components and adds all tool and action buttons to the |
* puts all tool buttons and all actions buttons to the tool bar. |
243 |
* tool bar. |
*/ |
244 |
*/ |
protected void init() { |
245 |
protected void initToolBar() { |
initToolsAndActions(); |
246 |
setAlignmentY( 1f ); |
|
247 |
removeAll(); |
addSeparator(SEPERATOR0, new JToolBar.Separator()); |
248 |
// Separator to the left of the tool actions to start |
addSeparator(SEPERATOR1, new JToolBar.Separator()); |
249 |
// the tool buttons with the map (not with the coordinate grid) |
|
250 |
Dimension dimension = new Dimension( 49,10); |
initToolBar(); |
251 |
addSeparator(dimension); |
} |
252 |
// Tool buttons |
|
253 |
for (JToggleButton b : toolButtons.values()) |
/** |
254 |
add(b); |
* Creates the tool buttons and action buttons and seperators, adds them to |
255 |
// Space between tool buttons and action buttons |
* {@link #toolAndActionButtons} and finally creates a button group for all |
256 |
Dimension dimension2 = new Dimension( 10,10); |
* tools. So sub-classes which override this method should FIRST add their |
257 |
this.addSeparator(dimension2); |
* new tool buttons to {@link #toolAndActionButtons} before calling {@code |
258 |
// Action buttons |
* super.initTools()}. |
259 |
for (JButton b : actionButtons.values()) |
*/ |
260 |
add(b); |
protected void initToolsAndActions() { |
261 |
} |
// Panning |
262 |
|
addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon( |
263 |
/** |
MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false); |
264 |
* Performs the activation of a tool. |
// Info |
265 |
* @param tool the tool to activate |
addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon( |
266 |
* @param e the event of the button |
MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false); |
267 |
*/ |
|
268 |
public void performToolButton(int tool, ActionEvent e) { |
// Zoom in |
269 |
if ( mapPane == null ) |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon( |
270 |
return; |
MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")), |
271 |
|
false); |
272 |
selectedTool = tool; |
// Zoom out |
273 |
|
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon( |
274 |
switch( tool ) { |
MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")), |
275 |
case TOOL_PAN: |
false); |
276 |
// Set the mouse tool to "Panning" |
|
277 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
// Action button to revert the last zoom |
278 |
mapPane.setState(JMapPane.PAN); |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
279 |
mapPane.setHighlight(false); |
new ImageIcon(MapView.class |
280 |
mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
.getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false); |
281 |
break; |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
282 |
case TOOL_INFO: |
|
283 |
// Set the mouse tool to "Info" |
// Action button to redo the last zoom |
284 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "", |
285 |
mapPane.setState(JMapPane.SELECT_TOP); |
new ImageIcon(MapView.class |
286 |
mapPane.setHighlight(true); |
.getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")), |
287 |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
false); |
288 |
break; |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
289 |
case TOOL_ZOOMIN: |
|
290 |
// Set the mouse tool to "Zoom in" |
// set the selected tool enabled |
291 |
mapPane.setWindowSelectionState(JMapPane.ZOOM_IN); |
setSelectedTool(selectedTool); |
292 |
mapPane.setState(JMapPane.ZOOM_IN); |
|
293 |
mapPane.setHighlight(false); |
} |
294 |
mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
|
295 |
break; |
/** |
296 |
case TOOL_ZOOMOUT: |
* Clears the GUI of all components and adds all tool and action buttons to |
297 |
// Set the mouse tool to "Zoom out" |
* the tool bar. |
298 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
*/ |
299 |
mapPane.setState(JMapPane.ZOOM_OUT); |
public void initToolBar() { |
300 |
mapPane.setHighlight(false); |
setAlignmentY(1f); |
301 |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
removeAll(); |
302 |
break; |
// Separator to the left of the tool actions to start |
303 |
default: |
// the tool buttons with the map (not with the coordinate grid) |
304 |
// Set map actions to default |
Dimension dimension = new Dimension(49, 10); |
305 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
addSeparator(dimension); |
306 |
mapPane.setState(JMapPane.NONE); |
// Tool buttons |
307 |
mapPane.setHighlight(false); |
for (JComponent b : toolAndActionButtons.values()) |
308 |
mapPane.setNormalCursor(null); |
add(b); |
309 |
break; |
|
310 |
} |
if (!toolAndActionButtons.containsKey(selectedTool)) { |
311 |
mapPane.updateCursor(); |
/** |
312 |
} |
* This might be a bit specific, but IF selection buttons are |
313 |
|
* available, select one of them.. if not, select the INFO tool. |
314 |
/** |
*/ |
315 |
* Performs the action of an action button. |
|
316 |
* @param tool the action |
if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) { |
317 |
* @param e the event of the button |
setSelectedTool(TOOL_SELECTION_SET); |
318 |
*/ |
} else if (toolAndActionButtons.containsKey(TOOL_INFO)) { |
319 |
protected void performActionButton(int action, ActionEvent e) { |
setSelectedTool(TOOL_INFO); |
320 |
if ( mapPane == null ) |
} else { |
321 |
return; |
// TODO What to do now?! |
322 |
|
setSelectedTool(null); |
323 |
// Perform the action "Zoom back": Revert the last zoom |
} |
324 |
if ( action == ACTION_ZOOM_BACK ) { |
|
325 |
if (zoomBackIndex <= 1) |
} |
326 |
return; |
|
327 |
|
revalidate(); |
328 |
zoomBackForwardButtonInAction = true; |
repaint(); |
329 |
zoomBackIndex--; |
} |
330 |
getButton(ACTION_ZOOM_FORWARD).setEnabled(true); |
|
331 |
getButton(ACTION_ZOOM_BACK).setEnabled( zoomBackIndex > 1 ); |
// Space between tool buttons and action buttons |
332 |
|
// SK: Seperators are now als manages like actions and tools |
333 |
mapPane.setMapArea( lastZooms.get(zoomBackIndex-1) ); |
// Dimension dimension2 = new Dimension( 10,10); |
334 |
mapPane.refresh(); |
// this.addSeparator(dimension2); |
335 |
} |
|
336 |
|
// // Action buttons |
337 |
// Perform the action "Zoom forward": Redo the last zoom |
// for (JButton b : actionButtons.values()) |
338 |
if ( action == ACTION_ZOOM_FORWARD ) { |
// add(b); |
339 |
if (zoomBackIndex < lastZooms.size()) { |
// } |
340 |
zoomBackForwardButtonInAction = true; |
|
341 |
zoomBackIndex++; |
/** |
342 |
getButton(ACTION_ZOOM_BACK).setEnabled(true); |
* Performs the activation of a tool. |
343 |
getButton(ACTION_ZOOM_FORWARD).setEnabled(zoomBackIndex < lastZooms.size()); |
* |
344 |
|
* @param tool |
345 |
mapPane.setMapArea( lastZooms.get(zoomBackIndex-1) ); |
* the tool to activate |
346 |
mapPane.refresh(); |
* @param e |
347 |
} |
* the event of the button |
348 |
} |
*/ |
349 |
} |
public void performToolButton(int tool, ActionEvent e) { |
350 |
|
if (mapPane == null) |
351 |
|
return; |
352 |
/** |
|
353 |
* Adds a tool to the tool bar. Does nothing if a tool or action with the |
selectedTool = tool; |
354 |
* specified ID already exists! |
|
355 |
* @param buttonAction action for the toggle button |
switch (tool) { |
356 |
* @param resetToolBar indicates whether the toolbar GUI is reset after adding |
case TOOL_PAN: |
357 |
* the button (if adding several actions it useful only to |
// Set the mouse tool to "Panning" |
358 |
* reset the GUI for the last added tool) |
mapPane.setWindowSelectionState(JMapPane.NONE); |
359 |
*/ |
mapPane.setState(JMapPane.PAN); |
360 |
public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) { |
mapPane.setHighlight(false); |
361 |
if ( isButtonIDUsed(buttonAction.getID()) ) { |
mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
362 |
LOGGER.warn("addTool(.) ignored because ID already used for tool or action: "+buttonAction.getID()); |
break; |
363 |
return; |
case TOOL_INFO: |
364 |
} |
// Set the mouse tool to "Info" |
365 |
JToggleButton button = new JToggleButton(buttonAction); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
366 |
button.setBorder( BorderFactory.createRaisedBevelBorder() ); |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
367 |
toolButtonGroup.add(button); |
// JMapPane.SELECT_TOP_ONEONLY |
368 |
toolButtons.put(buttonAction.getID(), button); |
mapPane.setHighlight(false);// SK: Was true, but since it not works |
369 |
if ( resetToolBar ) |
// properly removed it to save |
370 |
initToolBar(); |
// performance |
371 |
} |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
372 |
|
break; |
373 |
/** |
case TOOL_ZOOMIN: |
374 |
* Adds a tool to the tool bar and resets the toolbar GUI. |
// Set the mouse tool to "Zoom in" |
375 |
* @param buttonAction action for the toggle button |
mapPane.setWindowSelectionState(JMapPane.ZOOM_IN); |
376 |
*/ |
mapPane.setState(JMapPane.ZOOM_IN); |
377 |
public void addTool(MapPaneToolBarAction buttonAction) { |
mapPane.setHighlight(false); |
378 |
addTool(buttonAction, true); |
mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
379 |
} |
break; |
380 |
|
case TOOL_ZOOMOUT: |
381 |
/** |
// Set the mouse tool to "Zoom out" |
382 |
* Adds an action to the tool bar. Does nothing if a tool or action with the |
mapPane.setWindowSelectionState(JMapPane.NONE); |
383 |
* specified ID already exists! |
mapPane.setState(JMapPane.ZOOM_OUT); |
384 |
* @param buttonAction action for the button |
mapPane.setHighlight(false); |
385 |
* @param resetToolBar indicates whether the toolbar GUI is reset after adding |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
386 |
* the button (if adding several actions it useful only to |
break; |
387 |
* reset the GUI for the last added tool) |
default: |
388 |
*/ |
// Set map actions to default |
389 |
public void addAction(MapPaneToolBarAction buttonAction, boolean resetToolBar) { |
mapPane.setWindowSelectionState(JMapPane.NONE); |
390 |
if ( isButtonIDUsed(buttonAction.getID()) ) { |
mapPane.setState(JMapPane.NONE); |
391 |
LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "+buttonAction.getID()); |
mapPane.setHighlight(false); |
392 |
return; |
mapPane.setNormalCursor(null); |
393 |
} |
break; |
394 |
JButton button = new JButton(buttonAction); |
} |
395 |
actionButtons.put( buttonAction.getID(), button ); |
mapPane.updateCursor(); |
396 |
if ( resetToolBar ) |
} |
397 |
initToolBar(); |
|
398 |
} |
/** |
399 |
|
* @param id |
400 |
/** |
* The ID of the Component to remove. The change will not be |
401 |
* Adds an action to the tool bar and resets the toolbar GUI. |
* visible until {@link #initToolBar()} is called. |
402 |
* @param buttonAction action for the toggle button |
* @return <code>null</code> or the component that has been removed. |
403 |
*/ |
*/ |
404 |
public void addAction(MapPaneToolBarAction buttonAction) { |
public JComponent removeId(int id) { |
405 |
addAction(buttonAction, true); |
return toolAndActionButtons.remove(id); |
406 |
} |
} |
407 |
|
|
408 |
/** |
/** |
409 |
* Returns the button for a specific tool or action. |
* Performs the action of an action button. |
410 |
* @param id the constant for a tool |
* |
411 |
* @return a {@link JButton} if {@code id} specifies an {@linkplain #getActionButton(int) action button} |
* @param tool |
412 |
* or {@link JToogleButton} if {@code id} specifies a {@linkplain #getToolButton(int) tool button} |
* the action |
413 |
*/ |
* @param e |
414 |
public AbstractButton getButton(int id) { |
* the event of the button |
415 |
AbstractButton button = toolButtons.get(id); |
*/ |
416 |
if ( button == null ) |
protected void performActionButton(int action, ActionEvent e) { |
417 |
button = actionButtons.get(id); |
if (mapPane == null) |
418 |
if ( button == null ) |
return; |
419 |
LOGGER.warn("Unknown tool or action ID: "+id); |
|
420 |
return button; |
// Perform the action "Zoom back": Revert the last zoom |
421 |
} |
if (action == ACTION_ZOOM_BACK) { |
422 |
|
if (zoomBackIndex <= 1) |
423 |
/** |
return; |
424 |
* Returns the button for a specific tool. |
|
425 |
* @param tool the constant for a tool |
zoomBackForwardButtonInAction = true; |
426 |
*/ |
zoomBackIndex--; |
427 |
public JToggleButton getToolButton(int tool) { |
getButton(ACTION_ZOOM_FORWARD).setEnabled(true); |
428 |
AbstractButton button = getButton(tool); |
getButton(ACTION_ZOOM_BACK).setEnabled(zoomBackIndex > 1); |
429 |
if ( button != null && !(button instanceof JToggleButton) ) { |
|
430 |
LOGGER.warn("ID specifies no tool: "+tool); |
mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1)); |
431 |
button = null; |
mapPane.refresh(); |
432 |
} |
} |
433 |
return (JToggleButton)button; |
|
434 |
} |
// Perform the action "Zoom forward": Re-do the last zoom |
435 |
|
if (action == ACTION_ZOOM_FORWARD) { |
436 |
/** |
if (zoomBackIndex < lastZooms.size()) { |
437 |
* Returns the button for a specific action. |
zoomBackForwardButtonInAction = true; |
438 |
* @param action the constant an action |
zoomBackIndex++; |
439 |
*/ |
getButton(ACTION_ZOOM_BACK).setEnabled(true); |
440 |
public JButton getActionButton(int action) { |
getButton(ACTION_ZOOM_FORWARD).setEnabled( |
441 |
AbstractButton button = getButton(action); |
zoomBackIndex < lastZooms.size()); |
442 |
if ( button != null && !(button instanceof JButton) ) { |
|
443 |
LOGGER.warn("ID specifies no action: "+action); |
mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1)); |
444 |
button = null; |
mapPane.refresh(); |
445 |
} |
} |
446 |
return (JButton)button; |
} |
447 |
|
} |
448 |
} |
|
449 |
|
/** |
450 |
/** |
* Adds a tool to the tool bar. Does nothing if a tool or action with the |
451 |
* Sets the selected tool. |
* specified ID already exists! |
452 |
* @param tool ID of the tool |
* |
453 |
*/ |
* @param buttonAction |
454 |
public void setSelectedTool(Integer tool) { |
* action for the toggle button |
455 |
if ( tool == null ) |
* @param resetToolBar |
456 |
toolButtonGroup.setUnselected(); |
* indicates whether the toolbar GUI is reset after adding the |
457 |
|
* button (if adding several actions it useful only to reset the |
458 |
JToggleButton button = getToolButton(tool); |
* GUI for the last added tool) |
459 |
if ( button == null ) |
*/ |
460 |
return; |
public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) { |
461 |
button.setSelected( true ); |
if (isButtonIDUsed(buttonAction.getID())) { |
462 |
button.getAction().actionPerformed(null); |
LOGGER |
463 |
} |
.warn("addTool(.) ignored because ID already used for tool or action: " |
464 |
|
+ buttonAction.getID()); |
465 |
/** |
return; |
466 |
* Returns the selected tool. |
} |
467 |
* @return -1 if no tool is active |
JToggleButton button = new JToggleButton(buttonAction); |
468 |
*/ |
button.setBorder(BorderFactory.createRaisedBevelBorder()); |
469 |
public int getSelectedTool() { |
toolButtonGroup.add(button); |
470 |
if ( toolButtonGroup.getSelectedButton() == null ) |
toolAndActionButtons.put(buttonAction.getID(), button); |
471 |
return -1; |
if (resetToolBar) |
472 |
return selectedTool; |
initToolBar(); |
473 |
} |
} |
474 |
|
|
475 |
/** |
/** |
476 |
* Sets whether a tool or action is activated or not. The visible property |
* Adds a tool to the tool bar and resets the toolbar GUI. |
477 |
* of the button is not affected. |
* |
478 |
* @param id tool or actionID |
* @param buttonAction |
479 |
* @param enabled if {@code true} the tool becomes available |
* action for the toggle button |
480 |
*/ |
*/ |
481 |
public void setButtonEnabled(int id, boolean enabled) { |
public void addTool(MapPaneToolBarAction buttonAction) { |
482 |
AbstractButton button = getButton(id); |
addTool(buttonAction, true); |
483 |
if ( button == null ) |
} |
484 |
return; |
|
485 |
button.setEnabled( enabled ); |
/** |
486 |
} |
* Adds an action to the tool bar. Does nothing if a tool or action with the |
487 |
|
* specified ID already exists! |
488 |
/** |
* |
489 |
* Sets whether a tool or action is activated or not. |
* @param buttonAction |
490 |
* @param id tool or actionID |
* action for the button |
491 |
* @param enabled if {@code true} the tool becomes available |
* @param resetToolBar |
492 |
* @param hideOnDisable if {@code true} the button is also hidden if |
* indicates whether the toolbar GUI is reset after adding the |
493 |
* {@code enabled} is {@code false} |
* button (if adding several actions it useful only to reset the |
494 |
*/ |
* GUI for the last added tool) |
495 |
public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) { |
*/ |
496 |
AbstractButton button = getButton(id); |
public void addAction(MapPaneToolBarAction buttonAction, |
497 |
if ( button == null ) |
boolean resetToolBar) { |
498 |
return; |
if (isButtonIDUsed(buttonAction.getID())) { |
499 |
button.setEnabled( enabled ); |
LOGGER |
500 |
// if button is enabled, it becomes visible anyway |
.warn("addAction(.) ignored because ID already used for tool or action: " |
501 |
// if button is disabled and the "hide" option is set, it is also hidden |
+ buttonAction.getID()); |
502 |
if ( enabled ) |
return; |
503 |
button.setVisible( true ); |
} |
504 |
else |
JButton button = new JButton(buttonAction); |
505 |
button.setVisible( !hideOnDisable ); |
button.setBorder(BorderFactory.createRaisedBevelBorder()); |
506 |
} |
toolAndActionButtons.put(buttonAction.getID(), button); |
507 |
|
if (resetToolBar) |
508 |
/** |
initToolBar(); |
509 |
* Checks whether a ID is already used for a tool or action. |
} |
510 |
* @param tool tool ID |
|
511 |
*/ |
public void addSeparator(int id, Separator separator) { |
512 |
public boolean isButtonIDUsed(int id) { |
if (isButtonIDUsed(id)) { |
513 |
return toolButtons.get(id) != null || actionButtons.get(id) != null; |
LOGGER |
514 |
} |
.warn("addSeparator(.) ignored because ID already used for tool or action. "); |
515 |
|
return; |
516 |
/** |
} |
517 |
* Checks whether a tool is activated. |
toolAndActionButtons.put(id, separator); |
518 |
* @param tool tool ID |
} |
519 |
* @return {@code false} if an unknown ID is specified |
|
520 |
*/ |
/** |
521 |
public boolean isButtonEnabled(int id) { |
* Adds an action to the tool bar and resets the toolbar GUI. |
522 |
AbstractButton button = getButton(id); |
* |
523 |
if ( button != null ) |
* @param buttonAction |
524 |
return button.isEnabled(); |
* action for the toggle button |
525 |
return false; |
*/ |
526 |
} |
public void addAction(MapPaneToolBarAction buttonAction) { |
527 |
|
addAction(buttonAction, true); |
528 |
/** |
} |
529 |
* Sets the activation for all tools. |
|
530 |
* @param enabled if {@code true} all tools becomes available |
/** |
531 |
* @param hideOnDisable if {@code true} the buttons are also hidden if |
* Returns the button for a specific tool or action. |
532 |
* {@code enabled} is {@code false} |
* |
533 |
*/ |
* @param id |
534 |
public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) { |
* the constant for any button in the {@link MapPaneToolBar} |
535 |
for (int tool : toolButtons.keySet()) |
* @return a {@link JButton} if {@code id} specifies an |
536 |
setButtonEnabled(tool,enabled,hideOnDisable); |
* {@linkplain #getActionButton(int) action button} or |
537 |
} |
* {@link JToogleButton} if {@code id} specifies a |
538 |
|
* {@linkplain #getToolButton(int) tool button} |
539 |
/** |
*/ |
540 |
* Sets the activation for all actions. |
public AbstractButton getButton(int id) { |
541 |
* @param enabled if {@code true} all actions becomes available |
AbstractButton button = (AbstractButton) toolAndActionButtons.get(id); |
542 |
* @param hideOnDisable if {@code true} the buttons are also hidden if |
if (button == null) |
543 |
* {@code enabled} is {@code false} |
LOGGER.warn("Unknown tool or action ID: " + id); |
544 |
*/ |
return button; |
545 |
public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) { |
} |
546 |
for (int tool : actionButtons.keySet()) |
|
547 |
setButtonEnabled(tool,enabled,hideOnDisable); |
/** |
548 |
} |
* Returns the button for a specific tool. |
549 |
|
* |
550 |
/** |
* @param tool |
551 |
* Returns the maximum ID of tools. |
* the constant for a tool |
552 |
*/ |
*/ |
553 |
public int getMaxToolID() { |
public JToggleButton getToolButton(int tool) { |
554 |
return toolButtons.lastKey(); |
AbstractButton button = getButton(tool); |
555 |
} |
if (button != null && !(button instanceof JToggleButton)) { |
556 |
|
LOGGER.warn("ID specifies no tool: " + tool); |
557 |
/** |
button = null; |
558 |
* Returns the minimum ID of tools. |
} |
559 |
*/ |
return (JToggleButton) button; |
560 |
public int getMinToolID() { |
} |
561 |
return toolButtons.firstKey(); |
|
562 |
} |
/** |
563 |
|
* Returns the button for a specific action. |
564 |
/** |
* |
565 |
* Returns the maximum ID of actions. |
* @param action |
566 |
*/ |
* the constant an action |
567 |
public int getMaxActionID() { |
*/ |
568 |
return actionButtons.lastKey(); |
public JButton getActionButton(int action) { |
569 |
} |
AbstractButton button = getButton(action); |
570 |
|
if (button != null && !(button instanceof JButton)) { |
571 |
/** |
LOGGER.warn("ID specifies no action: " + action); |
572 |
* Returns the minimum ID of actions. |
button = null; |
573 |
*/ |
} |
574 |
public int getMinActionID() { |
return (JButton) button; |
575 |
return actionButtons.firstKey(); |
|
576 |
} |
} |
577 |
|
|
578 |
/** |
/** |
579 |
* Extends the {@link AbstractAction} with maintaining an ID and |
* Sets the selected tool. |
580 |
* the {@link MapPaneToolBar} the actions controls. |
* |
581 |
* Additionally this class automatically calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} |
* @param tool |
582 |
* or {@link MapPaneToolBar#performActionButton(int, ActionEvent)} |
* ID of the tool |
583 |
* depending on whether the action is added via {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} |
*/ |
584 |
* or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}. |
public void setSelectedTool(Integer tool) { |
585 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
if (tool == null) |
586 |
*/ |
toolButtonGroup.setUnselected(); |
587 |
public static class MapPaneToolBarAction extends AbstractAction { |
|
588 |
/** The ID of the action */ |
JToggleButton button = getToolButton(tool); |
589 |
protected int id = -1; |
if (button == null) |
590 |
/** The tool bar, this action is made for. */ |
return; |
591 |
protected MapPaneToolBar toolBar = null; |
button.setSelected(true); |
592 |
|
button.getAction().actionPerformed(null); |
593 |
/** |
|
594 |
* Creates a new action with a dummy description and no icon. |
selectedTool = tool; |
595 |
* @param id unique ID for the action |
} |
596 |
* @param toolBar toolbar this action is made for |
|
597 |
*/ |
/** |
598 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) { |
* Returns the selected tool. |
599 |
this(id,toolBar,""+id); |
* |
600 |
} |
* @return -1 if no tool is active |
601 |
|
*/ |
602 |
/** |
public int getSelectedTool() { |
603 |
* Creates a new action without an icon. |
if (toolButtonGroup.getSelectedButton() == null) |
604 |
* @param id unique ID for the action |
return -1; |
605 |
* @param toolBar toolbar this action is made for |
return selectedTool; |
606 |
* @param name description used for buttons or menus |
} |
607 |
*/ |
|
608 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) { |
/** |
609 |
this(id,toolBar,name,null); |
* Sets whether a tool or action is activated or not. The visible property |
610 |
} |
* of the button is not affected. |
611 |
|
* |
612 |
/** |
* @param id |
613 |
* Creates a new action. |
* tool or actionID |
614 |
* @param id unique ID for the action |
* @param enabled |
615 |
* @param toolBar toolbar this action is made for |
* if {@code true} the tool becomes available |
616 |
* @param name description used for buttons or menus |
*/ |
617 |
* @param icon icon used for buttons or menus |
public void setButtonEnabled(int id, boolean enabled) { |
618 |
*/ |
AbstractButton button = getButton(id); |
619 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name, Icon icon) { |
if (button == null) |
620 |
super(name,icon); |
return; |
621 |
this.id = id; |
button.setEnabled(enabled); |
622 |
this.toolBar = toolBar; |
} |
623 |
} |
|
624 |
|
/** |
625 |
/** |
* Sets whether a tool or action is activated or not. |
626 |
* Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} |
* |
627 |
* or {@link MapPaneToolBar#performActionButton(int, ActionEvent)} |
* @param id |
628 |
* depending on whether the action is added to the toolbar via |
* tool or actionID |
629 |
* {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} |
* @param enabled |
630 |
* or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}. |
* if {@code true} the tool becomes available |
631 |
*/ |
* @param hideOnDisable |
632 |
public void actionPerformed(ActionEvent e) { |
* if {@code true} the button is also hidden if {@code enabled} |
633 |
if ( toolBar.toolButtons.get(id) != null ) |
* is {@code false} |
634 |
toolBar.performToolButton(id, e); |
*/ |
635 |
if ( toolBar.actionButtons.get(id) != null ) |
public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) { |
636 |
toolBar.performActionButton(id, e); |
AbstractButton button = getButton(id); |
637 |
} |
if (button == null) |
638 |
|
return; |
639 |
/** |
button.setEnabled(enabled); |
640 |
* Returns the (unique) id of this action. |
// if button is enabled, it becomes visible anyway |
641 |
* @return |
// if button is disabled and the "hide" option is set, it is also hidden |
642 |
*/ |
if (enabled) |
643 |
public int getID() { |
button.setVisible(true); |
644 |
return id; |
else |
645 |
} |
button.setVisible(!hideOnDisable); |
646 |
} |
} |
647 |
} |
|
648 |
|
/** |
649 |
|
* Checks whether a ID is already used for a tool or action. |
650 |
|
* |
651 |
|
* @param tool |
652 |
|
* tool ID |
653 |
|
*/ |
654 |
|
public boolean isButtonIDUsed(int id) { |
655 |
|
return toolAndActionButtons.get(id) != null; |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
|
* Checks whether a tool is activated. |
660 |
|
* |
661 |
|
* @param tool |
662 |
|
* tool ID |
663 |
|
* @return {@code false} if an unknown ID is specified |
664 |
|
*/ |
665 |
|
public boolean isButtonEnabled(int id) { |
666 |
|
AbstractButton button = getButton(id); |
667 |
|
if (button != null) |
668 |
|
return button.isEnabled(); |
669 |
|
return false; |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
|
* Sets the activation for all tools. |
674 |
|
* |
675 |
|
* @param enabled |
676 |
|
* if {@code true} all tools becomes available |
677 |
|
* @param hideOnDisable |
678 |
|
* if {@code true} the buttons are also hidden if {@code enabled} |
679 |
|
* is {@code false} |
680 |
|
*/ |
681 |
|
public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) { |
682 |
|
for (int tool : toolAndActionButtons.keySet()) |
683 |
|
setButtonEnabled(tool, enabled, hideOnDisable); |
684 |
|
} |
685 |
|
|
686 |
|
/** |
687 |
|
* Sets the activation for all actions. |
688 |
|
* |
689 |
|
* @param enabled |
690 |
|
* if {@code true} all actions becomes available |
691 |
|
* @param hideOnDisable |
692 |
|
* if {@code true} the buttons are also hidden if {@code enabled} |
693 |
|
* is {@code false} |
694 |
|
*/ |
695 |
|
public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) { |
696 |
|
for (int id : toolAndActionButtons.keySet()) { |
697 |
|
if (toolAndActionButtons.get(id) instanceof JButton) { |
698 |
|
setButtonEnabled(id, enabled, hideOnDisable); |
699 |
|
} |
700 |
|
} |
701 |
|
|
702 |
|
} |
703 |
|
|
704 |
|
/** |
705 |
|
* Returns the maximum ID of tools. |
706 |
|
*/ |
707 |
|
public int getMaxToolID() { |
708 |
|
return toolAndActionButtons.lastKey(); |
709 |
|
} |
710 |
|
|
711 |
|
/** |
712 |
|
* Returns the minimum ID of tools. |
713 |
|
*/ |
714 |
|
public int getMinToolID() { |
715 |
|
return toolAndActionButtons.firstKey(); |
716 |
|
} |
717 |
|
|
718 |
|
/** |
719 |
|
* Extends the {@link AbstractAction} with maintaining an ID and the |
720 |
|
* {@link MapPaneToolBar} the actions controls. Additionally this class |
721 |
|
* automatically calls |
722 |
|
* {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or |
723 |
|
* {@link MapPaneToolBar#performActionButton(int, ActionEvent)} depending on |
724 |
|
* whether the action is added via |
725 |
|
* {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or |
726 |
|
* {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}. |
727 |
|
* |
728 |
|
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
729 |
|
* (University of Bonn/Germany) |
730 |
|
*/ |
731 |
|
public static class MapPaneToolBarAction extends AbstractAction { |
732 |
|
/** The ID of the action */ |
733 |
|
protected int id = -1; |
734 |
|
/** The tool bar, this action is made for. */ |
735 |
|
protected MapPaneToolBar toolBar = null; |
736 |
|
|
737 |
|
/** |
738 |
|
* Creates a new action with a dummy description and no icon. |
739 |
|
* |
740 |
|
* @param id |
741 |
|
* unique ID for the action |
742 |
|
* @param toolBar |
743 |
|
* toolbar this action is made for |
744 |
|
*/ |
745 |
|
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) { |
746 |
|
this(id, toolBar, "" + id); |
747 |
|
} |
748 |
|
|
749 |
|
/** |
750 |
|
* Creates a new action without an icon. |
751 |
|
* |
752 |
|
* @param id |
753 |
|
* unique ID for the action |
754 |
|
* @param toolBar |
755 |
|
* toolbar this action is made for |
756 |
|
* @param name |
757 |
|
* description used for buttons or menus |
758 |
|
*/ |
759 |
|
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) { |
760 |
|
this(id, toolBar, name, null); |
761 |
|
} |
762 |
|
|
763 |
|
/** |
764 |
|
* Creates a new action. |
765 |
|
* |
766 |
|
* @param id |
767 |
|
* unique ID for the action |
768 |
|
* @param toolBar |
769 |
|
* toolbar this action is made for |
770 |
|
* @param name |
771 |
|
* description used for buttons or menus |
772 |
|
* @param icon |
773 |
|
* icon used for buttons or menus |
774 |
|
*/ |
775 |
|
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
776 |
|
String name, Icon icon) { |
777 |
|
this (id, toolBar, name, icon, null); |
778 |
|
} |
779 |
|
|
780 |
|
/** |
781 |
|
* Creates a new action. |
782 |
|
* |
783 |
|
* @param id |
784 |
|
* unique ID for the action |
785 |
|
* @param toolBar |
786 |
|
* The {@link MapPaneToolBar} this action is made for |
787 |
|
* @param name |
788 |
|
* description used for buttons or menus |
789 |
|
* @param icon |
790 |
|
* icon used for buttons or menus |
791 |
|
* @param toolTip |
792 |
|
* Tooltip to use for the button or menu |
793 |
|
*/ |
794 |
|
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
795 |
|
String name, Icon icon, String toolTip) { |
796 |
|
super(name, icon); |
797 |
|
|
798 |
|
if (toolTip != null && !toolTip.trim().isEmpty()){ |
799 |
|
putValue(Action.SHORT_DESCRIPTION, toolTip); |
800 |
|
} |
801 |
|
|
802 |
|
this.id = id; |
803 |
|
this.toolBar = toolBar; |
804 |
|
} |
805 |
|
|
806 |
|
/** |
807 |
|
* Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or |
808 |
|
* {@link MapPaneToolBar#performActionButton(int, ActionEvent)} |
809 |
|
* depending on whether the action is added to the toolbar via |
810 |
|
* {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or |
811 |
|
* {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}. |
812 |
|
*/ |
813 |
|
public void actionPerformed(ActionEvent e) { |
814 |
|
if (toolBar.toolAndActionButtons.get(id) instanceof JToggleButton) |
815 |
|
toolBar.performToolButton(id, e); |
816 |
|
else if (toolBar.toolAndActionButtons.get(id) instanceof JButton) |
817 |
|
toolBar.performActionButton(id, e); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
|
* Returns the (unique) id of this action. |
822 |
|
* |
823 |
|
* @return |
824 |
|
*/ |
825 |
|
public int getID() { |
826 |
|
return id; |
827 |
|
} |
828 |
|
} |
829 |
|
|
830 |
|
/** |
831 |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht wird) wird. Dann werden wird der |
832 |
|
* Hintergrund auf WEISS gesetzt. |
833 |
|
* |
834 |
|
* @author <a href="mailto:[email protected]">Stefan Alfons |
835 |
|
* Krüger</a> |
836 |
|
*/ |
837 |
|
@Override |
838 |
|
public void print(Graphics g) { |
839 |
|
Color orig = getBackground(); |
840 |
|
setBackground(Color.WHITE); |
841 |
|
// wrap in try/finally so that we always restore the state |
842 |
|
try { |
843 |
|
super.print(g); |
844 |
|
} finally { |
845 |
|
setBackground(orig); |
846 |
|
} |
847 |
|
} |
848 |
|
} |