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