43 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
44 |
import javax.swing.AbstractButton; |
import javax.swing.AbstractButton; |
45 |
import javax.swing.Action; |
import javax.swing.Action; |
|
import javax.swing.BorderFactory; |
|
46 |
import javax.swing.Icon; |
import javax.swing.Icon; |
47 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
48 |
import javax.swing.JButton; |
import javax.swing.JButton; |
60 |
import schmitzm.lang.ResourceProvider; |
import schmitzm.lang.ResourceProvider; |
61 |
import schmitzm.swing.ButtonGroup; |
import schmitzm.swing.ButtonGroup; |
62 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
63 |
|
import skrueger.swing.SmallButton; |
64 |
|
import skrueger.swing.SmallToggleButton; |
65 |
|
|
66 |
import com.vividsolutions.jts.geom.Envelope; |
import com.vividsolutions.jts.geom.Envelope; |
67 |
|
|
79 |
public class MapPaneToolBar extends JToolBar { |
public class MapPaneToolBar extends JToolBar { |
80 |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
81 |
.getName()); |
.getName()); |
82 |
|
|
83 |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
84 |
.extendPackagePath(MapPaneToolBar.class, |
.extendPackagePath(MapPaneToolBar.class, |
85 |
"resource.locales.mapPaneToolbar"), Locale.ENGLISH); |
"resource.locales.mapPaneToolbar"), Locale.ENGLISH); |
86 |
|
|
87 |
public static String R(String key, Object... values) { |
public static String R(String key, Object... values) { |
88 |
return RESOURCE.getString(key, values); |
return RESOURCE.getString(key, values); |
89 |
} |
} |
159 |
/** Listeners what want to be informed about a change of the selected tool **/ |
/** Listeners what want to be informed about a change of the selected tool **/ |
160 |
protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>(); |
protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>(); |
161 |
|
|
162 |
/** This listener is added to all {@link JToggleButton} **/ |
/** This listener is added to all {@link JToggleButton} **/ |
163 |
private final ActionListener toolSelectedListener = new ActionListener(){ |
private final ActionListener toolSelectedListener = new ActionListener() { |
164 |
|
|
165 |
@Override |
@Override |
166 |
public void actionPerformed(ActionEvent e) { |
public void actionPerformed(ActionEvent e) { |
167 |
JToggleButton tb = (JToggleButton) e.getSource(); |
JToggleButton tb = (JToggleButton) e.getSource(); |
168 |
|
|
169 |
// Inform the listeners about a newly selected tool |
// Inform the listeners about a newly selected tool |
170 |
for (MapPaneToolSelectedListener l : toolSelectionListeners) { |
for (MapPaneToolSelectedListener l : toolSelectionListeners) { |
171 |
l.toolSelected(Integer.valueOf( tb.getName() ) ) ; |
l.toolSelected(Integer.valueOf(tb.getName())); |
172 |
} |
} |
173 |
} |
} |
174 |
|
|
175 |
}; |
}; |
176 |
|
|
177 |
/** |
/** |
181 |
public MapPaneToolBar() { |
public MapPaneToolBar() { |
182 |
this(null); |
this(null); |
183 |
} |
} |
184 |
|
|
185 |
public void addButtonSelectedListener(MapPaneToolSelectedListener listener ) { |
public void addButtonSelectedListener(MapPaneToolSelectedListener listener) { |
186 |
toolSelectionListeners.add(listener); |
toolSelectionListeners.add(listener); |
187 |
} |
} |
188 |
|
|
189 |
public void removeButtonSelectedListener(MapPaneToolSelectedListener listener ) { |
public void removeButtonSelectedListener( |
190 |
toolSelectionListeners .remove(listener); |
MapPaneToolSelectedListener listener) { |
191 |
|
toolSelectionListeners.remove(listener); |
192 |
} |
} |
193 |
|
|
194 |
/** |
/** |
199 |
*/ |
*/ |
200 |
public MapPaneToolBar(JMapPane mapPane) { |
public MapPaneToolBar(JMapPane mapPane) { |
201 |
super("Control the map", JToolBar.HORIZONTAL); |
super("Control the map", JToolBar.HORIZONTAL); |
202 |
|
|
203 |
// I want to see nothing on the background |
// I want to see nothing on the background |
204 |
setOpaque(false); |
setOpaque(false); |
205 |
setBorder(null); |
setBorder(null); |
206 |
|
|
207 |
this.toolButtonGroup = new ButtonGroup(); |
this.toolButtonGroup = new ButtonGroup(); |
208 |
|
|
209 |
// Create a Listener to listen to the zooms on the JMapPane |
// Create a Listener to listen to the zooms on the JMapPane |
210 |
this.mapPaneListener = new JMapPaneListener() { |
this.mapPaneListener = new JMapPaneListener() { |
211 |
public void performMapPaneEvent(JMapPaneEvent e) { |
public void performMapPaneEvent(JMapPaneEvent e) { |
219 |
|
|
220 |
final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e; |
final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e; |
221 |
Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea(); |
Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea(); |
222 |
|
|
223 |
final Envelope mapArea = mapAreaChangedEvent.getNewMapArea(); |
final Envelope mapArea = mapAreaChangedEvent.getNewMapArea(); |
224 |
if (mapArea == null || mapArea.equals(oldMapArea) ) { |
if (mapArea == null || mapArea.equals(oldMapArea) |
225 |
// If the MapArea didn't change... we don't want to register it as a zoom action. |
|| Double.isNaN(mapArea.getMinX()) |
226 |
|
|| Double.isNaN(mapArea.getMaxX()) |
227 |
|
|| Double.isNaN(mapArea.getMinY()) |
228 |
|
|| Double.isNaN(mapArea.getMaxY())) { |
229 |
|
// If the MapArea didn't change... we don't want to register |
230 |
|
// it as a zoom action. |
231 |
return; |
return; |
232 |
} |
} |
233 |
|
|
234 |
if (lastZooms.size() == 0 && oldMapArea != null) { |
if (lastZooms.size() == 0 |
235 |
|
&& oldMapArea != null |
236 |
|
&& !oldMapArea.isNull() |
237 |
|
&& !(Double.isNaN(oldMapArea.getMinX()) |
238 |
|
|| Double.isNaN(oldMapArea.getMaxX()) |
239 |
|
|| Double.isNaN(oldMapArea.getMinY()) || Double |
240 |
|
.isNaN(oldMapArea.getMaxY()))) { |
241 |
lastZooms.add(oldMapArea); |
lastZooms.add(oldMapArea); |
242 |
zoomBackIndex = 1; |
zoomBackIndex = 1; |
243 |
} |
} |
289 |
* puts all tool buttons and all actions buttons to the tool bar. |
* puts all tool buttons and all actions buttons to the tool bar. |
290 |
*/ |
*/ |
291 |
protected void init() { |
protected void init() { |
292 |
|
|
293 |
initToolsAndActions(); |
initToolsAndActions(); |
294 |
|
|
295 |
addSeparator(SEPERATOR0, new JToolBar.Separator()); |
addSeparator(SEPERATOR0, new JToolBar.Separator()); |
308 |
protected void initToolsAndActions() { |
protected void initToolsAndActions() { |
309 |
// Panning |
// Panning |
310 |
addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon( |
311 |
MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false); |
MapView.class.getResource("resource/icons/pan.png")), |
312 |
|
R("MapPaneButtons.Pan.TT")), false); |
313 |
// Info |
// Info |
314 |
addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon( |
315 |
MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false); |
MapView.class.getResource("resource/icons/info.png")), |
316 |
|
R("MapPaneButtons.Info.TT")), false); |
317 |
|
|
318 |
// Zoom in |
// Zoom in |
319 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon( |
320 |
MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")), |
MapView.class.getResource("resource/icons/zoom_in.png")), |
321 |
false); |
R("MapPaneButtons.ZoomIn.TT")), false); |
322 |
// Zoom out |
// Zoom out |
323 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon( |
324 |
MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")), |
MapView.class.getResource("resource/icons/zoom_out.png")), |
325 |
false); |
R("MapPaneButtons.ZoomOut.TT")), false); |
326 |
|
|
327 |
// Action button to revert the last zoom |
// Action button to revert the last zoom |
328 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
329 |
new ImageIcon(MapView.class |
new ImageIcon(MapView.class |
330 |
.getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false); |
.getResource("resource/icons/zoom_back.png")), |
331 |
|
R("MapPaneButtons.LastZoom.TT")), false); |
332 |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
333 |
|
|
334 |
// Action button to redo the last zoom |
// Action button to redo the last zoom |
335 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "", |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "", |
336 |
new ImageIcon(MapView.class |
new ImageIcon(MapView.class |
337 |
.getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")), |
.getResource("resource/icons/zoom_forward.png")), |
338 |
false); |
R("MapPaneButtons.NextZoom.TT")), false); |
339 |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
340 |
|
|
341 |
// set the selected tool enabled |
// set the selected tool enabled |
342 |
setSelectedTool(selectedTool); |
setSelectedTool(selectedTool); |
343 |
|
|
344 |
} |
} |
345 |
|
|
346 |
@Override |
@Override |
347 |
public void paint(Graphics g) { |
public void paint(Graphics g) { |
348 |
super.paint(g); |
super.paint(g); |
353 |
* the tool bar. |
* the tool bar. |
354 |
*/ |
*/ |
355 |
public void initToolBar() { |
public void initToolBar() { |
356 |
// setOpaque(true); |
// setOpaque(true); |
357 |
// |
// |
358 |
// setAlignmentY(1f); |
// setAlignmentY(1f); |
359 |
// setAlignmentX(0.5f); |
// setAlignmentX(0.5f); |
360 |
removeAll(); |
removeAll(); |
361 |
|
|
362 |
// // Separator to the left of the tool actions to start |
// // Separator to the left of the tool actions to start |
363 |
// // the tool buttons with the map (not with the coordinate grid) |
// // the tool buttons with the map (not with the coordinate grid) |
364 |
// Dimension dimension = new Dimension(49, 10); |
// Dimension dimension = new Dimension(49, 10); |
365 |
// addSeparator(dimension); |
// addSeparator(dimension); |
366 |
|
|
|
|
|
367 |
// Tool buttons |
// Tool buttons |
368 |
for (Integer bKey : toolAndActionButtons.keySet()) { |
for (Integer bKey : toolAndActionButtons.keySet()) { |
369 |
|
|
370 |
JComponent b = toolAndActionButtons.get(bKey); |
JComponent b = toolAndActionButtons.get(bKey); |
371 |
|
|
372 |
if (b instanceof JToggleButton) { |
if (b instanceof JToggleButton) { |
373 |
JToggleButton tb = (JToggleButton) b; |
JToggleButton tb = (JToggleButton) b; |
374 |
tb.setName(bKey.toString()); |
tb.setName(bKey.toString()); |
375 |
tb.addActionListener( toolSelectedListener ); |
tb.addActionListener(toolSelectedListener); |
376 |
} |
} |
377 |
|
|
378 |
add(b); |
add(b); |
379 |
} |
} |
380 |
|
|
394 |
} |
} |
395 |
|
|
396 |
} |
} |
397 |
|
|
398 |
revalidate(); |
revalidate(); |
399 |
repaint(); |
repaint(); |
400 |
} |
} |
530 |
+ buttonAction.getID()); |
+ buttonAction.getID()); |
531 |
return; |
return; |
532 |
} |
} |
533 |
JToggleButton button = new JToggleButton(buttonAction); |
JToggleButton button = new SmallToggleButton(buttonAction); |
534 |
button.setBorder(BorderFactory.createRaisedBevelBorder()); |
|
535 |
|
// button.setBorder(BorderFactory.createRaisedBevelBorder()); |
536 |
toolButtonGroup.add(button); |
toolButtonGroup.add(button); |
537 |
toolAndActionButtons.put(buttonAction.getID(), button); |
toolAndActionButtons.put(buttonAction.getID(), button); |
538 |
if (resetToolBar) |
if (resetToolBar) |
539 |
initToolBar(); |
initToolBar(); |
540 |
} |
} |
541 |
|
|
|
|
|
|
|
|
542 |
/** |
/** |
543 |
* Adds a tool to the tool bar and resets the toolbar GUI. |
* Adds a tool to the tool bar and resets the toolbar GUI. |
544 |
* |
* |
568 |
+ buttonAction.getID()); |
+ buttonAction.getID()); |
569 |
return; |
return; |
570 |
} |
} |
571 |
JButton button = new JButton(buttonAction); |
JButton button = new SmallButton(buttonAction); |
572 |
button.setBorder(BorderFactory.createRaisedBevelBorder()); |
// button.setBorder(BorderFactory.createRaisedBevelBorder()); |
573 |
toolAndActionButtons.put(buttonAction.getID(), button); |
toolAndActionButtons.put(buttonAction.getID(), button); |
574 |
if (resetToolBar) |
if (resetToolBar) |
575 |
initToolBar(); |
initToolBar(); |
576 |
} |
} |
577 |
|
|
578 |
/** |
/** |
579 |
* Adds any JComponent to the tool bar. Does nothing if a tool or action with the |
* Adds any JComponent to the tool bar. Does nothing if a tool or action |
580 |
* specified ID already exists! |
* with the specified ID already exists! |
581 |
* |
* |
582 |
* @param component A {@link JComponent} that shall be added |
* @param component |
583 |
* @param id The ID associaded with the {@link JComponent} |
* A {@link JComponent} that shall be added |
584 |
|
* @param id |
585 |
|
* The ID associaded with the {@link JComponent} |
586 |
* @param resetToolBar |
* @param resetToolBar |
587 |
* indicates whether the toolbar GUI is reset after adding the |
* indicates whether the toolbar GUI is reset after adding the |
588 |
* button (if adding several actions it useful only to reset the |
* button (if adding several actions it useful only to reset the |
589 |
* GUI for the last added tool) |
* GUI for the last added tool) |
590 |
*/ |
*/ |
591 |
public void addJComponent(JComponent component, int id, |
public void addJComponent(JComponent component, int id, boolean resetToolBar) { |
|
boolean resetToolBar) { |
|
592 |
|
|
593 |
if (isButtonIDUsed(id)) { |
if (isButtonIDUsed(id)) { |
594 |
LOGGER |
LOGGER |
595 |
.warn("addAction(.) ignored because ID already used for tool or action: " |
.warn("addAction(.) ignored because ID already used for tool or action: " |
596 |
+ id); |
+ id); |
597 |
return; |
return; |
598 |
} |
} |
599 |
|
|
600 |
toolAndActionButtons.put(id, component); |
toolAndActionButtons.put(id, component); |
601 |
if (resetToolBar) |
if (resetToolBar) |
602 |
initToolBar(); |
initToolBar(); |
868 |
*/ |
*/ |
869 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
870 |
String name, Icon icon) { |
String name, Icon icon) { |
871 |
this (id, toolBar, name, icon, null); |
this(id, toolBar, name, icon, null); |
872 |
} |
} |
873 |
|
|
874 |
/** |
/** |
875 |
* Creates a new action. |
* Creates a new action. |
876 |
* |
* |
888 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
889 |
String name, Icon icon, String toolTip) { |
String name, Icon icon, String toolTip) { |
890 |
super(name, icon); |
super(name, icon); |
891 |
|
|
892 |
if (toolTip != null && !toolTip.trim().isEmpty()){ |
if (toolTip != null && !toolTip.trim().isEmpty()) { |
893 |
putValue(Action.SHORT_DESCRIPTION, toolTip); |
putValue(Action.SHORT_DESCRIPTION, toolTip); |
894 |
} |
} |
895 |
|
|
896 |
this.id = id; |
this.id = id; |
897 |
this.toolBar = toolBar; |
this.toolBar = toolBar; |
898 |
} |
} |
920 |
return id; |
return id; |
921 |
} |
} |
922 |
} |
} |
923 |
|
|
924 |
/** |
/** |
925 |
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht wird) wird. Dann werden wird der |
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht |
926 |
* Hintergrund auf WEISS gesetzt. |
* wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt. |
927 |
* |
* |
928 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons |
929 |
* Krüger</a> |
* Krüger</a> |
930 |
*/ |
*/ |
931 |
@Override |
@Override |
932 |
public void print(Graphics g) { |
public void print(Graphics g) { |
933 |
Color orig = getBackground(); |
Color orig = getBackground(); |
934 |
setBackground(Color.WHITE); |
setBackground(Color.WHITE); |
935 |
// wrap in try/finally so that we always restore the state |
// wrap in try/finally so that we always restore the state |
936 |
try { |
try { |
937 |
super.print(g); |
super.print(g); |
938 |
} finally { |
} finally { |
939 |
setBackground(orig); |
setBackground(orig); |
940 |
} |
} |
941 |
} |
} |
942 |
|
|
943 |
} |
} |