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-2006 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:
042: package org.netbeans.modules.form.palette;
043:
044: import java.io.File;
045: import javax.swing.JFileChooser;
046: import javax.swing.BorderFactory;
047: import javax.swing.event.*;
048: import java.awt.event.*;
049: import java.beans.*;
050: import java.util.ArrayList;
051: import java.util.List;
052: import org.netbeans.modules.form.project.ClassSource;
053:
054: import org.openide.WizardDescriptor;
055: import org.openide.util.ChangeSupport;
056:
057: /**
058: * The first panel in the wizard for adding new components to the palette from
059: * a JAR. In this panel (as the first step) the user chooses the JAR file.
060: * The alternative first steps are ChooseLibraryWizardPanel and
061: * ChooseProjectWizardPanel.
062: *
063: * @author Tomas Pavek
064: */
065:
066: class ChooseJARWizardPanel implements
067: WizardDescriptor.Panel<AddToPaletteWizard> {
068:
069: private JFileChooser fileChooser;
070: private static String lastDirectoryUsed;
071:
072: private AddToPaletteWizard wizard;
073:
074: private final ChangeSupport cs = new ChangeSupport(this );
075:
076: // ----------
077: // WizardDescriptor.Panel implementation
078:
079: public java.awt.Component getComponent() {
080: if (fileChooser == null) { // create the UI component for the wizard step
081: fileChooser = new JFileChooser(lastDirectoryUsed);
082: fileChooser.setBorder(BorderFactory.createEmptyBorder(0, 0,
083: 0, 0));
084:
085: // wizard API: set the caption and index of this panel
086: fileChooser.setName(PaletteUtils
087: .getBundleString("CTL_SelectJAR_Caption")); // NOI18N
088: fileChooser.putClientProperty(
089: "WizardPanel_contentSelectedIndex", // NOI18N
090: new Integer(0));
091:
092: fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
093: fileChooser.setAcceptAllFileFilterUsed(true);
094: fileChooser.setControlButtonsAreShown(false);
095: fileChooser.setMultiSelectionEnabled(true);
096:
097: fileChooser
098: .setFileFilter(new javax.swing.filechooser.FileFilter() {
099: public boolean accept(File f) {
100: return f.isDirectory()
101: || f.getName().toLowerCase()
102: .endsWith(".jar"); // NOI18N
103: }
104:
105: public String getDescription() {
106: return PaletteUtils
107: .getBundleString("CTL_JarArchivesMask"); // NOI18N
108: }
109: });
110:
111: fileChooser.addActionListener(new ActionListener() {
112: public void actionPerformed(ActionEvent ev) {
113: if (JFileChooser.APPROVE_SELECTION.equals(ev
114: .getActionCommand()))
115: wizard.stepToNext();
116: else if (JFileChooser.CANCEL_SELECTION.equals(ev
117: .getActionCommand()))
118: fileChooser.getTopLevelAncestor().setVisible(
119: false);
120: }
121: });
122:
123: fileChooser
124: .addPropertyChangeListener(new PropertyChangeListener() {
125: public void propertyChange(
126: PropertyChangeEvent ev) {
127: if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY
128: .equals(ev.getPropertyName()))
129: cs.fireChange();
130: }
131: });
132:
133: fileChooser
134: .getAccessibleContext()
135: .setAccessibleDescription(
136: PaletteUtils
137: .getBundleString("CTL_SelectJAR_Step")); // NOI18N
138: }
139:
140: return fileChooser;
141: }
142:
143: public org.openide.util.HelpCtx getHelp() {
144: // PENDING
145: return new org.openide.util.HelpCtx("beans.adding"); // NOI18N
146: }
147:
148: public boolean isValid() {
149: if (fileChooser != null
150: && fileChooser.getSelectedFiles().length > 0) {
151: lastDirectoryUsed = fileChooser.getCurrentDirectory()
152: .getAbsolutePath();
153: return true;
154: }
155: return false;
156: }
157:
158: public void readSettings(AddToPaletteWizard settings) {
159: wizard = settings;
160: }
161:
162: public void storeSettings(AddToPaletteWizard settings) {
163: if (fileChooser != null) {
164: List<ClassSource.JarEntry> entries = new ArrayList<ClassSource.JarEntry>();
165: for (File jar : fileChooser.getSelectedFiles()) {
166: entries.add(new ClassSource.JarEntry(jar));
167: }
168: settings.setJARFiles(entries);
169: }
170: }
171:
172: public void addChangeListener(ChangeListener listener) {
173: cs.addChangeListener(listener);
174: }
175:
176: public void removeChangeListener(ChangeListener listener) {
177: cs.removeChangeListener(listener);
178: }
179:
180: }
|