From: <aki...@us...> - 2006-12-22 19:43:29
|
Revision: 1203 http://svn.sourceforge.net/gridarta/?rev=1203&view=rev Author: akirschbaum Date: 2006-12-22 11:43:29 -0800 (Fri, 22 Dec 2006) Log Message: ----------- Introduce MainControlListener interface to listen for changes in MainControl. Modified Paths: -------------- trunk/crossfire/src/cfeditor/CMainControl.java trunk/daimonin/src/daieditor/CMainControl.java trunk/src/app/net/sf/gridarta/MainControl.java Added Paths: ----------- trunk/src/app/net/sf/gridarta/MainControlListener.java Modified: trunk/crossfire/src/cfeditor/CMainControl.java =================================================================== --- trunk/crossfire/src/cfeditor/CMainControl.java 2006-12-22 19:26:11 UTC (rev 1202) +++ trunk/crossfire/src/cfeditor/CMainControl.java 2006-12-22 19:43:29 UTC (rev 1203) @@ -53,8 +53,10 @@ import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; +import javax.swing.event.EventListenerList; import javax.swing.filechooser.FileFilter; import net.sf.gridarta.MainControl; +import net.sf.gridarta.MainControlListener; import net.sf.gridarta.gameobject.Collectable; import net.sf.gridarta.gameobject.Collector; import net.sf.gridarta.gui.HideFileFilterProxy; @@ -188,6 +190,9 @@ private final MapCursorControl mapCursorControl; + /** The MainControlListeners to inform of changes. */ + private EventListenerList listenerList = new EventListenerList(); + /** Actions used by this instance. */ private final MainActions mainActions = new MainActions(this); @@ -1461,15 +1466,42 @@ */ public void setCurrentLevel(final MapControl map) { + if (currentMap == map) { + return; + } + currentMap = map; refreshMenusAndToolbars(); mapCursorControl.refreshMenus(); //CMainStatusbar.getInstance().setLevelInfo(level); - mainActions.currentMapChanged(); + fireCurrentMapChangedEvent(); } + /** + * Notify all listeners about a changed current map. + */ + private void fireCurrentMapChangedEvent() { + final Object[] listeners = listenerList.getListenerList(); + for (int i = listeners.length - 2; i >= 0; i -= 2) { + //noinspection ObjectEquality + if (listeners[i] == MainControlListener.class) { + ((MainControlListener) listeners[i + 1]).currentMapChanged(currentMap); + } + } + } + + /** {@inheritDoc} */ + public void addMainControlListener(@NotNull final MainControlListener listener) { + listenerList.add(MainControlListener.class, listener); + } + + /** {@inheritDoc} */ + public void removeMainControlListener(final MainControlListener listener) { + listenerList.remove(MainControlListener.class, listener); + } + /** Invoked when user wants to revert the current map to previously saved state. */ public void revert() { final MapControl modmap = currentMap; // "modified map" to be reverted @@ -1750,7 +1782,7 @@ /** * Encapsulates actions and related functions. */ - public static final class MainActions implements MapCursorListener, MapGridListener { + public static final class MainActions implements MainControlListener, MapCursorListener, MapGridListener { /** The MainControl. */ private final CMainControl mainControl; @@ -1795,6 +1827,8 @@ */ public MainActions(@NotNull final CMainControl mainControl) { this.mainControl = mainControl; + + mainControl.addMainControlListener(this); } /** @@ -1814,10 +1848,8 @@ aSelectAll.setEnabled(isSelectAllEnabled()); } - /** - * Called when the current map of the attached MainControl has changed. - */ - public void currentMapChanged() { + /* {@inheritDoc} */ + public void currentMapChanged(@Nullable final net.sf.gridarta.map.MapControl mapControl) { refreshMenus(); } Modified: trunk/daimonin/src/daieditor/CMainControl.java =================================================================== --- trunk/daimonin/src/daieditor/CMainControl.java 2006-12-22 19:26:11 UTC (rev 1202) +++ trunk/daimonin/src/daieditor/CMainControl.java 2006-12-22 19:43:29 UTC (rev 1203) @@ -95,8 +95,10 @@ import javax.swing.JFileChooser; import javax.swing.JOptionPane; import static javax.swing.KeyStroke.getKeyStroke; +import javax.swing.event.EventListenerList; import javax.swing.filechooser.FileFilter; import net.sf.gridarta.MainControl; +import net.sf.gridarta.MainControlListener; import net.sf.gridarta.gameobject.Collectable; import net.sf.gridarta.gameobject.Collector; import net.sf.gridarta.gui.HideFileFilterProxy; @@ -296,6 +298,9 @@ private final MapCursorControl mapCursorControl; + /** The MainControlListeners to inform of changes. */ + private EventListenerList listenerList = new EventListenerList(); + /** Actions used by this instance. */ private final MainActions mainActions = new MainActions(this); @@ -1688,15 +1693,42 @@ */ public void setCurrentLevel(final MapControl map) { + if (currentMap == map) { + return; + } + currentMap = map; refreshMenusAndToolbars(); mapCursorControl.refreshMenus(); //CMainStatusbar.getInstance().setLevelInfo(level); - mainActions.currentMapChanged(); + fireCurrentMapChangedEvent(); } + /** + * Notify all listeners about a changed current map. + */ + private void fireCurrentMapChangedEvent() { + final Object[] listeners = listenerList.getListenerList(); + for (int i = listeners.length - 2; i >= 0; i -= 2) { + //noinspection ObjectEquality + if (listeners[i] == MainControlListener.class) { + ((MainControlListener) listeners[i + 1]).currentMapChanged(currentMap); + } + } + } + + /** {@inheritDoc} */ + public void addMainControlListener(@NotNull final MainControlListener listener) { + listenerList.add(MainControlListener.class, listener); + } + + /** {@inheritDoc} */ + public void removeMainControlListener(final MainControlListener listener) { + listenerList.remove(MainControlListener.class, listener); + } + /** Invoked when user wants to revert the current map to previously saved state. */ public void revert() { final MapControl modmap = currentMap; // "modified map" to be reverted @@ -2093,7 +2125,7 @@ /** * Encapsulates actions and related functions. */ - public static final class MainActions implements MapCursorListener, MapGridListener { + public static final class MainActions implements MainControlListener, MapCursorListener, MapGridListener { /** The MainControl. */ private final CMainControl mainControl; @@ -2138,6 +2170,8 @@ */ public MainActions(@NotNull final CMainControl mainControl) { this.mainControl = mainControl; + + mainControl.addMainControlListener(this); } /** @@ -2157,10 +2191,8 @@ aSelectAll.setEnabled(isSelectAllEnabled()); } - /** - * Called when the current map of the attached MainControl has changed. - */ - public void currentMapChanged() { + /* {@inheritDoc} */ + public void currentMapChanged(@Nullable final net.sf.gridarta.map.MapControl mapControl) { refreshMenus(); } Modified: trunk/src/app/net/sf/gridarta/MainControl.java =================================================================== --- trunk/src/app/net/sf/gridarta/MainControl.java 2006-12-22 19:26:11 UTC (rev 1202) +++ trunk/src/app/net/sf/gridarta/MainControl.java 2006-12-22 19:43:29 UTC (rev 1203) @@ -70,4 +70,16 @@ */ @Deprecated void setTabPaneEnabled(boolean tabPaneEnabled); + /** + * Register a MainControlListener. + * @param listener MainControlListener to register + */ + void addMainControlListener(@NotNull final MainControlListener listener); + + /** + * Remove a MainControlListener. + * @param listener MainControlListener to remove + */ + void removeMainControlListener(@NotNull final MainControlListener listener); + } // interface MainControl Added: trunk/src/app/net/sf/gridarta/MainControlListener.java =================================================================== --- trunk/src/app/net/sf/gridarta/MainControlListener.java (rev 0) +++ trunk/src/app/net/sf/gridarta/MainControlListener.java 2006-12-22 19:43:29 UTC (rev 1203) @@ -0,0 +1,42 @@ +/* + * Gridarta Map Editor. + * Copyright (C) 2006 The Gridarta Developers + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.gridarta; + +import java.util.EventListener; +import net.sf.gridarta.map.MapControl; +import org.jetbrains.annotations.Nullable; + +/** + * Interface for listeners listening to CMainControl changes. + * + * @author Andreas Kirschbaum + */ +public interface MainControlListener extends EventListener { + + /** + * This event handler is called when the current map has changed. + * + * @param mapControl the new map control, or <code>null</code> if no opened + * map exists + */ + void currentMapChanged(@Nullable MapControl mapControl); + +} Property changes on: trunk/src/app/net/sf/gridarta/MainControlListener.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |