From: <aki...@us...> - 2008-08-03 13:46:13
|
Revision: 4720 http://gridarta.svn.sourceforge.net/gridarta/?rev=4720&view=rev Author: akirschbaum Date: 2008-08-03 13:46:19 +0000 (Sun, 03 Aug 2008) Log Message: ----------- Rewrite/cleanup archetype chooser. Modified Paths: -------------- trunk/crossfire/src/cfeditor/CMainControl.java trunk/daimonin/src/daieditor/CMainControl.java trunk/src/app/net/sf/gridarta/gui/ArchetypesActions.java trunk/src/app/net/sf/gridarta/gui/MainView.java trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserControl.java trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypePanel.java trunk/src/app/net/sf/gridarta/gui/archetypechooser/DirectionPane.java trunk/src/app/net/sf/gridarta/gui/objectchooser/DefaultObjectChooser.java trunk/src/app/net/sf/gridarta/map/DefaultMapControl.java trunk/src/app/net/sf/gridarta/map/DefaultMapModel.java trunk/src/test/net/sf/gridarta/map/DefaultMapModelTest.java Added Paths: ----------- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModel.java trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModelListener.java trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserView.java trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserViewListener.java Removed Paths: ------------- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserListener.java Modified: trunk/crossfire/src/cfeditor/CMainControl.java =================================================================== --- trunk/crossfire/src/cfeditor/CMainControl.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/crossfire/src/cfeditor/CMainControl.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -358,7 +358,7 @@ } scriptControl.loadScripts(new File(globalSettings.getMapDir(), IGUIConstants.SCRIPTS_DIR)); - archetypeChooserControl.finishBuildProcess(); + archetypeChooserControl.getArchetypeChooserView().finishBuildProcess(); // load the autojoin lists autojoinLists.loadList(archetypeSet, getConfigurationDirectory()); Modified: trunk/daimonin/src/daieditor/CMainControl.java =================================================================== --- trunk/daimonin/src/daieditor/CMainControl.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/daimonin/src/daieditor/CMainControl.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -436,7 +436,7 @@ // browse arch archive // load object from a arch file you found - archetypeChooserControl.finishBuildProcess(); + archetypeChooserControl.getArchetypeChooserView().finishBuildProcess(); // load the autojoin lists autojoinLists.loadList(archetypeSet, getConfigurationDirectory()); Modified: trunk/src/app/net/sf/gridarta/gui/ArchetypesActions.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/ArchetypesActions.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/src/app/net/sf/gridarta/gui/ArchetypesActions.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -24,7 +24,8 @@ import net.sf.gridarta.gameobject.Archetype; import net.sf.gridarta.gameobject.GameObject; import net.sf.gridarta.gui.archetypechooser.ArchetypeChooserControl; -import net.sf.gridarta.gui.archetypechooser.ArchetypeChooserListener; +import net.sf.gridarta.gui.archetypechooser.ArchetypeChooserView; +import net.sf.gridarta.gui.archetypechooser.ArchetypeChooserViewListener; import net.sf.gridarta.gui.findarchetypes.FindArchetypesDialog; import net.sf.gridarta.gui.map.MapViewBasic; import net.sf.gridarta.gui.objectchooser.ObjectChooser; @@ -32,7 +33,6 @@ import net.sf.japi.swing.ActionFactory; import net.sf.japi.swing.ToggleAction; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; /** * Manages actions in the "archetypes" menu. @@ -47,14 +47,9 @@ private final ToggleAction aDisplayObjectNames; /** The listener to detect attribute changes. */ - private final ArchetypeChooserListener<G, A, R> archetypeChooserListener = new ArchetypeChooserListener<G, A, R>() { + private final ArchetypeChooserViewListener<G, A, R> archetypeChooserViewListener = new ArchetypeChooserViewListener<G, A, R>() { /** {@inheritDoc} */ - public void selectedArchetypeChanged(@Nullable final G archetype) { - // ignore - } - - /** {@inheritDoc} */ public void displayObjectNamesChanged(final boolean displayObjectNames) { updateActions(displayObjectNames); } @@ -65,16 +60,17 @@ * Create a new instance. * @param parent The parent component for the "find archetypes" dialog. * @param archetypeChooserControl The archetype chooser control. + * @param archetypeChooserView the archetype chooser view * @param objectChooser The insertion object chooser to use when selecting * search results in the "find archetypes" dialog. * @param archetypeTypeSet the instance for looking up archetype types */ - public ArchetypesActions(@NotNull final Component parent, @NotNull final ArchetypeChooserControl<G, A, R, V> archetypeChooserControl, @NotNull final ObjectChooser<G, A, R> objectChooser, final ArchetypeTypeSet archetypeTypeSet) { + public ArchetypesActions(@NotNull final Component parent, @NotNull final ArchetypeChooserControl<G, A, R, V> archetypeChooserControl, @NotNull final ArchetypeChooserView<G, A, R, V> archetypeChooserView, @NotNull final ObjectChooser<G, A, R> objectChooser, final ArchetypeTypeSet<G, A, R> archetypeTypeSet) { final FindArchetypesDialog<G, A, R> findArchetypesDialog = new FindArchetypesDialog<G, A, R>(parent, archetypeChooserControl, objectChooser, archetypeTypeSet); - aDisplayObjectNames = (ToggleAction) ACTION_FACTORY.createToggle(true, "displayObjectNames", archetypeChooserControl); + aDisplayObjectNames = (ToggleAction) ACTION_FACTORY.createToggle(true, "displayObjectNames", archetypeChooserView); ACTION_FACTORY.createAction(true, "findArchetypes", findArchetypesDialog); - archetypeChooserControl.addArchetypeChooserListener(archetypeChooserListener); - updateActions(archetypeChooserControl.isDisplayObjectNames()); + archetypeChooserView.addArchetypeChooserViewListener(archetypeChooserViewListener); + updateActions(archetypeChooserView.isDisplayObjectNames()); } /** Modified: trunk/src/app/net/sf/gridarta/gui/MainView.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/MainView.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/src/app/net/sf/gridarta/gui/MainView.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -399,7 +399,7 @@ toolbarPanel.add(createCenterPanel(defwidth, defheight, selectedSquareView, mapTileListBottom, gameObjectAttributesPanel, objectChooser), BorderLayout.CENTER); add(toolbarPanel, BorderLayout.CENTER); - archetypesActions = new ArchetypesActions<G, A, R, V>(this, archetypeChooserControl, objectChooser, archetypeTypeSet); + archetypesActions = new ArchetypesActions<G, A, R, V>(this, archetypeChooserControl, archetypeChooserControl.getArchetypeChooserView(), objectChooser, archetypeTypeSet); setJMenuBar(actionFactory.createMenuBar(true, "main")); objectChooser.setPickmapFoldersMenu((JMenu) actionFactory.find(getJMenuBar(), "pickmapFolders")); viewActions.init(gameObjectMatchers); Modified: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserControl.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserControl.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserControl.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -19,7 +19,6 @@ package net.sf.gridarta.gui.archetypechooser; -import java.awt.BorderLayout; import java.awt.Point; import java.util.ArrayList; import java.util.Collections; @@ -27,16 +26,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.prefs.Preferences; -import javax.swing.BorderFactory; -import javax.swing.JComponent; -import javax.swing.JPanel; -import javax.swing.JTabbedPane; -import javax.swing.SwingConstants; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; -import javax.swing.event.EventListenerList; -import net.sf.gridarta.CommonConstants; import net.sf.gridarta.MainControl; import net.sf.gridarta.gameobject.Archetype; import net.sf.gridarta.gameobject.ArchetypeSet; @@ -49,88 +38,45 @@ import org.jetbrains.annotations.Nullable; /** - * That archetype chooser. + * That control of the archetype chooser. * @author Andreas Kirschbaum */ public class ArchetypeChooserControl<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>, V extends MapViewBasic<G, A, R, V>> implements Iterable<R> { - /** Preferences. */ - private static final Preferences prefs = Preferences.userNodeForPackage(MainControl.class); - /** - * The key for saving the display mode selection. + * Maps archetype to archetype panel containing the archetype. */ - private static final String DISPLAY_OBJECT_NAMES = "archetypesDisplayObjectNames"; + private final Map<R, ArchetypePanel<G, A, R, V>> archetypes = new HashMap<R, ArchetypePanel<G, A, R, V>>(); - /** The listeners to inform of changes. */ - private final EventListenerList listenerList = new EventListenerList(); - - /** The main control. */ - private final MainControl<G, A, R, V> mainControl; - /** - * The archetype set to display. + * The archetype chooser's model. */ - @NotNull - private final ArchetypeSet<G, A, R> archetypeSet; + private final ArchetypeChooserModel<G, A, R, V> archetypeChooserModel; /** - * Name of the selected archetype. - * @serial include + * The archetype chooser's view. */ - private G selectedArch = null; + private final ArchetypeChooserView<G, A, R, V> archetypeChooserView; - /** The Tab panel with arch lists. */ - private final JTabbedPane tabDesktop = new JTabbedPane(SwingConstants.TOP); - - /** The active panel. */ - private ArchetypePanel<G, A, R, V> selectedPanel; - /** - * The panel which allows setting the direction of newly inserted - * archetypes. - */ - private final DirectionPane<G, A, R> directionPane = new DirectionPane<G, A, R>(); - - /** Maps archetype to panel containing the archetype. */ - private final Map<R, ArchetypePanel<G, A, R, V>> archetypes = new HashMap<R, ArchetypePanel<G, A, R, V>>(); - - /** - * The panel with the ArchetypePanel (tabDesktop) and the direction - * setting. - */ - private final JPanel archPane = new JPanel(new BorderLayout()); - - /** Indicates whether object names are shown. */ - private boolean displayObjectNames = prefs.getBoolean(DISPLAY_OBJECT_NAMES, false); - - /** - * Create a new instance. + * Creates a new instance. * @param mainControl The main control. * @param archetypeSet the archetype set to display * @param createDirectionPane Whether to create a "direction" panel. */ public ArchetypeChooserControl(@NotNull final MainControl<G, A, R, V> mainControl, @NotNull final ArchetypeSet<G, A, R> archetypeSet, final boolean createDirectionPane) { - this.mainControl = mainControl; - this.archetypeSet = archetypeSet; - tabDesktop.setBorder(BorderFactory.createEmptyBorder(CommonConstants.SPACE_PICKARCH_TOP, 0, 0, 0)); - archPane.add(tabDesktop); - if (createDirectionPane) { - archPane.add(directionPane, BorderLayout.SOUTH); - } + archetypeChooserModel = new ArchetypeChooserModel<G, A, R, V>(archetypeSet); + archetypeChooserView = new ArchetypeChooserView<G, A, R, V>(createDirectionPane, mainControl, archetypeChooserModel); } - public JComponent getArchetypePanel() { - return archPane; - } - /** - * Get the currently selected archetype. + * Returns the currently selected archetype. * @return the selected archetype, or <code>null</code> if none is selected */ @Nullable - public G getSelection() { - return selectedPanel != null ? selectedPanel.getArchListObject() : null; + public R getSelection() { + final ArchetypePanel<G, A, R, V> selectedPanel = archetypeChooserModel.getSelectedPanel(); + return selectedPanel != null ? selectedPanel.getSelectedArchetype() : null; } /** @@ -138,176 +84,30 @@ * @return The selected archetypes. */ @NotNull - public List<G> getSelections() { - final G archObject = getSelection(); + public List<G> getSelections() { // XXX: should return List<R> + final R archObject = getSelection(); if (archObject == null) { return Collections.emptyList(); } final List<G> result = new ArrayList<G>(1); - result.add(archObject); + result.add((G) archObject); return result; } /** - * Return a panel by name. If this panel does not exist, create a new one. - * @param panelName the panel name - * @return the panel - */ - @NotNull - private ArchetypePanel<G, A, R, V> findOrCreatePanel(@NotNull final String panelName) { - for (int i = 0; i < tabDesktop.getTabCount(); i++) { - if (panelName.equals(tabDesktop.getTitleAt(i))) { - return (ArchetypePanel<G, A, R, V>) tabDesktop.getComponentAt(i); - } - } - - final ArchetypePanel<G, A, R, V> newPanel = createArchetypePanel(); - - // insert new panel in alphabetical order - int i; - for (i = 0; i < tabDesktop.getTabCount() && panelName.compareToIgnoreCase(tabDesktop.getTitleAt(i)) > 0; i++) { - ; - } - tabDesktop.insertTab(panelName, null, newPanel, null, i); - - return newPanel; - } - - /** - * After the build process is completed and the panels have been fully - * constructed, we must correct the 'selectedPanel' setting and make sure - * the arch list is properly drawn. - */ - public void finishBuildProcess() { - for (int i = 0; i < tabDesktop.getTabCount(); i++) { - ((ArchetypePanel<?, ?, ?, ?>) tabDesktop.getComponentAt(i)).finishBuildProcess(); - } - - selectedPanel = (ArchetypePanel<G, A, R, V>) tabDesktop.getSelectedComponent(); - if (selectedPanel != null) { - selectedPanel.showArchList(); - } - - // we must set the list of the selected list depend on combo selection - tabDesktop.addChangeListener(new ChangeListener() { - public void stateChanged(final ChangeEvent e) { - final JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); - setSelectedArch(null); - selectedPanel = (ArchetypePanel<G, A, R, V>) tabDesktop.getComponentAt(tabbedPane.getSelectedIndex()); - if (selectedPanel != null) { - selectedPanel.showArchList(); - } - } - }); - } - - /** - * Set selected Arch. - * @param selectedArch name of selected arch in arch panel - */ - public void setSelectedArch(@Nullable final G selectedArch) { - if (this.selectedArch == selectedArch) { - return; - } - - directionPane.setEnabled(selectedArch); - - this.selectedArch = selectedArch; - fireSelectedArchetypeChangedEvent(selectedArch); - } - - /** - * Register a listener. - * @param listener The listener to register. - */ - public void addArchetypeChooserListener(@NotNull final ArchetypeChooserListener<G, A, R> listener) { - listenerList.add(ArchetypeChooserListener.class, listener); - } - - /** - * Remove a listener. - * @param listener The listener to remove. - */ - public void removeArchetypeChooserListener(@NotNull final ArchetypeChooserListener<G, A, R> listener) { - listenerList.remove(ArchetypeChooserListener.class, listener); - } - - /** - * Notify all listeners about a changed selected archetype. - * @param archetype The new selected archetype. - */ - private void fireSelectedArchetypeChangedEvent(@Nullable final G archetype) { - for (final ArchetypeChooserListener<G, A, R> listener : listenerList.getListeners(ArchetypeChooserListener.class)) { - listener.selectedArchetypeChanged(archetype); - } - } - - /** - * Notifies all listeners that the display mode has changed. - */ - private void fireDisplayObjectNamesChangedEvent() { - for (final ArchetypeChooserListener<G, A, R> listener : listenerList.getListeners(ArchetypeChooserListener.class)) { - listener.displayObjectNamesChanged(displayObjectNames); - } - } - - /** - * Add a new archetype. Create the panel and/or folder if necessary. + * Adds a new archetype. Creates the panel and/or folder if necessary. * @param panelName the panel name to add the archetype to * @param folderName the folder name to add the archetype to * @param arch the archetype to add */ public void addArchetype(@NotNull final String panelName, @NotNull final String folderName, @NotNull final R arch) { - final ArchetypePanel<G, A, R, V> panel = findOrCreatePanel(panelName); + final ArchetypePanel<G, A, R, V> panel = archetypeChooserView.findOrCreatePanel(panelName); panel.addArchetype(folderName, arch); archetypes.put(arch, panel); } /** - * Create a new archetype panel. - * @return The new archetype panel. - */ - @NotNull - private ArchetypePanel<G, A, R, V> createArchetypePanel() { - return new ArchetypePanel<G, A, R, V>(this, mainControl, archetypeSet); - } - - /** - * Returns the currently selected direction. - * @return The currently selected direction or <code>null</code> if the - * archetype's default direction should be used. - */ - @Nullable - public Integer getCurrentDirection() { - return directionPane.getCurrentDirection(); - } - - /** - * Action method for "display object names". - * @return <code>true</code> if object names are shown, or - * <code>false</code> if archetype names are shown. - */ - public boolean isDisplayObjectNames() { - return displayObjectNames; - } - - /** - * Sets whether the object or archetype names are shown. - * @param displayObjectNames The new setting. - * @see #isDisplayObjectNames() - */ - public void setDisplayObjectNames(final boolean displayObjectNames) { - if (this.displayObjectNames == displayObjectNames) { - return; - } - - this.displayObjectNames = displayObjectNames; - prefs.putBoolean(DISPLAY_OBJECT_NAMES, displayObjectNames); - fireDisplayObjectNamesChangedEvent(); - } - - /** * Select an archetype. If necessary, select the archetype's tab. * @param archetype The archetype to select. */ @@ -317,9 +117,7 @@ return; } - selectedPanel = panel; - tabDesktop.setSelectedComponent(panel); - panel.showArchList(); + archetypeChooserView.setSelectedPanel(panel); panel.selectArchetype(archetype); } @@ -332,22 +130,13 @@ } /** - * Return whether a given panel is the currently active panel. - * @param panel The panel to check. - * @return Whether the panel is active. - */ - public boolean isSelectedPanel(@NotNull final ArchetypePanel<G, A, R, V> panel) { - return selectedPanel == panel; - } - - /** * Returns whether the selected archetype matches a given game object. - * @param gameObject the game object to match + * @param archetype the archetype to match * @return whether the selected archetype matches; returns * <code>false</code> if no archetype is selected */ - public boolean isMatching(final G gameObject) { - return gameObject.getArchetype() == selectedArch; + public boolean isMatching(final R archetype) { + return archetypeChooserModel.getSelectedArchetype() == archetype; } /** @@ -362,7 +151,44 @@ */ @Nullable public G insertSelectedArchetype(final MapModel<G, A, R> mapModel, final Point pos, final boolean allowMany, final InsertionMode insertionMode) { - return selectedArch == null ? null : mapModel.insertArchetype(selectedArch.getArchetype(), pos, allowMany, true, insertionMode); + final R selectedArchetype = archetypeChooserModel.getSelectedArchetype(); + return selectedArchetype == null ? null : mapModel.insertArchetype(selectedArchetype, pos, allowMany, true, insertionMode); } + /** + * Returns the archetype chooser view. + * @return the archetype chooser view + */ + @Deprecated + @NotNull + public ArchetypeChooserView<G, A, R, V> getArchetypeChooserView() { + return archetypeChooserView; + } + + /** + * Returns the archetype chooser model. + * @return the archetype chooser model + */ + @Deprecated + @NotNull + public ArchetypeChooserModel<G,A,R,?> getArchetypeChooserModel() { + return archetypeChooserModel; + } + + /** + * Adds an archetype chooser model listener to be notified. + * @param listener the listener to add + */ + public void addArchetypeChooserModelListener(final ArchetypeChooserModelListener<G, A, R, V> listener) { + archetypeChooserModel.addArchetypeChooserModelListener(listener); + } + + /** + * Removes an archetype chooser model listener to be notified. + * @param listener the listener to remove + */ + public void removeArchetypeChooserModelListener(final ArchetypeChooserModelListener<G, A, R, V> listener) { + archetypeChooserModel.removeArchetypeChooserModelListener(listener); + } + } // class ArchetypeChooserControl Deleted: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserListener.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserListener.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserListener.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -1,47 +0,0 @@ -/* - * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. - * Copyright (C) 2000-2007 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package net.sf.gridarta.gui.archetypechooser; - -import java.util.EventListener; -import net.sf.gridarta.gameobject.Archetype; -import net.sf.gridarta.gameobject.GameObject; -import net.sf.gridarta.map.MapArchObject; -import org.jetbrains.annotations.Nullable; - -/** - * Interface for listeners listening to Archetype Chooser events. - * @author Andreas Kirschbaum - */ -public interface ArchetypeChooserListener<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends EventListener { - - /** - * The event handler is called when the selected archetype has changed. - * @param archetype The new selected archetype. - */ - void selectedArchetypeChanged(@Nullable G archetype); - - /** - * This event handler is called when an attribute "display object names" has - * changed. - * @param displayObjectNames The new attribute value. - */ - void displayObjectNamesChanged(boolean displayObjectNames); - -} // interface ArchetypeChooserListener Added: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModel.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModel.java (rev 0) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModel.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -0,0 +1,187 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2007 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package net.sf.gridarta.gui.archetypechooser; + +import java.util.ArrayList; +import java.util.List; +import net.sf.gridarta.gameobject.Archetype; +import net.sf.gridarta.gameobject.ArchetypeSet; +import net.sf.gridarta.gameobject.GameObject; +import net.sf.gridarta.gui.map.MapViewBasic; +import net.sf.gridarta.map.MapArchObject; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * The view of the archetype chooser. + * @author Andreas Kirschbaum + */ +public class ArchetypeChooserModel<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>, V extends MapViewBasic<G, A, R, V>> { + + /** + * The registered listeners. + */ + @NotNull + private final List<ArchetypeChooserModelListener<G, A, R, V>> listeners = new ArrayList<ArchetypeChooserModelListener<G, A, R, V>>(); + + /** + * The archetype set to display. + */ + @NotNull + private final ArchetypeSet<G, A, R> archetypeSet; + + /** + * The currently selected panel. + */ + @Nullable + private ArchetypePanel<G, A, R, V> selectedPanel = null; + + /** + * The currently selected archetype. + */ + @Nullable + private R selectedArchetype = null; + + /** + * The default direction for game objects created from archetypes. Set to + * <code>null</code> for default direction. + */ + @Nullable + private Integer direction; + + /** + * Creates a new instance. + * @param archetypeSet the archetype set to display + */ + public ArchetypeChooserModel(@NotNull final ArchetypeSet<G, A, R> archetypeSet) { + this.archetypeSet = archetypeSet; + } + + /** + * Returns the archetype set to display. + * @return the archetype set + */ + @NotNull + public final ArchetypeSet<G, A, R> getArchetypeSet() { + return archetypeSet; + } + + /** + * Sets the selected panel. + * @param selectedPanel the new selected panel or <code>null</code> to + * select no panel. + */ + public void setSelectedPanel(@Nullable final ArchetypePanel<G, A, R, V> selectedPanel) { + if (this.selectedPanel == selectedPanel) { + return; + } + + this.selectedPanel = selectedPanel; + if (selectedPanel != null) { + selectedPanel.showArchList(); + } + fireSelectedPanelChangedEvent(); + } + + /** + * Returns the selected archetype panel. + * @return the selected archetype panel or <code>null</code> if none is + * selected. + */ + @Nullable + public ArchetypePanel<G, A, R, V> getSelectedPanel() { + return selectedPanel; + } + + /** + * Sets the selected archetype. + * @param selectedArchetype the new selected archetype or <code>null</code> + * to select no archetype + */ + public void setSelectedArchetype(@Nullable final R selectedArchetype) { + if (this.selectedArchetype == selectedArchetype) { + return; + } + + this.selectedArchetype = selectedArchetype; + fireSelectedArchetypeChangedEvent(); + } + + /** + * Returns the selected archetype. + * @return the selected archetype or <code>null</code> if none is selected + */ + @Nullable + public R getSelectedArchetype() { + return selectedArchetype; + } + + /** + * Adds a listener to be notified. + * @param listener the listener to add + */ + public void addArchetypeChooserModelListener(@NotNull final ArchetypeChooserModelListener<G, A, R, V> listener) { + listeners.add(listener); + } + + /** + * Removes a listener to be notified. + * @param listener the listener to remove + */ + public void removeArchetypeChooserModelListener(@NotNull final ArchetypeChooserModelListener<G, A, R, V> listener) { + listeners.remove(listener); + } + + /** + * Notifies all listener that the selected panel has changed. + */ + private void fireSelectedPanelChangedEvent() { + for (final ArchetypeChooserModelListener<G, A, R, V> listener : listeners) { + listener.selectedPanelChanged(selectedPanel); + } + } + + /** + * Notifies all listener that the selected archetype has changed. + */ + private void fireSelectedArchetypeChangedEvent() { + for (final ArchetypeChooserModelListener<G, A, R, V> listener : listeners) { + listener.selectedArchetypeChanged(selectedArchetype); + } + } + + /** + * Returns the default direction for game objects created from archetypes. + * @return the direction or <code>null</code> for default + */ + @Nullable + public Integer getDirection() { + return direction; + } + + /** + * Sets the default direction for game objects created from archetypes. + * @param direction the direction or <code>null</code> for default + */ + public void setDirection(@Nullable final Integer direction) { + this.direction = direction; + } + +} // class ArchetypeChooserModel Property changes on: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModel.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF Added: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModelListener.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModelListener.java (rev 0) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModelListener.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -0,0 +1,47 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2007 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package net.sf.gridarta.gui.archetypechooser; + +import net.sf.gridarta.gameobject.Archetype; +import net.sf.gridarta.gameobject.GameObject; +import net.sf.gridarta.gui.map.MapViewBasic; +import net.sf.gridarta.map.MapArchObject; +import org.jetbrains.annotations.Nullable; + +/** + * Interface for listeners interested in events of {@link + * ArchetypeChooserModel} instances. + * @author Andreas Kirschbaum + */ +public interface ArchetypeChooserModelListener<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>, V extends MapViewBasic<G, A, R, V>> { + + /** + * The selected panel has changed. + * @param selectedPanel the selected panel + */ + void selectedPanelChanged(@Nullable ArchetypePanel<G, A, R, V> selectedPanel); + + /** + * The selected archetype has changed. + * @param selectedArchetype the selected archetype + */ + void selectedArchetypeChanged(@Nullable R selectedArchetype); + +} // interface ArchetypeChooserModelListener Property changes on: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserModelListener.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF Added: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserView.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserView.java (rev 0) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserView.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -0,0 +1,219 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2007 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package net.sf.gridarta.gui.archetypechooser; + +import java.awt.BorderLayout; +import java.util.ArrayList; +import java.util.List; +import java.util.prefs.Preferences; +import javax.swing.BorderFactory; +import javax.swing.JPanel; +import javax.swing.JTabbedPane; +import javax.swing.SwingConstants; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import net.sf.gridarta.CommonConstants; +import net.sf.gridarta.MainControl; +import net.sf.gridarta.gameobject.Archetype; +import net.sf.gridarta.gameobject.GameObject; +import net.sf.gridarta.gui.map.MapViewBasic; +import net.sf.gridarta.map.MapArchObject; +import org.jetbrains.annotations.NotNull; + +/** + * The view of the archetype chooser. + * @author Andreas Kirschbaum + */ +public class ArchetypeChooserView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>, V extends MapViewBasic<G, A, R, V>> extends JPanel { + + /** + * The key for saving the display mode selection. + */ + private static final String DISPLAY_OBJECT_NAMES = "archetypesDisplayObjectNames"; + + /** + * The serial version UID. + */ + private static final long serialVersionUID = 1; + + /** Preferences. */ + private static final Preferences prefs = Preferences.userNodeForPackage(MainControl.class); + + /** + * The registered listeners. + */ + @NotNull + private final List<ArchetypeChooserViewListener<G, A, R>> listeners = new ArrayList<ArchetypeChooserViewListener<G, A, R>>(); + + /** + * The main control. + */ + @NotNull + private final MainControl<G, A, R, V> mainControl; + + /** + * The archetype chooser model. + */ + @NotNull + private final ArchetypeChooserModel<G, A, R, V> archetypeChooserModel; + + /** + * The tab panel containing the archetype lists. + */ + private final JTabbedPane tabDesktop = new JTabbedPane(SwingConstants.TOP); + + /** + * Indicates whether object names are shown. + */ + private boolean displayObjectNames = prefs.getBoolean(DISPLAY_OBJECT_NAMES, false); + + /** + * The change listener attached to {@link #tabDesktop}. + */ + private final ChangeListener changeListener = new ChangeListener() { + + /** {@inheritDoc} */ + public void stateChanged(final ChangeEvent e) { + final JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); + archetypeChooserModel.setSelectedArchetype(null); + archetypeChooserModel.setSelectedPanel((ArchetypePanel<G, A, R, V>) tabDesktop.getComponentAt(tabbedPane.getSelectedIndex())); + } + }; + + /** + * Creates a new instance. + * @param createDirectionPane whether the direction pane should be shown + * @param mainControl the main control + * @param archetypeChooserModel the model to use + */ + public ArchetypeChooserView(final boolean createDirectionPane, @NotNull final MainControl<G, A, R, V> mainControl, @NotNull final ArchetypeChooserModel<G, A, R, V> archetypeChooserModel) { + super(new BorderLayout()); + this.mainControl = mainControl; + this.archetypeChooserModel = archetypeChooserModel; + tabDesktop.setBorder(BorderFactory.createEmptyBorder(CommonConstants.SPACE_PICKARCH_TOP, 0, 0, 0)); + add(tabDesktop); + if (createDirectionPane) { + add(new DirectionPane<G, A, R, V>(archetypeChooserModel), BorderLayout.SOUTH); + } + } + + /** + * Returns a panel by name. If this panel does not exist, create a new one. + * @param panelName the panel name + * @return the panel + */ + @NotNull + public ArchetypePanel<G, A, R, V> findOrCreatePanel(@NotNull final String panelName) { + for (int i = 0; i < tabDesktop.getTabCount(); i++) { + if (panelName.equals(tabDesktop.getTitleAt(i))) { + return (ArchetypePanel<G, A, R, V>) tabDesktop.getComponentAt(i); + } + } + + final ArchetypePanel<G, A, R, V> newPanel = new ArchetypePanel<G, A, R, V>(this, archetypeChooserModel, mainControl, archetypeChooserModel.getArchetypeSet()); + + // insert new panel in alphabetical order + int i; + for (i = 0; i < tabDesktop.getTabCount() && panelName.compareToIgnoreCase(tabDesktop.getTitleAt(i)) > 0; i++) { + ; + } + tabDesktop.insertTab(panelName, null, newPanel, null, i); + + return newPanel; + } + + /** + * After the build process is completed and the panels have been fully + * constructed, we must correct the 'selectedPanel' setting and make sure + * the arch list is properly drawn. + */ + public void finishBuildProcess() { + for (int i = 0; i < tabDesktop.getTabCount(); i++) { + ((ArchetypePanel<?, ?, ?, ?>) tabDesktop.getComponentAt(i)).finishBuildProcess(); + } + + archetypeChooserModel.setSelectedPanel((ArchetypePanel<G, A, R, V>) tabDesktop.getSelectedComponent()); + tabDesktop.addChangeListener(changeListener); + } + + /** + * Selects an archetype panel. Does nothing if the panel does not exist. + * @param selectedPanel the archetype panel to show + */ + public void setSelectedPanel(final ArchetypePanel<G, A, R, V> selectedPanel) { + final int selectedIndex = tabDesktop.indexOfComponent(selectedPanel); + if (selectedIndex == -1 || selectedIndex == tabDesktop.getSelectedIndex()) { + return; + } + + archetypeChooserModel.setSelectedPanel(selectedPanel); + tabDesktop.setSelectedIndex(selectedIndex); + } + + /** + * Notifies all listeners that the display mode has changed. + */ + private void fireDisplayObjectNamesChangedEvent() { + for (final ArchetypeChooserViewListener<G, A, R> listener : listeners) { + listener.displayObjectNamesChanged(displayObjectNames); + } + } + + /** + * Action method for "display object names". + * @return <code>true</code> if object names are shown, or + * <code>false</code> if archetype names are shown. + */ + public boolean isDisplayObjectNames() { + return displayObjectNames; + } + + /** + * Sets whether object or archetype names are shown. + * @param displayObjectNames the new setting + * @see #isDisplayObjectNames() + */ + public void setDisplayObjectNames(final boolean displayObjectNames) { + if (this.displayObjectNames == displayObjectNames) { + return; + } + + this.displayObjectNames = displayObjectNames; + prefs.putBoolean(DISPLAY_OBJECT_NAMES, displayObjectNames); + fireDisplayObjectNamesChangedEvent(); + } + + /** + * Adds a listener to be notified of events. + * @param listener the listener to add + */ + public void addArchetypeChooserViewListener(@NotNull final ArchetypeChooserViewListener<G, A, R> listener) { + listeners.add(listener); + } + + /** + * Removes a listener to be notified of events. + * @param listener the listener to remove + */ + public void removeArchetypeChooserViewListener(@NotNull final ArchetypeChooserViewListener<G, A, R> listener) { + listeners.remove(listener); + } + +} // class ArchetypeChooserView Property changes on: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserView.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF Added: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserViewListener.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserViewListener.java (rev 0) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserViewListener.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -0,0 +1,40 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2007 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package net.sf.gridarta.gui.archetypechooser; + +import java.util.EventListener; +import net.sf.gridarta.gameobject.Archetype; +import net.sf.gridarta.gameobject.GameObject; +import net.sf.gridarta.map.MapArchObject; + +/** + * Interface for listeners listening to Archetype Chooser events. + * @author Andreas Kirschbaum + */ +public interface ArchetypeChooserViewListener<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends EventListener { + + /** + * This event handler is called when an attribute "display object names" has + * changed. + * @param displayObjectNames The new attribute value. + */ + void displayObjectNamesChanged(boolean displayObjectNames); + +} // interface ArchetypeChooserViewListener Property changes on: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypeChooserViewListener.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF Modified: trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypePanel.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypePanel.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/ArchetypePanel.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -35,6 +35,7 @@ import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JViewport; +import javax.swing.ListCellRenderer; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @@ -51,8 +52,9 @@ import org.jetbrains.annotations.Nullable; /** - * Common base class for the panel for Archetypes. + * A panel showing a set of archetypes. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @author Andreas Kirschbaum */ public class ArchetypePanel<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>, V extends MapViewBasic<G, A, R, V>> extends JPanel { @@ -67,8 +69,8 @@ private final JPopupMenu popupMenu = createListPopupMenu(); /** - * List of archetypes for each folder. The folders correspond to the entries - * in {@link #comboBox}. + * List of archetypes for each folder. The folders correspond to the + * entries in {@link #comboBox}. * @note The first entry contains the union of all following entries. */ private final List<Set<R>> folders = new ArrayList<Set<R>>(); @@ -79,32 +81,57 @@ */ private final JList archList; - private final DefaultListModel model; + /** + * The list model of {@link #archList}. + */ + private final DefaultListModel listModel; + /** + * The combo box for selecting the active archetype panel. + */ private final JComboBox comboBox; - /** The Archetype Chooser. */ - private final ArchetypeChooserControl<G, A, R, V> archetypeChooserControl; + /** + * The associated archetype chooser model. + */ + @NotNull + private final ArchetypeChooserModel<G, A, R, V> archetypeChooserModel; - /** The main control. */ + /** + * The main control. + */ @NotNull private final MainControl<G, A, R, V> mainControl; /** - * The archetype set to display. + * The list cell renderer displaying object names. */ @NotNull - private final ArchetypeSet<G, A, R> archetypeSet; + private final ListCellRenderer displayNameCellRenderer; /** + * The list cell renderer displaying archetype names. + */ + @NotNull + private final ListCellRenderer archetypeNameCellRenderer; + + /** + * The comparator for sorting archetypes in the active panel. + */ + @NotNull + private Comparator<Archetype<?, ?, ?>> comparator; + + /** * Comparator used to sort entries shown in {@link #archList} according to * archetype name. */ private static final Comparator<Archetype<?, ?, ?>> archetypeNameComparator = new Comparator<Archetype<?, ?, ?>>() { + /** {@inheritDoc} */ public int compare(final Archetype<?, ?, ?> o1, final Archetype<?, ?, ?> o2) { return o1.getArchetypeName().compareToIgnoreCase(o2.getArchetypeName()); } + }; /** @@ -112,6 +139,7 @@ * display name. */ private static final Comparator<Archetype<?, ?, ?>> displayNameComparator = new Comparator<Archetype<?, ?, ?>>() { + /** {@inheritDoc} */ public int compare(final Archetype<?, ?, ?> o1, final Archetype<?, ?, ?> o2) { final int cmp = o1.getBestName().compareToIgnoreCase(o2.getBestName()); @@ -121,17 +149,15 @@ return o1.getArchetypeName().compareToIgnoreCase(o2.getArchetypeName()); } + }; - /** The listener to detect attribute changes. */ - private final ArchetypeChooserListener<G, A, R> archetypeChooserListener = new ArchetypeChooserListener<G, A, R>() { + /** + * The listener to detect attribute changes. + */ + private final ArchetypeChooserViewListener<G, A, R> archetypeChooserViewListener = new ArchetypeChooserViewListener<G, A, R>() { /** {@inheritDoc} */ - public void selectedArchetypeChanged(@Nullable final G archetype) { - // ignore - } - - /** {@inheritDoc} */ public void displayObjectNamesChanged(final boolean displayObjectNames) { updateCellRenderer(displayObjectNames); } @@ -139,21 +165,21 @@ }; /** - * Creates an ArchetypePanel. - * @param archetypeChooserControl The Archetype Chooser. - * @param mainControl The main control. + * Creates a new instance. + * @param archetypeChooserView the associated archetype chooser view + * @param archetypeChooserModel the associated archetype chooser model + * @param mainControl the main control * @param archetypeSet the archetype set to display */ - protected ArchetypePanel(@NotNull final ArchetypeChooserControl<G, A, R, V> archetypeChooserControl, @NotNull final MainControl<G, A, R, V> mainControl, @NotNull final ArchetypeSet<G, A, R> archetypeSet) { + protected ArchetypePanel(@NotNull final ArchetypeChooserView<G, A, R, V> archetypeChooserView, @NotNull final ArchetypeChooserModel<G, A, R, V> archetypeChooserModel, @NotNull final MainControl<G, A, R, V> mainControl, @NotNull final ArchetypeSet<G, A, R> archetypeSet) { super(new BorderLayout()); - this.archetypeChooserControl = archetypeChooserControl; + this.archetypeChooserModel = archetypeChooserModel; this.mainControl = mainControl; - this.archetypeSet = archetypeSet; - + displayNameCellRenderer = new DisplayNameCellRenderer<R>(archetypeSet); + archetypeNameCellRenderer = new ArchetypeNameCellRenderer<R>(archetypeSet); comboBox = new JComboBox(); - - model = new DefaultListModel(); - archList = new JList(model); + listModel = new DefaultListModel(); + archList = new JList(listModel); //noinspection AbstractMethodCallInConstructor archList.setFocusable(false); archList.setBackground(CommonConstants.BG_COLOR); @@ -163,21 +189,19 @@ add(comboBox, BorderLayout.NORTH); scrollPane.setAutoscrolls(true); scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE); - comboBox.setAutoscrolls(true); - - archetypeChooserControl.addArchetypeChooserListener(archetypeChooserListener); - updateCellRenderer(archetypeChooserControl.isDisplayObjectNames()); + archetypeChooserView.addArchetypeChooserViewListener(archetypeChooserViewListener); + updateCellRenderer(archetypeChooserView.isDisplayObjectNames()); } /** - * Update the cell renderer according to {@link ArchetypeChooserControl#isDisplayObjectNames()} - * and repaint the list. - * @param displayObjectNames If set, display object names; else display - * archetype names. + * Updates the cell renderer state. + * @param displayObjectNames if set, display object names; else display + * archetype names */ private void updateCellRenderer(final boolean displayObjectNames) { - archList.setCellRenderer(displayObjectNames ? new DisplayNameCellRenderer<R>(archetypeSet) : new ArchetypeNameCellRenderer<R>(archetypeSet)); + comparator = displayObjectNames ? displayNameComparator : archetypeNameComparator; + archList.setCellRenderer(displayObjectNames ? displayNameCellRenderer : archetypeNameCellRenderer); final Object selectedValue = archList.getSelectedValue(); showArchList(); if (selectedValue != null) { @@ -194,28 +218,24 @@ archList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(final ListSelectionEvent e) { - updateSelectedArchListObject(); + final R archetype = (R) archList.getSelectedValue(); + archetypeChooserModel.setSelectedArchetype(archetype); } }); } /** - * Get the GameObject currently selected in the list. - * @return GameObject or <code>null</code> if no selection + * Returns the archetype currently selected in the list. + * @return the archetype or <code>null</code> if no selection exists */ - public G getArchListObject() { - return (G) archList.getSelectedValue(); + @Nullable + public R getSelectedArchetype() { + return (R) archList.getSelectedValue(); } - /** Show the selected archetype. */ - private void updateSelectedArchListObject() { - final G archetype = (G) archList.getSelectedValue(); - archetypeChooserControl.setSelectedArch(archetype); - } - /** - * Add an archetype to this panel. + * Adds an archetype to this panel. * @param folderName the folder to add it to * @param archetype the archetype to add */ @@ -226,7 +246,7 @@ } /** - * Return a folder by name. If this folder does not exist, create a new + * Returns a folder by name. If this folder does not exist, create a new * one. * @param folderName the folder name * @return the folder @@ -262,19 +282,24 @@ } } - /** Update {@link #archList} to contain the currently selected folder. */ + /** + * Updates {@link #archList} to contain the currently selected folder. + */ public void showArchList() { final int index = comboBox.getSelectedIndex(); - synchronized (archList.getTreeLock()) { - model.removeAllElements(); - if (index >= 0 && archetypeChooserControl.isSelectedPanel(this)) { + listModel.removeAllElements(); + if (index >= 0 && archetypeChooserModel.getSelectedPanel() == this) { final List<R> archetypes = new ArrayList<R>(); archetypes.addAll(folders.get(index)); - Collections.sort(archetypes, archetypeChooserControl.isDisplayObjectNames() ? displayNameComparator : archetypeNameComparator); + Collections.sort(archetypes, comparator); for (final R archetype : archetypes) { - model.addElement(archetype); + listModel.addElement(archetype); } + if (!archetypes.isEmpty()) { + archList.setSelectedIndex(0); // XXX: save previously selected index? + archList.ensureIndexIsVisible(0); + } } } } @@ -288,12 +313,12 @@ /** Action method for the popup menu to edit a default arch. */ @ActionMethod public void editPopup() { - mainControl.openAttrDialog(getArchListObject()); + mainControl.openAttrDialog((/*XXX*/G) getSelectedArchetype()); } /** - * Select an archetype. If necessary, switch to the correct sub-folder. - * @param archetype The archetype to select. + * Selects an archetype. If necessary, switches to the correct sub-folder. + * @param archetype the archetype to select */ public void selectArchetype(final R archetype) { for (int index = 1; index < folders.size(); index++) { @@ -307,4 +332,4 @@ } -} // class ArchetypePanel. +} // class ArchetypePanel Modified: trunk/src/app/net/sf/gridarta/gui/archetypechooser/DirectionPane.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/archetypechooser/DirectionPane.java 2008-08-03 08:12:38 UTC (rev 4719) +++ trunk/src/app/net/sf/gridarta/gui/archetypechooser/DirectionPane.java 2008-08-03 13:46:19 UTC (rev 4720) @@ -29,12 +29,14 @@ import javax.swing.JToggleButton; import net.sf.gridarta.gameobject.Archetype; import net.sf.gridarta.gameobject.GameObject; +import net.sf.gridarta.gui.map.MapViewBasic; import net.sf.gridarta.map.MapArchObject; import net.sf.japi.swing.ActionFactory; import net.sf.japi.swing.ActionMethod; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class DirectionPane<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JPanel { +public class DirectionPane<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>, V extends MapViewBasic<G, A, R, V>> extends JPanel { /** The serial version UID. */ private static final long serialVersionUID = 1; @@ -50,11 +52,26 @@ /** The buttons in {@link #directionButtonGroup}. */ private final List<JToggleButton> directionButtons = new ArrayList<JToggleButton>(); - @Nullable - private Integer currentDirection = null; + @NotNull + private final ArchetypeChooserModel<G, A, R, V> archetypeChooserModel; - public DirectionPane() { + private ArchetypeChooserModelListener<G, A, R, V> archetypeChooserModelListener = new ArchetypeChooserModelListener<G, A, R, V>() { + + /** {@inheritDoc} */ + public void selectedPanelChanged(@Nullable final ArchetypePanel<G, A, R, V> selectedPanel) { + // ignore + } + + /** {@inheritDoc} */ + public void selectedArchetypeChanged(@Nullable final R selectedArchetype) { + updateEnabled(selectedArchetype); + } + + }; + + public DirectionPane(@NotNull final ArchetypeChooserModel<G, A, R, V> archetypeChooserModel) { super(new GridBagLayout()); + this.archetypeChooserModel = archetypeChooserModel; final GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.anchor = GridBagConstraints.CENTER; @@ -70,19 +87,15 @@ createButton(3, this, gbc, 2, 2); gbc.gridwidth = 3; createButton(null, this, gbc, 0, 3); - setEnabled(null); + archetypeChooserModel.addArchetypeChooserModelListener(archetypeChooserModelListener); + updateEnabled(archetypeChooserModel.getSelectedArchetype()); } - @Nullable - public Integer getCurrentDirection() { - return currentDirection; - } - /** * Enable/disable the direction buttons for a given archetype. * @param archetype The archetype to check. */ - public void setEnabled(@Nullable final G archetype) { + private void updateEnabled(@Nullable final R archetype) { final boolean enabled = archetype != null && archetype.usesDirection(); for (final JToggleButton button : directionButtons) { button.setEnabled(enabled); @@ -110,72 +123,64 @@ button.setSelected(true); } - /** - * Action proxy for direction. - * @param direction Direction number - */ - private void direction(final Integer direction) { - currentDirection = direction; - } - /** Action method for direction. */ @ActionMethod public void direction0() { - direction(0); + archetypeChooserModel.setDirection(0); } /** Action method for direction. */ @ActionMethod public void direction1() { - direction(1); + archetypeChooserModel.setDirection(1); } /** Action method for direction. */ @ActionMethod public void direction2() { - direction(2); + archetypeChooserModel.setDirection(2); } /** Action method for direction. */ @ActionMethod public void direction3() { - direction(3);... [truncated message content] |