1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Image;
|
---|
9 | import java.awt.Rectangle;
|
---|
10 | import java.awt.Toolkit;
|
---|
11 | import java.awt.event.ComponentEvent;
|
---|
12 | import java.awt.event.ComponentListener;
|
---|
13 | import java.awt.event.WindowAdapter;
|
---|
14 | import java.awt.event.WindowEvent;
|
---|
15 | import java.beans.PropertyChangeListener;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Objects;
|
---|
18 | import java.util.stream.Collectors;
|
---|
19 | import java.util.stream.Stream;
|
---|
20 |
|
---|
21 | import javax.swing.ImageIcon;
|
---|
22 | import javax.swing.JFrame;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.data.UserIdentityManager;
|
---|
26 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
---|
27 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
---|
28 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
---|
29 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
30 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
31 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.LayerStateChangeListener;
|
---|
32 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
33 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 | import 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 | */
|
---|
42 | public 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.requiresSaveToFile()
|
---|
169 | || (editLayer.requiresUploadToServer() && !editLayer.isUploadDiscouraged()));
|
---|
170 | String userInfo = UserIdentityManager.getInstance().getUserName();
|
---|
171 | if (userInfo != null && Config.getPref().getBoolean("draw.show-user", false))
|
---|
172 | userInfo = tr(" ({0})", "@" + userInfo);
|
---|
173 | else
|
---|
174 | userInfo = "";
|
---|
175 | setTitle((dirty ? "* " : "") + tr("Java OpenStreetMap Editor") + userInfo);
|
---|
176 | getRootPane().putClientProperty("Window.documentModified", dirty);
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void onLayerChange(OsmDataLayer layer) {
|
---|
180 | if (layer == MainApplication.getLayerManager().getEditLayer()) {
|
---|
181 | refreshTitle();
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | static final class ExitWindowAdapter extends WindowAdapter {
|
---|
186 | @Override
|
---|
187 | public void windowClosing(final WindowEvent evt) {
|
---|
188 | MainApplication.exitJosm(true, 0, null);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Manages the layer listeners, adds them to every layer.
|
---|
194 | */
|
---|
195 | private final class ManageLayerListeners implements LayerChangeListener {
|
---|
196 | @Override
|
---|
197 | public void layerAdded(LayerAddEvent e) {
|
---|
198 | if (e.getAddedLayer() instanceof OsmDataLayer) {
|
---|
199 | OsmDataLayer osmDataLayer = (OsmDataLayer) e.getAddedLayer();
|
---|
200 | osmDataLayer.addLayerStateChangeListener(updateTitleOnLayerStateChange);
|
---|
201 | }
|
---|
202 | e.getAddedLayer().addPropertyChangeListener(updateTitleOnSaveChange);
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
207 | if (e.getRemovedLayer() instanceof OsmDataLayer) {
|
---|
208 | OsmDataLayer osmDataLayer = (OsmDataLayer) e.getRemovedLayer();
|
---|
209 | osmDataLayer.removeLayerStateChangeListener(updateTitleOnLayerStateChange);
|
---|
210 | }
|
---|
211 | e.getRemovedLayer().removePropertyChangeListener(updateTitleOnSaveChange);
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
216 | // not used
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | private class WindowPositionSizeListener extends WindowAdapter implements ComponentListener {
|
---|
221 | @Override
|
---|
222 | public void windowStateChanged(WindowEvent e) {
|
---|
223 | windowState = e.getNewState();
|
---|
224 | }
|
---|
225 |
|
---|
226 | @Override
|
---|
227 | public void componentHidden(ComponentEvent e) {
|
---|
228 | // Do nothing
|
---|
229 | }
|
---|
230 |
|
---|
231 | @Override
|
---|
232 | public void componentMoved(ComponentEvent e) {
|
---|
233 | handleComponentEvent(e);
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | public void componentResized(ComponentEvent e) {
|
---|
238 | handleComponentEvent(e);
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Override
|
---|
242 | public void componentShown(ComponentEvent e) {
|
---|
243 | // Do nothing
|
---|
244 | }
|
---|
245 |
|
---|
246 | private void handleComponentEvent(ComponentEvent e) {
|
---|
247 | Component c = e.getComponent();
|
---|
248 | if (c instanceof JFrame && c.isVisible()) {
|
---|
249 | if (windowState == JFrame.NORMAL) {
|
---|
250 | geometry = new WindowGeometry((JFrame) c);
|
---|
251 | } else {
|
---|
252 | geometry.fixScreen((JFrame) c);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | }
|
---|