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