source: josm/trunk/src/org/openstreetmap/josm/gui/MainFrame.java@ 12328

Last change on this file since 12328 was 12128, checked in by Don-vip, 7 years ago

see #14741 - rework initialization of Main.contentPanePrivate

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Image;
9import java.awt.Rectangle;
10import java.awt.Toolkit;
11import java.awt.event.ComponentEvent;
12import java.awt.event.ComponentListener;
13import java.awt.event.WindowAdapter;
14import java.awt.event.WindowEvent;
15import java.beans.PropertyChangeListener;
16import java.util.LinkedList;
17import java.util.List;
18
19import javax.swing.JFrame;
20import javax.swing.JPanel;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
24import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
25import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
26import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
27import org.openstreetmap.josm.gui.layer.OsmDataLayer;
28import org.openstreetmap.josm.gui.layer.OsmDataLayer.LayerStateChangeListener;
29import org.openstreetmap.josm.tools.ImageProvider;
30import org.openstreetmap.josm.tools.WindowGeometry;
31
32/**
33 * This is the JOSM main window. It updates it's title.
34 * @author Michael Zangl
35 * @since 10340
36 */
37public class MainFrame extends JFrame {
38 private final transient LayerStateChangeListener updateTitleOnLayerStateChange = (layer, newValue) -> onLayerChange(layer);
39
40 private final transient PropertyChangeListener updateTitleOnSaveChange = evt -> {
41 if (evt.getPropertyName().equals(OsmDataLayer.REQUIRES_SAVE_TO_DISK_PROP)
42 || evt.getPropertyName().equals(OsmDataLayer.REQUIRES_UPLOAD_TO_SERVER_PROP)) {
43 OsmDataLayer layer = (OsmDataLayer) evt.getSource();
44 onLayerChange(layer);
45 }
46 };
47
48 protected transient WindowGeometry geometry;
49 protected int windowState = JFrame.NORMAL;
50 private final MainPanel panel;
51 private MainMenu menu;
52
53 /**
54 * Create a new main window.
55 */
56 public MainFrame() {
57 this(new WindowGeometry(new Rectangle(10, 10, 500, 500)));
58 }
59
60 /**
61 * Create a new main window. The parameter will be removed in the future.
62 * @param geometry The inital geometry to use.
63 * @since 12127
64 */
65 public MainFrame(WindowGeometry geometry) {
66 super();
67 this.geometry = geometry;
68 this.panel = new MainPanel(Main.getLayerManager());
69 setContentPane(new JPanel(new BorderLayout()));
70 }
71
72 /**
73 * Initializes the content of the window and get the current status panel.
74 */
75 public void initialize() {
76 menu = new MainMenu();
77 addComponentListener(new WindowPositionSizeListener());
78 addWindowStateListener(new WindowPositionSizeListener());
79
80 setJMenuBar(menu);
81 geometry.applySafe(this);
82 List<Image> l = new LinkedList<>();
83 l.add(ImageProvider.get("logo_16x16x32").getImage());
84 l.add(ImageProvider.get("logo_16x16x8").getImage());
85 l.add(ImageProvider.get("logo_32x32x32").getImage());
86 l.add(ImageProvider.get("logo_32x32x8").getImage());
87 l.add(ImageProvider.get("logo_48x48x32").getImage());
88 l.add(ImageProvider.get("logo_48x48x8").getImage());
89 l.add(ImageProvider.get("logo").getImage());
90 setIconImages(l);
91 addWindowListener(new ExitWindowAdapter());
92 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
93
94 // This listener is never removed, since the main frame exists forever.
95 Main.getLayerManager().addActiveLayerChangeListener(e -> refreshTitle());
96 Main.getLayerManager().addAndFireLayerChangeListener(new ManageLayerListeners());
97
98 refreshTitle();
99
100 getContentPane().add(panel, BorderLayout.CENTER);
101 menu.initialize();
102 }
103
104 /**
105 * Stores the current state of the main frame.
106 */
107 public void storeState() {
108 if (geometry != null) {
109 geometry.remember("gui.geometry");
110 }
111 Main.pref.put("gui.maximized", (windowState & JFrame.MAXIMIZED_BOTH) != 0);
112 }
113
114 /**
115 * Gets the main menu used for this window.
116 * @return The main menu.
117 * @throws IllegalStateException if the main frame has not been initialized yet
118 * @see #initialize
119 */
120 public MainMenu getMenu() {
121 if (menu == null) {
122 throw new IllegalStateException("Not initialized.");
123 }
124 return menu;
125 }
126
127 /**
128 * Gets the main panel.
129 * @return The main panel.
130 * @since 12125
131 */
132 public MainPanel getPanel() {
133 return panel;
134 }
135
136 /**
137 * Sets this frame to be maximized.
138 * @param maximized <code>true</code> if the window should be maximized.
139 */
140 public void setMaximized(boolean maximized) {
141 if (maximized) {
142 if (Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
143 windowState = JFrame.MAXIMIZED_BOTH;
144 setExtendedState(windowState);
145 } else {
146 Main.debug("Main window: maximizing not supported");
147 }
148 } else {
149 throw new UnsupportedOperationException("Unimplemented.");
150 }
151 }
152
153 /**
154 * Update the title of the window to reflect the current content.
155 */
156 public void refreshTitle() {
157 OsmDataLayer editLayer = Main.getLayerManager().getEditLayer();
158 boolean dirty = editLayer != null && (editLayer.requiresSaveToFile()
159 || (editLayer.requiresUploadToServer() && !editLayer.isUploadDiscouraged()));
160 setTitle((dirty ? "* " : "") + tr("Java OpenStreetMap Editor"));
161 getRootPane().putClientProperty("Window.documentModified", dirty);
162 }
163
164 private void onLayerChange(OsmDataLayer layer) {
165 if (layer == Main.getLayerManager().getEditLayer()) {
166 refreshTitle();
167 }
168 }
169
170 static final class ExitWindowAdapter extends WindowAdapter {
171 @Override
172 public void windowClosing(final WindowEvent evt) {
173 Main.exitJosm(true, 0, null);
174 }
175 }
176
177 /**
178 * Manages the layer listeners, adds them to every layer.
179 */
180 private final class ManageLayerListeners implements LayerChangeListener {
181 @Override
182 public void layerAdded(LayerAddEvent e) {
183 if (e.getAddedLayer() instanceof OsmDataLayer) {
184 OsmDataLayer osmDataLayer = (OsmDataLayer) e.getAddedLayer();
185 osmDataLayer.addLayerStateChangeListener(updateTitleOnLayerStateChange);
186 }
187 e.getAddedLayer().addPropertyChangeListener(updateTitleOnSaveChange);
188 }
189
190 @Override
191 public void layerRemoving(LayerRemoveEvent e) {
192 if (e.getRemovedLayer() instanceof OsmDataLayer) {
193 OsmDataLayer osmDataLayer = (OsmDataLayer) e.getRemovedLayer();
194 osmDataLayer.removeLayerStateChangeListener(updateTitleOnLayerStateChange);
195 }
196 e.getRemovedLayer().removePropertyChangeListener(updateTitleOnSaveChange);
197 }
198
199 @Override
200 public void layerOrderChanged(LayerOrderChangeEvent e) {
201 // not used
202 }
203 }
204
205 private class WindowPositionSizeListener extends WindowAdapter implements ComponentListener {
206 @Override
207 public void windowStateChanged(WindowEvent e) {
208 windowState = e.getNewState();
209 }
210
211 @Override
212 public void componentHidden(ComponentEvent e) {
213 // Do nothing
214 }
215
216 @Override
217 public void componentMoved(ComponentEvent e) {
218 handleComponentEvent(e);
219 }
220
221 @Override
222 public void componentResized(ComponentEvent e) {
223 handleComponentEvent(e);
224 }
225
226 @Override
227 public void componentShown(ComponentEvent e) {
228 // Do nothing
229 }
230
231 private void handleComponentEvent(ComponentEvent e) {
232 Component c = e.getComponent();
233 if (c instanceof JFrame && c.isVisible()) {
234 if (windowState == JFrame.NORMAL) {
235 geometry = new WindowGeometry((JFrame) c);
236 } else {
237 geometry.fixScreen((JFrame) c);
238 }
239 }
240 }
241 }
242
243}
Note: See TracBrowser for help on using the repository browser.