001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.starter;
025:
026: import jacareto.record.RecordException;
027: import jacareto.record.XMLRecord;
028: import jacareto.system.Environment;
029:
030: import java.io.File;
031:
032: import java.util.Enumeration;
033: import java.util.Iterator;
034:
035: import javax.swing.DefaultListModel;
036:
037: /**
038: * The list of starters.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.0
042: */
043: public class StarterList extends DefaultListModel {
044: /** The env. */
045: private Environment env;
046:
047: /**
048: * Creates a new starter list with the starters contained in the given xml file.
049: *
050: * @param env the environment
051: * @param filename the filename of the xml file containing the starters
052: */
053: public StarterList(Environment env, String filename) {
054: this .env = env;
055:
056: if (filename != null) {
057: loadStarters(filename);
058: }
059: }
060:
061: /**
062: * Creates a new, initially empty starter list.
063: *
064: * @param env the environment
065: */
066: public StarterList(Environment env) {
067: this (env, null);
068: }
069:
070: /**
071: * Returns the first starter with the given name, or <code>null</code> if the starter is not in
072: * the list.
073: *
074: * @param name the name of the starter
075: *
076: * @return the starter, or <code>null</code> if the starter is not contained in the list
077: */
078: public Starter getStarter(String name) {
079: Enumeration e = elements();
080:
081: while (e.hasMoreElements()) {
082: Starter starter = (Starter) e.nextElement();
083:
084: if (starter.getName().equals(name)) {
085: return starter;
086: }
087: }
088:
089: return null;
090: }
091:
092: /**
093: * Loads starters out of a xml file. The list will not be cleared; the starters contained in
094: * the xml file will be added to the current list.
095: *
096: * @param filename the filename of the xml file
097: */
098: public void loadStarters(String filename) {
099: try {
100: if ((filename != null) && (new File(filename)).exists()) {
101: XMLRecord record = new XMLRecord(env, filename,
102: "StarterList.XMLRecord", XMLRecord.INIT_CUSTOM);
103: record.setSaveBeforeClosing(false);
104: record.open();
105:
106: Iterator i = record.iterator();
107:
108: while (i.hasNext()) {
109: addElement(i.next());
110: }
111:
112: record.close();
113: } else {
114: env
115: .getLogger()
116: .error(
117: env
118: .getLanguage()
119: .getString(
120: "Starters.StarterList.Error.OpenStarterFile"));
121: }
122: } catch (RecordException l) {
123: env.getLogger().error(
124: env.getLanguage().getString(
125: "Starters.StarterList.Error.LoadStarters"));
126: }
127: }
128:
129: /**
130: * Saves the starters to a xml file with the specified filename.
131: *
132: * @param filename the filename of the xml file
133: */
134: public void saveStarters(String filename) {
135: try {
136: if (filename != null) {
137: XMLRecord record = new XMLRecord(env, filename,
138: "StarterList.XMLRecord", XMLRecord.INIT_CUSTOM);
139: record.open();
140: record.clear();
141:
142: Enumeration e = elements();
143:
144: while (e.hasMoreElements()) {
145: record.record((Starter) e.nextElement());
146: }
147:
148: record.close();
149: }
150: } catch (RecordException l) {
151: env.getLogger().error(
152: env.getLanguage().getString(
153: "Starters.StarterList.Error.SaveStarters"));
154: }
155: }
156: }
|