root/src/org/openstreetmap/josm/gui/Main.java @ 1

Revision 1, 3.8 KB (checked in by imi, 5 years ago)

wiki 19:37, 27. Sep 2005

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