create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / help / tours / SlideShowDialog.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.KeyEvent;
7
8 import javax.swing.AbstractAction;
9 import javax.swing.Action;
10 import javax.swing.JButton;
11 import javax.swing.JComponent;
12 import javax.swing.JDialog;
13 import javax.swing.JPanel;
14 import javax.swing.JRootPane;
15 import javax.swing.KeyStroke;
16
17 import net.miginfocom.swing.MigLayout;
18 import net.sf.openrocket.gui.util.GUIUtil;
19 import net.sf.openrocket.l10n.Translator;
20 import net.sf.openrocket.logging.LogHelper;
21 import net.sf.openrocket.startup.Application;
22 import net.sf.openrocket.util.BugException;
23 import net.sf.openrocket.util.Chars;
24
25 public class SlideShowDialog extends JDialog {
26         
27         private static final LogHelper log = Application.getLogger();
28         private static final Translator trans = Application.getTranslator();
29         
30         private SlideShowComponent slideShowComponent;
31         private SlideSet slideSet;
32         private int position;
33         
34         private JButton nextButton;
35         private JButton prevButton;
36         private JButton closeButton;
37         
38         
39         public SlideShowDialog(Window parent) {
40                 super(parent, ModalityType.MODELESS);
41                 
42                 JPanel panel = new JPanel(new MigLayout("fill"));
43                 
44                 slideShowComponent = new SlideShowComponent();
45                 slideShowComponent.addHyperlinkListener(new SlideShowLinkListener(parent));
46                 panel.add(slideShowComponent, "spanx, grow, wrap para");
47                 
48                 
49                 JPanel sub = new JPanel(new MigLayout("ins 0, fill"));
50                 
51                 prevButton = new JButton(Chars.LEFT_ARROW + " " + trans.get("btn.prev"));
52                 prevButton.addActionListener(new ActionListener() {
53                         @Override
54                         public void actionPerformed(ActionEvent e) {
55                                 log.user("Clicked previous button");
56                                 setPosition(position - 1);
57                         }
58                 });
59                 sub.add(prevButton, "left");
60                 
61                 
62                 
63                 nextButton = new JButton(trans.get("btn.next") + " " + Chars.RIGHT_ARROW);
64                 nextButton.addActionListener(new ActionListener() {
65                         @Override
66                         public void actionPerformed(ActionEvent e) {
67                                 log.user("Clicked next button");
68                                 setPosition(position + 1);
69                         }
70                 });
71                 sub.add(nextButton, "left, gapleft para");
72                 
73                 
74                 sub.add(new JPanel(), "growx");
75                 
76                 
77                 closeButton = new JButton(trans.get("button.close"));
78                 closeButton.addActionListener(new ActionListener() {
79                         @Override
80                         public void actionPerformed(ActionEvent e) {
81                                 SlideShowDialog.this.dispose();
82                         }
83                 });
84                 sub.add(closeButton, "right");
85                 
86                 
87                 panel.add(sub, "growx");
88                 
89                 this.add(panel);
90                 updateEnabled();
91                 addKeyActions();
92                 GUIUtil.setDisposableDialogOptions(this, nextButton);
93                 nextButton.grabFocus();
94                 GUIUtil.rememberWindowPosition(this);
95                 GUIUtil.rememberWindowSize(this);
96                 //              this.setAlwaysOnTop(true);
97         }
98         
99         public void setSlideSet(SlideSet slideSet, int position) {
100                 this.slideSet = slideSet;
101                 this.setTitle(slideSet.getTitle() + " " + Chars.EMDASH + " OpenRocket");
102                 slideShowComponent.setStyleSheet(slideSet.getStyleSheet());
103                 setPosition(position);
104         }
105         
106         public void setPosition(int position) {
107                 if (this.slideSet == null) {
108                         throw new BugException("setPosition called when slideSet is null");
109                 }
110                 
111                 if (position < 0 || position >= slideSet.getSlideCount()) {
112                         throw new BugException("position exceeds slide count, position=" + position +
113                                         " slideCount=" + slideSet.getSlideCount());
114                 }
115                 
116                 this.position = position;
117                 slideShowComponent.setSlide(slideSet.getSlide(position));
118                 updateEnabled();
119         }
120         
121         
122         private void updateEnabled() {
123                 if (slideSet == null) {
124                         prevButton.setEnabled(false);
125                         nextButton.setEnabled(false);
126                         return;
127                 }
128                 
129                 prevButton.setEnabled(position > 0);
130                 nextButton.setEnabled(position < slideSet.getSlideCount() - 1);
131         }
132         
133         
134         
135         
136         
137         private void addKeyActions() {
138                 Action next = new AbstractAction() {
139                         @Override
140                         public void actionPerformed(ActionEvent event) {
141                                 log.user("Key action for next slide");
142                                 if (position < slideSet.getSlideCount() - 1) {
143                                         setPosition(position + 1);
144                                 }
145                         }
146                 };
147                 
148                 Action previous = new AbstractAction() {
149                         @Override
150                         public void actionPerformed(ActionEvent event) {
151                                 log.user("Key action for previous slide");
152                                 if (position > 0) {
153                                         setPosition(position - 1);
154                                 }
155                         }
156                 };
157                 
158                 String nextKey = "slide:next";
159                 String prevKey = "slide:previous";
160                 
161                 JRootPane root = this.getRootPane();
162                 root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), nextKey);
163                 root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0), nextKey);
164                 root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), prevKey);
165                 root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0), prevKey);
166                 
167                 root.getActionMap().put(nextKey, next);
168                 root.getActionMap().put(prevKey, previous);
169         }
170         
171         
172         
173 }