1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
package ca.spaz.wizard; |
5 |
|
|
6 |
|
import java.awt.*; |
7 |
|
import java.awt.event.*; |
8 |
|
import java.util.*; |
9 |
|
|
10 |
|
import javax.swing.*; |
11 |
|
|
12 |
|
import com.aimedia.ui.*; |
13 |
|
|
14 |
|
import ca.spaz.util.ToolBox; |
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
public class Wizard extends JDialog implements ICancellable { |
26 |
|
private String title; |
27 |
|
private JPanel mainPanel; |
28 |
|
private JPanel outlinePanel; |
29 |
|
private JPanel titlePanel; |
30 |
|
private JPanel contentPanel; |
31 |
|
private JPanel navPanel; |
32 |
|
private JLabel curPanelTitle; |
33 |
|
private CardLayout contentCards; |
34 |
|
private Vector wizardPanels; |
35 |
|
private JButton cancelBtn, nextBtn, prevBtn, finishBtn; |
36 |
|
private HashMap titleMap; |
37 |
|
|
38 |
|
private int curPanel = 0; |
39 |
|
|
40 |
|
public Wizard(JFrame parent, String title) { |
41 |
|
super(parent); |
42 |
|
this.title = title; |
43 |
|
setModal(true); |
44 |
|
setTitle(getTitle()); |
45 |
|
getContentPane().setLayout(new BorderLayout(4,4)); |
46 |
|
getContentPane().add(getMainPanel(), BorderLayout.CENTER); |
47 |
|
} |
48 |
|
|
49 |
|
public void startWizard() { |
50 |
|
showPanel(0); |
51 |
|
pack(); |
52 |
|
ToolBox.centerFrame(this); |
53 |
|
setVisible(true); |
54 |
|
} |
55 |
|
|
56 |
|
public void addWizardPanel(WizardPanel wp) { |
57 |
|
getWizardPanels().add(wp); |
58 |
|
getWizardPanel().add(wp, wp.getWizardPanelTitle()); |
59 |
|
JLabel wpLabel = makeOutlineLabel(wp); |
60 |
|
getTitleMap().put(wp, wpLabel); |
61 |
|
|
62 |
|
getOutlinePanel().add(wpLabel); |
63 |
|
getOutlinePanel().add(Box.createVerticalStrut(10)); |
64 |
|
} |
65 |
|
|
66 |
|
private HashMap getTitleMap() { |
67 |
|
if (titleMap == null) { |
68 |
|
titleMap = new HashMap(); |
69 |
|
} |
70 |
|
return titleMap; |
71 |
|
} |
72 |
|
|
73 |
|
private JLabel makeOutlineLabel(WizardPanel wp) { |
74 |
|
JLabel label = new JLabel(wp.getWizardPanelTitle()); |
75 |
|
label.setForeground(Color.DARK_GRAY); |
76 |
|
label.setAlignmentX(0.5f); |
77 |
|
return label; |
78 |
|
} |
79 |
|
|
80 |
|
private WizardPanel getWizardPanel(int i) { |
81 |
|
return (WizardPanel)getWizardPanels().get(i); |
82 |
|
} |
83 |
|
|
84 |
|
private void showPanel(int i) { |
85 |
|
WizardPanel wp = getWizardPanel(curPanel); |
86 |
|
if (wp != null) { |
87 |
|
JLabel label = (JLabel)getTitleMap().get(wp); |
88 |
|
if (label != null) { |
89 |
|
label.setForeground(Color.DARK_GRAY); |
90 |
|
label.setFont(label.getFont().deriveFont(Font.PLAIN)); |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
|
wp = getWizardPanel(i); |
95 |
|
assert (wp != null); |
96 |
|
if (wp != null) { |
97 |
|
getContentCards().show(getWizardPanel(), wp.getWizardPanelTitle()); |
98 |
|
getCurrentPanelTitleLabel().setText(wp.getWizardPanelTitle()); |
99 |
|
} |
100 |
|
curPanel = i; |
101 |
|
getPrevButton().setEnabled(i>0); |
102 |
|
getNextButton().setEnabled(getWizardPanels().size() > i+1); |
103 |
|
getFinishButton().setEnabled(getWizardPanels().size() == i+1); |
104 |
|
JLabel label = (JLabel)getTitleMap().get(wp); |
105 |
|
if (label != null) { |
106 |
|
label.setForeground(Color.BLACK); |
107 |
|
label.setFont(label.getFont().deriveFont(Font.BOLD)); |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
|
private Vector getWizardPanels() { |
112 |
|
if (wizardPanels == null) { |
113 |
|
wizardPanels = new Vector(); |
114 |
|
} |
115 |
|
return wizardPanels; |
116 |
|
} |
117 |
|
|
118 |
|
public int getNumPanels() { |
119 |
|
return getWizardPanels().size(); |
120 |
|
} |
121 |
|
|
122 |
|
private void showNext() { |
123 |
|
if (curPanel < getNumPanels() - 1) { |
124 |
|
showPanel(curPanel+1); |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
|
private void showPrevious() { |
129 |
|
if (curPanel > 0) { |
130 |
|
showPanel(curPanel-1); |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
|
private JPanel getMainPanel() { |
135 |
|
if (mainPanel == null) { |
136 |
|
mainPanel = new JPanel(class="keyword">new BorderLayout(4,4)); |
137 |
|
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
138 |
|
mainPanel.add(getTitlePanel(), BorderLayout.NORTH); |
139 |
|
mainPanel.add(getOutlinePanel(), BorderLayout.WEST); |
140 |
|
mainPanel.add(getWizardPanel(), BorderLayout.CENTER); |
141 |
|
mainPanel.add(getNavPanel(), BorderLayout.SOUTH); |
142 |
|
} |
143 |
|
return mainPanel; |
144 |
|
} |
145 |
|
|
146 |
|
private JLabel getCurrentPanelTitleLabel() { |
147 |
|
if (curPanelTitle == null) { |
148 |
|
curPanelTitle = new JLabel("", JLabel.CENTER); |
149 |
|
curPanelTitle.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
150 |
|
curPanelTitle.setFont(curPanelTitle.getFont().deriveFont(Font.BOLD)); |
151 |
|
curPanelTitle.setForeground(Color.WHITE); |
152 |
|
} |
153 |
|
return curPanelTitle; |
154 |
|
} |
155 |
|
|
156 |
|
private JPanel getTitlePanel() { |
157 |
|
if (titlePanel == null) { |
158 |
|
|
159 |
|
JLabel titleLabel = new JLabel(getTitle()); |
160 |
|
titleLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
161 |
|
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD)); |
162 |
|
titleLabel.setForeground(Color.WHITE); |
163 |
|
|
164 |
|
titlePanel = new JPanel(class="keyword">new BorderLayout(4,4)); |
165 |
|
titlePanel.setBackground(Color.BLACK); |
166 |
|
titlePanel.setBorder(BorderFactory.createRaisedBevelBorder()); |
167 |
|
titlePanel.add(titleLabel, BorderLayout.WEST); |
168 |
|
titlePanel.add(getCurrentPanelTitleLabel(), BorderLayout.CENTER); |
169 |
|
} |
170 |
|
return titlePanel; |
171 |
|
} |
172 |
|
|
173 |
|
private JPanel getOutlinePanel() { |
174 |
|
if (outlinePanel == null) { |
175 |
|
outlinePanel = new JPanel(); |
176 |
|
outlinePanel.setLayout(new BoxLayout(outlinePanel, BoxLayout.Y_AXIS)); |
177 |
|
outlinePanel.setBackground(Color.LIGHT_GRAY); |
178 |
|
outlinePanel.setBorder(BorderFactory.createCompoundBorder( |
179 |
|
BorderFactory.createEtchedBorder(), |
180 |
|
BorderFactory.createEmptyBorder(8,8,8,8) )); |
181 |
|
outlinePanel.add(Box.createRigidArea(new Dimension(120,10)), BorderLayout.CENTER); |
182 |
|
} |
183 |
|
return outlinePanel; |
184 |
|
} |
185 |
|
|
186 |
|
private JPanel getWizardPanel() { |
187 |
|
if (contentPanel == null) { |
188 |
|
contentPanel = new JPanel(getContentCards()); |
189 |
|
contentPanel.setBorder(BorderFactory.createLoweredBevelBorder()); |
190 |
|
contentPanel.add(Box.createRigidArea(new Dimension(400,300)), BorderLayout.CENTER); |
191 |
|
} |
192 |
|
return contentPanel; |
193 |
|
} |
194 |
|
|
195 |
|
private CardLayout getContentCards() { |
196 |
|
if (contentCards == null) { |
197 |
|
contentCards = new CardLayout(); |
198 |
|
} |
199 |
|
return contentCards; |
200 |
|
} |
201 |
|
|
202 |
|
private JPanel getNavPanel() { |
203 |
|
if (navPanel == null) { |
204 |
|
navPanel = new JPanel(); |
205 |
|
navPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
206 |
|
navPanel.setLayout(new BoxLayout(navPanel, BoxLayout.X_AXIS)); |
207 |
|
navPanel.add(getCancelButton()); |
208 |
|
navPanel.add(Box.createGlue()); |
209 |
|
navPanel.add(Box.createHorizontalStrut(10)); |
210 |
|
navPanel.add(getPrevButton()); |
211 |
|
navPanel.add(Box.createHorizontalStrut(10)); |
212 |
|
navPanel.add(getNextButton()); |
213 |
|
navPanel.add(Box.createHorizontalStrut(40)); |
214 |
|
navPanel.add(getFinishButton()); |
215 |
|
} |
216 |
|
return navPanel; |
217 |
|
} |
218 |
|
|
219 |
|
public String getTitle() { |
220 |
|
return title; |
221 |
|
} |
222 |
|
|
223 |
|
public void doCancel() { |
224 |
|
dispose(); |
225 |
|
} |
226 |
|
|
227 |
|
public void doFinish() { |
228 |
|
Iterator iter = getWizardPanels().iterator(); |
229 |
|
while (iter.hasNext()) { |
230 |
|
((WizardPanel)iter.next()).commitChanges(); |
231 |
|
} |
232 |
|
dispose(); |
233 |
|
} |
234 |
|
|
235 |
|
private JButton getCancelButton() { |
236 |
|
if (cancelBtn == null) { |
237 |
|
cancelBtn = new JButton(class="keyword">new CancelAction(this)); |
238 |
|
} |
239 |
|
return cancelBtn; |
240 |
|
} |
241 |
|
|
242 |
|
private JButton getNextButton() { |
243 |
|
if (nextBtn == null) { |
244 |
|
nextBtn = new JButton("Next"); |
245 |
|
nextBtn.addActionListener(new ActionListener() { |
246 |
|
public void actionPerformed(ActionEvent e) { |
247 |
|
if (getWizardPanel(curPanel).isValid()) { |
248 |
|
showNext(); |
249 |
|
} |
250 |
|
} |
251 |
|
}); |
252 |
|
} |
253 |
|
return nextBtn; |
254 |
|
} |
255 |
|
|
256 |
|
private JButton getPrevButton() { |
257 |
|
if (prevBtn == null) { |
258 |
|
prevBtn = new JButton("Back"); |
259 |
|
prevBtn.setEnabled(false); |
260 |
|
prevBtn.addActionListener(new ActionListener() { |
261 |
|
public void actionPerformed(ActionEvent e) { |
262 |
|
showPrevious(); |
263 |
|
} |
264 |
|
}); |
265 |
|
} |
266 |
|
return prevBtn; |
267 |
|
} |
268 |
|
|
269 |
|
private JButton getFinishButton() { |
270 |
|
if (finishBtn == null) { |
271 |
|
finishBtn = new JButton("Finish"); |
272 |
|
finishBtn.setEnabled(false); |
273 |
|
finishBtn.addActionListener(new ActionListener() { |
274 |
0 |
public void actionPerformed(ActionEvent e) { |
275 |
0 |
if (getWizardPanel(curPanel).isValid()) { |
276 |
0 |
doFinish(); |
277 |
|
} |
278 |
0 |
} |
279 |
|
}); |
280 |
|
} |
281 |
|
return finishBtn; |
282 |
|
} |
283 |
|
} |