From: <aki...@us...> - 2008-08-21 22:03:00
|
Revision: 4969 http://gridarta.svn.sourceforge.net/gridarta/?rev=4969&view=rev Author: akirschbaum Date: 2008-08-21 22:03:05 +0000 (Thu, 21 Aug 2008) Log Message: ----------- Extract unrelated code from Spells into SpellsUtils. Modified Paths: -------------- trunk/daimonin/src/daieditor/CMainControl.java trunk/daimonin/src/daieditor/spells/Spells.java Added Paths: ----------- trunk/src/app/net/sf/gridarta/spells/SpellsUtils.java Modified: trunk/daimonin/src/daieditor/CMainControl.java =================================================================== --- trunk/daimonin/src/daieditor/CMainControl.java 2008-08-21 21:51:50 UTC (rev 4968) +++ trunk/daimonin/src/daieditor/CMainControl.java 2008-08-21 22:03:05 UTC (rev 4969) @@ -39,6 +39,7 @@ import daieditor.map.validation.checks.ExitChecker; import daieditor.map.validation.checks.SlayingChecker; import daieditor.map.validation.checks.TilePathsChecker; +import net.sf.gridarta.spells.SpellsUtils; import java.awt.Point; import java.io.BufferedReader; import java.io.File; @@ -492,7 +493,7 @@ /** Collect Spells. */ @ActionMethod public void collectSpells() { - numberSpells.importSpellsWanted(getConfigurationDirectory(), mainView); + new SpellsUtils(IGUIConstants.SPELL_FILE).importSpellsWanted(getConfigurationDirectory(), mainView); } /** Edit an existing script. */ Modified: trunk/daimonin/src/daieditor/spells/Spells.java =================================================================== --- trunk/daimonin/src/daieditor/spells/Spells.java 2008-08-21 21:51:50 UTC (rev 4968) +++ trunk/daimonin/src/daieditor/spells/Spells.java 2008-08-21 22:03:05 UTC (rev 4969) @@ -19,28 +19,7 @@ package daieditor.spells; -import daieditor.IGUIConstants; -import java.awt.Component; -import java.io.BufferedReader; -import java.io.EOFException; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.util.Date; -import java.util.Map; -import java.util.TreeMap; -import javax.swing.JFileChooser; -import javax.swing.filechooser.FileFilter; -import net.sf.gridarta.io.IOUtils; import net.sf.gridarta.spells.NumberSpell; -import net.sf.japi.swing.ActionFactory; -import net.sf.japi.util.filter.file.FilenameFileFilter; -import org.apache.log4j.Logger; -import org.jetbrains.annotations.NotNull; /** * This class manages the spells and spell lists. @@ -49,132 +28,4 @@ */ public final class Spells extends net.sf.gridarta.spells.Spells<NumberSpell> { - /** The Logger for printing log messages. */ - private static final Logger log = Logger.getLogger(Spells.class); - - /** Action Factory. */ - private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("daieditor"); - - /** File filter for filtering spellist.h files. */ - private static final FileFilter SPELLIST_H_FILE_FILTER = new FilenameFileFilter(true, "spellist.h", "spellist.h"); - - /** - * Opens a file chooser to select the spellist file, then import spells. - * @param baseDir the directory to use - * @param parent Component the parent component for dialog boxes - */ - public static void importSpellsWanted(@NotNull final String baseDir, @NotNull final Component parent) { - // open a file chooser window - final JFileChooser fileChooser = new JFileChooser(); - fileChooser.setDialogTitle("Open CF Spellist File"); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setMultiSelectionEnabled(false); - fileChooser.setFileFilter(SPELLIST_H_FILE_FILTER); // apply file filter - final File cd = new File(System.getProperty("user.dir")); - final File sd = new File(cd, "../server/src/include"); - fileChooser.setCurrentDirectory(sd.exists() ? sd : cd); - - final int returnVal = fileChooser.showOpenDialog(parent); - - if (returnVal == JFileChooser.APPROVE_OPTION) { - // now import spells from selected file - final File spellfile = fileChooser.getSelectedFile(); - final int spnum = importSpells(spellfile, baseDir); - if (spnum > 0) { - // yeah it worked - ACTION_FACTORY.showMessageDialog(parent, "importSpellsSuccess", spnum); - } else { - // spell collect failed - ACTION_FACTORY.showMessageDialog(parent, "importSpellsFailed"); - } - } - } - - /** - * Read all spells from a Crossfire or Daimonin spellist file and write an - * alphabetical list into "spells.def". - * @param spellfile spellfile to read - * @param baseDir The base directory to load the spells file from. - * @return number of successfully collected spells - */ - private static int importSpells(final File spellfile, final String baseDir) { - final Map<String, String> spells = new TreeMap<String, String>(); - if (spellfile.getName().equalsIgnoreCase("spellist.h")) { - try { - final BufferedReader in = new BufferedReader(new FileReader(spellfile.getAbsolutePath())); - try { - IOUtils.readUntil(in, "spell spells", null); - IOUtils.readUntil(in, "{", null); - - int counter = 0; - for (; ;) { - IOUtils.readUntil(in, "{", "}"); - IOUtils.readUntil(in, "\"", null); - final String name = IOUtils.readUntil(in, "\"").trim(); - IOUtils.readUntil(in, "}", null); - - spells.put(name, Integer.toString(counter++)); - } - } finally { - in.close(); - } - } catch (final FileNotFoundException e) { - log.error("File '" + spellfile.getAbsolutePath() + "' not found!"); - } catch (final EOFException e) { - // Eventually expected exception, don't handle. - } catch (final IOException e) { - log.error("Cannot read file '" + spellfile.getAbsolutePath() + "'!"); - } - } - - // --------- now write the "spells.def" file --------- - if (!spells.isEmpty()) { - // FIXME: This should use DOM for writing the file. - final File dfile; - // create new file for writing (replaces old one if existent) - if (baseDir.length() > 0) { - final File dir = new File(baseDir); - if (!dir.exists() || !dir.isDirectory()) { // FIXME What if dir exists and is not a directory? mkdir will fail then! - // create the config dir - new File("resource").mkdir(); - new File(baseDir).mkdir(); - } - dfile = new File(baseDir, IGUIConstants.SPELL_FILE); - } else { - dfile = new File(IGUIConstants.SPELL_FILE); - } - - try { - final PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(dfile), "utf-8")); - try { - // header: - out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); - out.println("<!DOCTYPE spells SYSTEM \"spells.dtd\">"); - out.println("<!--"); - out.println(" - ##########################################################"); - out.println(" - # You may add new spells to this file, but there's no #"); - out.println(" - # need to do it because the file can be autogenerated. #"); - out.println(" - # In the editor, select menu \"Resources->Collect Spells\" #"); - out.println(" - # to generate a new version of this file. #"); - out.println(" - ##########################################################"); - out.println(" -->"); - out.println("<!-- Generated on: " + new Date() + " -->"); - out.println("<spells>"); - - final String[] spaces = {" ", " ", ""}; - for (final String name : spells.keySet()) { - final String id = spells.get(name); - out.println(" <spell id=\"" + id + '\"' + spaces[id.length() - 1] + " name=\"" + name + "\" />"); - } - out.println("</spells>"); - } finally { - out.close(); - } - } catch (final IOException e) { - log.error("Cannot write file '" + dfile.getAbsolutePath() + "'!"); - } - } - return spells.size(); - } - } // class Spells Added: trunk/src/app/net/sf/gridarta/spells/SpellsUtils.java =================================================================== --- trunk/src/app/net/sf/gridarta/spells/SpellsUtils.java (rev 0) +++ trunk/src/app/net/sf/gridarta/spells/SpellsUtils.java 2008-08-21 22:03:05 UTC (rev 4969) @@ -0,0 +1,190 @@ +/* + * 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.spells; + +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; +import net.sf.japi.swing.ActionFactory; +import net.sf.japi.util.filter.file.FilenameFileFilter; +import net.sf.gridarta.io.IOUtils; +import javax.swing.filechooser.FileFilter; +import javax.swing.JFileChooser; +import java.awt.Component; +import java.io.File; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileNotFoundException; +import java.io.EOFException; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.OutputStreamWriter; +import java.io.FileOutputStream; +import java.util.Map; +import java.util.TreeMap; +import java.util.Date; + +/** + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class SpellsUtils { + + /** The Logger for printing log messages. */ + private static final Logger log = Logger.getLogger(SpellsUtils.class); + + /** Action Factory. */ + private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("net.sf.gridarta"); + + /** File filter for filtering spellist.h files. */ + private static final FileFilter SPELLIST_H_FILE_FILTER = new FilenameFileFilter(true, "spellist.h", "spellist.h"); + + /** + * The spell file name. + */ + @NotNull + private String spellFile; + + /** + * Creates a new instance. + * @param spellFile the spell file name + */ + public SpellsUtils(@NotNull final String spellFile) { + this.spellFile = spellFile; + } + + /** + * Opens a file chooser to select the spellist file, then import spells. + * @param baseDir the directory to use + * @param parent Component the parent component for dialog boxes + */ + public void importSpellsWanted(@NotNull final String baseDir, @NotNull final Component parent) { + // open a file chooser window + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Open CF Spellist File"); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setMultiSelectionEnabled(false); + fileChooser.setFileFilter(SPELLIST_H_FILE_FILTER); // apply file filter + final File cd = new File(System.getProperty("user.dir")); + final File sd = new File(cd, "../server/src/include"); + fileChooser.setCurrentDirectory(sd.exists() ? sd : cd); + + final int returnVal = fileChooser.showOpenDialog(parent); + + if (returnVal == JFileChooser.APPROVE_OPTION) { + // now import spells from selected file + final File spellfile = fileChooser.getSelectedFile(); + final int spnum = importSpells(spellfile, baseDir); + if (spnum > 0) { + // yeah it worked + ACTION_FACTORY.showMessageDialog(parent, "importSpellsSuccess", spnum); + } else { + // spell collect failed + ACTION_FACTORY.showMessageDialog(parent, "importSpellsFailed"); + } + } + } + + /** + * Read all spells from a Crossfire or Daimonin spellist file and write an + * alphabetical list into "spells.def". + * @param spellfile spellfile to read + * @param baseDir The base directory to load the spells file from. + * @return number of successfully collected spells + */ + private int importSpells(final File spellfile, final String baseDir) { + final Map<String, String> spells = new TreeMap<String, String>(); + if (spellfile.getName().equalsIgnoreCase("spellist.h")) { + try { + final BufferedReader in = new BufferedReader(new FileReader(spellfile.getAbsolutePath())); + try { + IOUtils.readUntil(in, "spell spells", null); + IOUtils.readUntil(in, "{", null); + + int counter = 0; + for (; ;) { + IOUtils.readUntil(in, "{", "}"); + IOUtils.readUntil(in, "\"", null); + final String name = IOUtils.readUntil(in, "\"").trim(); + IOUtils.readUntil(in, "}", null); + + spells.put(name, Integer.toString(counter++)); + } + } finally { + in.close(); + } + } catch (final FileNotFoundException e) { + log.error("File '" + spellfile.getAbsolutePath() + "' not found!"); + } catch (final EOFException e) { + // Eventually expected exception, don't handle. + } catch (final IOException e) { + log.error("Cannot read file '" + spellfile.getAbsolutePath() + "'!"); + } + } + + // --------- now write the "spells.def" file --------- + if (!spells.isEmpty()) { + // FIXME: This should use DOM for writing the file. + final File dfile; + // create new file for writing (replaces old one if existent) + if (baseDir.length() > 0) { + final File dir = new File(baseDir); + if (!dir.exists() || !dir.isDirectory()) { // FIXME What if dir exists and is not a directory? mkdir will fail then! + // create the config dir + new File("resource").mkdir(); + new File(baseDir).mkdir(); + } + dfile = new File(baseDir, spellFile); + } else { + dfile = new File(spellFile); + } + + try { + final PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(dfile), "utf-8")); + try { + // header: + out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); + out.println("<!DOCTYPE spells SYSTEM \"spells.dtd\">"); + out.println("<!--"); + out.println(" - ##########################################################"); + out.println(" - # You may add new spells to this file, but there's no #"); + out.println(" - # need to do it because the file can be autogenerated. #"); + out.println(" - # In the editor, select menu \"Resources->Collect Spells\" #"); + out.println(" - # to generate a new version of this file. #"); + out.println(" - ##########################################################"); + out.println(" -->"); + out.println("<!-- Generated on: " + new Date() + " -->"); + out.println("<spells>"); + + final String[] spaces = {" ", " ", ""}; + for (final String name : spells.keySet()) { + final String id = spells.get(name); + out.println(" <spell id=\"" + id + '\"' + spaces[id.length() - 1] + " name=\"" + name + "\" />"); + } + out.println("</spells>"); + } finally { + out.close(); + } + } catch (final IOException e) { + log.error("Cannot write file '" + dfile.getAbsolutePath() + "'!"); + } + } + return spells.size(); + } + +} // class SpellsUtils Property changes on: trunk/src/app/net/sf/gridarta/spells/SpellsUtils.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |