001: /* ===========================================================================
002: * $RCSfile: WizardPanel.java,v $
003: * ===========================================================================
004: *
005: * RetroGuard -- an obfuscation package for Java classfiles.
006: *
007: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
008: *
009: * This program can be redistributed and/or modified under the terms of the
010: * Version 2 of the GNU General Public License as published by the Free
011: * Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: */
019:
020: package COM.rl.util;
021:
022: import java.io.*;
023: import java.util.*;
024: import java.awt.*;
025: import java.awt.event.*;
026:
027: /**
028: * A Panel which contains a CardLayout of other Components with prev/next/finish
029: * buttons -- used for 'wizards'.
030: *
031: * @author Mark Welsh
032: */
033: public class WizardPanel extends Panel {
034: // Constants -------------------------------------------------------------
035:
036: // Fields ----------------------------------------------------------------
037: public Panel cards;
038: public Panel buttonCards;
039: private int cardShown = 0;
040: private int cardCount = 0;
041: private Button finishButton;
042: private Label statusField;
043:
044: // Class Methods ---------------------------------------------------------
045:
046: // Instance Methods ------------------------------------------------------
047: /** Ctor. */
048: public WizardPanel(Panel[] panels,
049: ActionListener firstNextListener,
050: ActionListener finishListener) {
051: // Background color to dialog-gray
052: setBackground(Color.lightGray);
053:
054: // Set up the CardLayout with 'prev'/'next'/'finish' buttons.
055: setLayout(new BorderLayout());
056:
057: // (cards)
058: cards = new Panel();
059: cards.setLayout(new CardLayout());
060: for (int i = 0; i < panels.length; i++) {
061: cards.add(Integer.toString(i), panels[i]);
062: }
063: cardCount = panels.length;
064: showPanelCard();
065:
066: // (buttons)
067: buttonCards = new Panel();
068: buttonCards.setLayout(new CardLayout());
069:
070: Panel buttonsPanelFirst = new Panel();
071: buttonsPanelFirst.setLayout(new FlowLayout(FlowLayout.RIGHT));
072: Button nextButton = new Button("next>");
073: nextButton.addActionListener(firstNextListener);
074: buttonsPanelFirst.add(nextButton);
075:
076: Panel buttonsPanelNormal = new Panel();
077: buttonsPanelNormal.setLayout(new FlowLayout(FlowLayout.RIGHT));
078: Button prevButton = new Button("<prev");
079: nextButton = new Button("next>");
080: prevButton.addActionListener(new ActionListener() {
081: public void actionPerformed(ActionEvent e) {
082: previousCard();
083: }
084: });
085: nextButton.addActionListener(new ActionListener() {
086: public void actionPerformed(ActionEvent e) {
087: nextCard();
088: }
089: });
090: buttonsPanelNormal.add(prevButton);
091: buttonsPanelNormal.add(nextButton);
092:
093: Panel buttonsPanelLast = new Panel();
094: buttonsPanelLast.setLayout(new FlowLayout(FlowLayout.RIGHT));
095: prevButton = new Button("<prev");
096: finishButton = new Button("finish");
097: prevButton.addActionListener(new ActionListener() {
098: public void actionPerformed(ActionEvent e) {
099: previousCard();
100: }
101: });
102: finishButton.addActionListener(finishListener);
103: buttonsPanelLast.add(prevButton);
104: buttonsPanelLast.add(finishButton);
105:
106: buttonCards.add("first", buttonsPanelFirst);
107: buttonCards.add("normal", buttonsPanelNormal);
108: buttonCards.add("last", buttonsPanelLast);
109: showButtonCard();
110:
111: // A status bar to the left of the flip buttons
112: statusField = new Label(
113: " ");
114: statusField.setBackground(Color.lightGray);
115: statusField.setFont(new Font("Helvetica", Font.PLAIN, 12));
116: Panel statusBorder = new Panel() {
117: public void paint(Graphics g) {
118: g.setColor(Color.lightGray);
119: g.draw3DRect(6, 6, getSize().width - 12,
120: getSize().height - 12, false);
121: }
122: };
123: statusBorder.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 7));
124: statusBorder.add(statusField);
125: Panel lower = new Panel();
126: GridBagLayout gridbag = new GridBagLayout();
127: GridBagConstraints c = new GridBagConstraints();
128: lower.setLayout(gridbag);
129: c.fill = GridBagConstraints.BOTH;
130: c.weightx = 1.0;
131: c.insets = new Insets(4, 4, 4, 4);
132: c.gridwidth = 1;
133: gridbag.setConstraints(statusBorder, c);
134: lower.add(statusBorder);
135: c.weightx = 0.0;
136: c.gridwidth = GridBagConstraints.REMAINDER; //end of row
137: gridbag.setConstraints(buttonCards, c);
138: lower.add(buttonCards);
139:
140: add("South", lower);
141: add("Center", cards);
142: }
143:
144: /** Flip from first to second card. */
145: public void flipFirst() {
146: nextCard();
147: }
148:
149: /** Activate the finish button? */
150: public void setFinishEnabled(boolean active) {
151: finishButton.setEnabled(active);
152: }
153:
154: /** Set the status field */
155: public void setStatus(String status) {
156: statusField.setText(status);
157: }
158:
159: // Flip to previous card.
160: void previousCard() {
161: if (cardShown > 0) {
162: cardShown--;
163: showButtonCard();
164: showPanelCard();
165: }
166: }
167:
168: // Flip to next card.
169: void nextCard() {
170: if (cardShown < cardCount - 1) {
171: cardShown++;
172: showButtonCard();
173: showPanelCard();
174: }
175: }
176:
177: // Show the appropriate panel.
178: private void showPanelCard() {
179: ((CardLayout) cards.getLayout()).show(cards, Integer
180: .toString(cardShown));
181: }
182:
183: // Show the appropriate buttons.
184: private void showButtonCard() {
185: if (cardShown == cardCount - 1) {
186: ((CardLayout) buttonCards.getLayout()).show(buttonCards,
187: "last");
188: } else if (cardShown == 0) {
189: ((CardLayout) buttonCards.getLayout()).show(buttonCards,
190: "first");
191: } else {
192: ((CardLayout) buttonCards.getLayout()).show(buttonCards,
193: "normal");
194: }
195: }
196: }
|