3 |
import java.awt.Dimension; |
import java.awt.Dimension; |
4 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
5 |
import java.util.ArrayList; |
import java.util.ArrayList; |
6 |
|
import java.util.Locale; |
7 |
import java.util.SortedMap; |
import java.util.SortedMap; |
8 |
import java.util.TreeMap; |
import java.util.TreeMap; |
9 |
|
|
10 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
11 |
import javax.swing.AbstractButton; |
import javax.swing.AbstractButton; |
12 |
|
import javax.swing.Action; |
13 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
14 |
import javax.swing.Icon; |
import javax.swing.Icon; |
15 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
24 |
import schmitzm.geotools.map.event.JMapPaneEvent; |
import schmitzm.geotools.map.event.JMapPaneEvent; |
25 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import schmitzm.geotools.map.event.JMapPaneListener; |
26 |
import schmitzm.geotools.map.event.MapAreaChangedEvent; |
import schmitzm.geotools.map.event.MapAreaChangedEvent; |
27 |
|
import schmitzm.lang.LangUtil; |
28 |
|
import schmitzm.lang.ResourceProvider; |
29 |
import schmitzm.swing.ButtonGroup; |
import schmitzm.swing.ButtonGroup; |
30 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
31 |
|
|
45 |
public class MapPaneToolBar extends JToolBar { |
public class MapPaneToolBar extends JToolBar { |
46 |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
47 |
.getName()); |
.getName()); |
48 |
|
|
49 |
|
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
50 |
|
.extendPackagePath(MapPaneToolBar.class, |
51 |
|
"resource.locales.mapPaneToolbar"), Locale.ENGLISH); |
52 |
|
|
53 |
|
public static String R(String key, Object... values) { |
54 |
|
return RESOURCE.getString(key, values); |
55 |
|
} |
56 |
|
|
57 |
/** Constant for the tool "Panning" (10). */ |
/** Constant for the tool "Panning" (10). */ |
58 |
public static final int TOOL_PAN = 10; |
public static final int TOOL_PAN = 10; |
59 |
/** Constant for the tool "Info" (20). */ |
/** Constant for the tool "Info" (20). */ |
138 |
super("Control the map", JToolBar.HORIZONTAL); |
super("Control the map", JToolBar.HORIZONTAL); |
139 |
this.toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
this.toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
140 |
this.toolButtonGroup = new ButtonGroup(); |
this.toolButtonGroup = new ButtonGroup(); |
141 |
// Create a Listener to sniff the zooms on the JMapPane |
|
142 |
|
// Create a Listener to listen to the zooms on the JMapPane |
143 |
this.mapPaneListener = new JMapPaneListener() { |
this.mapPaneListener = new JMapPaneListener() { |
144 |
public void performMapPaneEvent(JMapPaneEvent e) { |
public void performMapPaneEvent(JMapPaneEvent e) { |
145 |
if (!(e instanceof MapAreaChangedEvent)) |
if (!(e instanceof MapAreaChangedEvent)) |
150 |
return; |
return; |
151 |
} |
} |
152 |
|
|
153 |
Envelope oldMapArea = ((MapAreaChangedEvent) e).getOldMapArea(); |
final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e; |
154 |
|
Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea(); |
155 |
|
|
156 |
|
final Envelope mapArea = mapAreaChangedEvent.getNewMapArea(); |
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. |
159 |
|
return; |
160 |
|
} |
161 |
|
|
162 |
if (lastZooms.size() == 0 && oldMapArea != null) { |
if (lastZooms.size() == 0 && oldMapArea != null) { |
163 |
lastZooms.add(oldMapArea); |
lastZooms.add(oldMapArea); |
164 |
zoomBackIndex = 1; |
zoomBackIndex = 1; |
165 |
} |
} |
|
|
|
|
final Envelope mapArea = ((MapAreaChangedEvent) e) |
|
|
.getNewMapArea(); |
|
166 |
if (mapArea == null) |
if (mapArea == null) |
167 |
return; |
return; |
168 |
|
|
229 |
protected void initToolsAndActions() { |
protected void initToolsAndActions() { |
230 |
// Panning |
// Panning |
231 |
addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon( |
232 |
MapView.class.getResource("resource/icons/pan.png"))), false); |
MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false); |
233 |
// Info |
// Info |
234 |
addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon( |
235 |
MapView.class.getResource("resource/icons/info.png"))), false); |
MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false); |
236 |
|
|
237 |
// Zoom in |
// Zoom in |
238 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon( |
239 |
MapView.class.getResource("resource/icons/zoom_in.png"))), |
MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")), |
240 |
false); |
false); |
241 |
// Zoom out |
// Zoom out |
242 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon( |
243 |
MapView.class.getResource("resource/icons/zoom_out.png"))), |
MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")), |
244 |
false); |
false); |
245 |
|
|
246 |
// Action button to revert the last zoom |
// Action button to revert the last zoom |
247 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
248 |
new ImageIcon(MapView.class |
new ImageIcon(MapView.class |
249 |
.getResource("resource/icons/zoom_back.png"))), false); |
.getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false); |
250 |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
251 |
|
|
252 |
// Action button to redo the last zoom |
// Action button to redo the last zoom |
253 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "", |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "", |
254 |
new ImageIcon(MapView.class |
new ImageIcon(MapView.class |
255 |
.getResource("resource/icons/zoom_forward.png"))), |
.getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")), |
256 |
false); |
false); |
257 |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
258 |
|
|
265 |
* Clears the GUI of all components and adds all tool and action buttons to |
* Clears the GUI of all components and adds all tool and action buttons to |
266 |
* the tool bar. |
* the tool bar. |
267 |
*/ |
*/ |
268 |
protected void initToolBar() { |
public void initToolBar() { |
269 |
setAlignmentY(1f); |
setAlignmentY(1f); |
270 |
removeAll(); |
removeAll(); |
271 |
// Separator to the left of the tool actions to start |
// Separator to the left of the tool actions to start |
275 |
// Tool buttons |
// Tool buttons |
276 |
for (JComponent b : toolAndActionButtons.values()) |
for (JComponent b : toolAndActionButtons.values()) |
277 |
add(b); |
add(b); |
278 |
|
|
279 |
|
repaint(); |
280 |
|
|
281 |
|
if (!toolAndActionButtons.containsKey(selectedTool)) { |
282 |
|
/** |
283 |
|
* This might be a bit specific, but IF selection buttons are |
284 |
|
* available, select one of them.. if not, select the INFO tool. |
285 |
|
*/ |
286 |
|
|
287 |
|
if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) { |
288 |
|
setSelectedTool(TOOL_SELECTION_SET); |
289 |
|
} else if (toolAndActionButtons.containsKey(TOOL_INFO)) { |
290 |
|
setSelectedTool(TOOL_INFO); |
291 |
|
} else { |
292 |
|
// TODO What to do now?! |
293 |
|
setSelectedTool(null); |
294 |
|
} |
295 |
|
|
296 |
|
} |
297 |
} |
} |
298 |
|
|
299 |
// Space between tool buttons and action buttons |
// Space between tool buttons and action buttons |
332 |
// Set the mouse tool to "Info" |
// Set the mouse tool to "Info" |
333 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
334 |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
335 |
// JMapPane.SELECT_TOP_ONEONLY |
// JMapPane.SELECT_TOP_ONEONLY |
336 |
mapPane.setHighlight(false);// SK: Was true, but since it not works |
mapPane.setHighlight(false);// SK: Was true, but since it not works |
337 |
// properly removed it to save |
// properly removed it to save |
338 |
// performance |
// performance |
339 |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
340 |
break; |
break; |
341 |
case TOOL_ZOOMIN: |
case TOOL_ZOOMIN: |
352 |
mapPane.setHighlight(false); |
mapPane.setHighlight(false); |
353 |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
354 |
break; |
break; |
355 |
// default: |
default: |
356 |
// // Set map actions to default |
// Set map actions to default |
357 |
// mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
358 |
// mapPane.setState(JMapPane.NONE); |
mapPane.setState(JMapPane.NONE); |
359 |
// mapPane.setHighlight(false); |
mapPane.setHighlight(false); |
360 |
// mapPane.setNormalCursor(null); |
mapPane.setNormalCursor(null); |
361 |
// break; |
break; |
362 |
} |
} |
363 |
mapPane.updateCursor(); |
mapPane.updateCursor(); |
364 |
} |
} |
365 |
|
|
366 |
/** |
/** |
367 |
* @param id The ID of the Component to remove |
* @param id |
368 |
|
* The ID of the Component to remove. The change will not be |
369 |
|
* visible until {@link #initToolBar()} is called. |
370 |
* @return <code>null</code> or the component that has been removed. |
* @return <code>null</code> or the component that has been removed. |
371 |
*/ |
*/ |
372 |
public JComponent removeId(int id){ |
public JComponent removeId(int id) { |
373 |
return toolAndActionButtons.remove(id); |
return toolAndActionButtons.remove(id); |
374 |
} |
} |
375 |
|
|
412 |
mapPane.refresh(); |
mapPane.refresh(); |
413 |
} |
} |
414 |
} |
} |
|
|
|
415 |
} |
} |
416 |
|
|
417 |
/** |
/** |
558 |
return; |
return; |
559 |
button.setSelected(true); |
button.setSelected(true); |
560 |
button.getAction().actionPerformed(null); |
button.getAction().actionPerformed(null); |
561 |
|
|
562 |
|
selectedTool = tool; |
563 |
} |
} |
564 |
|
|
565 |
/** |
/** |
742 |
*/ |
*/ |
743 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
744 |
String name, Icon icon) { |
String name, Icon icon) { |
745 |
|
this (id, toolBar, name, icon, null); |
746 |
|
} |
747 |
|
|
748 |
|
/** |
749 |
|
* Creates a new action. |
750 |
|
* |
751 |
|
* @param id |
752 |
|
* unique ID for the action |
753 |
|
* @param toolBar |
754 |
|
* The {@link MapPaneToolBar} this action is made for |
755 |
|
* @param name |
756 |
|
* description used for buttons or menus |
757 |
|
* @param icon |
758 |
|
* icon used for buttons or menus |
759 |
|
* @param toolTip |
760 |
|
* Tooltip to use for the button or menu |
761 |
|
*/ |
762 |
|
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
763 |
|
String name, Icon icon, String toolTip) { |
764 |
super(name, icon); |
super(name, icon); |
765 |
|
|
766 |
|
if (toolTip != null && !toolTip.trim().isEmpty()){ |
767 |
|
putValue(Action.SHORT_DESCRIPTION, toolTip); |
768 |
|
} |
769 |
|
|
770 |
this.id = id; |
this.id = id; |
771 |
this.toolBar = toolBar; |
this.toolBar = toolBar; |
772 |
} |
} |