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