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.awt.Component;
045: import java.awt.Dimension;
046: import javax.swing.*;
047: import javax.swing.border.*;
048: import java.util.*;
049: import javax.swing.event.*;
050: import java.awt.event.*;
051:
052: import org.openide.WizardDescriptor;
053:
054: import org.netbeans.modules.form.project.ClassSource;
055: import org.openide.util.ChangeSupport;
056: import org.openide.util.NbCollections;
057:
058: /**
059: * The second panel in the wizard for adding new components to the palette.
060: * Lets the user choose components from a list of all available components
061: * in selected source.
062: *
063: * @author Tomas Pavek
064: */
065:
066: class ChooseBeansWizardPanel implements
067: WizardDescriptor.Panel<AddToPaletteWizard> {
068:
069: private List<? extends ClassSource.Entry> currentFiles; // roots (typically JAR files) chosen by the user
070:
071: private List<BeanInstaller.ItemInfo> markedBeans; // beans marked in JAR manifest
072: private List<BeanInstaller.ItemInfo> allBeans; // all bean classes under given roots
073: private Class<? extends ClassSource.Entry> sourceType;
074:
075: private BeanSelector beanSelector;
076: private JLabel noBeansLabel;
077:
078: private final ChangeSupport cs = new ChangeSupport(this );
079:
080: // ----------
081: // WizardDescriptor.Panel implementation
082:
083: public Component getComponent() {
084: if ((markedBeans == null)
085: && ((allBeans == null) || (allBeans.size() == 0))) {
086: // No beans found
087: String messageKey;
088: if (sourceType == ClassSource.JarEntry.class)
089: messageKey = "MSG_NoBeanInJAR"; // NOI18N
090: else if (sourceType == ClassSource.LibraryEntry.class)
091: messageKey = "MSG_NoBeanInLibrary"; // NOI18N
092: else if (sourceType == ClassSource.ProjectEntry.class)
093: messageKey = "MSG_NoBeanInProject"; // NOI18N
094: else
095: throw new IllegalArgumentException();
096: noBeansLabel = new JLabel(PaletteUtils
097: .getBundleString(messageKey));
098: noBeansLabel.setPreferredSize(new Dimension(400, 300));
099: noBeansLabel.setVerticalAlignment(SwingConstants.TOP);
100: noBeansLabel.setName(PaletteUtils
101: .getBundleString("CTL_NoBeans_Caption")); // NOI18N
102: noBeansLabel.putClientProperty(
103: "WizardPanel_contentSelectedIndex", // NOI18N
104: new Integer(1));
105: return noBeansLabel;
106: } else {
107: if (beanSelector == null) { // create the UI component for the wizard step
108: beanSelector = new BeanSelector();
109:
110: // wizard API: set the caption and index of this panel
111: beanSelector.setName(PaletteUtils
112: .getBundleString("CTL_SelectBeans_Caption")); // NOI18N
113: beanSelector.putClientProperty(
114: "WizardPanel_contentSelectedIndex", // NOI18N
115: new Integer(1));
116: if (markedBeans != null || allBeans != null)
117: beanSelector.setBeans(markedBeans, allBeans);
118:
119: Listener listener = new Listener();
120: beanSelector.list.addListSelectionListener(listener);
121: beanSelector.radio1.addActionListener(listener);
122: beanSelector.radio2.addActionListener(listener);
123: }
124: }
125:
126: return beanSelector;
127: }
128:
129: public org.openide.util.HelpCtx getHelp() {
130: // PENDING
131: return new org.openide.util.HelpCtx("beans.adding"); // NOI18N
132: }
133:
134: public boolean isValid() {
135: return beanSelector != null
136: && beanSelector.getSelectedBeans().size() > 0;
137: }
138:
139: public void readSettings(AddToPaletteWizard wizard) {
140: sourceType = wizard.getSourceType();
141: List<? extends ClassSource.Entry> jarFiles = wizard
142: .getJARFiles();
143:
144: if (jarFiles.equals(currentFiles)) {
145: return; // no change from the last time
146: }
147:
148: currentFiles = jarFiles;
149:
150: allBeans = null; // don't read all the beans until needed
151: markedBeans = BeanInstaller.findJavaBeansInJar(jarFiles);
152: if (markedBeans != null) {
153: Collections.sort(markedBeans);
154: } else {
155: allBeans = BeanInstaller.findJavaBeans(jarFiles);
156: Collections.sort(allBeans);
157: }
158:
159: if (beanSelector != null)
160: beanSelector.setBeans(markedBeans, allBeans);
161: }
162:
163: public void storeSettings(AddToPaletteWizard settings) {
164: if (beanSelector != null) {
165: List<BeanInstaller.ItemInfo> itemList = beanSelector
166: .getSelectedBeans();
167: BeanInstaller.ItemInfo[] itemArray = new BeanInstaller.ItemInfo[itemList
168: .size()];
169: itemList.toArray(itemArray);
170: settings.setSelectedBeans(itemArray);
171: }
172: }
173:
174: public void addChangeListener(ChangeListener listener) {
175: cs.addChangeListener(listener);
176: }
177:
178: public void removeChangeListener(ChangeListener listener) {
179: cs.removeChangeListener(listener);
180: }
181:
182: // -----
183:
184: static class BeanSelector extends JPanel {
185:
186: JList list;
187: JRadioButton radio1, radio2;
188:
189: BeanSelector() {
190: setLayout(new java.awt.GridBagLayout());
191: java.awt.GridBagConstraints gridBagConstraints;
192:
193: JLabel label1 = new JLabel();
194: org.openide.awt.Mnemonics.setLocalizedText(label1,
195: PaletteUtils.getBundleString("CTL_SelectBeans")); // NOI18N
196: gridBagConstraints = new java.awt.GridBagConstraints();
197: gridBagConstraints.gridwidth = 3;
198: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
199: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
200: add(label1, gridBagConstraints);
201:
202: list = new JList();
203: list.setLayoutOrientation(JList.VERTICAL_WRAP);
204: list.setVisibleRowCount(0);
205: list.setCellRenderer(new ItemInfoRenderer());
206: list.getAccessibleContext().setAccessibleDescription(
207: PaletteUtils
208: .getBundleString("ACSD_CTL_SelectBeans")); // NOI18N
209: label1.setLabelFor(list);
210:
211: JScrollPane scrollPane = new JScrollPane();
212: scrollPane.setViewportView(list);
213: gridBagConstraints = new java.awt.GridBagConstraints();
214: gridBagConstraints.gridx = 0;
215: gridBagConstraints.gridy = 1;
216: gridBagConstraints.gridwidth = 3;
217: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
218: gridBagConstraints.weightx = 1.0;
219: gridBagConstraints.weighty = 1.0;
220: add(scrollPane, gridBagConstraints);
221:
222: radio1 = new JRadioButton();
223: radio1.setActionCommand("SHOW MARKED"); // NOI18N
224: org.openide.awt.Mnemonics.setLocalizedText(radio1,
225: PaletteUtils.getBundleString("CTL_ShowMarked")); // NOI18N
226: radio1.setToolTipText(PaletteUtils
227: .getBundleString("HINT_ShowMarked")); // NOI18N
228: gridBagConstraints = new java.awt.GridBagConstraints();
229: gridBagConstraints.gridx = 0;
230: gridBagConstraints.gridy = 2;
231: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
232: add(radio1, gridBagConstraints);
233: // PENDING A11Y
234:
235: radio2 = new JRadioButton();
236: radio2.setActionCommand("SHOW ALL"); // NOI18N
237: org.openide.awt.Mnemonics.setLocalizedText(radio2,
238: PaletteUtils.getBundleString("CTL_ShowAllClasses")); // NOI18N
239: radio2.setToolTipText(PaletteUtils
240: .getBundleString("HINT_ShowAllClasses")); // NOI18N
241: gridBagConstraints = new java.awt.GridBagConstraints();
242: gridBagConstraints.gridx = 1;
243: gridBagConstraints.gridy = 2;
244: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
245: add(radio2, gridBagConstraints);
246: // PENDING A11Y
247:
248: ButtonGroup buttonGroup = new ButtonGroup();
249: buttonGroup.add(radio1);
250: buttonGroup.add(radio2);
251:
252: getAccessibleContext().setAccessibleDescription(
253: PaletteUtils
254: .getBundleString("ACSD_SelectBeansDialog")); // NOI18N
255: }
256:
257: void setBeans(List<BeanInstaller.ItemInfo> markedBeans,
258: List<BeanInstaller.ItemInfo> allBeans) {
259: if (markedBeans == null) {
260: radio1.setEnabled(false);
261: } else {
262: radio1.setEnabled(true);
263: radio1.setSelected(true);
264: setDisplayedBeans(markedBeans);
265: }
266: if (allBeans != null && markedBeans == null) {
267: radio2.setSelected(true);
268: setDisplayedBeans(allBeans);
269: }
270: }
271:
272: void setDisplayedBeans(final List<BeanInstaller.ItemInfo> beans) {
273: list.setModel(new AbstractListModel() {
274: public int getSize() {
275: return beans.size();
276: }
277:
278: public Object getElementAt(int i) {
279: return beans.get(i);
280: }
281: });
282: }
283:
284: List<BeanInstaller.ItemInfo> getSelectedBeans() {
285: return NbCollections.checkedListByCopy(Arrays.asList(list
286: .getSelectedValues()),
287: BeanInstaller.ItemInfo.class, true);
288: }
289:
290: @Override
291: public Dimension getPreferredSize() {
292: return new Dimension(400, 300);
293: }
294: }
295:
296: // --------
297:
298: private static class ItemInfoRenderer extends JLabel implements
299: ListCellRenderer {
300: private static final Border hasFocusBorder = new LineBorder(
301: UIManager.getColor("List.focusCellHighlight")); // NOI18N
302: private static final Border noFocusBorder = BorderFactory
303: .createEmptyBorder(1, 1, 1, 1);
304:
305: public ItemInfoRenderer() {
306: setOpaque(true);
307: setBorder(noFocusBorder);
308: }
309:
310: public Component getListCellRendererComponent(JList list,
311: Object value, int index, boolean isSelected,
312: boolean cellHasFocus) {
313: String name = ((BeanInstaller.ItemInfo) value).classname;
314: setToolTipText(name); // full class name as tooltip
315:
316: int i = name.lastIndexOf('.');
317: if (i >= 0)
318: name = name.substring(i + 1);
319:
320: setText(name); // short class name as the label text
321:
322: if (isSelected) {
323: setBackground(UIManager
324: .getColor("List.selectionBackground")); // NOI18N
325: setForeground(UIManager
326: .getColor("List.selectionForeground")); // NOI18N
327: } else {
328: setBackground(list.getBackground());
329: setForeground(list.getForeground());
330: }
331: setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
332: return this ;
333: }
334: }
335:
336: // -------
337:
338: class Listener implements ListSelectionListener, ActionListener {
339:
340: public void valueChanged(ListSelectionEvent e) {
341: cs.fireChange();
342: }
343:
344: public void actionPerformed(ActionEvent ev) {
345: if ("SHOW MARKED".equals(ev.getActionCommand())) { // NOI18N
346: beanSelector.setDisplayedBeans(markedBeans);
347: } else if ("SHOW ALL".equals(ev.getActionCommand())) { // NOI18N
348: if (allBeans == null) { // not read yet
349: // PENDING wait cursor
350: allBeans = BeanInstaller
351: .findJavaBeans(currentFiles);
352: Collections.sort(allBeans);
353: }
354: beanSelector.setDisplayedBeans(allBeans);
355: }
356: }
357: }
358: }
|