source: josm/trunk/src/org/openstreetmap/josm/gui/MainPanel.java@ 10482

Last change on this file since 10482 was 10444, checked in by Don-vip, 8 years ago

fix #13018 - Use new test rules for AutosaveTaskTest, increase coverage (patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.awt.BorderLayout;
5import java.util.List;
6import java.util.concurrent.CopyOnWriteArrayList;
7
8import javax.swing.JPanel;
9import javax.swing.SwingUtilities;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.actions.mapmode.MapMode;
13import org.openstreetmap.josm.gui.layer.Layer;
14import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
15import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
16import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
17import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
18import org.openstreetmap.josm.gui.layer.MainLayerManager;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20
21/**
22 * This is the content panel inside the {@link MainFrame}. It displays the content the user is working with.
23 * <p>
24 * If there is no active layer, there is no content displayed. As soon as there are active layers, the {@link MapFrame} is displayed.
25 *
26 * @author Michael Zangl
27 * @since 10432
28 */
29public class MainPanel extends JPanel {
30 private MapFrame map;
31 // Needs to be lazy because we need to wait for preferences to set up.
32 private GettingStarted gettingStarted;
33 private final CopyOnWriteArrayList<MapFrameListener> mapFrameListeners = new CopyOnWriteArrayList<>();
34 private final transient MainLayerManager layerManager;
35
36 /**
37 * Create a new main panel
38 * @param layerManager The layer manager to use to display the content.
39 */
40 public MainPanel(MainLayerManager layerManager) {
41 super(new BorderLayout());
42 this.layerManager = layerManager;
43 }
44
45 /**
46 * Update the content of this {@link MainFrame} to either display the map or display the welcome screen.
47 */
48 protected void updateContent() {
49 GuiHelper.assertCallFromEdt();
50 MapFrame old = map;
51 boolean showMap = !layerManager.getLayers().isEmpty();
52 if (old != null && showMap) {
53 // no state change
54 return;
55 }
56
57 // remove old content
58 setVisible(false);
59 removeAll();
60 if (old != null) {
61 old.destroy();
62 }
63
64 // create new content
65 if (showMap) {
66 map = createNewMapFrame();
67 } else {
68 map = null;
69 Main.map = map;
70 add(getGettingStarted(), BorderLayout.CENTER);
71 }
72 setVisible(true);
73
74 if (old == null && !showMap) {
75 // listeners may not be able to handle this...
76 return;
77 }
78
79 // Notify map frame listeners, mostly plugins.
80 for (MapFrameListener listener : mapFrameListeners) {
81 MapView.fireDeprecatedListenerOnAdd = true;
82 listener.mapFrameInitialized(old, map);
83 MapView.fireDeprecatedListenerOnAdd = false;
84 }
85 if (map == null && Main.currentProgressMonitor != null) {
86 Main.currentProgressMonitor.showForegroundDialog();
87 }
88 }
89
90 private MapFrame createNewMapFrame() {
91 MapFrame mapFrame = new MapFrame(null, null);
92 // Required by many components.
93 Main.map = mapFrame;
94
95 mapFrame.fillPanel(this);
96
97 //TODO: Move this to some better place
98 List<Layer> layers = Main.getLayerManager().getLayers();
99 if (!layers.isEmpty()) {
100 mapFrame.selectMapMode((MapMode) mapFrame.getDefaultButtonAction(), layers.get(0));
101 }
102 mapFrame.initializeDialogsPane();
103 mapFrame.setVisible(true);
104 return mapFrame;
105 }
106
107 /**
108 * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes.
109 * <p>
110 * It will fire an initial mapFrameInitialized event
111 * when the MapFrame is present. Otherwise will only fire when the MapFrame is created
112 * or destroyed.
113 * @param listener The MapFrameListener
114 * @return {@code true} if the listeners collection changed as a result of the call.
115 */
116 public boolean addAndFireMapFrameListener(MapFrameListener listener) {
117 boolean changed = addMapFrameListener(listener);
118 if (changed && map != null) {
119 listener.mapFrameInitialized(null, map);
120 }
121 return changed;
122 }
123
124 /**
125 * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes
126 * @param listener The MapFrameListener
127 * @return {@code true} if the listeners collection changed as a result of the call
128 */
129 public boolean addMapFrameListener(MapFrameListener listener) {
130 return listener != null && mapFrameListeners.add(listener);
131 }
132
133 /**
134 * Unregisters the given {@code MapFrameListener} from MapFrame changes
135 * @param listener The MapFrameListener
136 * @return {@code true} if the listeners collection changed as a result of the call
137 */
138 public boolean removeMapFrameListener(MapFrameListener listener) {
139 return listener != null && mapFrameListeners.remove(listener);
140 }
141
142 /**
143 * Gets the {@link GettingStarted} panel.
144 * @return The panel.
145 */
146 public GettingStarted getGettingStarted() {
147 if (gettingStarted == null) {
148 gettingStarted = new GettingStarted();
149 }
150 return gettingStarted;
151 }
152
153 /**
154 * Re-adds the layer listeners. Never call this in production, only needed for testing.
155 */
156 public void reAddListeners() {
157 layerManager.addLayerChangeListener(new LayerChangeListener() {
158 @Override
159 public void layerAdded(LayerAddEvent e) {
160 updateContent();
161 }
162
163 @Override
164 public void layerRemoving(final LayerRemoveEvent e) {
165 // Delay main.map removal until after all listeners are finished.
166 // Some components rely on this and e.g. get the MapView that way.
167 SwingUtilities.invokeLater(new Runnable() {
168 @Override
169 public void run() {
170 updateContent();
171 }
172 });
173 }
174
175 @Override
176 public void layerOrderChanged(LayerOrderChangeEvent e) {
177 // ignored
178 }
179 });
180 }
181}
Note: See TracBrowser for help on using the repository browser.