Source code: javatools/swing/AbstractWizard.java
1 /*
2 * AbstractWizard.java
3 *
4 * Created on 24 febbraio 2003, 9.57
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing;
26
27 import java.io.*;
28 import java.util.*;
29
30 /**
31 * Class to represent a generic wizard, with much code written and ready to use.
32 * @author Antonio Petrelli
33 * @version 0.1.10
34 */
35
36 public abstract class AbstractWizard extends javax.swing.JDialog {
37
38 public static final int FINISH_OPTION = 1;
39 public static final int CANCEL_OPTION = 2;
40
41 public AbstractWizard() {
42 selectedOption = CANCEL_OPTION;
43 javatoolsBundle = java.util.ResourceBundle.getBundle("res/JavatoolsBundle");
44 initComponents();
45 backgroundFile = "";
46 initialPanelDims = new java.awt.Dimension(500, 400);
47 helpForm = null;
48 }
49
50 /** Creates new form AbstractWizard */
51 public AbstractWizard(java.awt.Frame parent, boolean modal) {
52 super(parent, modal);
53 selectedOption = CANCEL_OPTION;
54 javatoolsBundle = java.util.ResourceBundle.getBundle("res/JavatoolsBundle");
55 initComponents();
56 backgroundFile = "";
57 initialPanelDims = new java.awt.Dimension(500, 400);
58 helpForm = null;
59 }
60
61 /** Executes after collecting all necessary information.
62 * @throws WizardException If something goes wrong in executing.
63 */
64 public abstract void execute() throws WizardException;
65
66 /** Sets if it should exit the program on closing the window.
67 * @param value <CODE>true</CODE>: when window is closed, exits the program;
68 * <CODE>false</CODE>: when window is closed, disposes the window.
69 */
70 public void setExitOnClose(boolean value) {
71 exitOnClose = value;
72 }
73
74 /** Sets a wizard value. It will be searched between all the assigned panes.
75 * @param numValue The number of the needed value.
76 * @param value The value to be assigned.
77 * @throws IndexOutOfBoundsException If <CODE>numValue</CODE> is not valid.
78 */
79 public void setValue (int numValue, String value) throws IndexOutOfBoundsException {
80 int i, actStart, numPanels, numValuesPanel;
81 boolean found;
82
83 if (numValue >= 0) {
84 actStart = 0;
85 i = 0;
86 numPanels = wizPanels.length;
87 found = false;
88 while (i < numPanels) {
89 numValuesPanel = wizPanels[i].getNumValues();
90 if (numValue < (actStart+numValuesPanel)) {
91 try {
92 wizPanels[i].setValue(numValue-actStart, value);
93 }
94 catch (WizardException e) {
95 System.out.println ("Something weird happened, a contradiction into the functions for setting values");
96 System.exit(1);
97 }
98 found = true;
99 i = numPanels;
100 }
101 else {
102 i++;
103 actStart += numValuesPanel;
104 }
105 }
106 if (! found)
107 throw new IndexOutOfBoundsException();
108 }
109 else
110 throw new IndexOutOfBoundsException();
111 }
112
113 /** Sets a wizard value. It will be searched between all the assigned panes.
114 * @param name The name of the needed value.
115 * @param value The value to be assigned.
116 */
117 public void setValue (String name, String value) {
118 int pos;
119
120 pos = getValuePos(name);
121 if (pos >= 0) {
122 try {
123 setValue(pos, value);
124 }
125 catch (IndexOutOfBoundsException e) {
126 System.out.println ("Something weird happened, a found wizard value is not present!");
127 System.exit(1);
128 }
129 }
130 }
131
132 /** Gets a wizard value. It will be searched between all the assigned panes.
133 * @param numValue The number of the needed value.
134 * @throws IndexOutOfBoundsException If <CODE>numValue</CODE> is not valid.
135 * @return The needed value.
136 */
137 public String getValue (int numValue) throws IndexOutOfBoundsException {
138 int i, actStart, numPanels, numValuesPanel, newActStart;
139 boolean found;
140
141 if (numValue >= 0) {
142 actStart = 0;
143 i = 0;
144 numPanels = wizPanels.length;
145 found = false;
146 while (i < numPanels) {
147 numValuesPanel = wizPanels[i].getNumValues();
148 newActStart = actStart+numValuesPanel;
149 if (numValue < newActStart) {
150 try {
151 return wizPanels[i].getValue(numValue-actStart);
152 }
153 catch (WizardException e) {
154 System.out.println ("Something weird happened, a contradiction into the functions for getting values");
155 System.exit(1);
156 }
157 found = true;
158 i = numPanels;
159 }
160 else {
161 i++;
162 actStart = newActStart;
163 }
164 }
165 if (! found)
166 throw new IndexOutOfBoundsException();
167 }
168 else
169 throw new IndexOutOfBoundsException();
170 return "";
171 }
172
173 /** Gets a wizard value. It will be searched between all the assigned panes.
174 * @param name The name of the value.
175 * @return The needed value.
176 */
177 public String getValue (String name) {
178 int pos;
179
180 pos = getValuePos(name);
181 if (pos >= 0) {
182 try {
183 return getValue(pos);
184 }
185 catch (IndexOutOfBoundsException e) {
186 System.out.println ("Something weird happened, a found wizard value is not present!");
187 System.exit(1);
188 }
189 }
190 return "";
191 }
192
193 /** Searches the position of a value whose name is the one passed as a parameter.
194 * @param name The name of the needed value.
195 * @return The position of the needed value.
196 */
197 public int getValuePos(String name) {
198 int i, actStart, numPanels, numValuesPanel, posValue;
199 boolean found;
200
201 actStart = 0;
202 i = 0;
203 numPanels = wizPanels.length;
204 found = false;
205 posValue = -1;
206 while (i < numPanels) {
207 posValue = wizPanels[i].getValuePos(name);
208 numValuesPanel = wizPanels[i].getNumValues();
209 if (posValue >= 0) {
210 found = true;
211 i = numPanels;
212 }
213 else {
214 i++;
215 actStart += numValuesPanel;
216 }
217 }
218 if (found)
219 return posValue+actStart;
220 else
221 return -1;
222 }
223
224 /** Sets a filename that represents an image file, to be displayed as a background on the left.
225 * @param fileName The filename of the image file.
226 * @throws WizardException If the file does not exist.
227 */
228 public void setBackgroundFile(String fileName) throws WizardException {
229 File imgFile;
230
231 imgFile = new File(fileName);
232 if (imgFile.exists())
233 backgroundFile = fileName;
234 else
235 throw new WizardException("Background File "+fileName+" not found!");
236 }
237
238 /** Returns the filename of the image file used as a background.
239 * @return The filename.
240 */
241 public String getBackgroundFile() {
242 return backgroundFile;
243 }
244
245 /** Sets a reference to the form which will be displayed by pressing the "Help" button.
246 * @param pHelpForm The reference to the help form.
247 */
248 public void setHelpForm (javax.swing.JFrame pHelpForm) {
249 helpForm = pHelpForm;
250 }
251
252 /** Shows the next pane.
253 */
254 protected void showNextPane() {
255 int lastPanel;
256
257 lastPanel = wizPanels.length-1;
258 if (visiblePane < lastPanel) {
259 wizPanels[visiblePane].setVisible(false);
260 visiblePane++;
261 wizPanels[visiblePane].setVisible(true);
262 butBack.setEnabled(true);
263 if (visiblePane == lastPanel)
264 butNext.setEnabled(false);
265 else
266 butNext.setEnabled(true);
267 initDescription();
268 if (visiblePane == lastPanel)
269 setEnabledFinishButton(true);
270 }
271 }
272
273 /** Shows the previous pane.
274 */
275 protected void showPreviousPane() {
276 if (visiblePane > 0) {
277 wizPanels[visiblePane].setVisible(false);
278 visiblePane--;
279 wizPanels[visiblePane].setVisible(true);
280 butNext.setEnabled(true);
281 if (visiblePane == 0)
282 butBack.setEnabled(false);
283 else
284 butBack.setEnabled(true);
285 initDescription();
286 }
287 }
288
289 /** Initialized all the panes, with correct dimensions.
290 */
291 protected void initPanels() {
292 int i, numPanels;
293 String descPanel;
294
295 initDescription();
296 numPanels = wizPanels.length;
297 for (i=0; i<numPanels; i++) {
298 wizPanels[i].setRefButton(butNext);
299 panWizPanel.add(wizPanels[i], java.awt.BorderLayout.CENTER);
300 wizPanels[i].setVisible(false);
301 }
302 butBack.setEnabled(false);
303 if (numPanels == 1)
304 butNext.setEnabled(false);
305 else
306 butNext.setEnabled(true);
307 if (numPanels >= 0) {
308 wizPanels[0].setVisible(true);
309 visiblePane = 0;
310 }
311 resizePanels(initialPanelDims);
312 pack();
313 }
314
315 /** Returns all the inserted values into the wizard as a properties object.
316 * @return The properties representing all the inserted values.
317 */
318 protected Properties buildProperties() {
319 int i, j, numPanels, numValues;
320 Properties tempProps;
321
322 tempProps = new Properties();
323 numPanels = wizPanels.length;
324 for (i=0; i<numPanels; i++) {
325 numValues = wizPanels[i].getNumValues();
326 for (j=0; j<numValues; j++) {
327 try {
328 tempProps.setProperty(wizPanels[i].getValueName(j), wizPanels[i].getValue(j));
329 }
330 catch (WizardException e) {
331 System.out.println("Something weird happened, cannot build properties!");
332 }
333 }
334 }
335 return tempProps;
336 }
337
338 /** It should be used to disable finishing if all the needed information has not yet been collected.
339 * @param value <CODE>true</CODE>: the Finish button is enabled.
340 * <CODE>false</CODE>: the Finish button is disabled.
341 */
342 protected void setEnabledFinishButton(boolean value) {
343 butFinish.setEnabled(value);
344 }
345
346 /** The panels of this wizards. They must be of <CODE>InputPanel</CODE> class.
347 */
348 protected InputPanel[] wizPanels;
349 /** It contains the number of the visible pane in a certain moment.
350 */
351 protected int visiblePane;
352
353 /** This method is called from within the constructor to
354 * initialize the form.
355 * WARNING: Do NOT modify this code. The content of this method is
356 * always regenerated by the Form Editor.
357 */
358 private void initComponents() {//GEN-BEGIN:initComponents
359 panLabelContainer = new javax.swing.JPanel();
360 lblDescription = new javax.swing.JLabel();
361 panButWizard = new javax.swing.JPanel();
362 panInButWizard = new javax.swing.JPanel();
363 butBack = new javax.swing.JButton();
364 butNext = new javax.swing.JButton();
365 butFinish = new javax.swing.JButton();
366 butCancel = new javax.swing.JButton();
367 butHelp = new javax.swing.JButton();
368 panWizPanel = new javax.swing.JPanel();
369
370 addWindowListener(new java.awt.event.WindowAdapter() {
371 public void windowClosing(java.awt.event.WindowEvent evt) {
372 closeDialog(evt);
373 }
374 });
375
376 panLabelContainer.setLayout(new java.awt.BorderLayout());
377
378 panLabelContainer.setMinimumSize(new java.awt.Dimension(200, 400));
379 panLabelContainer.setPreferredSize(new java.awt.Dimension(200, 400));
380 lblDescription.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
381 lblDescription.setVerticalAlignment(javax.swing.SwingConstants.TOP);
382 lblDescription.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
383 lblDescription.setMaximumSize(new java.awt.Dimension(30000, 3000));
384 lblDescription.setMinimumSize(new java.awt.Dimension(200, 200));
385 lblDescription.setPreferredSize(new java.awt.Dimension(262, 400));
386 panLabelContainer.add(lblDescription, java.awt.BorderLayout.CENTER);
387
388 getContentPane().add(panLabelContainer, java.awt.BorderLayout.WEST);
389
390 panButWizard.setLayout(new java.awt.BorderLayout());
391
392 panInButWizard.setLayout(new javax.swing.BoxLayout(panInButWizard, javax.swing.BoxLayout.X_AXIS));
393
394 butBack.setText(javatoolsBundle.getString("Wizard_Back"));
395 butBack.setToolTipText(javatoolsBundle.getString("Go_to_previous_panel"));
396 butBack.setPreferredSize(new java.awt.Dimension(95, 27));
397 butBack.addActionListener(new java.awt.event.ActionListener() {
398 public void actionPerformed(java.awt.event.ActionEvent evt) {
399 butBackActionPerformed(evt);
400 }
401 });
402
403 panInButWizard.add(butBack);
404
405 butNext.setText(javatoolsBundle.getString("Wizard_Next"));
406 butNext.setToolTipText(javatoolsBundle.getString("Go_to_next_panel"));
407 butNext.setPreferredSize(new java.awt.Dimension(80, 27));
408 butNext.addActionListener(new java.awt.event.ActionListener() {
409 public void actionPerformed(java.awt.event.ActionEvent evt) {
410 butNextActionPerformed(evt);
411 }
412 });
413
414 panInButWizard.add(butNext);
415
416 butFinish.setText(javatoolsBundle.getString("Finish"));
417 butFinish.setToolTipText(javatoolsBundle.getString("Execute_operations"));
418 butFinish.setPreferredSize(new java.awt.Dimension(80, 27));
419 butFinish.addActionListener(new java.awt.event.ActionListener() {
420 public void actionPerformed(java.awt.event.ActionEvent evt) {
421 butFinishActionPerformed(evt);
422 }
423 });
424
425 panInButWizard.add(butFinish);
426
427 butCancel.setText(javatoolsBundle.getString("Cancel"));
428 butCancel.setToolTipText(javatoolsBundle.getString("Cancel_operations"));
429 butCancel.setPreferredSize(new java.awt.Dimension(80, 27));
430 butCancel.addActionListener(new java.awt.event.ActionListener() {
431 public void actionPerformed(java.awt.event.ActionEvent evt) {
432 butCancelActionPerformed(evt);
433 }
434 });
435
436 panInButWizard.add(butCancel);
437
438 butHelp.setText("Help");
439 butHelp.setToolTipText("Opens a help form");
440 butHelp.setPreferredSize(new java.awt.Dimension(80, 27));
441 butHelp.addActionListener(new java.awt.event.ActionListener() {
442 public void actionPerformed(java.awt.event.ActionEvent evt) {
443 butHelpActionPerformed(evt);
444 }
445 });
446
447 panInButWizard.add(butHelp);
448
449 panButWizard.add(panInButWizard, java.awt.BorderLayout.EAST);
450
451 getContentPane().add(panButWizard, java.awt.BorderLayout.SOUTH);
452
453 panWizPanel.setLayout(new java.awt.BorderLayout());
454
455 panWizPanel.setPreferredSize(new java.awt.Dimension(500, 10));
456 getContentPane().add(panWizPanel, java.awt.BorderLayout.CENTER);
457
458 pack();
459 }//GEN-END:initComponents
460
461 private void butHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butHelpActionPerformed
462 // Add your handling code here:
463 if (helpForm != null)
464 helpForm.show();
465 }//GEN-LAST:event_butHelpActionPerformed
466
467 private void butCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butCancelActionPerformed
468 // Add your handling code here:
469 selectedOption = CANCEL_OPTION;
470 closeDialog(null);
471 this.processWindowEvent(new java.awt.event.WindowEvent(this, java.awt.event.WindowEvent.WINDOW_CLOSING));
472 }//GEN-LAST:event_butCancelActionPerformed
473
474 private void butFinishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butFinishActionPerformed
475 // Add your handling code here:
476 java.awt.event.WindowEvent evt2;
477
478 selectedOption = FINISH_OPTION;
479 try {
480 execute();
481 }
482 catch (WizardException e) {
483 }
484 closeDialog(null);
485 this.processWindowEvent(new java.awt.event.WindowEvent(this, java.awt.event.WindowEvent.WINDOW_CLOSING));
486 }//GEN-LAST:event_butFinishActionPerformed
487
488 private void butNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butNextActionPerformed
489 // Add your handling code here:
490 showNextPane();
491 }//GEN-LAST:event_butNextActionPerformed
492
493 private void butBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butBackActionPerformed
494 // Add your handling code here:
495 showPreviousPane();
496 }//GEN-LAST:event_butBackActionPerformed
497
498 /** Closes the dialog */
499 private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
500 setVisible(false);
501 dispose();
502 if (exitOnClose)
503 System.exit(0);
504 }//GEN-LAST:event_closeDialog
505
506 private void initDescription() {
507 int i, j, numPanels;
508 StringBuffer tempBuf;
509 String descPanel;
510
511 tempBuf = new StringBuffer (100+(wizPanels.length)*100);
512 tempBuf.append("<HTML> <BODY");
513 if (!backgroundFile.equals(""))
514 tempBuf.append(" background=\"file:///"+backgroundFile+"\">");
515 else
516 tempBuf.append(">");
517 numPanels = wizPanels.length;
518 tempBuf.append(" <UL> ");
519 for (i=0; i<numPanels; i++) {
520 descPanel = wizPanels[i].getDescription();
521 if (!descPanel.equals("")) {
522 tempBuf.append("<LI>");
523 if (visiblePane == i)
524 tempBuf.append("<B>");
525 tempBuf.append(descPanel);
526 if (visiblePane == i)
527 tempBuf.append("</B>");
528 for (j=0; j<30; j++)
529 tempBuf.append(" ");
530 }
531 else
532 tempBuf.append("<P>");
533 }
534 tempBuf.append(" </UL> ");
535 for (i=0; i<30; i++)
536 tempBuf.append(" <P> ");
537 tempBuf.append(" </BODY></HTML>");
538 lblDescription.setText(tempBuf.toString());
539 }
540
541 private void resizePanels(java.awt.Dimension panelDims) {
542 int i, numPanels;
543 java.awt.Dimension dims;
544
545 numPanels = wizPanels.length;
546 for (i=0; i<numPanels; i++) {
547 wizPanels[i].setPreferredSize(panelDims);
548 wizPanels[i].setSize(panelDims);
549 }
550 }
551
552 // Variables declaration - do not modify//GEN-BEGIN:variables
553 private javax.swing.JPanel panInButWizard;
554 private javax.swing.JButton butFinish;
555 private javax.swing.JButton butCancel;
556 private javax.swing.JButton butBack;
557 private javax.swing.JPanel panLabelContainer;
558 private javax.swing.JButton butHelp;
559 private javax.swing.JLabel lblDescription;
560 private javax.swing.JPanel panWizPanel;
561 private javax.swing.JButton butNext;
562 private javax.swing.JPanel panButWizard;
563 // End of variables declaration//GEN-END:variables
564
565 protected static int selectedOption;
566
567 private String backgroundFile;
568 private java.awt.Dimension initialFormDims, initialFakeDims, initialPanelDims;
569 private javax.swing.JFrame helpForm;
570 private boolean exitOnClose = false;
571 private java.util.ResourceBundle javatoolsBundle;
572 }