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