001: package net.refractions.udig.catalog.ui;
002:
003: import org.eclipse.jface.dialogs.IDialogSettings;
004: import org.eclipse.jface.viewers.IStructuredSelection;
005: import org.eclipse.jface.viewers.StructuredSelection;
006: import org.eclipse.jface.wizard.IWizardPage;
007: import org.eclipse.jface.wizard.Wizard;
008: import org.eclipse.jface.wizard.WizardPage;
009: import org.eclipse.ui.IWorkbench;
010: import org.eclipse.ui.IWorkbenchWizard;
011:
012: /**
013: * A wizard which is used to import data into udig.
014: * <p>
015: * This is wizard is made up of <b>primary</b> pages, and <b>secondary</b> pages. A primary page
016: * is an
017: *
018: * @see org.eclipse.jface.wizard.IWizardPage that the wizard declares it will contain. A secondary
019: * page is a page which is dynamically contributed to the wizard via a primary page, or another
020: * secondary page.
021: * </p>
022: * <p>
023: * Sublcasses declare the ordered set of primary pages with the getPrimaryPages() method.
024: * Secondary pages are contributed dynamically by returning them from a call to
025: * @see org.eclipse.jface.wizard.IWizardPage#getNextPage().
026: * </p>
027: * <p>
028: * Secondary page processing will continue, until a page returns null from getNextPage().
029: * Processing then continues at the next primary page. If no more primary pages exist, the
030: * wizard finishes.
031: * </p>
032: * <p>
033: * If using an IDataWizard outside of the workbench wizard framework, it is up to client code
034: * to call
035: * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
036: * org.eclipse.jface.viewers.IStructuredSelection) immediatly after instantiating the wizard.
037: * </p>
038: * <p>
039: * The following are requirements on the pages of the wizard.
040: * <ul>
041: * <li>They must extend from
042: * @see org.eclipse.jface.wizard.WizardPage
043: * <li>getNextPage must either return a new wizard page, or super.getNextPage()
044: * <li>init(WizardPage) must be called before returning a page from
045: * </ul>
046: * <p>
047: * Example: <code>
048: * public IWizardPage getNextPage() {
049: * if (returnNewPage) {
050: * WizardPage newPage = new WizardPage(....);
051: * IDataWizard wizard = (IDataWizard)getWizard();
052: * wizard.init(page);
053: * return page;
054: * }
055: *
056: * return super.getNextPage();
057: * }
058: * </code>
059: * </p>
060: * It is important to note that pages inside this wizard must return a page or
061: * super.getNextPage() from getNextPage().
062: * </p>
063: * <p>
064: * This wizard creates dialog settings for pages upon creation.
065: * </p>
066: * @author Justin Deoliveira,Refractions Research Inc.,jdeolive@refractions.net
067: */
068: public abstract class IDataWizard extends Wizard implements
069: IWorkbenchWizard {
070:
071: static final String SETTINGS = "IDataWizard"; //$NON-NLS-1$
072:
073: /** the primary wizard pages * */
074: WizardPage[] pages;
075:
076: /** the selection * */
077: IStructuredSelection selection = new StructuredSelection();
078:
079: /** the workbench * */
080: IWorkbench workbench;
081:
082: public IDataWizard() {
083: // set up dialog settings
084: IDialogSettings settings = CatalogUIPlugin.getDefault()
085: .getDialogSettings().getSection(SETTINGS);
086: if (settings == null) {
087: settings = CatalogUIPlugin.getDefault().getDialogSettings()
088: .addNewSection(SETTINGS);
089: }
090: setDialogSettings(settings);
091: }
092:
093: /**
094: * @see IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
095: * org.eclipse.jface.viewers.IStructuredSelection)
096: */
097: public void init(IWorkbench workbench,
098: IStructuredSelection selection) {
099: this .workbench = workbench;
100: this .selection = selection;
101:
102: setNeedsProgressMonitor(true);
103: }
104:
105: /**
106: * @return the selection the wizard was initialized with.
107: */
108: public IStructuredSelection getSelection() {
109: return selection;
110: }
111:
112: /**
113: * @return the workbench the wizard was initialized with.
114: */
115: public IWorkbench getWorkbench() {
116: return workbench;
117: }
118:
119: /**
120: * Adds the primary pages to the wizard.
121: *
122: * @see org.eclipse.jface.wizard.IWizard#addPages()
123: */
124: public void addPages() {
125: this .pages = getPrimaryPages();
126: for (int i = 0; i < pages.length; i++) {
127: init(pages[i]);
128: addPage(pages[i]);
129: }
130: }
131:
132: /**
133: * Returns the next primary page in the page sequence. This method is called by the
134: *
135: * @see org.eclipse.jface.wizard.IWizardContainer when a page does not contribute a secondary
136: * page.
137: * @see org.eclipse.jface.wizard.IWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
138: */
139: public IWizardPage getNextPage(IWizardPage page) {
140: // a string of pages is done, find next primary
141: return getNextPrimaryPage(page);
142: }
143:
144: /**
145: * Determines if the wizard has any more primary pages.
146: *
147: * @return True if so, otherwise false.
148: */
149: public boolean hasMorePrimaryPages() {
150: return getNextPrimaryPage(getContainer().getCurrentPage()) != null;
151: }
152:
153: /**
154: * Returns the next primary page, based on the current page.
155: *
156: * @param page A primary page, or secondary page.
157: * @return The next primary page, or null if no more in sequence.
158: */
159: private IWizardPage getNextPrimaryPage(IWizardPage page) {
160: // back up until we reach a primary page
161: IWizardPage[] primary = getPages();
162:
163: while (page != null) {
164: // do an index of in primary list
165: for (int i = 0; i < primary.length; i++) {
166: if (page == primary[i]) {
167: // found it, make sure not last page
168: if (i == primary.length - 1) {
169: return null;
170: }
171: return primary[i + 1];
172: }
173: }
174: page = page.getPreviousPage();
175: }
176:
177: return null;
178: }
179:
180: /**
181: * @see org.eclipse.jface.wizard.IWizard#needsPreviousAndNextButtons()
182: */
183: public boolean needsPreviousAndNextButtons() {
184: return true;
185: }
186:
187: @Override
188: /**
189: * Determines if there are any more pages in the sequence.
190: * <p>
191: * The wizard can finish if the following 3 properies hold.
192: * <ol>
193: * <li>The current page is complete (@see IWizardPage#isPageComplete())
194: * <li>The current page can not flip to the next page (@see IWizardPage#canFlipToNextPage())
195: * <li>There are no more primary pages.
196: * </ol>
197: * </p>
198: *
199: * @see org.eclipse.jface.wizard.IWizard#canFinish()
200: */
201: public boolean canFinish() {
202: // check if current page is complete
203: IWizardPage page = getContainer().getCurrentPage();
204: if (!page.isPageComplete())
205: return false;
206:
207: // first as the page if it has more pages
208: if (page.canFlipToNextPage()) {
209: return false;
210: }
211:
212: // find out if there is another primary page
213: return getNextPrimaryPage(page) == null;
214: }
215:
216: /**
217: * Initializes a wizard page for use in the data wizard. This method should be called by pages
218: * returning a new page from getNextPage().
219: *
220: * @param page The page to be initialized.
221: */
222: public void init(WizardPage page) {
223: page.setWizard(this );
224: }
225:
226: /**
227: * Returns the set of primary pages. This method is called while the wizard is being
228: * instantiated.
229: */
230: protected abstract WizardPage[] getPrimaryPages();
231:
232: }
|