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.visualweb.web.ui.dt.component.vforms;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Graphics;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.ResourceBundle;
049: import javax.swing.DefaultCellEditor;
050: import javax.swing.DefaultComboBoxModel;
051: import javax.swing.Icon;
052: import javax.swing.JComboBox;
053: import javax.swing.JOptionPane;
054: import javax.swing.JPanel;
055: import javax.swing.JTable;
056: import javax.swing.JTextField;
057: import javax.swing.ListSelectionModel;
058: import javax.swing.UIManager;
059: import javax.swing.table.AbstractTableModel;
060: import javax.swing.table.DefaultTableCellRenderer;
061: import javax.swing.table.TableCellEditor;
062: import javax.swing.table.TableColumn;
063: import com.sun.rave.designtime.DesignBean;
064: import com.sun.rave.designtime.DesignContext;
065: import com.sun.rave.designtime.DesignProperty;
066: import com.sun.rave.designtime.Result;
067: import com.sun.rave.designtime.ext.componentgroup.ComponentGroupHolder;
068: import com.sun.rave.designtime.ext.componentgroup.impl.ColorWrapperImpl;
069: import com.sun.rave.designtime.ext.componentgroup.util.ComponentGroupHelper;
070: import com.sun.rave.web.ui.component.Form;
071: import javax.swing.DefaultListCellRenderer;
072: import javax.swing.JList;
073: import org.netbeans.modules.visualweb.web.ui.dt.component.FormDesignInfo;
074:
075: public class VirtualFormsCustomizerPanel extends JPanel {
076:
077: protected VirtualFormsCustomizer customizer;
078: private ArrayList vformsList = new ArrayList();
079: private FormsTableModel vformsTableModel = new FormsTableModel();
080: private HashMap colorMap = new HashMap();
081:
082: public VirtualFormsCustomizerPanel(VirtualFormsCustomizer customizer) {
083: this .customizer = customizer;
084: initComponents();
085: readVFormInfo();
086: }
087:
088: public Result applyChanges() {
089: // store the virtual forms config
090: Form.VirtualFormDescriptor[] vforms = (Form.VirtualFormDescriptor[]) vformsList
091: .toArray(new Form.VirtualFormDescriptor[vformsList
092: .size()]);
093: String vfConfig = Form.generateVirtualFormsConfig(vforms);
094: DesignProperty vfcProp = customizer.getDesignBean()
095: .getProperty("virtualFormsConfig"); // NOI18N
096: vfcProp.setValue(vfConfig);
097:
098: // store off the form colors
099: DesignContext context = customizer.getDesignBean()
100: .getDesignContext();
101: for (int i = 0; vforms != null && i < vforms.length; i++) {
102: String vfName = vforms[i].getName();
103: String key = getColorKey(vfName);
104: Color c = (Color) colorMap.get(key);
105: if (c != null) {
106: context.setContextData(key, new ColorWrapperImpl(c));
107: }
108: }
109:
110: // reset the customizer modified state
111: customizer.setModified(false);
112: return null;
113: }
114:
115: class FormsTableModel extends AbstractTableModel {
116: public FormsTableModel() {
117: }
118:
119: public int getRowCount() {
120: return vformsList.size();
121: }
122:
123: public int getColumnCount() {
124: return 2;
125: }
126:
127: public String getColumnName(int columnIndex) {
128: switch (columnIndex) {
129: case 0: // color
130: return ResourceBundle
131: .getBundle(
132: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
133: .getString("colorHeader"); // NOI18N
134: case 1: // virtual form name
135: return ResourceBundle
136: .getBundle(
137: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
138: .getString("nameHeader"); // NOI18N
139: }
140: return null;
141: }
142:
143: public Class getColumnClass(int columnIndex) {
144: switch (columnIndex) {
145: case 0: // color
146: return Color.class;
147: case 1: // virtual form name
148: return String.class;
149: }
150: return null;
151: }
152:
153: public boolean isCellEditable(int rowIndex, int columnIndex) {
154: return true;
155: }
156:
157: public Object getValueAt(int rowIndex, int columnIndex) {
158: Form.VirtualFormDescriptor vform = (Form.VirtualFormDescriptor) vformsList
159: .get(rowIndex);
160: if (vform != null) {
161: switch (columnIndex) {
162: case 0: // color
163: String vfName = vform.getName();
164: String key = getColorKey(vfName);
165: return ComponentGroupHelper.getMappedColor(key,
166: colorMap);
167: case 1: // virtual form name
168: return vform.getName();
169: }
170: }
171: return null;
172: }
173:
174: public void setValueAt(Object aValue, int rowIndex,
175: int columnIndex) {
176: Form.VirtualFormDescriptor vform = (Form.VirtualFormDescriptor) vformsList
177: .get(rowIndex);
178: if (vform != null) {
179: String vfName, colorKey;
180: switch (columnIndex) {
181: case 0: // color
182: vfName = vform.getName();
183: colorKey = getColorKey(vfName);
184: colorMap.put(colorKey, aValue);
185: customizer.setModified(true);
186: return;
187: case 1: // virtual form name
188: String name = aValue.toString();
189: name = name.trim();
190: name = name.replaceAll("\\|", "_"); // NOI18N
191: name = name.replaceAll(",", "_"); // NOI18N
192: if (name.length() < 1) {
193: name = VirtualFormsHelper
194: .getNewVirtualFormName(vformsList);
195: }
196: vfName = vform.getName();
197: colorKey = getColorKey(vfName);
198: Color c = (Color) colorMap.get(colorKey);
199: colorMap.remove(colorKey);
200: vform.setName(name);
201: colorKey = getColorKey(name);
202: colorMap.put(colorKey, c);
203: customizer.setModified(true);
204: return;
205: }
206: }
207: }
208: }
209:
210: private void readVFormInfo() {
211: DesignBean formBean = customizer.getDesignBean();
212: DesignContext dcontext = formBean.getDesignContext();
213: ComponentGroupHolder[] holders = ComponentGroupHelper
214: .getComponentGroupHolders(dcontext);
215: ComponentGroupHelper.populateColorMap(dcontext, holders,
216: colorMap);
217: Form form = (Form) formBean.getInstance();
218: Form.VirtualFormDescriptor[] vforms = form.getVirtualForms();
219: for (int i = 0; vforms != null && i < vforms.length; i++) {
220: Form.VirtualFormDescriptor vformCopy = new Form.VirtualFormDescriptor(
221: vforms[i].getName());
222: vformCopy.setParticipatingIds(vforms[i]
223: .getParticipatingIds());
224: vformCopy.setSubmittingIds(vforms[i].getSubmittingIds());
225: vformsList.add(vformCopy);
226: }
227:
228: vformsTable.setModel(vformsTableModel);
229: vformsTable
230: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
231:
232: TableColumn colorCol = vformsTable.getColumnModel()
233: .getColumn(0);
234: colorCol.setCellRenderer(new ColorCellRenderer());
235: colorCol.setCellEditor(new DefaultCellEditor(
236: new ColorComboBox()));
237:
238: // Takes two click to have the list popup
239: ((DefaultCellEditor) colorCol.getCellEditor())
240: .setClickCountToStart(2);
241:
242: // Have the first row selected by default
243: if (vformsTableModel.getRowCount() > 0)
244: vformsTable.changeSelection(0, 0, false, false);
245: }
246:
247: private String getColorKey(String vfName) {
248: DesignBean formBean = customizer.getDesignBean();
249: String formBeanId = formBean.getInstanceName();
250: String formIdDotVfName = formBeanId + "." + vfName;
251: return ComponentGroupHelper.getComponentGroupColorKey(
252: FormDesignInfo.VIRTUAL_FORM_HOLDER_NAME,
253: formIdDotVfName);
254: }
255:
256: class ColorCellRenderer extends DefaultTableCellRenderer {
257:
258: Color SELECTION_BACKGROUND = UIManager.getDefaults().getColor(
259: "TextField.selectionBackground");
260:
261: Color SELECTION_FOREGROUND = UIManager.getDefaults().getColor(
262: "TextField.selectionForeground");
263:
264: Color BACKGROUND = UIManager.getDefaults().getColor(
265: "TextField.background");
266:
267: Color FOREGROUND = UIManager.getDefaults().getColor(
268: "TextField.foreground");
269:
270: public Component getTableCellRendererComponent(JTable table,
271: Object value, boolean isSelected, boolean hasFocus,
272: int row, int column) {
273:
274: Component c = super .getTableCellRendererComponent(table,
275: value, isSelected, hasFocus, row, column);
276:
277: if (isSelected) {
278: c.setBackground(SELECTION_BACKGROUND);
279: c.setForeground(SELECTION_FOREGROUND);
280: } else {
281: c.setBackground(BACKGROUND);
282: c.setForeground(FOREGROUND);
283: }
284:
285: Color color = (Color) value;
286: setIcon(new ColorIcon(color));
287: setText(null);
288:
289: return c;
290: }
291: }
292:
293: class ColorListRenderer extends DefaultListCellRenderer {
294: public Component getListCellRendererComponent(JList list,
295: Object value, int index, boolean isSelected,
296: boolean hasFocus) {
297:
298: super .getListCellRendererComponent(list, value, index,
299: isSelected, hasFocus);
300:
301: Color c = (Color) value;
302: setIcon(new ColorIcon(c));
303: setText(null);
304:
305: return this ;
306: }
307: }
308:
309: class ColorIcon implements Icon {
310: private Color color;
311:
312: public ColorIcon(Color color) {
313: this .color = color;
314: }
315:
316: public void paintIcon(Component c, Graphics g, int x, int y) {
317: g.setColor(color);
318: g.fillRect(x, y, 8, 8);
319: g.setColor(Color.black);
320: g.drawRect(x, y, 8, 8);
321: }
322:
323: public int getIconWidth() {
324: return 8;
325: }
326:
327: public int getIconHeight() {
328: return 8;
329: }
330: }
331:
332: class ColorComboBox extends JComboBox {
333: public ColorComboBox() {
334: super ();
335: DefaultComboBoxModel cbm = new DefaultComboBoxModel();
336: for (int i = 0; i < ComponentGroupHelper.DEFAULT_COLOR_SET.length; i++) {
337: Color c = ComponentGroupHelper.DEFAULT_COLOR_SET[i];
338: cbm.addElement(c);
339: }
340: setModel(cbm);
341: setRenderer(new ColorListRenderer());
342: getAccessibleContext()
343: .setAccessibleName(
344: ResourceBundle
345: .getBundle(
346: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
347: .getString(
348: "colorComboAccessibleName")); // NOI18N
349: getAccessibleContext()
350: .setAccessibleDescription(
351: ResourceBundle
352: .getBundle(
353: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
354: .getString(
355: "colorComboAccessibleDescription")); // NOI18N
356: }
357: }
358:
359: /** This method is called from within the constructor to
360: * initialize the form.
361: * WARNING: Do NOT modify this code. The content of this method is
362: * always regenerated by the Form Editor.
363: */
364: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
365: private void initComponents() {
366: java.awt.GridBagConstraints gridBagConstraints;
367:
368: tableLabel = new javax.swing.JLabel();
369: jScrollPane1 = new javax.swing.JScrollPane();
370: vformsTable = new javax.swing.JTable();
371:
372: // Only one row selectable at a time
373: vformsTable
374: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
375:
376: // Create a default cell editor for String values that forces a stop
377: // editing event whenever focus is lost.
378: JTextField textField = new JTextField();
379: final TableCellEditor cellEditor = new TextFieldCellEditor(
380: vformsTable, textField);
381: vformsTable.setDefaultEditor(String.class, cellEditor);
382: vformsTable.putClientProperty("terminateEditOnFocusLost",
383: Boolean.TRUE); //NOI18N
384:
385: // Single click to start editing cells with String
386: ((DefaultCellEditor) vformsTable.getDefaultEditor(String.class))
387: .setClickCountToStart(1);
388:
389: // Create a default cell renderer for String values that consistently renders
390: // background colors.
391: vformsTable.setDefaultRenderer(String.class,
392: new HomogonousCellRenderer());
393:
394: // Stop the editing when the table lost its focus
395: vformsTable.putClientProperty("terminateEditOnFocusLost",
396: Boolean.TRUE);
397:
398: jPanel1 = new javax.swing.JPanel();
399: btnNew = new javax.swing.JButton();
400: btnDelete = new javax.swing.JButton();
401:
402: setLayout(new java.awt.GridBagLayout());
403:
404: getAccessibleContext().setAccessibleName(
405: "Edit Virtual Forms dialog box");
406: getAccessibleContext()
407: .setAccessibleDescription(
408: "Use this table to view and edit properties of the virutal forms defined on this page.");
409: tableLabel
410: .setDisplayedMnemonic(java.util.ResourceBundle
411: .getBundle(
412: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
413: .getString("labelMnemonic").charAt(0));
414: tableLabel.setLabelFor(vformsTable);
415: tableLabel
416: .setText(java.util.ResourceBundle
417: .getBundle(
418: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
419: .getString("customizeLabel"));
420: gridBagConstraints = new java.awt.GridBagConstraints();
421: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
422: gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 10);
423: add(tableLabel, gridBagConstraints);
424: tableLabel
425: .getAccessibleContext()
426: .setAccessibleName(
427: java.util.ResourceBundle
428: .getBundle(
429: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
430: .getString("tableLabelAccessibleName"));
431: tableLabel
432: .getAccessibleContext()
433: .setAccessibleDescription(
434: java.util.ResourceBundle
435: .getBundle(
436: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
437: .getString(
438: "tableLabelAccessibleDescription"));
439:
440: jScrollPane1.setBackground(java.awt.SystemColor.window);
441: vformsTable.setModel(new javax.swing.table.DefaultTableModel(
442: new Object[][] {
443:
444: }, new String[] {
445:
446: }));
447: vformsTable.setShowVerticalLines(false);
448: jScrollPane1.setViewportView(vformsTable);
449: vformsTable
450: .getAccessibleContext()
451: .setAccessibleName(
452: java.util.ResourceBundle
453: .getBundle(
454: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
455: .getString("vformsTableAccessibleName"));
456: vformsTable
457: .getAccessibleContext()
458: .setAccessibleDescription(
459: java.util.ResourceBundle
460: .getBundle(
461: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
462: .getString(
463: "vformsTableAccessibleDescription"));
464:
465: gridBagConstraints = new java.awt.GridBagConstraints();
466: gridBagConstraints.gridx = 0;
467: gridBagConstraints.gridy = 1;
468: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
469: gridBagConstraints.weightx = 1.0;
470: gridBagConstraints.weighty = 1.0;
471: gridBagConstraints.insets = new java.awt.Insets(0, 10, 17, 10);
472: add(jScrollPane1, gridBagConstraints);
473: jScrollPane1
474: .getAccessibleContext()
475: .setAccessibleName(
476: java.util.ResourceBundle
477: .getBundle(
478: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
479: .getString("vformsTableAccessibleName"));
480: jScrollPane1
481: .getAccessibleContext()
482: .setAccessibleDescription(
483: java.util.ResourceBundle
484: .getBundle(
485: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
486: .getString(
487: "vformsTableAccessibleDescription"));
488:
489: jPanel1.setLayout(new java.awt.GridLayout(2, 1, 0, 5));
490:
491: btnNew
492: .setMnemonic(java.util.ResourceBundle
493: .getBundle(
494: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
495: .getString("newButtonMnemonic").charAt(0));
496: btnNew
497: .setText(java.util.ResourceBundle
498: .getBundle(
499: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
500: .getString("newButton"));
501: btnNew
502: .setToolTipText(java.util.ResourceBundle
503: .getBundle(
504: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
505: .getString("NewVf"));
506: btnNew.addActionListener(new java.awt.event.ActionListener() {
507: public void actionPerformed(java.awt.event.ActionEvent evt) {
508: btnNewActionPerformed(evt);
509: }
510: });
511:
512: jPanel1.add(btnNew);
513: btnNew
514: .getAccessibleContext()
515: .setAccessibleName(
516: java.util.ResourceBundle
517: .getBundle(
518: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
519: .getString("btnNewAccessibleName"));
520: btnNew
521: .getAccessibleContext()
522: .setAccessibleDescription(
523: java.util.ResourceBundle
524: .getBundle(
525: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
526: .getString(
527: "btnNewAccessibleDescription"));
528:
529: btnDelete
530: .setMnemonic(java.util.ResourceBundle
531: .getBundle(
532: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
533: .getString("deleteButtonMnemonic").charAt(0));
534: btnDelete
535: .setText(java.util.ResourceBundle
536: .getBundle(
537: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
538: .getString("deleteButton"));
539: btnDelete
540: .setToolTipText(java.util.ResourceBundle
541: .getBundle(
542: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
543: .getString("DeleteVf"));
544: btnDelete
545: .addActionListener(new java.awt.event.ActionListener() {
546: public void actionPerformed(
547: java.awt.event.ActionEvent evt) {
548: btnDeleteActionPerformed(evt);
549: }
550: });
551:
552: jPanel1.add(btnDelete);
553: btnDelete
554: .getAccessibleContext()
555: .setAccessibleName(
556: java.util.ResourceBundle
557: .getBundle(
558: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
559: .getString("btnDeleteAccessibleName"));
560: btnDelete
561: .getAccessibleContext()
562: .setAccessibleDescription(
563: java.util.ResourceBundle
564: .getBundle(
565: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
566: .getString(
567: "btnDeleteAccessibleDescription"));
568:
569: gridBagConstraints = new java.awt.GridBagConstraints();
570: gridBagConstraints.gridx = 1;
571: gridBagConstraints.gridy = 1;
572: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
573: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 10);
574: add(jPanel1, gridBagConstraints);
575:
576: }
577:
578: // </editor-fold>//GEN-END:initComponents
579:
580: private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
581: int vfidx = vformsTable.getSelectedRow();
582: if (vfidx > -1 && vfidx < vformsList.size()) {
583: Form.VirtualFormDescriptor vform = (Form.VirtualFormDescriptor) vformsList
584: .get(vfidx);
585: String title = ResourceBundle
586: .getBundle(
587: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
588: .getString("confirmDeleteTitle"); // NOI18N
589: String msg = java.text.MessageFormat
590: .format(
591: java.util.ResourceBundle
592: .getBundle(
593: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
594: .getString("confirmDeleteMessage"),
595: new Object[] { vform.getName() }); // NOI18N
596: if (JOptionPane.showConfirmDialog(this , msg, title,
597: JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
598: vformsList.remove(vfidx);
599: vformsTableModel.fireTableDataChanged();
600: if (vformsList.size() <= vfidx) {
601: vfidx--;
602: }
603: if (vfidx >= 0) {
604: vformsTable.getSelectionModel()
605: .setSelectionInterval(vfidx, vfidx);
606: }
607: }
608: }
609: }//GEN-LAST:event_btnDeleteActionPerformed
610:
611: private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
612: String name = VirtualFormsHelper
613: .getNewVirtualFormName(vformsList);
614:
615: Form.VirtualFormDescriptor vform = new Form.VirtualFormDescriptor(
616: name);
617: vformsList.add(vform);
618:
619: vformsTableModel.fireTableDataChanged();
620: vformsTable.getSelectionModel().setSelectionInterval(
621: vformsList.size() - 1, vformsList.size() - 1);
622: }//GEN-LAST:event_btnNewActionPerformed
623:
624: // Variables declaration - do not modify//GEN-BEGIN:variables
625: private javax.swing.JButton btnDelete;
626: private javax.swing.JButton btnNew;
627: private javax.swing.JPanel jPanel1;
628: private javax.swing.JScrollPane jScrollPane1;
629: private javax.swing.JLabel tableLabel;
630: private javax.swing.JTable vformsTable;
631: // End of variables declaration//GEN-END:variables
632: }
|