c2ede5e2daf1427529fabac1ad7507480e82b7b6
[debian/openrocket] / src / net / sf / openrocket / gui / help / tours / GuidedTourSelectionDialog.java
1 package net.sf.openrocket.gui.help.tours;
2
3 import java.awt.Window;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.MouseAdapter;
7 import java.awt.event.MouseEvent;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.util.List;
11
12 import javax.swing.AbstractListModel;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JEditorPane;
16 import javax.swing.JLabel;
17 import javax.swing.JList;
18 import javax.swing.JPanel;
19 import javax.swing.JScrollPane;
20 import javax.swing.ListSelectionModel;
21 import javax.swing.event.ListSelectionEvent;
22 import javax.swing.event.ListSelectionListener;
23 import javax.swing.text.html.HTMLDocument;
24 import javax.swing.text.html.StyleSheet;
25
26 import net.miginfocom.swing.MigLayout;
27 import net.sf.openrocket.gui.components.StyledLabel;
28 import net.sf.openrocket.gui.components.StyledLabel.Style;
29 import net.sf.openrocket.gui.util.GUIUtil;
30 import net.sf.openrocket.l10n.Translator;
31 import net.sf.openrocket.startup.Application;
32 import net.sf.openrocket.util.BugException;
33 import net.sf.openrocket.util.Named;
34
35 public class GuidedTourSelectionDialog extends JDialog {
36         
37         private static final Translator trans = Application.getTranslator();
38         
39         private static final String TOURS_BASE_DIR = "datafiles/tours";
40         
41
42         private final SlideSetManager slideSetManager;
43         private final List<String> tourNames;
44         
45         private SlideShowDialog slideShowDialog;
46         
47         private JList tourList;
48         private JEditorPane tourDescription;
49         private JLabel tourLength;
50         
51         
52         public GuidedTourSelectionDialog(Window parent) {
53                 super(parent, trans.get("title"), ModalityType.MODELESS);
54                 
55                 try {
56                         
57                         slideSetManager = new SlideSetManager(TOURS_BASE_DIR);
58                         slideSetManager.load();
59                         
60                         tourNames = slideSetManager.getSlideSetNames();
61                         if (tourNames.isEmpty()) {
62                                 throw new FileNotFoundException("No tours found.");
63                         }
64                         
65                 } catch (IOException e) {
66                         throw new BugException(e);
67                 }
68                 
69
70                 JPanel panel = new JPanel(new MigLayout("fill"));
71                 
72                 panel.add(new StyledLabel(trans.get("lbl.selectTour"), Style.BOLD), "spanx, wrap rel");
73                 
74                 tourList = new JList(new TourListModel());
75                 tourList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
76                 tourList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
77                         @Override
78                         public void valueChanged(ListSelectionEvent e) {
79                                 updateText();
80                         }
81                 });
82                 tourList.addMouseListener(new MouseAdapter() {
83                         @Override
84                         public void mouseClicked(MouseEvent e) {
85                                 if (e.getClickCount() == 2) {
86                                         startTour();
87                                 }
88                         }
89                 });
90                 panel.add(new JScrollPane(tourList), "grow, gapright unrel, w 200lp, h 150lp");
91                 
92
93
94                 //  Sub-panel containing description and start button
95                 JPanel sub = new JPanel(new MigLayout("fill, ins 0"));
96                 sub.add(new StyledLabel(trans.get("lbl.description"), -1), "wrap rel");
97                 
98                 tourDescription = new JEditorPane("text/html", "");
99                 tourDescription.setEditable(false);
100                 StyleSheet ss = slideSetManager.getSlideSet(tourNames.get(0)).getStyleSheet();
101                 ((HTMLDocument) tourDescription.getDocument()).getStyleSheet().addStyleSheet(ss);
102                 sub.add(new JScrollPane(tourDescription), "grow, wrap rel");
103                 
104                 tourLength = new StyledLabel(-1);
105                 sub.add(tourLength, "wrap unrel");
106                 
107                 JButton start = new JButton(trans.get("btn.start"));
108                 start.addActionListener(new ActionListener() {
109                         @Override
110                         public void actionPerformed(ActionEvent e) {
111                                 startTour();
112                         }
113                 });
114                 sub.add(start, "growx");
115                 
116                 panel.add(sub, "grow, wrap para, w 200lp, h 150lp");
117                 
118
119
120                 JButton close = new JButton(trans.get("button.close"));
121                 close.addActionListener(new ActionListener() {
122                         @Override
123                         public void actionPerformed(ActionEvent e) {
124                                 GuidedTourSelectionDialog.this.dispose();
125                         }
126                 });
127                 panel.add(close, "spanx, right");
128                 
129                 this.add(panel);
130                 GUIUtil.setDisposableDialogOptions(this, close);
131                 tourList.setSelectedIndex(0);
132         }
133         
134         
135         private void startTour() {
136                 SlideSet ss = getSelectedSlideSet();
137                 if (ss == null) {
138                         return;
139                 }
140                 
141                 if (slideShowDialog != null && !slideShowDialog.isVisible()) {
142                         closeTour();
143                 }
144                 
145                 if (slideShowDialog == null) {
146                         slideShowDialog = new SlideShowDialog(this);
147                 }
148                 
149                 slideShowDialog.setSlideSet(ss, 0);
150                 slideShowDialog.setVisible(true);
151         }
152         
153         
154         private void closeTour() {
155                 if (slideShowDialog != null) {
156                         slideShowDialog.dispose();
157                         slideShowDialog = null;
158                 }
159         }
160         
161         
162         private void updateText() {
163                 SlideSet ss = getSelectedSlideSet();
164                 if (ss != null) {
165                         tourDescription.setText(ss.getDescription());
166                         tourLength.setText(trans.get("lbl.length") + " " + ss.getSlideCount());
167                 } else {
168                         tourDescription.setText("");
169                         tourLength.setText(trans.get("lbl.length"));
170                 }
171         }
172         
173         
174         @SuppressWarnings("unchecked")
175         private SlideSet getSelectedSlideSet() {
176                 return ((Named<SlideSet>) tourList.getSelectedValue()).get();
177         }
178         
179         private class TourListModel extends AbstractListModel {
180                 
181                 @Override
182                 public Object getElementAt(int index) {
183                         String name = tourNames.get(index);
184                         SlideSet set = slideSetManager.getSlideSet(name);
185                         return new Named<SlideSet>(set, set.getTitle());
186                 }
187                 
188                 @Override
189                 public int getSize() {
190                         return tourNames.size();
191                 }
192                 
193         }
194         
195
196
197 }