create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / help / tours / SlideSetManager.java
1 package net.sf.openrocket.gui.help.tours;
2
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;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.swing.text.html.StyleSheet;
13
14 import net.sf.openrocket.util.BugException;
15
16 /**
17  * A manager that loads a number of slide sets from a defined base directory
18  * and provides access to them.
19  * 
20  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21  */
22 public class SlideSetManager {
23         private static final String TOURS_BASE_DIR = "datafiles/tours";
24         
25         private static final String TOURS_FILE = "tours.txt";
26         private static final String STYLESHEET_FILE = "style.css";
27         
28         private static SlideSetManager slideSetManager = null;
29         
30         
31         private final String baseDir;
32         private final Map<String, SlideSet> slideSets = new LinkedHashMap<String, SlideSet>();
33         
34         
35         /**
36          * Sole constructor.
37          * 
38          * @param baseDir       the base directory containing the tours and style files.
39          */
40         public SlideSetManager(String baseDir) {
41                 if (baseDir.length() > 0 && !baseDir.endsWith("/")) {
42                         baseDir = baseDir + "/";
43                 }
44                 this.baseDir = baseDir;
45         }
46         
47         
48         /**
49          * Load all the tours.
50          */
51         public void load() throws IOException {
52                 slideSets.clear();
53                 
54                 List<String> tours = loadTourList();
55                 StyleSheet styleSheet = loadStyleSheet();
56                 
57                 for (String fileAndDir : tours) {
58                         String base;
59                         String file;
60                         
61                         String fullFileAndDir = baseDir + fileAndDir;
62                         int index = fullFileAndDir.lastIndexOf('/');
63                         if (index >= 0) {
64                                 base = fullFileAndDir.substring(0, index);
65                                 file = fullFileAndDir.substring(index + 1);
66                         } else {
67                                 base = "";
68                                 file = "";
69                         }
70                         
71                         SlideSetLoader loader = new SlideSetLoader(base);
72                         SlideSet set = loader.load(file);
73                         set.setStyleSheet(styleSheet);
74                         slideSets.put(fileAndDir, set);
75                 }
76                 
77         }
78         
79         
80         /**
81          * Return a set containing all the slide set names.
82          */
83         public List<String> getSlideSetNames() {
84                 return new ArrayList<String>(slideSets.keySet());
85         }
86         
87         /**
88          * Retrieve an individual slide set.
89          * 
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.
93          */
94         public SlideSet getSlideSet(String name) {
95                 SlideSet s = slideSets.get(name);
96                 if (s == null) {
97                         throw new IllegalArgumentException("Slide set with name '" + name + "' not found.");
98                 }
99                 return s;
100         }
101         
102         
103         private List<String> loadTourList() throws IOException {
104                 InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE);
105                 if (in == null) {
106                         throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found.");
107                 }
108                 
109                 try {
110                         
111                         List<String> tours = new ArrayList<String>();
112                         TextLineReader reader = new TextLineReader(in);
113                         while (reader.hasNext()) {
114                                 tours.add(reader.next());
115                         }
116                         return tours;
117                         
118                 } finally {
119                         in.close();
120                 }
121         }
122         
123         
124         private StyleSheet loadStyleSheet() throws IOException {
125                 InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE);
126                 if (in == null) {
127                         throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found.");
128                 }
129                 
130                 try {
131                         
132                         StyleSheet ss = new StyleSheet();
133                         InputStreamReader reader = new InputStreamReader(in, "UTF-8");
134                         ss.loadRules(reader, null);
135                         return ss;
136                         
137                 } finally {
138                         in.close();
139                 }
140                 
141         }
142         
143         
144         
145         /**
146          * Return a singleton implementation that has loaded the default tours.
147          */
148         public static SlideSetManager getSlideSetManager() {
149                 if (slideSetManager == null) {
150                         try {
151                                 SlideSetManager ssm = new SlideSetManager(TOURS_BASE_DIR);
152                                 ssm.load();
153                                 
154                                 if (ssm.getSlideSetNames().isEmpty()) {
155                                         throw new FileNotFoundException("No tours found.");
156                                 }
157                                 
158                                 slideSetManager = ssm;
159                         } catch (IOException e) {
160                                 throw new BugException(e);
161                         }
162                 }
163                 return slideSetManager;
164         }
165 }