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