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 org.openide.WizardDescriptor;
040:
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.HashMap;
044: import java.util.List;
045:
046: /**
047: * Provides a context for reading and storing intermediate values, properties, etc., among
048: * components in a wizard.
049: */
050: public class JDBCWizardContext extends HashMap {
051: /**
052: *
053: */
054: private static final long serialVersionUID = 1L;
055:
056: /** Default reserved key to reference wizard descriptor. */
057: public static final String WIZARD_DESCRIPTOR = "wizDesc";
058:
059: /** Key name used to reference collaboration name in wizard context. */
060: public static final String COLLABORATION_NAME = "collaboration_name";
061:
062: public static final String TARGETFOLDER_PATH = "targetfolder_path";
063:
064: public static final String SELECTEDTABLES = "selected_tables";
065:
066: public static final String CONNECTION_INFO = "connection_info";
067:
068: public static final String DBTYPE = "db_type";
069:
070: /** List of current reserved keys for this context. */
071: protected List reservedKeys;
072:
073: /** Creates a new instance of JDBCWizardContext */
074: public JDBCWizardContext() {
075: }
076:
077: /**
078: * Sets the property associated with the given key. Supplying a null value for key, or
079: * attempting to clear a property associated with a reserved key, results in an
080: * IllegalArgumentException.
081: *
082: * @param key key of property to be cleared
083: */
084: public void clearProperty(final String key) {
085: if (key == null) {
086: throw new IllegalArgumentException(
087: "Must supply non-null ref for key.");
088: }
089:
090: if (this .isReservedKey(key)) {
091: throw new IllegalArgumentException(
092: "Cannot use clear property using reserved key: "
093: + key.trim()
094: + "; use appropriate clear method instead.");
095: }
096:
097: this .remove(key);
098: }
099:
100: /**
101: * Clears the current wizard descriptor instance, if any.
102: */
103: public void clearWizardDescriptor() {
104: this .remove(JDBCWizardContext.WIZARD_DESCRIPTOR);
105: }
106:
107: /**
108: * Gets the property, if any, associated with the given key.
109: *
110: * @param key key of property to get
111: * @return associated property, or null if none exists
112: */
113: public Object getProperty(final String key) {
114: return this .get(key);
115: }
116:
117: /**
118: * Gets List of current reserved keys for this context.
119: *
120: * @return List of reserved keys
121: */
122: public List getReservedKeys() {
123: this .createReservedKeys();
124: return Collections.unmodifiableList(this .reservedKeys);
125: }
126:
127: /**
128: * Indicates the wizard option last selected by the user, provided a wizard descriptor has been
129: * set in this context. If no wizard descriptor is set, throws 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: final WizardDescriptor desc = this .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: final Object o = this .get(JDBCWizardContext.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(final String key) {
161: return this .getReservedKeys().contains(key);
162: }
163:
164: /**
165: * Sets the property associated with the given key. Null values for either argument results in
166: * an IllegalArgumentException.
167: *
168: * @param key key of property to be associated
169: * @param value property to be associated
170: */
171: public void setProperty(final String key, final Object value) {
172: if (key == null) {
173: throw new IllegalArgumentException(
174: "Must supply non-null ref for key.");
175: }
176:
177: if (this .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(final WizardDescriptor desc) {
193: if (desc == null) {
194: throw new IllegalArgumentException(
195: "Must supply non-null ref for desc.");
196: }
197:
198: this .put(JDBCWizardContext.WIZARD_DESCRIPTOR, desc);
199: }
200:
201: /**
202: * Creates list of reserved keys associated with this context instance.
203: */
204: protected void createReservedKeys() {
205: if (this .reservedKeys == null) {
206: this .reservedKeys = new ArrayList();
207: } else {
208: this.reservedKeys.clear();
209: }
210:
211: this.reservedKeys.add(JDBCWizardContext.WIZARD_DESCRIPTOR);
212: }
213: }
|