1 package net.sf.openrocket.gui.help.tours;
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.ArrayList;
8 import java.util.LinkedHashMap;
12 import javax.swing.text.html.StyleSheet;
14 import net.sf.openrocket.util.BugException;
17 * A manager that loads a number of slide sets from a defined base directory
18 * and provides access to them.
20 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22 public class SlideSetManager {
23 private static final String TOURS_BASE_DIR = "datafiles/tours";
25 private static final String TOURS_FILE = "tours.txt";
26 private static final String STYLESHEET_FILE = "style.css";
28 private static SlideSetManager slideSetManager = null;
31 private final String baseDir;
32 private final Map<String, SlideSet> slideSets = new LinkedHashMap<String, SlideSet>();
38 * @param baseDir the base directory containing the tours and style files.
40 public SlideSetManager(String baseDir) {
41 if (baseDir.length() > 0 && !baseDir.endsWith("/")) {
42 baseDir = baseDir + "/";
44 this.baseDir = baseDir;
51 public void load() throws IOException {
54 List<String> tours = loadTourList();
55 StyleSheet styleSheet = loadStyleSheet();
57 for (String fileAndDir : tours) {
61 String fullFileAndDir = baseDir + fileAndDir;
62 int index = fullFileAndDir.lastIndexOf('/');
64 base = fullFileAndDir.substring(0, index);
65 file = fullFileAndDir.substring(index + 1);
71 SlideSetLoader loader = new SlideSetLoader(base);
72 SlideSet set = loader.load(file);
73 set.setStyleSheet(styleSheet);
74 slideSets.put(fileAndDir, set);
81 * Return a set containing all the slide set names.
83 public List<String> getSlideSetNames() {
84 return new ArrayList<String>(slideSets.keySet());
88 * Retrieve an individual slide set.
90 * @param name the name of the slide set to retrieve.
91 * @return the slide set (never null)
92 * @throws IllegalArgumentException if the slide set with the name does not exist.
94 public SlideSet getSlideSet(String name) {
95 SlideSet s = slideSets.get(name);
97 throw new IllegalArgumentException("Slide set with name '" + name + "' not found.");
103 private List<String> loadTourList() throws IOException {
104 InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE);
106 throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found.");
111 List<String> tours = new ArrayList<String>();
112 TextLineReader reader = new TextLineReader(in);
113 while (reader.hasNext()) {
114 tours.add(reader.next());
124 private StyleSheet loadStyleSheet() throws IOException {
125 InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE);
127 throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found.");
132 StyleSheet ss = new StyleSheet();
133 InputStreamReader reader = new InputStreamReader(in, "UTF-8");
134 ss.loadRules(reader, null);
146 * Return a singleton implementation that has loaded the default tours.
148 public static SlideSetManager getSlideSetManager() {
149 if (slideSetManager == null) {
151 SlideSetManager ssm = new SlideSetManager(TOURS_BASE_DIR);
154 if (ssm.getSlideSetNames().isEmpty()) {
155 throw new FileNotFoundException("No tours found.");
158 slideSetManager = ssm;
159 } catch (IOException e) {
160 throw new BugException(e);
163 return slideSetManager;