From: <aki...@us...> - 2007-01-09 21:36:43
|
Revision: 1522 http://svn.sourceforge.net/gridarta/?rev=1522&view=rev Author: akirschbaum Date: 2007-01-09 13:36:37 -0800 (Tue, 09 Jan 2007) Log Message: ----------- Unify pickmap chooser implementation; sort pickmaps in daieditor. Modified Paths: -------------- trunk/crossfire/src/cfeditor/gui/pickmapchooser/PickmapChooserControl.java trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserControl.java trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserView.java trunk/daimonin/src/daieditor/map/MapControl.java Modified: trunk/crossfire/src/cfeditor/gui/pickmapchooser/PickmapChooserControl.java =================================================================== --- trunk/crossfire/src/cfeditor/gui/pickmapchooser/PickmapChooserControl.java 2007-01-09 08:04:17 UTC (rev 1521) +++ trunk/crossfire/src/cfeditor/gui/pickmapchooser/PickmapChooserControl.java 2007-01-09 21:36:37 UTC (rev 1522) @@ -136,34 +136,27 @@ */ public boolean openPickmap(final File mapFile) { // open the pickmaps - final CMapViewBasic bmapview; - final MapArchObject maparch; try { final CMapReader decoder = new CMapReader(mapFile); - final List<GameObject> objects = decoder.decodeMapFile(); // parse mapfile - maparch = decoder.getMapArch(); // get map arch + try { + final List<GameObject> objects = decoder.decodeMapFile(); + final MapArchObject maparch = decoder.getMapArch(); - if (objects == null) { - // The map is totally empty - newPickmap(null, maparch, mapFile); // init the map - } else { // go to ArchetypeParser and add the default arch list information to them - if (!mainControl.collectTempList(objects, mapFile)) { // get face names, face id, etc. + if (!mainControl.collectTempList(objects, mapFile)) { return false; } - mainControl.getArchetypeParser().sortTempList(objects); // sort the list (put multiparts at the end) - newPickmap(objects, maparch, mapFile); // init the map + mainControl.getArchetypeParser().sortTempList(objects); + newPickmap(objects, maparch, mapFile); // init the map + return true; + } finally { + decoder.close(); } } catch (final IOException e) { - // loading failed - could be a system file in that directory, - // or something else. Doesn't deserve more attention than a printout. - if (!mapFile.getName().startsWith(".")) { - log.warn("Couldn't load pickmap", e); - } + log.warn("Couldn't load pickmap", e); } - - return true; + return false; } /** @@ -192,7 +185,7 @@ * Add a new pickmap. * @param objects list of objects or <code>null</code> for empty * @param maparch the maparch of the pickmap - * @param mapFile The map file that's stored in the MapControl. + * @param mapFile the file name of the pickmap */ private void newPickmap(final List<GameObject> objects, final MapArchObject maparch, final File mapFile) { final MapControl mapControl = new MapControl(mainControl, objects, maparch, true, null); Modified: trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserControl.java =================================================================== --- trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserControl.java 2007-01-09 08:04:17 UTC (rev 1521) +++ trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserControl.java 2007-01-09 21:36:37 UTC (rev 1522) @@ -61,7 +61,10 @@ private final CMainControl mainControl; // main control reference - /** All open pickmaps (the map controllers get stored in the vector). */ + /** + * All open pickmaps. The sort order matches {@link + * PickmapChooserView#pickmapPanel}. + */ private final List<MapControl> pickmaps = new ArrayList<MapControl>(); /** The view for this control. */ @@ -127,17 +130,12 @@ updateActivePickmap(); // make sure we know which one is on top } - public boolean openPickmap(final File mapFile) { - return openPickmap(mapFile, -1); - } - /** * Open and load a pickmap from the given file. * @param mapFile the map file - * @param index the tab index where this pickmap should be inserted * @return true when pickmap was opened successfully */ - public boolean openPickmap(final File mapFile, final int index) { + public boolean openPickmap(final File mapFile) { // open the pickmaps try { @@ -146,17 +144,10 @@ try { final List<GameObject> objects = decoder.decodeMapFile(); final MapArchObject maparch = decoder.getMapArch(); - final CMapViewBasic bmapview; // go to ArchetypeParser and add the default arch list information to them - bmapview = newPickmap(objects, maparch, mapFile, index); // init the map - - // looks like it worked, so we add a panel and display this pickmap - if (bmapview != null) { - view.addPanel(mapFile.getName(), bmapview, index); - bmapview.getMapControl().setPickmap(true); - return true; - } + newPickmap(objects, maparch, mapFile); // init the map + return true; } finally { decoder.close(); } @@ -183,13 +174,7 @@ mapFile.getParentFile().mkdirs(); } - final CMapViewBasic bmapview = newPickmap(null, maparch, mapFile, -1); - if (bmapview == null) { - return false; - } - - bmapview.getLevel().save(); - setActivePickmap(view.addPanel(mapFile.getName(), bmapview, -1)); + newPickmap(null, maparch, mapFile); actions.setLoadComplete(true); return true; } @@ -198,25 +183,20 @@ * Add a new pickmap. * @param objects list of objects or <code>null</code> for empty * @param maparch the maparch of the pickmap - * @param mapFile File to load the pickmap from. - * @param index tab index to insert, -1 means add to the end - * @return basic mapview + * @param mapFile the file name of the pickmap */ - private CMapViewBasic newPickmap(final List<GameObject> objects, final MapArchObject maparch, final File mapFile, final int index) { + private void newPickmap(final List<GameObject> objects, final MapArchObject maparch, final File mapFile) { final MapControl mapControl = new MapControl(mainControl, objects, maparch, true, true); mapControl.getMapViewFrame().setAutoscrolls(true); mapControl.setMapFile(mapFile); - - // add pickmap to vector - if (index < 0 || index >= pickmaps.size()) { - pickmaps.add(mapControl); - } else { - pickmaps.add(index, mapControl); - } mapControl.resetModified(); - actions.setCurrentPickmap(mapControl); - return mapControl.getMapViewFrame().getBasicView(); + final int index = getIndex(mapFile); + pickmaps.add(index, mapControl); + view.addPanel(mapFile.getName(), mapControl.getMapViewFrame().getBasicView(), index); + + setActivePickmap(index); + actions.setCurrentPickmap(mapControl); } /** @@ -225,63 +205,34 @@ * @return true when closing successful */ public boolean closePickmap(final MapControl mapControl) { - boolean mapClosed = false; - for (int i = 0; !mapClosed && i < pickmaps.size(); i++) { - final MapControl tmpMap = pickmaps.get(i); - if (tmpMap == mapControl) { - pickmaps.remove(i); - view.removePanel(i); - mapClosed = true; - } + final int index = pickmaps.indexOf(mapControl); + if (index == -1) { + return false; } - if (mapClosed) { - updateActivePickmap(); - } - - return mapClosed; + pickmaps.remove(index); + view.removePanel(index); + updateActivePickmap(); + return true; } /** - * Get tab index of a pickmap in the JTabbedPane. - * @param mapControl control - * @return tab index of pickmap, or -1 if it doesn't exist + * Close a pickmap: Remove it from the panel and the data vector. + * @param file the map file name to remove + * @return true when closing successful */ - public int getPickmapTabIndex(final MapControl mapControl) { - for (int i = 0; i < pickmaps.size(); i++) { - if (pickmaps.get(i) == mapControl) { - return i; + public boolean closePickmap(final File file) { + for (final MapControl pickmap : pickmaps) { + if (pickmap.getMapFile().equals(file)) { + return closePickmap(pickmap); } } - return -1; + return false; } /** - * Get tab index of a pickmap in the JTabbedPane by absolut file filename. - * @param filename Filename of the pickmap. - * @return tab index of pickmap, or -1 if it doesn't exist - */ - public int getPickmapTabIndexByName(final String filename) { - for (int i = 0; i < pickmaps.size(); i++) { - if (pickmaps.get(i).getMapFileName().equals(filename)) { - return i; - } - } - return -1; - } - - /** - * Get a pickmap in the JTabbedPane by the index. - * @param index index number - * @return the map - */ - public MapControl getPickmapByIndex(final int index) { - return pickmaps.get(index); - } - - /** * Set pickmap with given tab index to be the active one (ontop). - * @param index tab index + * @param index the index into <code>pickmaps</code> */ public void setActivePickmap(final int index) { view.selectPanel(index); @@ -294,30 +245,13 @@ return; } - boolean foundMap = false; - - final String selectedName = view.getSelectedPanelTitle(); - - // Fix for Mantis #0000541: pickmap selection doesn't work on windows - // This has to be a '/' always, even on Windows, because the pickmap filename is a URI. - final String newName = '/' + selectedName; - - for (MapControl tmp : pickmaps) { - if (tmp != null && tmp.getMapFileName().endsWith(newName)) { - // this is the new active pickmap - actions.setCurrentPickmap(tmp); - foundMap = true; - if (log.isDebugEnabled()) { - log.debug("new pickmap: " + newName); - } - break; - } + final int index = view.getSelectedPanel(); + if (index == -1) { + actions.setCurrentPickmap(null); + return; } - if (!foundMap && isPickmapActive() && isLoadComplete()) { - // error: the new selected pickmap couldn't be found - log.error("Bad Error in PickmapChooserControl.updateActivePickmap: Selected pickmap couldn't be found!"); - } + actions.setCurrentPickmap(pickmaps.get(index)); } /** @@ -424,14 +358,8 @@ return; } - final int tabIndex = getPickmapTabIndex(pickmap); - if (tabIndex < 0) { - return; - } - mainControl.closeLevel(pickmap, true); // close the old map - openPickmap(mfile, tabIndex); // open the new map - setActivePickmap(tabIndex); + openPickmap(mfile); // open the new map // Update the main view so the new map instantly pops up. mainControl.getMainView().update(mainControl.getMainView().getGraphics()); @@ -445,4 +373,19 @@ return view.getPickmapPanel(); } + /** + * Determine where to insert a new pickmap into {@link #pickmaps}. + * + * @param file the file of the pickmap to insert + * + * @return the insertion index + */ + private int getIndex(final File file) { + int i; + for (i = 0; i < view.getPickmapPanel().getTabCount() && file.getName().compareToIgnoreCase(view.getPickmapPanel().getTitleAt(i)) > 0; i++) { + ; + } + return i; + } + } // class PickmapChooserControl Modified: trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserView.java =================================================================== --- trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserView.java 2007-01-09 08:04:17 UTC (rev 1521) +++ trunk/daimonin/src/daieditor/gui/pickmapchooser/PickmapChooserView.java 2007-01-09 21:36:37 UTC (rev 1522) @@ -44,7 +44,7 @@ private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("daieditor"); /** Panel with pickmaps. */ - private JTabbedPane pickmapPanel = new JTabbedPane(SwingConstants.TOP); + private final JTabbedPane pickmapPanel = new JTabbedPane(SwingConstants.TOP); public PickmapChooserView() { pickmapPanel.setComponentPopupMenu(ACTION_FACTORY.createPopupMenu(true, "pickmaps")); @@ -55,14 +55,8 @@ pickmapPanel.addChangeListener(changeListener); } - int addPanel(final String name, final Component mapView, final int index) { - if (index < 0 || index >= pickmapPanel.getTabCount()) { - pickmapPanel.addTab(name, mapView); - return pickmapPanel.getTabCount() - 1; - } else { - pickmapPanel.insertTab(name, null, mapView, null, index); - return index; - } + void addPanel(final String name, final Component mapView, final int index) { + pickmapPanel.insertTab(name, null, mapView, null, index); } void removePanel(final int index) { @@ -70,13 +64,11 @@ } void selectPanel(final int index) { - if (index >= 0 && index < pickmapPanel.getTabCount()) { - pickmapPanel.setSelectedIndex(index); - } + pickmapPanel.setSelectedIndex(index); } - String getSelectedPanelTitle() { - return pickmapPanel.getTitleAt(pickmapPanel.getSelectedIndex()); + int getSelectedPanel() { + return pickmapPanel.getSelectedIndex(); } /** Modified: trunk/daimonin/src/daieditor/map/MapControl.java =================================================================== --- trunk/daimonin/src/daieditor/map/MapControl.java 2007-01-09 08:04:17 UTC (rev 1521) +++ trunk/daimonin/src/daieditor/map/MapControl.java 2007-01-09 21:36:37 UTC (rev 1522) @@ -293,19 +293,8 @@ // TODO } } else { - final int i = mainControl.getMainView().getPickmapChooserControl().getPickmapTabIndexByName(getMapFileName()); - - // -1: this map is not a pickmap - every other value is the tab index - if (i != -1) { - mainControl.getMainView().getPickmapChooserControl().closePickmap(mainControl.getMainView().getPickmapChooserControl().getPickmapByIndex(i)); - mainControl.getMainView().getPickmapChooserControl().openPickmap(mapFile, -1); // open the new map - mainControl.getMainView().getPickmapChooserControl().setActivePickmap(mainControl.getMainView().getPickmapChooserControl().getPickmapTabIndexByName(getMapFileName())); - if (mapViewFrame != null) { - // Update the main view so the new map instantly pops up. - final Graphics g = mapViewFrame.getGraphics(); - assert mapViewFrame != null; - mapViewFrame.update(g); - } + if (mainControl.getMainView().getPickmapChooserControl().closePickmap(mapFile)) { + mainControl.getMainView().getPickmapChooserControl().openPickmap(mapFile); } } if (!isPickmap) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |