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