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.Container;
|
---|
9 | import java.awt.Image;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 | import java.awt.Toolkit;
|
---|
12 | import java.awt.event.ComponentEvent;
|
---|
13 | import java.awt.event.ComponentListener;
|
---|
14 | import java.awt.event.WindowAdapter;
|
---|
15 | import java.awt.event.WindowEvent;
|
---|
16 | import java.beans.PropertyChangeListener;
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | import javax.swing.JFrame;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
---|
25 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
---|
26 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
---|
27 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
28 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
29 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.LayerStateChangeListener;
|
---|
30 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
31 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * This is the JOSM main window. It updates it's title.
|
---|
35 | * @author Michael Zangl
|
---|
36 | * @since 10340
|
---|
37 | */
|
---|
38 | public class MainFrame extends JFrame {
|
---|
39 | private final transient LayerStateChangeListener updateTitleOnLayerStateChange = (layer, newValue) -> onLayerChange(layer);
|
---|
40 |
|
---|
41 | private final transient PropertyChangeListener updateTitleOnSaveChange = evt -> {
|
---|
42 | if (evt.getPropertyName().equals(OsmDataLayer.REQUIRES_SAVE_TO_DISK_PROP)
|
---|
43 | || evt.getPropertyName().equals(OsmDataLayer.REQUIRES_UPLOAD_TO_SERVER_PROP)) {
|
---|
44 | OsmDataLayer layer = (OsmDataLayer) evt.getSource();
|
---|
45 | onLayerChange(layer);
|
---|
46 | }
|
---|
47 | };
|
---|
48 |
|
---|
49 | protected transient WindowGeometry geometry;
|
---|
50 | protected int windowState = JFrame.NORMAL;
|
---|
51 | private MainMenu menu;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Create a new main window.
|
---|
55 | */
|
---|
56 | public MainFrame() {
|
---|
57 | this(new JPanel(), new MainPanel(Main.getLayerManager()), new WindowGeometry(new Rectangle(10, 10, 500, 500)));
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Create a new main window. The parameters will be removed in the future.
|
---|
62 | * @param contentPanePrivate The content
|
---|
63 | * @param mainPanel The main panel.
|
---|
64 | * @param geometry The inital geometry to use.
|
---|
65 | */
|
---|
66 | public MainFrame(Container contentPanePrivate, MainPanel mainPanel, WindowGeometry geometry) {
|
---|
67 | super();
|
---|
68 | this.geometry = geometry;
|
---|
69 | setContentPane(contentPanePrivate);
|
---|
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 WindowAdapter() {
|
---|
92 | @Override
|
---|
93 | public void windowClosing(final WindowEvent evt) {
|
---|
94 | Main.exitJosm(true, 0);
|
---|
95 | }
|
---|
96 | });
|
---|
97 | setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
---|
98 |
|
---|
99 | // This listener is never removed, since the main frame exists forever.
|
---|
100 | Main.getLayerManager().addActiveLayerChangeListener(e -> refreshTitle());
|
---|
101 | Main.getLayerManager().addLayerChangeListener(new ManageLayerListeners(), true);
|
---|
102 |
|
---|
103 | refreshTitle();
|
---|
104 |
|
---|
105 | getContentPane().add(Main.panel, BorderLayout.CENTER);
|
---|
106 | menu.initialize();
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Stores the current state of the main frame.
|
---|
111 | */
|
---|
112 | public void storeState() {
|
---|
113 | if (geometry != null) {
|
---|
114 | geometry.remember("gui.geometry");
|
---|
115 | }
|
---|
116 | Main.pref.put("gui.maximized", (windowState & JFrame.MAXIMIZED_BOTH) != 0);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Gets the main menu used for this window.
|
---|
121 | * @return The main menu.
|
---|
122 | */
|
---|
123 | public MainMenu getMenu() {
|
---|
124 | if (menu == null) {
|
---|
125 | throw new IllegalStateException("Not initialized.");
|
---|
126 | }
|
---|
127 | return menu;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Sets this frame to be maximized.
|
---|
132 | * @param maximized <code>true</code> if the window should be maximized.
|
---|
133 | */
|
---|
134 | public void setMaximized(boolean maximized) {
|
---|
135 | if (maximized) {
|
---|
136 | if (Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
|
---|
137 | windowState = JFrame.MAXIMIZED_BOTH;
|
---|
138 | setExtendedState(windowState);
|
---|
139 | } else {
|
---|
140 | Main.debug("Main window: maximizing not supported");
|
---|
141 | }
|
---|
142 | } else {
|
---|
143 | throw new UnsupportedOperationException("Unimplemented.");
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Update the title of the window to reflect the current content.
|
---|
149 | */
|
---|
150 | public void refreshTitle() {
|
---|
151 | OsmDataLayer editLayer = Main.getLayerManager().getEditLayer();
|
---|
152 | boolean dirty = editLayer != null && (editLayer.requiresSaveToFile()
|
---|
153 | || (editLayer.requiresUploadToServer() && !editLayer.isUploadDiscouraged()));
|
---|
154 | setTitle((dirty ? "* " : "") + tr("Java OpenStreetMap Editor"));
|
---|
155 | getRootPane().putClientProperty("Window.documentModified", dirty);
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void onLayerChange(OsmDataLayer layer) {
|
---|
159 | if (layer == Main.getLayerManager().getEditLayer()) {
|
---|
160 | refreshTitle();
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Manages the layer listeners, adds them to every layer.
|
---|
166 | */
|
---|
167 | private final class ManageLayerListeners implements LayerChangeListener {
|
---|
168 | @Override
|
---|
169 | public void layerAdded(LayerAddEvent e) {
|
---|
170 | if (e.getAddedLayer() instanceof OsmDataLayer) {
|
---|
171 | OsmDataLayer osmDataLayer = (OsmDataLayer) e.getAddedLayer();
|
---|
172 | osmDataLayer.addLayerStateChangeListener(updateTitleOnLayerStateChange);
|
---|
173 | }
|
---|
174 | e.getAddedLayer().addPropertyChangeListener(updateTitleOnSaveChange);
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
179 | if (e.getRemovedLayer() instanceof OsmDataLayer) {
|
---|
180 | OsmDataLayer osmDataLayer = (OsmDataLayer) e.getRemovedLayer();
|
---|
181 | osmDataLayer.removeLayerStateChangeListener(updateTitleOnLayerStateChange);
|
---|
182 | }
|
---|
183 | e.getRemovedLayer().removePropertyChangeListener(updateTitleOnSaveChange);
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
188 | // not used
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | private class WindowPositionSizeListener extends WindowAdapter implements ComponentListener {
|
---|
193 | @Override
|
---|
194 | public void windowStateChanged(WindowEvent e) {
|
---|
195 | windowState = e.getNewState();
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public void componentHidden(ComponentEvent e) {
|
---|
200 | // Do nothing
|
---|
201 | }
|
---|
202 |
|
---|
203 | @Override
|
---|
204 | public void componentMoved(ComponentEvent e) {
|
---|
205 | handleComponentEvent(e);
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public void componentResized(ComponentEvent e) {
|
---|
210 | handleComponentEvent(e);
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public void componentShown(ComponentEvent e) {
|
---|
215 | // Do nothing
|
---|
216 | }
|
---|
217 |
|
---|
218 | private void handleComponentEvent(ComponentEvent e) {
|
---|
219 | Component c = e.getComponent();
|
---|
220 | if (c instanceof JFrame && c.isVisible()) {
|
---|
221 | if (windowState == JFrame.NORMAL) {
|
---|
222 | geometry = new WindowGeometry((JFrame) c);
|
---|
223 | } else {
|
---|
224 | geometry.fixScreen((JFrame) c);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | }
|
---|