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