[1] | 1 | // Licence: GPL
|
---|
| 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
| 4 | import java.awt.BorderLayout;
|
---|
| 5 | import java.awt.Container;
|
---|
| 6 |
|
---|
| 7 | import javax.swing.JFrame;
|
---|
| 8 | import javax.swing.JMenu;
|
---|
| 9 | import javax.swing.JMenuBar;
|
---|
| 10 | import javax.swing.JOptionPane;
|
---|
| 11 | import javax.swing.JPanel;
|
---|
[19] | 12 | import javax.swing.JSeparator;
|
---|
[1] | 13 | import javax.swing.JToolBar;
|
---|
| 14 | import javax.swing.UIManager;
|
---|
| 15 |
|
---|
[19] | 16 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
[1] | 17 | import org.openstreetmap.josm.actions.ExitAction;
|
---|
| 18 | import org.openstreetmap.josm.actions.OpenGpxAction;
|
---|
[18] | 19 | import org.openstreetmap.josm.actions.OpenOsmServerAction;
|
---|
[1] | 20 | import org.openstreetmap.josm.actions.PreferencesAction;
|
---|
| 21 | import org.openstreetmap.josm.actions.SaveGpxAction;
|
---|
| 22 | import org.openstreetmap.josm.data.Preferences;
|
---|
| 23 | import org.openstreetmap.josm.data.Preferences.PreferencesException;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Main window class consisting of the mainframe MDI application.
|
---|
| 27 | *
|
---|
| 28 | * @author imi
|
---|
| 29 | */
|
---|
| 30 | public class Main extends JFrame {
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Global application window. Use this as JOPtionPane-parent to center on application.
|
---|
| 34 | */
|
---|
| 35 | public static Main main;
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Global application preferences
|
---|
| 39 | */
|
---|
[17] | 40 | public final static Preferences pref = new Preferences();
|
---|
[1] | 41 |
|
---|
| 42 | /**
|
---|
| 43 | * The main panel.
|
---|
| 44 | */
|
---|
| 45 | private Container panel;
|
---|
| 46 | /**
|
---|
| 47 | * The name of the current loaded mapFrame
|
---|
| 48 | */
|
---|
| 49 | private String name;
|
---|
| 50 | /**
|
---|
| 51 | * The mapFrame currently loaded.
|
---|
| 52 | */
|
---|
| 53 | private MapFrame mapFrame;
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Construct an main frame, ready sized and operating. Does not
|
---|
| 57 | * display the frame.
|
---|
| 58 | */
|
---|
| 59 | public Main() {
|
---|
| 60 | super("Java Open Street Map - Editor");
|
---|
| 61 | setLayout(new BorderLayout());
|
---|
| 62 | panel = new JPanel(new BorderLayout());
|
---|
| 63 | getContentPane().add(panel, BorderLayout.CENTER);
|
---|
| 64 | setSize(1000,740); // some strange default size
|
---|
| 65 | setExtendedState(MAXIMIZED_BOTH); // some platform are able to maximize
|
---|
| 66 |
|
---|
[7] | 67 | // creating actions
|
---|
[18] | 68 | OpenOsmServerAction openServerAction = new OpenOsmServerAction();
|
---|
[7] | 69 | OpenGpxAction openGpxAction = new OpenGpxAction();
|
---|
| 70 | SaveGpxAction saveGpxAction = new SaveGpxAction();
|
---|
| 71 | ExitAction exitAction = new ExitAction();
|
---|
| 72 | PreferencesAction preferencesAction = new PreferencesAction();
|
---|
[19] | 73 | AboutAction aboutAction = new AboutAction();
|
---|
[7] | 74 |
|
---|
[1] | 75 | // creating menu
|
---|
| 76 | JMenuBar mainMenu = new JMenuBar();
|
---|
| 77 | setJMenuBar(mainMenu);
|
---|
| 78 |
|
---|
| 79 | JMenu fileMenu = new JMenu("Files");
|
---|
| 80 | fileMenu.setMnemonic('F');
|
---|
[7] | 81 | fileMenu.add(openGpxAction);
|
---|
| 82 | fileMenu.add(saveGpxAction);
|
---|
[1] | 83 | fileMenu.addSeparator();
|
---|
[7] | 84 | fileMenu.add(exitAction);
|
---|
[1] | 85 | mainMenu.add(fileMenu);
|
---|
| 86 |
|
---|
[18] | 87 | JMenu connectionMenu = new JMenu("Connection");
|
---|
| 88 | connectionMenu.setMnemonic('C');
|
---|
| 89 | connectionMenu.add(openServerAction);
|
---|
| 90 | mainMenu.add(connectionMenu);
|
---|
| 91 |
|
---|
[1] | 92 | JMenu editMenu = new JMenu("Edit");
|
---|
| 93 | editMenu.setMnemonic('E');
|
---|
[7] | 94 | editMenu.add(preferencesAction);
|
---|
[1] | 95 | mainMenu.add(editMenu);
|
---|
[19] | 96 |
|
---|
| 97 | mainMenu.add(new JSeparator());
|
---|
| 98 | JMenu helpMenu = new JMenu("Help");
|
---|
| 99 | helpMenu.setMnemonic('H');
|
---|
| 100 | helpMenu.add(aboutAction);
|
---|
| 101 | mainMenu.add(helpMenu);
|
---|
[1] | 102 |
|
---|
| 103 | // creating toolbar
|
---|
| 104 | JToolBar toolBar = new JToolBar();
|
---|
| 105 | toolBar.setFloatable(false);
|
---|
[18] | 106 | toolBar.add(openServerAction);
|
---|
[7] | 107 | toolBar.add(openGpxAction);
|
---|
| 108 | toolBar.add(saveGpxAction);
|
---|
[1] | 109 | toolBar.addSeparator();
|
---|
[7] | 110 | toolBar.add(preferencesAction);
|
---|
[1] | 111 |
|
---|
| 112 | getContentPane().add(toolBar, BorderLayout.NORTH);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * Main application Startup
|
---|
| 117 | * @param args No parameters accepted.
|
---|
| 118 | */
|
---|
| 119 | public static void main(String[] args) {
|
---|
[16] | 120 | setupUiDefaults();
|
---|
| 121 |
|
---|
[1] | 122 | // load preferences
|
---|
| 123 | String errMsg = null;
|
---|
| 124 | try {
|
---|
| 125 | pref.load();
|
---|
| 126 | } catch (PreferencesException e1) {
|
---|
| 127 | e1.printStackTrace();
|
---|
[20] | 128 | errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferencesDir()+"preferences'.";
|
---|
[1] | 129 | try {
|
---|
| 130 | pref.save();
|
---|
| 131 | } catch (PreferencesException e) {
|
---|
| 132 | e.printStackTrace();
|
---|
| 133 | errMsg = "Preferences could not be loaded. Reverting to default.";
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | if (errMsg != null)
|
---|
| 137 | JOptionPane.showMessageDialog(null, errMsg);
|
---|
| 138 |
|
---|
| 139 | try {
|
---|
[16] | 140 | UIManager.setLookAndFeel(pref.laf.getClassName());
|
---|
[1] | 141 | } catch (Exception e) {
|
---|
| 142 | e.printStackTrace();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | main = new Main();
|
---|
| 146 | main.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
---|
| 147 | main.setVisible(true);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
[16] | 152 | * Set the main's mapframe. If a changed old mapFrame is already set,
|
---|
| 153 | * ask the user whether he want to save, discard or abort. If the user
|
---|
[1] | 154 | * aborts, nothing happens.
|
---|
| 155 | */
|
---|
[16] | 156 | public void setMapFrame(String name, MapFrame mapFrame) {
|
---|
[1] | 157 | //TODO: Check for changes and ask user
|
---|
| 158 | this.name = name;
|
---|
[17] | 159 | if (this.mapFrame != null)
|
---|
| 160 | this.mapFrame.setVisible(false);
|
---|
[1] | 161 | this.mapFrame = mapFrame;
|
---|
[7] | 162 | panel.setVisible(false);
|
---|
[1] | 163 | panel.removeAll();
|
---|
[17] | 164 | if (mapFrame != null) {
|
---|
| 165 | mapFrame.fillPanel(panel);
|
---|
| 166 | panel.setVisible(true);
|
---|
| 167 | mapFrame.setVisible(true);
|
---|
| 168 | }
|
---|
[1] | 169 | }
|
---|
| 170 | /**
|
---|
| 171 | * @return Returns the name.
|
---|
| 172 | */
|
---|
| 173 | public String getNameOfLoadedMapFrame() {
|
---|
| 174 | return name;
|
---|
| 175 | }
|
---|
| 176 | /**
|
---|
| 177 | * @return Returns the mapFrame.
|
---|
| 178 | */
|
---|
| 179 | public MapFrame getMapFrame() {
|
---|
| 180 | return mapFrame;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[16] | 183 |
|
---|
| 184 | /**
|
---|
| 185 | * Sets some icons to the ui.
|
---|
| 186 | */
|
---|
| 187 | private static void setupUiDefaults() {
|
---|
[17] | 188 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
[16] | 189 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
[17] | 190 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
[16] | 191 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
| 192 | }
|
---|
[1] | 193 | }
|
---|