abf5e74b888dcdd24e70aeb57988a31cfaccfd22
[debian/openrocket] / 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 /**
15  * A manager that loads a number of slide sets from a defined base directory
16  * and provides access to them.
17  * 
18  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19  */
20 public class SlideSetManager {
21         
22         private static final String TOURS_FILE = "tours.txt";
23         private static final String STYLESHEET_FILE = "style.css";
24         
25
26         private final String baseDir;
27         private final Map<String, SlideSet> slideSets = new LinkedHashMap<String, SlideSet>();
28         
29         
30         /**
31          * Sole constructor.
32          * 
33          * @param baseDir       the base directory containing the tours and style files.
34          */
35         public SlideSetManager(String baseDir) {
36                 if (baseDir.length() > 0 && !baseDir.endsWith("/")) {
37                         baseDir = baseDir + "/";
38                 }
39                 this.baseDir = baseDir;
40         }
41         
42         
43         /**
44          * Load all the tours.
45          */
46         public void load() throws IOException {
47                 slideSets.clear();
48                 
49                 List<String> tours = loadTourList();
50                 StyleSheet styleSheet = loadStyleSheet();
51                 
52                 for (String file : tours) {
53                         
54                         String base = baseDir + file;
55                         int index = base.lastIndexOf('/');
56                         if (index >= 0) {
57                                 base = base.substring(0, index);
58                         } else {
59                                 base = "";
60                         }
61                         
62                         SlideSetLoader loader = new SlideSetLoader(base);
63                         SlideSet set = loader.load(file);
64                         set.setStyleSheet(styleSheet);
65                         slideSets.put(file, set);
66                 }
67                 
68         }
69         
70         
71         /**
72          * Return a set containing all the slide set names.
73          */
74         public List<String> getSlideSetNames() {
75                 return new ArrayList<String>(slideSets.keySet());
76         }
77         
78         /**
79          * Retrieve an individual slide set.
80          * 
81          * @param name  the name of the slide set to retrieve.
82          * @return              the slide set (never null)
83          * @throws IllegalArgumentException             if the slide set with the name does not exist.
84          */
85         public SlideSet getSlideSet(String name) {
86                 SlideSet s = slideSets.get(name);
87                 if (s == null) {
88                         throw new IllegalArgumentException("Slide set with name '" + name + "' not found.");
89                 }
90                 return s;
91         }
92         
93         
94         private List<String> loadTourList() throws IOException {
95                 InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE);
96                 if (in == null) {
97                         throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found.");
98                 }
99                 
100                 try {
101                         
102                         List<String> tours = new ArrayList<String>();
103                         TextLineReader reader = new TextLineReader(in);
104                         while (reader.hasNext()) {
105                                 tours.add(reader.next());
106                         }
107                         return tours;
108                         
109                 } finally {
110                         in.close();
111                 }
112         }
113         
114         
115         private StyleSheet loadStyleSheet() throws IOException {
116                 InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE);
117                 if (in == null) {
118                         throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found.");
119                 }
120                 
121                 try {
122                         
123                         StyleSheet ss = new StyleSheet();
124                         InputStreamReader reader = new InputStreamReader(in, "UTF-8");
125                         ss.loadRules(reader, null);
126                         return ss;
127                         
128                 } finally {
129                         in.close();
130                 }
131                 
132         }
133         
134 }