001: /*
002: * DefaultWizardProcessModel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.wizard;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026: import javax.swing.JPanel;
027:
028: /* ----------------------------------------------------------
029: * CVS NOTE: Changes to the CVS repository prior to the
030: * release of version 3.0.0beta1 has meant a
031: * resetting of CVS revision numbers.
032: * ----------------------------------------------------------
033: */
034:
035: /**
036: * The default model for a wizard process panel.
037: *
038: * @author Takis Diakoumis
039: * @version $Revision: 1.6 $
040: * @date $Date: 2006/06/18 14:11:59 $
041: */
042: public class DefaultWizardProcessModel implements WizardProcessModel {
043:
044: /** the current selected index */
045: private int selectedIndex;
046:
047: /** the panels collection */
048: private List<JPanel> panels;
049:
050: /** the steps descriptions */
051: private String[] steps;
052:
053: /** the panel titles */
054: private String[] titles;
055:
056: /** Creates a new instance of WizardProcessModel */
057: public DefaultWizardProcessModel() {
058: }
059:
060: /** Creates a new instance of WizardProcessModel */
061: public DefaultWizardProcessModel(List panels) {
062: this (panels, null, null);
063: }
064:
065: /** Creates a new instance of WizardProcessModel */
066: public DefaultWizardProcessModel(List panels, String[] steps,
067: String[] titles) {
068: this .panels = panels;
069: this .steps = steps;
070: this .titles = titles;
071: }
072:
073: /**
074: * Returns the descriptive values for the wizard steps.
075: *
076: * @return the descriptive steps
077: */
078: public String[] getSteps() {
079: return steps;
080: }
081:
082: /**
083: * Returns the titles for each panel.
084: *
085: * @return the panel titles
086: */
087: public String[] getTitles() {
088: return titles;
089: }
090:
091: /**
092: * Returns the step text at the specified index.
093: *
094: * @param index - the index
095: */
096: public String getStep(int index) {
097: if (getSteps() == null) {
098: return null;
099: }
100: return getSteps()[index];
101: }
102:
103: /**
104: * Returns the title text at the specified index.
105: *
106: * @param index - the index
107: */
108: public String getTitle(int index) {
109: if (getTitles() == null) {
110: return null;
111: }
112: return getTitles()[index];
113: }
114:
115: /**
116: * Increments the current index.
117: */
118: public boolean next() {
119: selectedIndex++;
120: return true;
121: }
122:
123: /**
124: * Decrements the current index.
125: */
126: public boolean previous() {
127: selectedIndex--;
128: return true;
129: }
130:
131: /**
132: * Returns the next panel in the wizard and increments the
133: * selected index.
134: *
135: * @return the next panel
136: */
137: public JPanel getNextPanel() {
138: if (hasNext()) {
139: selectedIndex++;
140: return panels.get(selectedIndex);
141: }
142: return null;
143: }
144:
145: /**
146: * Returns the previous panel in the wizard and decrements the
147: * selected index.
148: *
149: * @return the previous panel
150: */
151: public JPanel getPreviousPanel() {
152: if (hasPrevious()) {
153: selectedIndex--;
154: return panels.get(selectedIndex);
155: }
156: return null;
157: }
158:
159: /**
160: * Returns whether there is a valid panel to be selected next.
161: *
162: * @return true | false
163: */
164: public boolean hasNext() {
165: return selectedIndex < (getSteps().length - 1);
166: }
167:
168: /**
169: * Returns whether there is a valid panel to be selected previous.
170: *
171: * @return true | false
172: */
173: public boolean hasPrevious() {
174: return selectedIndex > 0;
175: }
176:
177: /**
178: * Returns the current index in the model.
179: *
180: * @return the currently selected index
181: */
182: public int getSelectedIndex() {
183: return selectedIndex;
184: }
185:
186: /**
187: * Sets the selected index to that specified.
188: *
189: * @param the index to be selected
190: */
191: public void setSelectedIndex(int selectedIndex) {
192: this .selectedIndex = selectedIndex;
193: }
194:
195: /**
196: * Returns the index of the specified panel in this model.
197: *
198: * @param panel - the panel
199: * @return the panel index
200: */
201: public int getIndexOf(JPanel panel) {
202: if (panels == null) {
203: return -1;
204: }
205: return panels.indexOf(panel);
206: }
207:
208: /**
209: * Returns the panel at the specified index.
210: *
211: * @param index - the panel to be retrieved
212: */
213: public JPanel getPanelAt(int index) {
214: if (panels == null) {
215: return null;
216: }
217: return panels.get(index);
218: }
219:
220: /**
221: * Adds the specified panel to the end of the model.
222: *
223: * @param panel - the panel to be added
224: */
225: public void addPanel(JPanel panel) {
226: if (panels == null) {
227: panels = new ArrayList<JPanel>();
228: }
229: if (panels.indexOf(panel) == -1) {
230: panels.add(panel);
231: }
232: }
233:
234: /**
235: * Returns the number of panel components in this model.
236: *
237: * @return the panel count
238: */
239: public int getPanelCount() {
240: if (getSteps() == null) {
241: return 0;
242: }
243: return getSteps().length;
244: }
245:
246: /**
247: * Returns a list of panels making up this model.
248: *
249: * @return the panels collection
250: */
251: public List<JPanel> getPanels() {
252: return panels;
253: }
254:
255: /**
256: * Sets the list of panels making up this model.
257: *
258: * @param panels - the panels
259: */
260: public void setPanels(List<JPanel> panels) {
261: this .panels = panels;
262: }
263:
264: public void setSteps(String[] steps) {
265: this .steps = steps;
266: }
267:
268: public void setTitles(String[] titles) {
269: this.titles = titles;
270: }
271:
272: }
|