001: /*
002: * @(#)PageList.java
003: *
004: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
005: */
006: package com.jidesoft.dialog;
007:
008: import javax.swing.*;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: /**
013: * A list of AbstractDialogPage or its subclasses. It is used by
014: * MultiplePageDialog and Wizard.
015: */
016: public class PageList extends DefaultComboBoxModel {
017: /**
018: * If you know the full title of any page, use this method to get
019: * the actual page from the list.
020: *
021: * @param title the full title.
022: * @return the page with the title.
023: */
024: public AbstractDialogPage getPageByFullTitle(String title) {
025: for (int i = 0; i < getSize(); i++) {
026: AbstractDialogPage page = (AbstractDialogPage) getElementAt(i);
027: if (page.getFullTitle().equals(title)) {
028: return page;
029: }
030: }
031: return null;
032: }
033:
034: /**
035: * Gets the page index if you know the full title of the page.
036: *
037: * @param title the full title.
038: * @return the page index.
039: */
040: public int getPageIndexByFullTitle(String title) {
041: for (int i = 0; i < getSize(); i++) {
042: AbstractDialogPage page = (AbstractDialogPage) getElementAt(i);
043: if (page.getFullTitle().equals(title)) {
044: return i;
045: }
046: }
047: return -1;
048: }
049:
050: /**
051: * Gets the page count in the list.
052: *
053: * @return the page count in the list.
054: */
055: public int getPageCount() {
056: return getSize();
057: }
058:
059: /**
060: * Appends a page to the end of the list.
061: *
062: * @param page the page to be appended.
063: */
064: public void append(AbstractDialogPage page) {
065: addElement(page);
066: }
067:
068: /**
069: * Removes a page from the page list.
070: *
071: * @param page page to be removed.
072: */
073: public void remove(AbstractDialogPage page) {
074: removeElement(page);
075: }
076:
077: /**
078: * Clear the page list.
079: */
080: public void clear() {
081: removeAllElements();
082: }
083:
084: /**
085: * Inserts a page after the page with the specified full title. If we cannot find
086: * the page with the specified title, the page will be added to the end as append(page).
087: *
088: * @param page page to be inserted.
089: * @param title the title of the page after when the new page will be inserted.
090: */
091: public void insertAfter(AbstractDialogPage page, String title) {
092: int index = getPageIndexByFullTitle(title);
093: if (index == -1 || index == getPageCount() - 1) {
094: append(page);
095: } else {
096: insertElementAt(page, index + 1);
097: }
098: }
099:
100: /**
101: * Gets the page at position.
102: *
103: * @param i the index
104: * @return the page.
105: */
106: public AbstractDialogPage getPage(int i) {
107: return (AbstractDialogPage) getElementAt(i);
108: }
109:
110: /**
111: * Gets the all page titles as vector.
112: *
113: * @return the vector which has all the page titles.
114: */
115: public List<String> getPageTitlesAsList() {
116: List<String> list = new ArrayList<String>();
117: for (int i = 0; i < getPageCount(); i++) {
118: AbstractDialogPage page = getPage(i);
119: list.add(page.getTitle());
120: }
121: return list;
122: }
123:
124: /**
125: * Gets the current selected page.
126: *
127: * @return the current selected page.
128: */
129: public AbstractDialogPage getCurrentPage() {
130: return ((AbstractDialogPage) getSelectedItem());
131: }
132:
133: /**
134: * Sets the current selected page.
135: *
136: * @param page the dialog page.
137: */
138: public void setCurrentPage(AbstractDialogPage page) {
139: setSelectedItem(page);
140: }
141: }
|