001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: *
022: * Copyright 2005 Sun Microsystems, Inc.
023: *
024: * Licensed under the Apache License, Version 2.0 (the "License");
025: * you may not use this file except in compliance with the License.
026: * You may obtain a copy of the License at
027: *
028: * http://www.apache.org/licenses/LICENSE-2.0
029: *
030: * Unless required by applicable law or agreed to in writing, software
031: * distributed under the License is distributed on an "AS IS" BASIS,
032: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033: * See the License for the specific language governing permissions and
034: * limitations under the License.
035: *
036: */
037: package org.netbeans.modules.jdbcwizard.wizards;
038:
039: import java.awt.Dialog;
040:
041: import javax.swing.SwingUtilities;
042:
043: import org.openide.WizardDescriptor;
044: import org.openide.DialogDisplayer;
045:
046: /**
047: * Abstract base class for JDBC Wizards. Interested classes should instantiate the correct concrete
048: * implementation and call show() to display the wizard.
049: *
050: * @author Jonathan Giron
051: * @version
052: */
053: public abstract class JDBCWizard {
054:
055: /** Common context for panels to exchange and store data. */
056: protected JDBCWizardContext context;
057:
058: /** Creates a new instance of JDBCWizard */
059: protected JDBCWizard() {
060: this .context = new JDBCWizardContext();
061: }
062:
063: /**
064: * Gets context associated with this wizard.
065: *
066: * @return JDBCWizardContext instance associated with this wizard.
067: */
068: public JDBCWizardContext getContext() {
069: return this .context;
070: }
071:
072: /**
073: * Gets descriptor containing information on available panels. Concrete subclasses should
074: * instantiate and define this descriptor.
075: *
076: * @return WizardDescriptor associated with this object
077: */
078: public abstract WizardDescriptor getDescriptor();
079:
080: /**
081: * Gets iterator used to cycle through available panels for this wizard. Concrete subclasses
082: * should instantiate and define this iterator.
083: *
084: * @return WizardDescriptor.Iterator associated with this object
085: */
086: public abstract WizardDescriptor.Iterator getIterator();
087:
088: /**
089: * Displays the wizard and its associated panels.
090: *
091: * @return true if user completed the wizard normally; false otherwise.
092: */
093: public boolean show() {
094: // Set default return value.
095: boolean response = false;
096:
097: try {
098: this .initialize();
099:
100: final WizardDescriptor desc = this .getDescriptor();
101: final Dialog dlg = DialogDisplayer.getDefault()
102: .createDialog(desc);
103: dlg.setTitle(this .getDialogTitle());
104: dlg.setSize(650, 450);
105:
106: if (this .context != null && desc != null) {
107: this .context.setWizardDescriptor(desc);
108: }
109:
110: if (!SwingUtilities.isEventDispatchThread()) {
111: SwingUtilities.invokeAndWait(new Runnable() {
112: public synchronized void run() {
113: dlg.setVisible(true);
114: }
115: });
116: } else {
117: dlg.setVisible(true);
118: }
119:
120: if (desc != null) {
121: if (desc.getValue() == WizardDescriptor.FINISH_OPTION) {
122: // Call method in concrete implementation to handle committal.
123: // commit();
124: response = true;
125: } else {
126: // Call method in concrete implementation to handle cancellation.
127: this .cancel();
128: }
129: }
130: } catch (final Exception e) {
131:
132: } finally {
133: // Call method in concrete implementation to do any necessary cleanup.
134: this .cleanup();
135: }
136:
137: return response;
138: }
139:
140: /**
141: * Performs processing to handle cancellation of this wizard.
142: */
143: protected abstract void cancel();
144:
145: /**
146: * Performs processing to cleanup any resources used by this wizard.
147: */
148: protected abstract void cleanup();
149:
150: /**
151: * Gets string label to display as title of dialog window.
152: *
153: * @return String representing dialog title
154: */
155: protected String getDialogTitle() {
156: return "JDBC Wizard";
157: }
158:
159: /**
160: * Initializes the wizard.
161: */
162: protected abstract void initialize();
163: }
|