[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;
|
---|
| 12 | import javax.swing.JToolBar;
|
---|
| 13 | import javax.swing.UIManager;
|
---|
| 14 |
|
---|
| 15 | import org.openstreetmap.josm.actions.ExitAction;
|
---|
| 16 | import org.openstreetmap.josm.actions.OpenGpxAction;
|
---|
| 17 | import org.openstreetmap.josm.actions.PreferencesAction;
|
---|
| 18 | import org.openstreetmap.josm.actions.SaveGpxAction;
|
---|
| 19 | import org.openstreetmap.josm.data.Preferences;
|
---|
| 20 | import org.openstreetmap.josm.data.Preferences.PreferencesException;
|
---|
[15] | 21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 22 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
[1] | 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Main window class consisting of the mainframe MDI application.
|
---|
| 26 | *
|
---|
| 27 | * @author imi
|
---|
| 28 | */
|
---|
| 29 | public class Main extends JFrame {
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Global application window. Use this as JOPtionPane-parent to center on application.
|
---|
| 33 | */
|
---|
| 34 | public static Main main;
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Global application preferences
|
---|
| 38 | */
|
---|
| 39 | public static Preferences pref = new Preferences();
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * The main panel.
|
---|
| 43 | */
|
---|
| 44 | private Container panel;
|
---|
| 45 | /**
|
---|
| 46 | * The name of the current loaded mapFrame
|
---|
| 47 | */
|
---|
| 48 | private String name;
|
---|
| 49 | /**
|
---|
| 50 | * The mapFrame currently loaded.
|
---|
| 51 | */
|
---|
| 52 | private MapFrame mapFrame;
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Construct an main frame, ready sized and operating. Does not
|
---|
| 56 | * display the frame.
|
---|
| 57 | */
|
---|
| 58 | public Main() {
|
---|
| 59 | super("Java Open Street Map - Editor");
|
---|
| 60 | setLayout(new BorderLayout());
|
---|
| 61 | panel = new JPanel(new BorderLayout());
|
---|
| 62 | getContentPane().add(panel, BorderLayout.CENTER);
|
---|
| 63 | setSize(1000,740); // some strange default size
|
---|
| 64 | setExtendedState(MAXIMIZED_BOTH); // some platform are able to maximize
|
---|
| 65 |
|
---|
[7] | 66 | // creating actions
|
---|
| 67 | OpenGpxAction openGpxAction = new OpenGpxAction();
|
---|
| 68 | SaveGpxAction saveGpxAction = new SaveGpxAction();
|
---|
| 69 | ExitAction exitAction = new ExitAction();
|
---|
| 70 | PreferencesAction preferencesAction = new PreferencesAction();
|
---|
| 71 |
|
---|
[1] | 72 | // creating menu
|
---|
| 73 | JMenuBar mainMenu = new JMenuBar();
|
---|
| 74 | setJMenuBar(mainMenu);
|
---|
| 75 |
|
---|
| 76 | JMenu fileMenu = new JMenu("Files");
|
---|
| 77 | fileMenu.setMnemonic('F');
|
---|
[7] | 78 | fileMenu.add(openGpxAction);
|
---|
| 79 | fileMenu.add(saveGpxAction);
|
---|
[1] | 80 | fileMenu.addSeparator();
|
---|
[7] | 81 | fileMenu.add(exitAction);
|
---|
[1] | 82 | mainMenu.add(fileMenu);
|
---|
| 83 |
|
---|
| 84 | JMenu editMenu = new JMenu("Edit");
|
---|
| 85 | editMenu.setMnemonic('E');
|
---|
[7] | 86 | editMenu.add(preferencesAction);
|
---|
[1] | 87 | mainMenu.add(editMenu);
|
---|
| 88 |
|
---|
| 89 | // creating toolbar
|
---|
| 90 | JToolBar toolBar = new JToolBar();
|
---|
| 91 | toolBar.setFloatable(false);
|
---|
[7] | 92 | toolBar.add(openGpxAction);
|
---|
| 93 | toolBar.add(saveGpxAction);
|
---|
[1] | 94 | toolBar.addSeparator();
|
---|
[7] | 95 | toolBar.add(preferencesAction);
|
---|
[1] | 96 |
|
---|
| 97 | getContentPane().add(toolBar, BorderLayout.NORTH);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Main application Startup
|
---|
| 102 | * @param args No parameters accepted.
|
---|
| 103 | */
|
---|
| 104 | public static void main(String[] args) {
|
---|
| 105 | // load preferences
|
---|
| 106 | String errMsg = null;
|
---|
| 107 | try {
|
---|
| 108 | pref.load();
|
---|
| 109 | } catch (PreferencesException e1) {
|
---|
| 110 | e1.printStackTrace();
|
---|
| 111 | errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferencesFile()+"'.";
|
---|
| 112 | try {
|
---|
| 113 | pref.save();
|
---|
| 114 | } catch (PreferencesException e) {
|
---|
| 115 | e.printStackTrace();
|
---|
| 116 | errMsg = "Preferences could not be loaded. Reverting to default.";
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | if (errMsg != null)
|
---|
| 120 | JOptionPane.showMessageDialog(null, errMsg);
|
---|
| 121 |
|
---|
| 122 | try {
|
---|
[15] | 123 | UIManager.setLookAndFeel(pref.getLaf().getClassName());
|
---|
[1] | 124 | } catch (Exception e) {
|
---|
| 125 | e.printStackTrace();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | main = new Main();
|
---|
| 129 | main.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
---|
| 130 | main.setVisible(true);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
[15] | 135 | * Create and set the main's mapframe. If a changed old mapFrame is already
|
---|
| 136 | * set, ask the user whether he want to save, discard or abort. If the user
|
---|
[1] | 137 | * aborts, nothing happens.
|
---|
| 138 | */
|
---|
[15] | 139 | public void setMapFrame(String name, DataSet dataSet) {
|
---|
[1] | 140 | //TODO: Check for changes and ask user
|
---|
[15] | 141 | MapFrame mapFrame = new MapFrame(dataSet);
|
---|
[1] | 142 | this.name = name;
|
---|
| 143 | this.mapFrame = mapFrame;
|
---|
[7] | 144 | panel.setVisible(false);
|
---|
[1] | 145 | panel.removeAll();
|
---|
| 146 | panel.add(mapFrame, BorderLayout.CENTER);
|
---|
[9] | 147 | panel.add(mapFrame.toolBarActions, BorderLayout.WEST);
|
---|
| 148 | panel.add(mapFrame.statusLine, BorderLayout.SOUTH);
|
---|
[15] | 149 | for (Projection p : Preferences.allProjections)
|
---|
| 150 | p.init(dataSet);
|
---|
| 151 | mapFrame.layer.initDataSet();
|
---|
[7] | 152 | panel.setVisible(true);
|
---|
[1] | 153 | }
|
---|
| 154 | /**
|
---|
| 155 | * @return Returns the name.
|
---|
| 156 | */
|
---|
| 157 | public String getNameOfLoadedMapFrame() {
|
---|
| 158 | return name;
|
---|
| 159 | }
|
---|
| 160 | /**
|
---|
| 161 | * @return Returns the mapFrame.
|
---|
| 162 | */
|
---|
| 163 | public MapFrame getMapFrame() {
|
---|
| 164 | return mapFrame;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | }
|
---|