Statistics
| Revision:

root / org.gvsig.educa.batovi / trunk / org.gvsig.educa.batovi.mapviewer / org.gvsig.educa.batovi.mapviewer / src / main / java / org / gvsig / educa / batovi / mapviewer / Main.java @ 129

History | View | Annotate | Download (23.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.educa.batovi.mapviewer;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.Color;
26
import java.awt.Dimension;
27
import java.awt.Graphics;
28
import java.awt.Toolkit;
29
import java.awt.event.ActionEvent;
30
import java.awt.image.BufferedImage;
31
import java.io.File;
32
import java.io.IOException;
33
import java.net.URL;
34
import java.util.List;
35
import java.util.Locale;
36
import java.util.MissingResourceException;
37
import java.util.ResourceBundle;
38

    
39
import javax.swing.AbstractAction;
40
import javax.swing.Action;
41
import javax.swing.Box;
42
import javax.swing.ImageIcon;
43
import javax.swing.JButton;
44
import javax.swing.JFileChooser;
45
import javax.swing.JFrame;
46
import javax.swing.JToolBar;
47
import javax.swing.SwingUtilities;
48
import javax.swing.UIManager;
49
import javax.swing.WindowConstants;
50

    
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
import org.gvsig.educa.thematicmap.ThematicMapLocator;
55
import org.gvsig.educa.thematicmap.ThematicMapManager;
56
import org.gvsig.educa.thematicmap.map.ThematicMap;
57
import org.gvsig.educa.thematicmap.swing.ThematicMapSwingLocator;
58
import org.gvsig.educa.thematicmap.swing.ThematicMapSwingManager;
59
import org.gvsig.educa.thematicmap.swing.ThematicMapWindowManager;
60
import org.gvsig.educa.thematicmap.swing.ThematicMapWindowManager.CONFIRM_DIALOG_OPTION;
61
import org.gvsig.educa.thematicmap.swing.ThematicMapWindowManager.CONFIRM_DIALOG_TYPE;
62
import org.gvsig.educa.thematicmap.swing.ThematicMapWindowManager.MESSAGE_DIALOG_TYPE;
63
import org.gvsig.educa.thematicmap.swing.viewer.ThematicMapViewer;
64
import org.gvsig.installer.swing.api.SwingInstallerLocator;
65
import org.gvsig.installer.swing.api.SwingInstallerManager;
66
import org.gvsig.installer.swing.api.execution.InstallPackageWizardException;
67
import org.gvsig.installer.swing.api.wizard.AbstractInstallerWizard;
68
import org.gvsig.tools.ToolsLocator;
69
import org.gvsig.tools.dispose.DisposableManager;
70
import org.gvsig.tools.exception.BaseException;
71
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
72
import org.gvsig.tools.swing.api.ToolsSwingLocator;
73
import org.gvsig.tools.swing.api.usability.UsabilitySwingManager;
74
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
75

    
76
/**
77
 * Main executable class for testing the ThematicMap library.
78
 * 
79
 * @author gvSIG Team
80
 * @version $Id$
81
 */
82
public class Main {
83

    
84
    private static final Logger LOG = LoggerFactory.getLogger(Main.class);
85

    
86
    private static Main instance = null;
87

    
88
    private ResourceBundle resourceBundle = null;
89

    
90
    private final ThematicMapManager manager;
91
    private final ThematicMapSwingManager swingManager;
92
    private final ThematicMapWindowManager winManager;
93
    private final UsabilitySwingManager usabManager;
94
    private final DisposableManager disposeManager;
95
    private final SwingInstallerManager swingInstallerManager;
96

    
97
    private final JFileChooser fileChooser;
98
    private JFrame mainFrame;
99
    private ThematicMapViewer curViewer = null;
100

    
101
    private AbstractAction setPanToolAction;
102
    private AbstractAction setZoomToolAction;
103
    private AbstractAction zoomAllAction;
104
    private AbstractAction setThematiMapFolderAction;
105
    private AbstractAction showOpenThematicMapDialogAction;
106
    private AbstractAction exitAction;
107
    private AbstractAction closeThematicMapAction;
108

    
109
    private AbstractAction installThematicMaps;
110

    
111
    private AbstractAction addInstallUrl;
112

    
113
    private MapToolsRegistrant mapToolsRegistrant;
114

    
115
    private StatusBar statusBar;
116

    
117
    private AbstractAction showHideTocAction;
118

    
119
    private AbstractAction setMeasuerToolAction;
120

    
121
    private AbstractAction setAreaToolAction;
122

    
123
    private AbstractAction setInfoToolAction;
124

    
125
    private ImageIcon defaultIcon;
126

    
127
    public static void main(String args[]) {
128
        new DefaultLibrariesInitializer().fullInitialize();
129
        instance = new Main();
130
        instance.init();
131
        instance.show();
132
    }
133

    
134
    public static Main getInstance() {
135
        return instance;
136
    }
137

    
138
    /**
139
     * New instance of Main
140
     */
141
    public Main() {
142
        manager = ThematicMapLocator.getManager();
143
        swingManager = ThematicMapSwingLocator.getSwingManager();
144
        winManager = swingManager.getWindowManager();
145
        usabManager = ToolsSwingLocator.getUsabilitySwingManager();
146
        disposeManager = ToolsLocator.getDisposableManager();
147
        swingInstallerManager =
148
            SwingInstallerLocator.getSwingInstallerManager();
149
        fileChooser = new JFileChooser();
150
        LOG.info("Strating application");
151
    }
152

    
153
    private void init() {
154

    
155
        String defaultFolderPath =
156
            "gvSIG/plugins/org.gvsig.educa.thematicmap.app.viewer/thematicMaps";
157
        File defaultFolder =
158
            new File(System.getProperty("user.home"), defaultFolderPath);
159

    
160
        if (!defaultFolder.exists()) {
161
            defaultFolder.mkdirs();
162
        }
163
        LOG.info("Default map folder: {}", defaultFolder);
164
        manager.setInstallationMapFolder(defaultFolder.getAbsolutePath());
165

    
166
        mapToolsRegistrant = new MapToolsRegistrant();
167
        swingManager.addMapControlRegistrant(mapToolsRegistrant);
168

    
169
        try {
170
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
171
        } catch (Exception ex) {
172
            LOG.error("Exception setting system LookAndFeel: ".concat(UIManager
173
                .getSystemLookAndFeelClassName()), ex);
174
            throw new RuntimeException(ex);
175
        }
176

    
177
    }
178

    
179
    public ThematicMapWindowManager getWindowsManager() {
180
        return winManager;
181
    }
182

    
183
    /**
184
     * Shows main frame
185
     */
186
    public void show() {
187

    
188
        // Create actions
189
        createActions();
190

    
191
        // Create JFrame to show data
192
        mainFrame = new JFrame(translate("gvSIG_Batovi_Map_Viewer"));
193
        mainFrame.setUndecorated(true);
194
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
195
        mainFrame.setLayout(new BorderLayout(10, 10));
196

    
197
        // Create menu bar
198
        // createMenu();
199

    
200
        // Create tools bar
201
        createToolBar();
202

    
203
        // Create status bar
204
        createStatusBar();
205

    
206
        updateControls();
207

    
208
        // Display the window.
209
        mainFrame.pack();
210
        Toolkit toolkit = Toolkit.getDefaultToolkit();
211
        Dimension dimension = toolkit.getScreenSize();
212
        mainFrame.setSize(dimension);
213
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
214
        mainFrame.setLocation(0, 0);
215
        mainFrame.setVisible(true);
216
    }
217

    
218
    /**
219
     * Creates all the action for menu and tools bar
220
     * 
221
     */
222
    @SuppressWarnings("serial")
223
    private void createActions() {
224
        setThematiMapFolderAction =
225
            new AbstractAction(translate("Select_Thematic_Map_folder")) {
226

    
227
                public void actionPerformed(ActionEvent e) {
228
                    setThematicMapFolder();
229
                }
230
            };
231

    
232
        showOpenThematicMapDialogAction = new AbstractAction() {
233

    
234
            public void actionPerformed(ActionEvent e) {
235
                showOpenThematicMapDialog();
236
            }
237
        };
238
        showOpenThematicMapDialogAction.setEnabled(false);
239
        setActionIconAndTip(showOpenThematicMapDialogAction, "open",
240
            translate("Open_Thematic_map"));
241

    
242
        setZoomToolAction = new AbstractAction() {
243

    
244
            public void actionPerformed(ActionEvent e) {
245
                setZoomTool();
246
            }
247
        };
248
        setZoomToolAction.setEnabled(false);
249
        setActionIconAndTip(setZoomToolAction, "zoom-in", translate("Zoom"));
250

    
251
        setPanToolAction = new AbstractAction() {
252

    
253
            public void actionPerformed(ActionEvent e) {
254
                setPanTool();
255
            }
256
        };
257
        setPanToolAction.setEnabled(false);
258
        setActionIconAndTip(setPanToolAction, "pan", translate("Pan"));
259

    
260
        zoomAllAction = new AbstractAction() {
261

    
262
            public void actionPerformed(ActionEvent e) {
263
                zoomAll();
264
            }
265
        };
266
        zoomAllAction.setEnabled(false);
267
        setActionIconAndTip(zoomAllAction, "zoom-all", translate("Zoom_all"));
268

    
269
        setMeasuerToolAction = new AbstractAction() {
270

    
271
            public void actionPerformed(ActionEvent e) {
272
                setMeasuerTool();
273
            }
274
        };
275
        setMeasuerToolAction.setEnabled(false);
276
        setActionIconAndTip(setMeasuerToolAction, "rule", translate("measure"));
277

    
278
        setAreaToolAction = new AbstractAction(translate("area")) {
279

    
280
            public void actionPerformed(ActionEvent e) {
281
                setAreaTool();
282
            }
283
        };
284
        setAreaToolAction.setEnabled(false);
285
        setActionIconAndTip(setAreaToolAction, "area", translate("area"));
286

    
287
        setInfoToolAction = new AbstractAction() {
288

    
289
            public void actionPerformed(ActionEvent e) {
290
                setInfoTool();
291
            }
292
        };
293
        setInfoToolAction.setEnabled(false);
294
        setActionIconAndTip(setInfoToolAction, "info", translate("infoByPoint"));
295

    
296
        closeThematicMapAction = new AbstractAction() {
297

    
298
            public void actionPerformed(ActionEvent e) {
299
                closeMap();
300
            }
301
        };
302
        closeThematicMapAction.setEnabled(false);
303
        setActionIconAndTip(closeThematicMapAction, "close",
304
            translate("Close_Map"));
305

    
306
        exitAction = new AbstractAction() {
307

    
308
            public void actionPerformed(ActionEvent e) {
309
                exit();
310
            }
311
        };
312
        setActionIconAndTip(exitAction, "exit", translate("Exit"));
313

    
314
        addInstallUrl =
315
            new AbstractAction(translate("Add_a_ThematicMap_install_URL")) {
316

    
317
                public void actionPerformed(ActionEvent e) {
318
                    addThematicMapUrl();
319
                }
320

    
321
            };
322

    
323
        installThematicMaps = new AbstractAction() {
324

    
325
            public void actionPerformed(ActionEvent e) {
326
                installThematicMap();
327
            }
328

    
329
        };
330
        installThematicMaps.setEnabled(false);
331
        setActionIconAndTip(installThematicMaps, "install-map",
332
            translate("Install_ThematicMaps"));
333

    
334
        showHideTocAction = new AbstractAction() {
335

    
336
            public void actionPerformed(ActionEvent e) {
337
                showHideToc();
338
            }
339

    
340
        };
341
        showHideTocAction.setEnabled(false);
342
        setActionIconAndTip(showHideTocAction, "show-hide-toc",
343
            translate("Show_Hide_Toc"));
344

    
345
    }
346

    
347
    /**
348
     * Show/hides TOC of current view
349
     */
350
    public void showHideToc() {
351
        if (curViewer == null) {
352
            return;
353
        }
354
        curViewer.setTocVisible(!curViewer.isTocVisible());
355
    }
356

    
357
    /**
358
     * Creates the tools bar
359
     */
360
    private void createToolBar() {
361
        Dimension sepSize = new Dimension(20, 5);
362
        JToolBar toolBar = new JToolBar();
363
        toolBar.add(usabManager.createJButton(showOpenThematicMapDialogAction));
364
        toolBar.add(usabManager.createJButton(closeThematicMapAction));
365

    
366
        toolBar.addSeparator(sepSize);
367
        toolBar.add(usabManager.createJButton(showHideTocAction));
368

    
369
        toolBar.addSeparator(sepSize);
370
        toolBar.add(usabManager.createJButton(setPanToolAction));
371
        toolBar.add(usabManager.createJButton(setZoomToolAction));
372
        toolBar.add(usabManager.createJButton(setMeasuerToolAction));
373
        toolBar.add(usabManager.createJButton(setAreaToolAction));
374
        toolBar.add(usabManager.createJButton(setInfoToolAction));
375

    
376
        toolBar.addSeparator(sepSize);
377
        toolBar.add(usabManager.createJButton(zoomAllAction));
378

    
379
        toolBar.add(Box.createHorizontalGlue());
380
        toolBar.add(new JButton(installThematicMaps));
381
        toolBar.addSeparator(sepSize);
382
        toolBar.add(new JButton(exitAction));
383

    
384
        mainFrame.add(toolBar, BorderLayout.PAGE_START);
385
    }
386

    
387
    /**
388
     * Creates the tools bar
389
     */
390
    private void createStatusBar() {
391
        statusBar = new StatusBar();
392
        mapToolsRegistrant.setStatusBar(statusBar);
393
        mainFrame.add(statusBar, BorderLayout.PAGE_END);
394
    }
395

    
396
    /**
397
     * Shows file chooser to select thematic map folder
398
     */
399
    public void setThematicMapFolder() {
400
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
401
        int result =
402
            fileChooser.showDialog(mainFrame, translate("Select_folder"));
403
        if (result != JFileChooser.APPROVE_OPTION) {
404
            return;
405
        }
406
        File folder = fileChooser.getSelectedFile();
407
        List<ThematicMap> maps =
408
            manager.getInstalledMaps(folder.getAbsolutePath());
409
        if (maps.isEmpty()) {
410
            LOG.info("No maps foudn in required folder: {}",
411
                folder.getAbsolutePath());
412
            CONFIRM_DIALOG_OPTION resp =
413
                winManager.showConfirmDialog(mainFrame,
414
                    translate("No_maps_found._Select_this_folder_anyway"),
415
                    translate("Select_Thematic_Map_folder"),
416
                    CONFIRM_DIALOG_TYPE.YES_NO, MESSAGE_DIALOG_TYPE.WARNING);
417
            if (resp != CONFIRM_DIALOG_OPTION.OK_YES) {
418
                return;
419
            }
420
        }
421
        LOG.info("Selected map folder: {}", folder.getAbsolutePath());
422
        try {
423
            manager.setInstallationMapFolder(folder.getAbsolutePath());
424
        } catch (Exception e) {
425
            LOG.error("Error setting thematic map folder", e);
426
            winManager.showMessageDialog(mainFrame,
427
                translate("Select_Thematic_Map_folder"),
428
                translate("Problem_found_setting_Thematic_Map_folder"),
429
                MESSAGE_DIALOG_TYPE.ERROR);
430
        }
431
        updateControls();
432
    }
433

    
434
    /**
435
     * Shows a dialog to select a Thematic Map to open
436
     */
437
    public void showOpenThematicMapDialog() {
438
        if (manager.getInstallationMapFolder() == null) {
439
            LOG.warn("thematic map folder not set on open Thematic Map request");
440
            winManager.showMessageDialog(mainFrame,
441
                translate("Open_Thematic_map"),
442
                translate("Select_a_Thematic_Map_folder_first"),
443
                MESSAGE_DIALOG_TYPE.INFO);
444
            return;
445
        }
446
        OpenThematicMap openThematicMap =
447
            new OpenThematicMap(this, manager.getInstalledMaps());
448
        winManager.showWindow(openThematicMap, translate("Open_Thematic_map"),
449
            MODE.DIALOG);
450
    }
451

    
452
    /**
453
     * Opens a thematic map in main frame
454
     * 
455
     * @param curMap
456
     * @return
457
     */
458
    public boolean openMap(ThematicMap curMap) {
459
        LOG.info("Opening map: {}", curMap.getSourceFilePath());
460
        ThematicMapViewer viewer;
461
        try {
462
            viewer = swingManager.getViewer(curMap);
463
        } catch (Exception e) {
464
            LOG.error("Can't open map", e);
465
            winManager.showMessageDialog(mainFrame,
466
                translate("Open_Thematic_map"),
467
                translate("Problem_found_opening_map_see_log_for_details"),
468
                MESSAGE_DIALOG_TYPE.ERROR);
469
            return false;
470
        }
471
        doCloseMap();
472

    
473
        mainFrame.add(viewer.getSwingComponent(), BorderLayout.CENTER);
474
        curViewer = viewer;
475
        updateControls();
476
        LOG.info("Map opened");
477
        return true;
478
    }
479

    
480
    /**
481
     * Closes the current thematic map
482
     */
483
    public void closeMap() {
484
        doCloseMap();
485
        updateControls();
486
        SwingUtilities.invokeLater(new Runnable() {
487

    
488
            public void run() {
489
                mainFrame.repaint();
490
            }
491
        });
492
    }
493

    
494
    /**
495
     * Removes thematic map view from main frame
496
     */
497
    private void doCloseMap() {
498
        if (curViewer != null) {
499
            mainFrame.remove(curViewer.getSwingComponent());
500
            try {
501
                curViewer.getThematicMap().close();
502
            } catch (IOException e) {
503
                LOG.warn("Exception clossing map", e);
504
            }
505
            statusBar.clear();
506
            curViewer = null;
507
            LOG.info("Closed last map");
508
        }
509
    }
510

    
511
    /**
512
     * Update actions ability and updates main frame content
513
     */
514
    public void updateControls() {
515
        showOpenThematicMapDialogAction.setEnabled(manager
516
            .getInstallationMapFolder() != null);
517
        setZoomToolAction.setEnabled(curViewer != null);
518
        setPanToolAction.setEnabled(curViewer != null);
519
        setMeasuerToolAction.setEnabled(curViewer != null);
520
        setAreaToolAction.setEnabled(curViewer != null);
521
        setInfoToolAction.setEnabled(curViewer != null);
522
        showHideTocAction.setEnabled(curViewer != null);
523
        closeThematicMapAction.setEnabled(curViewer != null);
524
        zoomAllAction.setEnabled(curViewer != null);
525
        installThematicMaps
526
            .setEnabled(manager.getInstallationMapFolder() != null);
527

    
528
        mainFrame.getContentPane().invalidate();
529
        mainFrame.doLayout();
530
        Dimension d = mainFrame.getSize();
531
        d.width--;
532
        mainFrame.setSize(d);
533
        d.width++;
534
        mainFrame.setSize(d);
535

    
536
    }
537

    
538
    /**
539
     * Sets the zoom tool
540
     */
541
    public void setZoomTool() {
542
        if (curViewer != null) {
543
            curViewer.setTool(MapToolsRegistrant.TOOL_ZOOM_ID);
544
            statusBar.setInfo("");
545
            LOG.info("Selected Tool: {}", curViewer.getCurrentToolId());
546
        }
547
    }
548

    
549
    /**
550
     * Sets the pan tool
551
     */
552
    public void setPanTool() {
553
        if (curViewer != null) {
554
            curViewer.setTool(MapToolsRegistrant.TOOL_PAN_ID);
555
            statusBar.setInfo("");
556
            LOG.info("Selected Tool: {}", curViewer.getCurrentToolId());
557
        }
558
    }
559

    
560
    /**
561
     * Sets the pan tool
562
     */
563
    public void setInfoTool() {
564
        if (curViewer != null) {
565
            curViewer.setTool(MapToolsRegistrant.TOOL_INFO_ID);
566
            statusBar.setInfo("");
567
            LOG.info("Selected Tool: {}", curViewer.getCurrentToolId());
568
        }
569
    }
570

    
571
    /**
572
     * Zoom to all thematic map contents
573
     */
574
    public void zoomAll() {
575
        if (curViewer != null) {
576
            curViewer.zoomAll();
577
            LOG.info("Zoom all");
578
        }
579
    }
580

    
581
    /**
582
     * Exit from app
583
     */
584
    public void exit() {
585
        // Close current thematic map
586
        closeMap();
587

    
588
        // clean resources
589
        try {
590
            disposeManager.releaseAll();
591
        } catch (BaseException e) {
592
            LOG.warn("Problems dispossing", e);
593
        }
594

    
595
        try {
596
            Thread.sleep(200);
597
        } catch (InterruptedException e) {
598
            // Do nothing
599
        }
600
        LOG.info("Application exit");
601

    
602
        // exit app
603
        System.exit(0);
604
    }
605

    
606
    public void setMeasuerTool() {
607
        if (curViewer == null) {
608
            return;
609
        }
610
        curViewer.setTool(MapToolsRegistrant.TOOL_MEASURE_ID);
611
        statusBar.setInfo("");
612
        LOG.info("Selected Tool: {}", curViewer.getCurrentToolId());
613
    }
614

    
615
    public void setAreaTool() {
616
        if (curViewer == null) {
617
            return;
618
        }
619
        curViewer.setTool(MapToolsRegistrant.TOOL_AREA_ID);
620
        statusBar.setInfo("");
621
        LOG.info("Selected Tool: {}", curViewer.getCurrentToolId());
622
    }
623

    
624
    public void installThematicMap() {
625
        LOG.info("Opening install dialog");
626
        AbstractInstallerWizard panel;
627
        try {
628
            panel =
629
                swingInstallerManager.createInstallPackageWizard(new File("."),
630
                    new File(manager.getInstallationMapFolder()));
631
        } catch (InstallPackageWizardException ex) {
632
            LOG.error("Error opening installer panel", ex);
633
            winManager.showMessageDialog(translate("Install_Thematic_Map"),
634
                translate("Error_opening_installer_panel"),
635
                MESSAGE_DIALOG_TYPE.ERROR);
636
            return;
637

    
638
        }
639

    
640
        winManager.showWindow(panel, translate("Install_Thematic_Map"),
641
            MODE.DIALOG);
642

    
643
    }
644

    
645
    public void addThematicMapUrl() {
646
        String inUrl =
647
            winManager.showInputStringDialog(translate("Input_the_url"),
648
                translate("Add_a_ThematicMap_install_URL"), "http://",
649
                MESSAGE_DIALOG_TYPE.QUESTION);
650
        URL url;
651
        try {
652
            url = new URL(inUrl);
653
        } catch (Exception ex) {
654
            LOG.error("Invalid URL: ".concat(inUrl).concat("'"), ex);
655
            winManager.showMessageDialog(
656
                translate("Add_install_Thematic_Map_URL"),
657
                translate("Invalid_URL").concat(": '").concat(inUrl)
658
                    .concat("'"), MESSAGE_DIALOG_TYPE.ERROR);
659
            return;
660
        }
661
        swingInstallerManager.addDefaultDownloadURL(url);
662
        LOG.info("Added install URL: '{}'", url);
663
    }
664

    
665
    public String translate(String key) {
666
        if (resourceBundle == null) {
667
            try {
668
                resourceBundle = ResourceBundle.getBundle("mapviewer");
669
            } catch (MissingResourceException e) {
670
                Locale loc = new Locale(Locale.getDefault().getLanguage());
671
                LOG.warn("Can't load translation. Trying with ".concat(loc
672
                    .toString()), e);
673
                try {
674
                    resourceBundle = ResourceBundle.getBundle("mapviewer", loc);
675
                } catch (MissingResourceException e1) {
676
                    LOG.error("Can't load translation", e1);
677
                }
678
            }
679
            return key;
680
        }
681
        try {
682
            return resourceBundle.getString(key);
683
        } catch (MissingResourceException e) {
684
            LOG.warn("Missing Translation: '".concat(key).concat("'"));
685
            return key;
686
        } catch (NullPointerException e) {
687
            LOG.warn("No bundle for translation: '".concat(key).concat("'"));
688
            return key;
689
        } catch (Exception e) {
690
            LOG.warn("Missing Translation: '".concat(key).concat("'"), e);
691
            return key;
692
        }
693
    }
694

    
695
    public ImageIcon getImage(String id) {
696
        String location = "images/".concat(id).concat(".png");
697

    
698
        ImageIcon icon;
699
        try {
700
            icon =
701
                new ImageIcon(getClass().getClassLoader().getResource(location));
702
        } catch (Exception ex) {
703
            LOG.error(
704
                "Can't load icon '".concat(id).concat("' (").concat(location)
705
                    .concat(")"), ex);
706
            if (defaultIcon == null) {
707
                BufferedImage image =
708
                    new BufferedImage(24, 24, BufferedImage.TYPE_INT_RGB);
709
                Graphics g = image.getGraphics();
710
                g.setColor(Color.RED);
711
                g.drawRect(6, 6, 18, 18);
712
                defaultIcon = new ImageIcon(image);
713
            }
714
            icon = defaultIcon;
715
        }
716
        return icon;
717
    }
718

    
719
    /**
720
     * Set icon and tooltip to Action
721
     * 
722
     * @param action
723
     * @param iconId
724
     * @param tooltip
725
     */
726
    public void setActionIconAndTip(AbstractAction action, String iconId,
727
        String tooltip) {
728
        action.putValue(Action.LARGE_ICON_KEY, getImage(iconId));
729
        action.putValue(Action.SHORT_DESCRIPTION, tooltip);
730
    }
731
}