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: /*
042: * ComponentsPanel.java
043: *
044: * Created on March 8, 2005, 11:41 AM
045: */
046:
047: package org.netbeans.modules.visualweb.complib.ui;
048:
049: import java.util.Iterator;
050: import java.util.ResourceBundle;
051: import java.util.Set;
052:
053: import javax.swing.Icon;
054: import javax.swing.SwingConstants;
055: import javax.swing.table.AbstractTableModel;
056: import javax.swing.table.DefaultTableCellRenderer;
057: import javax.swing.table.TableColumnModel;
058:
059: import org.netbeans.modules.visualweb.complib.Complib;
060: import org.netbeans.modules.visualweb.complib.ComplibServiceProvider;
061: import org.netbeans.modules.visualweb.complib.IdeUtil;
062: import org.netbeans.modules.visualweb.complib.ComplibServiceProvider.ComponentInfo;
063: import org.netbeans.modules.visualweb.complib.api.ComplibException;
064: import org.openide.DialogDisplayer;
065: import org.openide.NotifyDescriptor;
066: import org.openide.util.NbBundle;
067:
068: /**
069: *
070: * @author jhoff
071: */
072: public class ComponentsPanel extends javax.swing.JPanel {
073:
074: private Complib complib;
075:
076: /**
077: * Encapsulates info for IconCellRenderer
078: *
079: * @author Edwin Goei
080: */
081: private static class IconCellInfo {
082:
083: private Icon icon;
084:
085: private String displayName;
086:
087: /**
088: * @param icon
089: * @param displayName
090: */
091: private IconCellInfo(Icon icon, String displayName) {
092: this .icon = icon;
093: this .displayName = displayName;
094: }
095:
096: public String getDisplayName() {
097: return displayName;
098: }
099:
100: public Icon getIcon() {
101: return icon;
102: }
103:
104: public String toString() {
105: return displayName;
106: }
107: }
108:
109: /**
110: * Custom TableModel
111: *
112: * @author Edwin Goei
113: */
114: private static class ComponentTableModel extends AbstractTableModel {
115: // Init the column header labels
116: private static ResourceBundle rb = NbBundle
117: .getBundle(ComponentTableModel.class);
118:
119: private static final String[] columnNames = {
120: rb.getString("manager.componentTable.displayName"),
121: rb.getString("manager.componentTable.categories"),
122: rb.getString("manager.componentTable.className") };
123:
124: private static Class[] types = new Class[] {
125: IconCellInfo.class, Set.class, java.lang.String.class };
126:
127: private ComponentInfo[] compInfos;
128:
129: private ComponentTableModel(ComponentInfo[] compInfos) {
130: this .compInfos = compInfos;
131: }
132:
133: public int getColumnCount() {
134: return columnNames.length;
135: }
136:
137: public Class<?> getColumnClass(int columnIndex) {
138: return types[columnIndex];
139: }
140:
141: public String getColumnName(int column) {
142: return columnNames[column];
143: }
144:
145: public int getRowCount() {
146: return compInfos.length;
147: }
148:
149: public Object getValueAt(int rowIndex, int columnIndex) {
150: ComponentInfo compInfo = compInfos[rowIndex];
151:
152: switch (columnIndex) {
153: case 0:
154: return new IconCellInfo(compInfo.getIcon(), compInfo
155: .getDisplayName());
156: case 1:
157: return compInfo.getInitialCategories();
158: case 2:
159: return compInfo.getClassName();
160: default:
161: return "unknown column index"; // NOI18N
162: }
163: }
164: }
165:
166: /**
167: * Creates new form ComponentsPanel
168: *
169: * @param complib
170: */
171: public ComponentsPanel(Complib complib) {
172: initComponents();
173:
174: this .complib = complib;
175:
176: try {
177: initComponentListJtable();
178: } catch (ComplibException e) {
179: IdeUtil.logError(e);
180: // TODO handle error here instead
181: return;
182: }
183: }
184:
185: /**
186: * Update the JTable model from the complib. Note that the JTable should
187: * already exist.
188: *
189: * @throws ComplibException
190: */
191: private void initComponentListJtable() throws ComplibException {
192: // Update component list
193: ComponentInfo[] compInfos = mgr.getComponentInfos(complib);
194:
195: // Handle sorted columns
196: ComponentTableModel ctm = new ComponentTableModel(compInfos);
197: TableSorter tableSorter = new TableSorter(ctm);
198: tblCompList.setModel(tableSorter);
199: tableSorter.setTableHeader(tblCompList.getTableHeader());
200:
201: // Apparently, changing the TableModel requires updating the ColumnModel
202: // too
203: TableColumnModel tcm = tblCompList.getColumnModel();
204:
205: // Icon and name
206: tcm.getColumn(0).setCellRenderer(
207: new DefaultTableCellRenderer() {
208: protected void setValue(Object value) {
209: ComponentsPanel.IconCellInfo iconCellInfo = (IconCellInfo) value;
210: if (iconCellInfo == null) {
211: return;
212: }
213: setHorizontalAlignment(SwingConstants.LEFT);
214: setIcon(iconCellInfo.getIcon());
215: setText(iconCellInfo.getDisplayName());
216: }
217: });
218: tcm.getColumn(0).setPreferredWidth(170);
219:
220: // Categories
221: tcm.getColumn(1).setCellRenderer(
222: new DefaultTableCellRenderer() {
223: protected void setValue(Object value) {
224: // Render set of categories as a list of comma separated values
225: Set categories = (Set) value;
226: if (categories == null) {
227: return;
228: }
229:
230: StringBuffer buf = new StringBuffer();
231: Iterator iter = categories.iterator();
232: boolean hasNext = iter.hasNext();
233: while (hasNext) {
234: String category = (String) iter.next();
235: buf.append(category);
236: hasNext = iter.hasNext();
237: if (hasNext) {
238: buf.append(", ");
239: }
240: }
241:
242: setText(buf.toString());
243: }
244:
245: });
246: tcm.getColumn(1).setPreferredWidth(170);
247:
248: // Class name
249: tcm.getColumn(2).setPreferredWidth(260);
250:
251: tblCompList.setRowHeight(18);
252: }
253:
254: /**
255: * This method is called from within the constructor to initialize the form.
256: * WARNING: Do NOT modify this code. The content of this method is always
257: * regenerated by the Form Editor.
258: */
259: // <editor-fold defaultstate="collapsed" desc=" Generated Code
260: // <editor-fold defaultstate="collapsed" desc=" Generated Code
261: // <editor-fold defaultstate="collapsed" desc=" Generated Code
262: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
263: private void initComponents() {
264: java.awt.GridBagConstraints gridBagConstraints;
265:
266: lblCompList = new javax.swing.JLabel();
267: scrollCompList = new javax.swing.JScrollPane();
268: tblCompList = new javax.swing.JTable();
269: btnDefaultPaletteSettings = new javax.swing.JButton();
270:
271: setLayout(new java.awt.GridBagLayout());
272:
273: lblCompList.setLabelFor(tblCompList);
274: org.openide.awt.Mnemonics
275: .setLocalizedText(
276: lblCompList,
277: org.openide.util.NbBundle
278: .getBundle(
279: "org/netbeans/modules/visualweb/complib/ui/Bundle")
280: .getString("manager.ComponentList"));
281: gridBagConstraints = new java.awt.GridBagConstraints();
282: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
283: gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
284: add(lblCompList, gridBagConstraints);
285: lblCompList.getAccessibleContext().setAccessibleDescription(
286: org.openide.util.NbBundle.getMessage(
287: ComponentsPanel.class,
288: "manager.ComponentListA11yDescription"));
289:
290: tblCompList.setModel(new javax.swing.table.DefaultTableModel(
291: new Object[][] {
292:
293: }, new String[] { "On Palette", "Name", "Category",
294: "Class Name" }) {
295: Class[] types = new Class[] { java.lang.Boolean.class,
296: java.lang.String.class, java.lang.String.class,
297: java.lang.String.class };
298: boolean[] canEdit = new boolean[] { true, false, false,
299: false };
300:
301: public Class getColumnClass(int columnIndex) {
302: return types[columnIndex];
303: }
304:
305: public boolean isCellEditable(int rowIndex, int columnIndex) {
306: return canEdit[columnIndex];
307: }
308: });
309: tblCompList
310: .setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
311: tblCompList.setIntercellSpacing(new java.awt.Dimension(2, 2));
312: tblCompList.setShowVerticalLines(false);
313: scrollCompList.setViewportView(tblCompList);
314:
315: gridBagConstraints = new java.awt.GridBagConstraints();
316: gridBagConstraints.gridx = 0;
317: gridBagConstraints.gridy = 1;
318: gridBagConstraints.gridwidth = 2;
319: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
320: gridBagConstraints.weightx = 1.0;
321: gridBagConstraints.weighty = 1.0;
322: gridBagConstraints.insets = new java.awt.Insets(6, 10, 10, 10);
323: add(scrollCompList, gridBagConstraints);
324:
325: org.openide.awt.Mnemonics
326: .setLocalizedText(btnDefaultPaletteSettings,
327: org.openide.util.NbBundle.getMessage(
328: ComponentsPanel.class,
329: "manager.DefaultPaletteSettingsButton"));
330: btnDefaultPaletteSettings
331: .addActionListener(new java.awt.event.ActionListener() {
332: public void actionPerformed(
333: java.awt.event.ActionEvent evt) {
334: btnDefaultPaletteSettingsActionPerformed(evt);
335: }
336: });
337:
338: gridBagConstraints = new java.awt.GridBagConstraints();
339: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
340: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
341: add(btnDefaultPaletteSettings, gridBagConstraints);
342: btnDefaultPaletteSettings
343: .getAccessibleContext()
344: .setAccessibleDescription(
345: org.openide.util.NbBundle
346: .getMessage(ComponentsPanel.class,
347: "manager.DefaultPaletteSettingsButtonA11yDescription"));
348:
349: }// </editor-fold>//GEN-END:initComponents
350:
351: private void btnDefaultPaletteSettingsActionPerformed(
352: java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDefaultPaletteSettingsActionPerformed
353: String message = NbBundle.getMessage(ComponentsPanel.class,
354: "manager.DefaultPaletteSettingsMessage"); // NOI18N
355: NotifyDescriptor nd = new NotifyDescriptor.Confirmation(
356: message, NotifyDescriptor.OK_CANCEL_OPTION);
357: Object result = DialogDisplayer.getDefault().notify(nd);
358: if (NotifyDescriptor.OK_OPTION == result) {
359: try {
360: mgr.resetToInitialPalette(complib);
361:
362: // Update the component list also
363: initComponentListJtable();
364: } catch (ComplibException e) {
365: IdeUtil.logError(e);
366: }
367: }
368: }// GEN-LAST:event_btnDefaultPaletteSettingsActionPerformed
369:
370: // Variables declaration - do not modify//GEN-BEGIN:variables
371: private javax.swing.JButton btnDefaultPaletteSettings;
372: private javax.swing.JLabel lblCompList;
373: private javax.swing.JScrollPane scrollCompList;
374: private javax.swing.JTable tblCompList;
375: // End of variables declaration//GEN-END:variables
376: private ComplibServiceProvider mgr = ComplibServiceProvider
377: .getInstance();
378: }
|