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