1 package net.sf.openrocket.gui.help.tours;
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;
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;
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;
35 public class GuidedTourSelectionDialog extends JDialog {
37 private static final Translator trans = Application.getTranslator();
39 private static final String TOURS_BASE_DIR = "datafiles/tours";
42 private final SlideSetManager slideSetManager;
43 private final List<String> tourNames;
45 private SlideShowDialog slideShowDialog;
47 private JList tourList;
48 private JEditorPane tourDescription;
49 private JLabel tourLength;
52 public GuidedTourSelectionDialog(Window parent) {
53 super(parent, trans.get("title"), ModalityType.MODELESS);
57 slideSetManager = new SlideSetManager(TOURS_BASE_DIR);
58 slideSetManager.load();
60 tourNames = slideSetManager.getSlideSetNames();
61 if (tourNames.isEmpty()) {
62 throw new FileNotFoundException("No tours found.");
65 } catch (IOException e) {
66 throw new BugException(e);
70 JPanel panel = new JPanel(new MigLayout("fill"));
72 panel.add(new StyledLabel(trans.get("lbl.selectTour"), Style.BOLD), "spanx, wrap rel");
74 tourList = new JList(new TourListModel());
75 tourList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
76 tourList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
78 public void valueChanged(ListSelectionEvent e) {
82 tourList.addMouseListener(new MouseAdapter() {
84 public void mouseClicked(MouseEvent e) {
85 if (e.getClickCount() == 2) {
90 panel.add(new JScrollPane(tourList), "grow, gapright unrel, w 200lp, h 150lp");
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");
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");
104 tourLength = new StyledLabel(-1);
105 sub.add(tourLength, "wrap unrel");
107 JButton start = new JButton(trans.get("btn.start"));
108 start.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
114 sub.add(start, "growx");
116 panel.add(sub, "grow, wrap para, w 200lp, h 150lp");
120 JButton close = new JButton(trans.get("button.close"));
121 close.addActionListener(new ActionListener() {
123 public void actionPerformed(ActionEvent e) {
124 GuidedTourSelectionDialog.this.dispose();
127 panel.add(close, "spanx, right");
130 GUIUtil.setDisposableDialogOptions(this, close);
131 tourList.setSelectedIndex(0);
135 private void startTour() {
136 SlideSet ss = getSelectedSlideSet();
141 if (slideShowDialog != null && !slideShowDialog.isVisible()) {
145 if (slideShowDialog == null) {
146 slideShowDialog = new SlideShowDialog(this);
149 slideShowDialog.setSlideSet(ss, 0);
150 slideShowDialog.setVisible(true);
154 private void closeTour() {
155 if (slideShowDialog != null) {
156 slideShowDialog.dispose();
157 slideShowDialog = null;
162 private void updateText() {
163 SlideSet ss = getSelectedSlideSet();
165 tourDescription.setText(ss.getDescription());
166 tourLength.setText(trans.get("lbl.length") + " " + ss.getSlideCount());
168 tourDescription.setText("");
169 tourLength.setText(trans.get("lbl.length"));
174 @SuppressWarnings("unchecked")
175 private SlideSet getSelectedSlideSet() {
176 return ((Named<SlideSet>) tourList.getSelectedValue()).get();
179 private class TourListModel extends AbstractListModel {
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());
189 public int getSize() {
190 return tourNames.size();