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