001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.etl.ui.view.wizards;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.HashMap;
046: import java.util.List;
047:
048: import org.openide.WizardDescriptor;
049:
050: /**
051: * Provides a context for reading and storing intermediate values,
052: * properties, etc., among components in a wizard.
053: *
054: */
055: public class ETLWizardContext extends HashMap {
056: /** Default reserved key to reference wizard descriptor. */
057: public static final String WIZARD_DESCRIPTOR = "wizDesc";
058:
059: /** List of current reserved keys for this context. */
060: protected List reservedKeys;
061:
062: /** Creates a new instance of ETLWizardContext */
063: public ETLWizardContext() {
064: }
065:
066: /**
067: * Creates a new instance of ETLWizardContext containing the given WizardDescriptor as
068: * a context object.
069: *
070: * @param desc WizardDescriptor to be added to the context.
071: */
072: public ETLWizardContext(WizardDescriptor desc) {
073: setWizardDescriptor(desc);
074: }
075:
076: /**
077: * Sets the property associated with the given key. Supplying a null value for key, or
078: * attempting to clear a property associated with a reserved key, results in an
079: * IllegalArgumentException.
080: *
081: * @param key key of property to be cleared
082: */
083: public void clearProperty(String key) {
084: if (key == null) {
085: throw new IllegalArgumentException(
086: "Must supply non-null ref for key.");
087: }
088:
089: if (isReservedKey(key)) {
090: throw new IllegalArgumentException(
091: "Cannot use clear property using reserved key: "
092: + key.trim()
093: + "; use appropriate clear method instead.");
094: }
095:
096: this .remove(key);
097: }
098:
099: /**
100: * Clears the current wizard descriptor instance, if any.
101: */
102: public void clearWizardDescriptor() {
103: this .remove(WIZARD_DESCRIPTOR);
104: }
105:
106: /**
107: * Gets the property, if any, associated with the given key.
108: *
109: * @param key key of property to get
110: * @return associated property, or null if none exists
111: */
112: public Object getProperty(String key) {
113: return this .get(key);
114: }
115:
116: /**
117: * Gets List of current reserved keys for this context.
118: *
119: * @return List of reserved keys
120: */
121: public List getReservedKeys() {
122: createReservedKeys();
123: return Collections.unmodifiableList(reservedKeys);
124: }
125:
126: /**
127: * Indicates the wizard option last selected by the user, provided a wizard descriptor
128: * has been set in this context. If no wizard descriptor is set, throws
129: * java.lang.IllegalStateException.
130: *
131: * @return Object representing selected wizard option.
132: * @see org.openide.WizardDescriptor#PREVIOUS_OPTION
133: * @see org.openide.WizardDescriptor#NEXT_OPTION
134: * @see org.openide.WizardDescriptor#FINISH_OPTION
135: * @see org.openide.WizardDescriptor#CANCEL_OPTION
136: * @see org.openide.WizardDescriptor#CLOSED_OPTION
137: */
138: public Object getSelectedOption() {
139: WizardDescriptor desc = getWizardDescriptor();
140: return (desc != null) ? desc.getValue() : null;
141: }
142:
143: /**
144: * Gets wizard descriptor, if any, from this context.
145: *
146: * @return WizardDescriptor instance, or null if not found.
147: */
148: public WizardDescriptor getWizardDescriptor() {
149: Object o = this .get(WIZARD_DESCRIPTOR);
150: return (o instanceof WizardDescriptor) ? (WizardDescriptor) o
151: : null;
152: }
153:
154: /**
155: * Indicates whether the given string is a reserved key;
156: *
157: * @param key String to be tested
158: * @return true if key is reserved; false otherwise
159: */
160: public boolean isReservedKey(String key) {
161: return getReservedKeys().contains(key);
162: }
163:
164: /**
165: * Sets the property associated with the given key. Null values for either argument
166: * results in an IllegalArgumentException.
167: *
168: * @param key key of property to be associated
169: * @param value property to be associated
170: */
171: public void setProperty(String key, Object value) {
172: if (key == null) {
173: throw new IllegalArgumentException(
174: "Must supply non-null ref for key.");
175: }
176:
177: if (isReservedKey(key)) {
178: throw new IllegalArgumentException(
179: "Cannot use set property using reserved key: "
180: + key.trim()
181: + "; use appropriate setter instead.");
182: }
183:
184: this .put(key, value);
185: }
186:
187: /**
188: * Sets wizard descriptor in this context to the given instance.
189: *
190: * @param desc WizardDescriptor instance to be set
191: */
192: public void setWizardDescriptor(WizardDescriptor desc) {
193: if (desc == null) {
194: throw new IllegalArgumentException(
195: "Must supply non-null ref for desc.");
196: }
197:
198: this .put(WIZARD_DESCRIPTOR, desc);
199: }
200:
201: /**
202: * Creates list of reserved keys associated with this context instance.
203: */
204: protected void createReservedKeys() {
205: if (reservedKeys == null) {
206: reservedKeys = new ArrayList();
207: } else {
208: reservedKeys.clear();
209: }
210:
211: reservedKeys.add(WIZARD_DESCRIPTOR);
212: }
213: }
|