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.awt.BorderLayout;
044: import java.awt.Component;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.event.KeyAdapter;
048: import java.awt.event.KeyEvent;
049: import java.util.HashSet;
050: import java.util.Iterator;
051: import java.util.Set;
052:
053: import javax.swing.JLabel;
054: import javax.swing.JPanel;
055: import javax.swing.JTextField;
056: import javax.swing.event.ChangeEvent;
057: import javax.swing.event.ChangeListener;
058:
059: import net.java.hulp.i18n.Logger;
060: import org.netbeans.modules.etl.logger.Localizer;
061: import org.netbeans.modules.etl.logger.LogUtil;
062: import org.openide.DialogDisplayer;
063: import org.openide.NotifyDescriptor;
064: import org.openide.WizardDescriptor;
065: import org.openide.filesystems.FileObject;
066: import org.openide.filesystems.Repository;
067: import org.openide.util.HelpCtx;
068:
069: /**
070: * Accepts input of unique name for new ETL Collaboration instance.
071: *
072: * @author Sanjeeth Duvuru
073: * @version $Revision$
074: */
075: public class ETLCollaborationWizardNamePanel extends JPanel implements
076: WizardDescriptor.Panel {
077:
078: private static transient final Logger mLogger = LogUtil
079: .getLogger(ETLCollaborationWizardNamePanel.class.getName());
080: private static transient final Localizer mLoc = Localizer.get();
081:
082: class NameFieldKeyAdapter extends KeyAdapter {
083:
084: /**
085: * Overrides default implementation to notify listeners of new collab name value
086: * in associated textfield.
087: *
088: * @param e KeyEvent to be handled
089: */
090: @Override
091: public void keyReleased(KeyEvent e) {
092: String collaborationName = ETLCollaborationWizardNamePanel.this .textField
093: .getText();
094:
095: if (collaborationName != null
096: && collaborationName.trim().length() != 0) {
097: ETLCollaborationWizardNamePanel.this .collabName = collaborationName
098: .trim();
099: } else {
100: ETLCollaborationWizardNamePanel.this .collabName = null;
101: }
102:
103: ETLCollaborationWizardNamePanel.this .fireChangeEvent();
104: }
105: }
106:
107: protected String collabName;
108:
109: /* Set <ChangeListeners> */
110: protected final Set listeners = new HashSet(1);
111: protected ETLCollaborationWizard owner;
112: protected JTextField textField;
113: protected String title;
114:
115: /**
116: * No-arg constructor for this wizard descriptor.
117: */
118: public ETLCollaborationWizardNamePanel() {
119: setLayout(new BorderLayout());
120:
121: JPanel outerPanel = new JPanel();
122: outerPanel.setLayout(new GridBagLayout());
123:
124: // Top filler panel to absorb 20% of any expansion up and down the page.
125: GridBagConstraints gbc = new GridBagConstraints();
126: gbc.gridx = 0;
127: gbc.gridy = 0;
128: gbc.anchor = GridBagConstraints.PAGE_START;
129: gbc.fill = GridBagConstraints.BOTH;
130: gbc.weightx = 1.0;
131: gbc.weighty = 0.2;
132: outerPanel.add(new JPanel(), gbc);
133:
134: // Text field label.
135: String nbBundle1 = mLoc.t("PRSR001: New Collaboration Name:");
136: JLabel header = new JLabel(Localizer.parse(nbBundle1));
137: header.setDisplayedMnemonic(Localizer.parse(nbBundle1)
138: .charAt(0));
139: header.getAccessibleContext().setAccessibleName(
140: Localizer.parse(nbBundle1));
141: gbc = new GridBagConstraints();
142: gbc.gridx = 0;
143: gbc.gridy = 1;
144: gbc.anchor = GridBagConstraints.LINE_START;
145: gbc.fill = GridBagConstraints.NONE;
146: gbc.insets.right = 10;
147: gbc.weightx = 0.0;
148: gbc.weighty = 0.0;
149: outerPanel.add(header, gbc);
150:
151: // Text field.
152: textField = new JTextField();
153: textField.addKeyListener(new NameFieldKeyAdapter());
154:
155: gbc = new GridBagConstraints();
156: gbc.gridx = 0;
157: gbc.gridy = 2;
158: gbc.anchor = GridBagConstraints.LINE_START;
159: gbc.fill = GridBagConstraints.HORIZONTAL;
160: gbc.weightx = 1.0;
161: gbc.weighty = 0.0;
162: outerPanel.add(textField, gbc);
163:
164: // Bottom filler panel to absorb 80% of any expansion up and down the page.
165: gbc = new GridBagConstraints();
166: gbc.gridx = 0;
167: gbc.gridy = 3;
168: gbc.anchor = GridBagConstraints.PAGE_START;
169: gbc.fill = GridBagConstraints.BOTH;
170: gbc.weightx = 1.0;
171: gbc.weighty = 0.8;
172: outerPanel.add(new JPanel(), gbc);
173:
174: add(outerPanel, BorderLayout.CENTER);
175: }
176:
177: /**
178: * Create the wizard panel descriptor, using the given panel title, content panel
179: *
180: * @param myOwner ETLWizard that owns this panel
181: * @param panelTitle text to display as panel title
182: */
183: public ETLCollaborationWizardNamePanel(
184: ETLCollaborationWizard myOwner, String panelTitle) {
185: this ();
186:
187: title = panelTitle;
188: this .setName(title);
189: owner = myOwner;
190: }
191:
192: /**
193: * @see ETLWizardPanel#addChangeListener
194: */
195: public final void addChangeListener(ChangeListener l) {
196: synchronized (listeners) {
197: listeners.add(l);
198: }
199: }
200:
201: /**
202: * @see ETLWizardPanel#fireChangeEvent
203: */
204: public void fireChangeEvent() {
205: Iterator it;
206:
207: synchronized (listeners) {
208: it = new HashSet(listeners).iterator();
209: }
210:
211: ChangeEvent ev = new ChangeEvent(this );
212: while (it.hasNext()) {
213: ((ChangeListener) it.next()).stateChanged(ev);
214: }
215: }
216:
217: // Get the visual component for the panel. In this template, the component
218: // is kept separate. This can be more efficient: if the wizard is created
219: // but never displayed, or not all panels are displayed, it is better to
220: // create only those which really need to be visible.
221: /**
222: * @see ETLWizardPanel#getComponent
223: */
224: public Component getComponent() {
225: return this ;
226: }
227:
228: /**
229: * Gets current value of collaboration name as entered by user.
230: *
231: * @return current user-specified name
232: */
233: public String getCollabName() {
234: return collabName;
235: }
236:
237: /**
238: * @see ETLWizardPanel#getHelp
239: */
240: public HelpCtx getHelp() {
241: return HelpCtx.DEFAULT_HELP;
242: }
243:
244: /**
245: * Indicates whether current contents of collaboration name textfield correspond to
246: * the name of an existing collaboration in the current project.
247: *
248: * @return true if textfield contains the name of an existing collab; false otherwise
249: */
250: public boolean isDuplicateCollabName() {
251: String collaborationName = textField.getText();
252: collaborationName = (collaborationName != null) ? collaborationName
253: .trim()
254: : null;
255:
256: boolean duplicated = false;
257:
258: //TODO - verify implementation. Where do collaboration files live?
259: FileObject fo = Repository.getDefault().getDefaultFileSystem()
260: .getRoot().getFileObject(collaborationName);
261:
262: if (fo != null) { // file exists
263: duplicated = true;
264: String nbBundle2 = mLoc
265: .t(
266: "PRSR001: An object already exists in this project with the name {0}. Please enter a unique name.",
267: collaborationName);
268: NotifyDescriptor.Message d1 = new NotifyDescriptor.Message(
269: Localizer.parse(nbBundle2),
270: NotifyDescriptor.INFORMATION_MESSAGE);
271: DialogDisplayer.getDefault().notify(d1);
272: textField.requestFocus();
273: }
274: return duplicated;
275: }
276:
277: /**
278: * @see ETLWizardPanel#isValid
279: */
280: @Override
281: public boolean isValid() {
282: boolean returnVal = false;
283: if (collabName != null) {
284: returnVal = true;
285: }
286: return returnVal;
287: }
288:
289: /**
290: * @see ETLWizardPanel#readSettings
291: */
292: public void readSettings(Object settings) {
293: WizardDescriptor wd = null;
294: if (settings instanceof ETLWizardContext) {
295: ETLWizardContext wizardContext = (ETLWizardContext) settings;
296: wd = (WizardDescriptor) wizardContext
297: .getProperty(ETLWizardContext.WIZARD_DESCRIPTOR);
298: } else if (settings instanceof WizardDescriptor) {
299: wd = (WizardDescriptor) settings;
300: }
301:
302: if (wd != null) {
303: String myCollabName = (String) wd
304: .getProperty(ETLCollaborationWizard.COLLABORATION_NAME);
305: textField.setText(myCollabName);
306: }
307: }
308:
309: /**
310: * @see ETLWizardPanel#removeChangeListener
311: */
312: public final void removeChangeListener(ChangeListener l) {
313: synchronized (listeners) {
314: listeners.remove(l);
315: }
316: }
317:
318: /**
319: * @see ETLWizardPanel#storeSettings
320: */
321: public void storeSettings(Object settings) {
322: WizardDescriptor wd = null;
323: if (settings instanceof ETLWizardContext) {
324: ETLWizardContext wizardContext = (ETLWizardContext) settings;
325: wd = (WizardDescriptor) wizardContext
326: .getProperty(ETLWizardContext.WIZARD_DESCRIPTOR);
327: } else if (settings instanceof WizardDescriptor) {
328: wd = (WizardDescriptor) settings;
329: this .owner.setDescriptor(wd);
330: }
331:
332: if (wd != null) {
333: final Object selectedOption = wd.getValue();
334: if (NotifyDescriptor.CANCEL_OPTION == selectedOption
335: || NotifyDescriptor.CLOSED_OPTION == selectedOption) {
336: return;
337: }
338: wd.putProperty(ETLCollaborationWizard.COLLABORATION_NAME,
339: collabName);
340: }
341: }
342: }
|