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

Last change on this file since 17626 was 17626, checked in by simon04, 4 years ago

fix #12326 - Layer list: "dirty" and "modified" indicators for data layers

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