From: <aki...@us...> - 2006-12-15 20:36:29
|
Revision: 1077 http://svn.sourceforge.net/gridarta/?rev=1077&view=rev Author: akirschbaum Date: 2006-12-15 12:36:27 -0800 (Fri, 15 Dec 2006) Log Message: ----------- Simplify CArchPanel/CArchPanelPan implementation. Modified Paths: -------------- trunk/crossfire/src/cfeditor/CArchPanel.java trunk/crossfire/src/cfeditor/CArchPanelPan.java Modified: trunk/crossfire/src/cfeditor/CArchPanel.java =================================================================== --- trunk/crossfire/src/cfeditor/CArchPanel.java 2006-12-15 20:11:50 UTC (rev 1076) +++ trunk/crossfire/src/cfeditor/CArchPanel.java 2006-12-15 20:36:27 UTC (rev 1077) @@ -57,7 +57,7 @@ private final transient CMainControl mainControl; /** - * Name of the selected arch. + * Name of the selected archetype. * @serial include */ private String selectedArch; @@ -74,9 +74,6 @@ /** The data/view of selected objects in tab panel. */ private final CArchQuickView archQuickPanel; - /** List of arch panels. */ - private static List<PanelNode> panelNodeList = new ArrayList<PanelNode>(); - /** The active panel. */ private CArchPanelPan selectedPanel; @@ -95,21 +92,19 @@ // archAndPickPane is the panel containing both archpanel and pickmaps archAndPickPane = new JTabbedPane(SwingConstants.TOP); - archAndPickPane.addTab(" Arch List ", tabDesktop); - archAndPickPane.addTab(" Pickmaps ", mainControl.getMainView().getPickmapPanel()); + archAndPickPane.addTab("Arch List", tabDesktop); + archAndPickPane.addTab("Pickmaps", mainControl.getMainView().getPickmapPanel()); // this listener informs the mainview which panel is active: archlist or pickmaps? CPickmapPanel.getInstance().addArchNPickChangeListener(archAndPickPane); - splitPane = new GSplitPane(JSplitPane.VERTICAL_SPLIT, archAndPickPane, archQuickPanel); - // calculate default value in case there is no settings file final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); final CSettings settings = CSettings.getInstance(IGUIConstants.APP_NAME); final int divLocation = Integer.parseInt(settings.getProperty(ARCHPANEL_LOCATION_KEY, String.valueOf((int) (0.77 * 0.9 * screen.getHeight())))); + splitPane = new GSplitPane(JSplitPane.VERTICAL_SPLIT, archAndPickPane, archQuickPanel); splitPane.setDividerLocation(divLocation); - splitPane.setDividerSize(5); add(splitPane, BorderLayout.CENTER); } @@ -148,19 +143,18 @@ } public void addPanel(final String name) { - final PanelNode newnode = new PanelNode(new CArchPanelPan(this, mainControl), name); + final CArchPanelPan newnode = new CArchPanelPan(this, mainControl); // insert new panels in alphabetical order int i; for (i = 0; i < tabDesktop.getTabCount() && name.compareToIgnoreCase(tabDesktop.getTitleAt(i)) > 0; i++) { ; } - panelNodeList.add(i, newnode); - tabDesktop.insertTab(name, null, newnode.data.getPanel(), null, i); + tabDesktop.insertTab(name, null, newnode, null, i); // careful: during the build process we are setting 'selectedPanel' // in spite of the fact that this panel might *not* actually be selected - selectedPanel = newnode.data; + selectedPanel = newnode; } /** @@ -169,25 +163,28 @@ * setting and make sure the arch list is properly drawn. */ public void finishBuildProcess() { - final Component sel = tabDesktop.getSelectedComponent(); - for (final PanelNode node : panelNodeList) { - if (node.data.getPanel() == sel) { - if (node.data != null) { - selectedPanel = node.data; + selectedPanel = (CArchPanelPan) 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(); + selectedPanel = (CArchPanelPan) tabDesktop.getComponentAt(tabbedPane.getSelectedIndex()); + if (selectedPanel != null) { selectedPanel.showArchList(); } - break; } - } + }); // 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); - final PanelNode node = panelNodeList.get(tabbedPane.getSelectedIndex()); - selectedPanel = node.getData(); + selectedPanel = (CArchPanelPan) tabDesktop.getComponentAt(tabbedPane.getSelectedIndex()); if (selectedPanel != null) { selectedPanel.showArchList(); } @@ -195,7 +192,7 @@ }); } - void appExitNotify() { + public void appExitNotify() { final CSettings settings = CSettings.getInstance(IGUIConstants.APP_NAME); settings.setProperty(ARCHPANEL_LOCATION_KEY, String.valueOf(splitPane.getDividerLocation())); } @@ -216,33 +213,9 @@ this.selectedArch = selectedArch; } - void refresh() { + public void refresh() { archQuickPanel.refresh(); repaint(); } - public static final class PanelNode { - - private final CArchPanelPan data; - - /** - * Title of this node. - */ - private final String title; - - public PanelNode(final CArchPanelPan data, final String title) { - this.data = data; - this.title = title; - } - - public String getTitle() { - return title; - } - - public CArchPanelPan getData() { - return data; - } - - } // class PanelNode - } // class CArchPanel Modified: trunk/crossfire/src/cfeditor/CArchPanelPan.java =================================================================== --- trunk/crossfire/src/cfeditor/CArchPanelPan.java 2006-12-15 20:11:50 UTC (rev 1076) +++ trunk/crossfire/src/cfeditor/CArchPanelPan.java 2006-12-15 20:36:27 UTC (rev 1077) @@ -33,7 +33,9 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListModel; import javax.swing.JComboBox; @@ -41,6 +43,8 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JViewport; +import javax.swing.ListModel; +import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import org.apache.log4j.Logger; @@ -56,40 +60,33 @@ /** Controller of this subview. */ private final CMainControl mainControl; - /** List of index-archetype name pairs. */ - private List<PanelEntry> archList = new ArrayList<PanelEntry>(); + /** Maps index to list of archetype names. */ + private Map<Integer, ArrayList<String>> archLists = new HashMap<Integer, ArrayList<String>>(); private final JList theList; private final DefaultListModel model; - private final JPanel panelDesktop; - private final JComboBox comboBox; - private int comboCounter; - private final CArchPanel archPanel; /* Build Panel */ CArchPanelPan(final CArchPanel controlPanel, final CMainControl mainControl) { + super(new BorderLayout()); this.mainControl = mainControl; archPanel = controlPanel; - comboCounter = 0; - setLayout(new BorderLayout()); - panelDesktop = new JPanel(); - panelDesktop.setLayout(new BorderLayout()); - comboBox = new JComboBox(); model = new DefaultListModel(); theList = new JList(model); theList.setCellRenderer(new MyCellRenderer()); theList.setBackground(IGUIConstants.BG_COLOR); + theList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); final JScrollPane scrollPane = new JScrollPane(theList); - panelDesktop.add(scrollPane, BorderLayout.CENTER); - panelDesktop.add(comboBox, BorderLayout.NORTH); + add(scrollPane, BorderLayout.CENTER); + add(comboBox, BorderLayout.NORTH); scrollPane.setAutoscrolls(true); scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE); @@ -106,17 +103,6 @@ showArchListObject(e.getFirstIndex()); } }); - - // listening for mouse-click events in the ArchPanel list - theList.addMouseListener(new MouseAdapter() { - @Override public void mousePressed(final MouseEvent e) { - // In JDK 1.4 it is required to transfer focus back to mapview after - // every click in the panel, otherwise the menu-shortcuts stop working - if (mainControl.getCurrentMap() != null && mainControl.getCurrentMap().getMapViewFrame() != null) { - mainControl.getCurrentMap().getMapViewFrame().requestFocus(); // focus to mapview - } - } - }); } /** @@ -140,22 +126,17 @@ return archetype; } - public void showArchListObject(int index) { + private void showArchListObject(int index) { GameObject archetype = null; - - if (index != -1) { - try { - final String archetypeName = theList.getSelectedValue().toString(); + String archetypeName = null; + if (0 <= index && index < model.getSize()) { + archetypeName = (String) theList.getSelectedValue(); + if (archetypeName != null) { archetype = mainControl.getArchetypeSet().getArchetype(archetypeName); - } catch (final NullPointerException e) { - /* - log.info("NullPointerException in showArchListObject()!", e); - This happens in JDK 1.4 when you select an arch in panel A, - then select Panel B, then Panel A again. (why??) - */ - } catch (final NumberFormatException e) { + archPanel.setSelectedArch(archetypeName); } } + mainControl.setStatusText(archetypeName == null ? "" : archetypeName); mainControl.showArchPanelQuickObject(archetype); // send it to quick view } @@ -165,59 +146,49 @@ * @param index index of subdir where to add */ public void addArchPanelArch(final String archetypeName, final int index) { - archList.add(new PanelEntry(index, archetypeName)); + final ArrayList<String> tmp = archLists.get(index); + final ArrayList<String> archList; + if (tmp == null) { + archList = new ArrayList<String>(); + archLists.put(index, archList); + } else { + archList = tmp; + } + + archList.add(archetypeName); } - int addArchPanelCombo(final String name) { - setEnabled(false); + /** + * Add a new category to the combo box. + * + * @param name the name of the combo box to create + * + * @return the combo box index + */ + public int addArchPanelCombo(final String name) { comboBox.addItem(name); - setEnabled(true); - return comboCounter++; + return comboBox.getItemCount() - 1; } - JPanel getPanel() { - return panelDesktop; - } - - public List<PanelEntry> getPanelEntries() { - return archList; - } - - void showArchList() { + public void showArchList() { final int index = comboBox.getSelectedIndex(); model.removeAllElements(); - for (PanelEntry p : archList) { - if (index >= 0) { - if (index == 0) { - model.addElement(p.getName()); - } else { - if (index == p.getIndex()) { - model.addElement(p.getName()); - } + if (index == 0) { + for (final List<String> archList : archLists.values()) { + for (final String archName : archList) { + model.addElement(archName); } } } - } - - public static final class PanelEntry { - - private int index; - - private String archName; - - public PanelEntry(final int i, final String name) { - index = i; - archName = name; + else if (index > 0) { + final List<String> archList = archLists.get(index); + if (archList != null) { + for (final String archName : archList) { + model.addElement(archName); + } + } } - - public int getIndex() { - return index; - } - - public String getName() { - return archName; - } } /** Cell Renderer for rendering cells in the ArchPanelPan. */ @@ -234,10 +205,6 @@ final ArchetypeSet archetypeSet = mainControl.getArchetypeSet(); final String archetypeName = (String) value; final GameObject archetype = archetypeSet.getArchetype(archetypeName); - if (isSelected) { // XXX it is not a good idea to query the selection information at this place. - archPanel.setSelectedArch(archetypeName); - mainControl.setStatusText(archetypeName); - } assert archetype != null : "Archetype not found: " + archetypeName; setText(archetypeName); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |