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.faces.component.ActionSource;
050: import javax.faces.component.EditableValueHolder;
051: import javax.swing.Icon;
052: import javax.swing.JTable;
053: import javax.swing.JOptionPane;
054: import javax.swing.JTextField;
055: import javax.swing.ListSelectionModel;
056: import javax.swing.UIManager;
057: import javax.swing.table.AbstractTableModel;
058: import javax.swing.table.DefaultTableCellRenderer;
059: import javax.swing.table.TableColumn;
060: import com.sun.rave.web.ui.component.Form;
061: import org.netbeans.modules.visualweb.web.ui.dt.component.FormDesignInfo;
062: import javax.swing.DefaultCellEditor;
063: import javax.swing.DefaultComboBoxModel;
064: import javax.swing.JComboBox;
065: import com.sun.rave.designtime.DesignBean;
066: import com.sun.rave.designtime.DesignContext;
067: import com.sun.rave.designtime.DesignProperty;
068: import com.sun.rave.designtime.Result;
069: import com.sun.rave.designtime.ext.componentgroup.ComponentGroupHolder;
070: import com.sun.rave.designtime.ext.componentgroup.impl.ColorWrapperImpl;
071: import com.sun.rave.designtime.ext.componentgroup.util.ComponentGroupHelper;
072: import javax.swing.DefaultListCellRenderer;
073: import javax.swing.JList;
074: import java.util.Iterator;
075: import java.util.List;
076: import java.util.Map;
077: import javax.swing.table.TableCellEditor;
078: import javax.faces.component.UIComponent;
079: import javax.faces.component.NamingContainer;
080:
081: /**
082: * @author mbohm
083: * @author jhoff
084: */
085: public class EditVirtualFormsCustomizerPanel extends javax.swing.JPanel {
086:
087: public Result applyChanges() {
088: // store the virtual forms config
089: Form.VirtualFormDescriptor[] vforms = (Form.VirtualFormDescriptor[]) vformsList
090: .toArray(new Form.VirtualFormDescriptor[vformsList
091: .size()]);
092: String vfConfig = Form.generateVirtualFormsConfig(vforms);
093: DesignProperty vfcProp = formBean
094: .getProperty("virtualFormsConfig"); // NOI18N
095: vfcProp.setValue(vfConfig);
096:
097: // store off the form colors
098: DesignContext context = customizer.getDesignBean()
099: .getDesignContext();
100: for (int i = 0; vforms != null && i < vforms.length; i++) {
101: String vfName = vforms[i].getName();
102: String key = getColorKey(vfName);
103: Color c = (Color) colorMap.get(key);
104: if (c != null) {
105: context.setContextData(key, new ColorWrapperImpl(c));
106: }
107: }
108:
109: // reset the customizer modified state
110: customizer.setModified(false);
111: return null;
112: }
113:
114: protected EditVirtualFormsCustomizer customizer;
115: protected DesignBean[] beans;
116:
117: public EditVirtualFormsCustomizerPanel(
118: EditVirtualFormsCustomizer customizer) {
119: this .customizer = customizer;
120: this .beans = customizer.getDesignBeans();
121: this .canParticipate = initCanParticipate();
122: this .canSubmit = initCanSubmit();
123: initComponents();
124: readVFormInfo();
125: }
126:
127: /** Creates new form EditVirtualFormDialogBox */
128: public EditVirtualFormsCustomizerPanel() {
129: initComponents();
130: }
131:
132: /** This method is called from within the constructor to
133: * initialize the form.
134: * WARNING: Do NOT modify this code. The content of this method is
135: * always regenerated by the Form Editor.
136: */
137: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
138: private void initComponents() {
139: java.awt.GridBagConstraints gridBagConstraints;
140:
141: tableLabel = new javax.swing.JLabel();
142: jScrollPane1 = new javax.swing.JScrollPane();
143: vformsTable = new javax.swing.JTable();
144:
145: // Only one row selectable at a time
146: vformsTable
147: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
148:
149: // Create a default cell editor for String values that forces a stop
150: // editing event whenever focus is lost.
151: JTextField textField = new JTextField();
152: final TableCellEditor cellEditor = new TextFieldCellEditor(
153: vformsTable, textField);
154:
155: vformsTable.setDefaultEditor(String.class, cellEditor);
156:
157: // Single click to start editing cells with String
158: ((DefaultCellEditor) vformsTable.getDefaultEditor(String.class))
159: .setClickCountToStart(1);
160:
161: // Create a default cell renderer for String values that consistently renders
162: // background colors.
163: vformsTable.setDefaultRenderer(String.class,
164: new HomogonousCellRenderer());
165:
166: // Stop the editing when the table lost its focus
167: vformsTable.putClientProperty("terminateEditOnFocusLost",
168: Boolean.TRUE);
169:
170: btnNew = new javax.swing.JButton();
171: btnDelete = new javax.swing.JButton();
172:
173: setLayout(new java.awt.GridBagLayout());
174:
175: setPreferredSize(new java.awt.Dimension(550, 250));
176: getAccessibleContext().setAccessibleName(
177: "Edit Virtual Forms dialog box");
178: getAccessibleContext()
179: .setAccessibleDescription(
180: "Use this table to view and edit properties of the virutal forms defined on this page.");
181: tableLabel
182: .setDisplayedMnemonic(java.util.ResourceBundle
183: .getBundle(
184: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
185: .getString("labelMnemonic").charAt(0));
186: tableLabel.setLabelFor(vformsTable);
187: tableLabel
188: .setText(java.text.MessageFormat
189: .format(
190: java.util.ResourceBundle
191: .getBundle(
192: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
193: .getString("editLabel"),
194: new Object[] { this
195: .getSelectedComponentsDisplayText() }));
196: gridBagConstraints = new java.awt.GridBagConstraints();
197: gridBagConstraints.gridwidth = 2;
198: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
200: gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 10);
201: add(tableLabel, gridBagConstraints);
202: tableLabel
203: .getAccessibleContext()
204: .setAccessibleName(
205: java.util.ResourceBundle
206: .getBundle(
207: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
208: .getString("tableLabelAccessibleName"));
209: tableLabel
210: .getAccessibleContext()
211: .setAccessibleDescription(
212: java.util.ResourceBundle
213: .getBundle(
214: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
215: .getString(
216: "editTableLabelAccessibleDescription"));
217:
218: jScrollPane1.setBackground(java.awt.SystemColor.window);
219: jScrollPane1.setViewportView(vformsTable);
220: vformsTable
221: .getAccessibleContext()
222: .setAccessibleName(
223: java.util.ResourceBundle
224: .getBundle(
225: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
226: .getString("vformsTableAccessibleName"));
227: vformsTable
228: .getAccessibleContext()
229: .setAccessibleDescription(
230: java.util.ResourceBundle
231: .getBundle(
232: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
233: .getString(
234: "editVformsTableAccessibleDescription"));
235:
236: gridBagConstraints = new java.awt.GridBagConstraints();
237: gridBagConstraints.gridx = 0;
238: gridBagConstraints.gridy = 1;
239: gridBagConstraints.gridheight = 2;
240: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
241: gridBagConstraints.weightx = 1.0;
242: gridBagConstraints.weighty = 1.0;
243: gridBagConstraints.insets = new java.awt.Insets(0, 10, 5, 5);
244: add(jScrollPane1, gridBagConstraints);
245: jScrollPane1
246: .getAccessibleContext()
247: .setAccessibleName(
248: java.util.ResourceBundle
249: .getBundle(
250: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
251: .getString("vformsTableAccessibleName"));
252: jScrollPane1
253: .getAccessibleContext()
254: .setAccessibleDescription(
255: java.util.ResourceBundle
256: .getBundle(
257: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
258: .getString(
259: "editVformsTableAccessibleDescription"));
260:
261: btnNew
262: .setMnemonic(java.util.ResourceBundle
263: .getBundle(
264: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
265: .getString("newButtonMnemonic").charAt(0));
266: btnNew
267: .setText(java.util.ResourceBundle
268: .getBundle(
269: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
270: .getString("New"));
271: btnNew
272: .setToolTipText(java.util.ResourceBundle
273: .getBundle(
274: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
275: .getString("NewVf"));
276: btnNew.addActionListener(new java.awt.event.ActionListener() {
277: public void actionPerformed(java.awt.event.ActionEvent evt) {
278: btnNewActionPerformed(evt);
279: }
280: });
281:
282: gridBagConstraints = new java.awt.GridBagConstraints();
283: gridBagConstraints.gridx = 1;
284: gridBagConstraints.gridy = 1;
285: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
286: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
287: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 10);
288: add(btnNew, gridBagConstraints);
289: btnNew
290: .getAccessibleContext()
291: .setAccessibleName(
292: java.util.ResourceBundle
293: .getBundle(
294: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
295: .getString("btnNewAccessibleName"));
296: btnNew
297: .getAccessibleContext()
298: .setAccessibleDescription(
299: java.util.ResourceBundle
300: .getBundle(
301: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
302: .getString(
303: "btnNewAccessibleDescription"));
304:
305: btnDelete
306: .setMnemonic(java.util.ResourceBundle
307: .getBundle(
308: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
309: .getString("deleteButtonMnemonic").charAt(0));
310: btnDelete
311: .setText(java.util.ResourceBundle
312: .getBundle(
313: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
314: .getString("Delete"));
315: btnDelete
316: .setToolTipText(java.util.ResourceBundle
317: .getBundle(
318: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
319: .getString("DeleteVf"));
320: btnDelete
321: .addActionListener(new java.awt.event.ActionListener() {
322: public void actionPerformed(
323: java.awt.event.ActionEvent evt) {
324: btnDeleteActionPerformed(evt);
325: }
326: });
327:
328: gridBagConstraints = new java.awt.GridBagConstraints();
329: gridBagConstraints.gridx = 1;
330: gridBagConstraints.gridy = 2;
331: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
332: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
333: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
334: add(btnDelete, gridBagConstraints);
335: btnDelete
336: .getAccessibleContext()
337: .setAccessibleName(
338: java.util.ResourceBundle
339: .getBundle(
340: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
341: .getString("btnDeleteAccessibleName"));
342: btnDelete
343: .getAccessibleContext()
344: .setAccessibleDescription(
345: java.util.ResourceBundle
346: .getBundle(
347: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
348: .getString(
349: "btnDeleteAccessibleDescription"));
350:
351: }
352:
353: // </editor-fold>//GEN-END:initComponents
354:
355: private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
356: int vfidx = vformsTable.getSelectedRow();
357: if (vfidx > -1 && vfidx < vformsList.size()) {
358: Form.VirtualFormDescriptor vform = (Form.VirtualFormDescriptor) vformsList
359: .get(vfidx);
360: String title = ResourceBundle
361: .getBundle(
362: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
363: .getString("confirmDeleteTitle"); // NOI18N
364: String msg = java.text.MessageFormat
365: .format(
366: java.util.ResourceBundle
367: .getBundle(
368: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
369: .getString("confirmDeleteMessage"),
370: new Object[] { vform.getName() }); // NOI18N
371: if (JOptionPane.showConfirmDialog(this , msg, title,
372: JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
373: vformsList.remove(vfidx);
374: vformsTableModel.fireTableDataChanged();
375: if (vformsList.size() <= vfidx) {
376: vfidx--;
377: }
378: if (vfidx >= 0) {
379: vformsTable.getSelectionModel()
380: .setSelectionInterval(vfidx, vfidx);
381: }
382: }
383: }
384: }//GEN-LAST:event_btnDeleteActionPerformed
385:
386: private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
387: String name = VirtualFormsHelper
388: .getNewVirtualFormName(vformsList);
389:
390: Form.VirtualFormDescriptor vform = new Form.VirtualFormDescriptor(
391: name);
392: vformsList.add(vform);
393:
394: vformsTableModel.fireTableDataChanged();
395: vformsTable.getSelectionModel().setSelectionInterval(
396: vformsList.size() - 1, vformsList.size() - 1);
397: }//GEN-LAST:event_btnNewActionPerformed
398:
399: class FormsTableModel extends AbstractTableModel {
400: public FormsTableModel() {
401: }
402:
403: public int getRowCount() {
404: return vformsList.size();
405: }
406:
407: public int getColumnCount() {
408: return 4;
409: }
410:
411: public String getColumnName(int columnIndex) {
412: switch (columnIndex) {
413: case 0: // color
414: return ResourceBundle
415: .getBundle(
416: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
417: .getString("colorHeader"); // NOI18N
418: case 1: // virtual form name
419: return ResourceBundle
420: .getBundle(
421: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
422: .getString("nameHeader"); // NOI18N
423: case 2: // participates
424: return ResourceBundle
425: .getBundle(
426: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
427: .getString("participateHeader"); // NOI18N
428: case 3: // submits
429: return ResourceBundle
430: .getBundle(
431: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
432: .getString("submitHeader"); // NOI18N
433: }
434: return null;
435: }
436:
437: public Class getColumnClass(int columnIndex) {
438: switch (columnIndex) {
439: case 0: // color
440: return Color.class;
441: case 1: // virtual form name
442: return String.class;
443: case 2: // participates: boolean
444: return String.class;
445: case 3: // submits: boolean
446: return String.class;
447: }
448: return null;
449: }
450:
451: public boolean isCellEditable(int rowIndex, int columnIndex) {
452: Form.VirtualFormDescriptor vform = (Form.VirtualFormDescriptor) vformsList
453: .get(rowIndex);
454: if (vform != null) {
455: switch (columnIndex) {
456: case 0: // virtual form color
457: return true;
458: case 1: // virtual form name
459: return true;
460: case 2: // participates
461: return EditVirtualFormsCustomizerPanel.this .canParticipate;
462: case 3: // submits
463: return EditVirtualFormsCustomizerPanel.this .canSubmit;
464: }
465: }
466: return false;
467: }
468:
469: public Object getValueAt(int rowIndex, int columnIndex) {
470: Form.VirtualFormDescriptor vform = (Form.VirtualFormDescriptor) vformsList
471: .get(rowIndex);
472: if (vform != null) {
473: switch (columnIndex) {
474: case 0: // virtual form color
475: String vfName = vform.getName();
476: String key = getColorKey(vfName);
477: return ComponentGroupHelper.getMappedColor(key,
478: colorMap);
479: case 1: // virtual form name
480: return vform.getName();
481: case 2: // participates
482: return getParticipates(vform);
483: case 3: // submits
484: return getSubmits(vform);
485:
486: }
487: }
488: return null;
489: }
490:
491: public void setValueAt(Object aValue, int rowIndex,
492: int columnIndex) {
493: Form.VirtualFormDescriptor vform = (Form.VirtualFormDescriptor) vformsList
494: .get(rowIndex);
495: if (vform != null) {
496: String vfName, colorKey;
497: switch (columnIndex) {
498: case 0: //color
499: vfName = vform.getName();
500: colorKey = getColorKey(vfName);
501: colorMap.put(colorKey, aValue);
502: customizer.setModified(true);
503: return;
504: case 1: // virtual form name/color
505: String name = aValue.toString();
506: name = name.trim();
507: name = name.replaceAll("\\|", "_"); // NOI18N
508: name = name.replaceAll(",", "_"); // NOI18N
509: if (name.length() < 1) {
510: name = VirtualFormsHelper
511: .getNewVirtualFormName(vformsList);
512: }
513: vfName = vform.getName();
514: colorKey = getColorKey(vfName);
515: Color c = (Color) colorMap.get(colorKey);
516: colorMap.remove(colorKey);
517: vform.setName(name);
518: colorKey = getColorKey(name);
519: colorMap.put(colorKey, c);
520: customizer.setModified(true);
521: return;
522: case 2: // participates
523: String pvalue = (String) aValue;
524: boolean participates = YES.equals(pvalue);
525: setParticipates(vform, participates);
526: return;
527: case 3: // submits
528: String svalue = (String) aValue;
529: boolean submits = YES.equals(svalue);
530: setSubmits(vform, submits);
531: if (submits) {
532: for (Iterator iter = vformsList.iterator(); iter
533: .hasNext();) {
534: Form.VirtualFormDescriptor vf = (Form.VirtualFormDescriptor) iter
535: .next();
536: if (!vf.getName().equals(vform.getName())) { //vf is different from the virtual form which the components now submit
537: setSubmits(vf, false);
538: }
539: }
540: fireTableDataChanged();
541: }
542: return;
543: }
544: }
545: }
546: }
547:
548: private boolean initCanParticipate() {
549: for (int i = 0; i < beans.length; i++) {
550: if (beans[i].getInstance() instanceof EditableValueHolder) {
551: return true;
552: }
553: }
554: return false;
555: }
556:
557: private String getParticipates(Form.VirtualFormDescriptor vform) {
558: boolean atLeastOneParticipates = false;
559: boolean atLeastOneDoesNot = false;
560: for (int i = 0; i < beans.length; i++) {
561: Object beanInstance = beans[i].getInstance();
562: boolean beanCouldParticipate = beanInstance instanceof EditableValueHolder;
563: if (beanCouldParticipate) {
564: String beanFqId = FormDesignInfo
565: .getFullyQualifiedId(beans[i]);
566: if (beanFqId == null) {
567: continue;
568: }
569: if (vform.hasParticipant(beanFqId)) {
570: atLeastOneParticipates = true;
571: if (atLeastOneDoesNot) {
572: return SOME_PARTICIPATE;
573: }
574: } else {
575: atLeastOneDoesNot = true;
576: if (atLeastOneParticipates) {
577: return SOME_PARTICIPATE;
578: }
579: }
580: }
581: }
582: if (atLeastOneParticipates) {
583: return YES;
584: } else {
585: return NO;
586: }
587: }
588:
589: private void setParticipates(Form.VirtualFormDescriptor vform,
590: boolean participate) {
591: String[] pids = vform.getParticipatingIds();
592: ArrayList pList = new ArrayList();
593: for (int i = 0; pids != null && i < pids.length; i++) {
594: pList.add(pids[i]);
595: }
596: for (int i = 0; i < beans.length; i++) {
597: Object beanInstance = beans[i].getInstance();
598: if (!(beanInstance instanceof UIComponent)) {
599: continue;
600: }
601: String fqId = FormDesignInfo.getFullyQualifiedId(beans[i]);
602: if (fqId == null) {
603: continue;
604: }
605: List relevantPids = new ArrayList();
606: for (Iterator iter = pList.iterator(); iter.hasNext();) {
607: String pid = (String) iter.next();
608: if (Form.fullyQualifiedIdMatchesPattern(fqId, pid)) {
609: relevantPids.add(pid);
610: }
611: }
612: boolean contains = relevantPids.size() > 0;
613: if (participate && !contains
614: && beanInstance instanceof EditableValueHolder) {
615: String idToAdd = fqId;
616: if (idToAdd.startsWith(String
617: .valueOf(NamingContainer.SEPARATOR_CHAR))
618: && idToAdd.length() > 1) {
619: idToAdd = idToAdd.substring(1, idToAdd.length());
620: }
621: pList.add(idToAdd);
622: customizer.setModified(true);
623: } else if (!participate && contains) {
624: for (Iterator iter = relevantPids.iterator(); iter
625: .hasNext();) {
626: Object relevantPid = iter.next();
627: pList.remove(relevantPid);
628: customizer.setModified(true);
629: }
630: }
631: }
632: pids = (String[]) pList.toArray(new String[pList.size()]);
633: vform.setParticipatingIds(pids);
634: }
635:
636: private boolean initCanSubmit() {
637: for (int i = 0; i < beans.length; i++) {
638: if (beans[i].getInstance() instanceof ActionSource
639: || beans[i].getInstance() instanceof EditableValueHolder) {
640: return true;
641: }
642: }
643: return false;
644: }
645:
646: private String getSubmits(Form.VirtualFormDescriptor vform) {
647: boolean atLeastOneSubmits = false;
648: boolean atLeastOneDoesNot = false;
649: for (int i = 0; i < beans.length; i++) {
650: String beanFqId = FormDesignInfo
651: .getFullyQualifiedId(beans[i]);
652: if (beanFqId == null) {
653: continue;
654: }
655: if (vform.isSubmittedBy(beanFqId)) {
656: atLeastOneSubmits = true;
657: if (atLeastOneDoesNot) {
658: return SOME_SUBMIT;
659: }
660: } else {
661: atLeastOneDoesNot = true;
662: if (atLeastOneSubmits) {
663: return SOME_SUBMIT;
664: }
665: }
666: }
667: if (atLeastOneSubmits) {
668: return YES;
669: } else {
670: return NO;
671: }
672: }
673:
674: private void setSubmits(Form.VirtualFormDescriptor vform,
675: boolean submit) {
676: String[] sids = vform.getSubmittingIds();
677: ArrayList sList = new ArrayList();
678: for (int i = 0; sids != null && i < sids.length; i++) {
679: sList.add(sids[i]);
680: }
681: for (int i = 0; i < beans.length; i++) {
682: Object beanInstance = beans[i].getInstance();
683: if (!(beanInstance instanceof UIComponent)) {
684: continue;
685: }
686: String fqId = FormDesignInfo.getFullyQualifiedId(beans[i]);
687: if (fqId == null) {
688: continue;
689: }
690: List relevantSids = new ArrayList();
691: for (Iterator iter = sList.iterator(); iter.hasNext();) {
692: String sid = (String) iter.next();
693: if (Form.fullyQualifiedIdMatchesPattern(fqId, sid)) {
694: relevantSids.add(sid);
695: }
696: }
697: boolean contains = relevantSids.size() > 0;
698: if (submit
699: && !contains
700: && (beanInstance instanceof ActionSource || beanInstance instanceof EditableValueHolder)) {
701: String idToAdd = fqId;
702: if (idToAdd.startsWith(String
703: .valueOf(NamingContainer.SEPARATOR_CHAR))
704: && idToAdd.length() > 1) {
705: idToAdd = idToAdd.substring(1, idToAdd.length());
706: }
707: sList.add(idToAdd);
708: customizer.setModified(true);
709: } else if (!submit && contains) {
710: for (Iterator iter = relevantSids.iterator(); iter
711: .hasNext();) {
712: Object relevantSid = iter.next();
713: sList.remove(relevantSid);
714: customizer.setModified(true);
715: }
716: }
717: }
718: sids = (String[]) sList.toArray(new String[sList.size()]);
719: vform.setSubmittingIds(sids);
720: }
721:
722: private DesignBean formBean;
723: private List inputBeans = new ArrayList();
724: private List actionBeans = new ArrayList();
725: private List vformsList = new ArrayList();
726: private Map colorMap = new HashMap();
727: private FormsTableModel vformsTableModel = new FormsTableModel();
728: private boolean canParticipate = true;
729: private boolean canSubmit = true;
730: private static final String YES = ResourceBundle
731: .getBundle(
732: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
733: .getString("yes"); // NOI18N
734: private static final String NO = ResourceBundle
735: .getBundle(
736: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
737: .getString("no"); // NOI18N
738: private static final String SOME_PARTICIPATE = ResourceBundle
739: .getBundle(
740: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
741: .getString("someParticipate"); // NOI18N
742: private static final String SOME_SUBMIT = ResourceBundle
743: .getBundle(
744: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
745: .getString("someSubmit"); // NOI18N
746: private static final String[] NO_ONLY = new String[] { NO };
747: private static final String[] YES_NO = new String[] { YES, NO };
748: private static final String[] YES_NO_SOME_PARTICIPATE = new String[] {
749: YES, NO, SOME_PARTICIPATE };
750: private static final String[] YES_NO_SOME_SUBMIT = new String[] {
751: YES, NO, SOME_SUBMIT };
752:
753: private void readVFormInfo() {
754: formBean = VirtualFormsHelper.findFormBean(beans);
755: DesignContext dcontext = formBean.getDesignContext();
756: ComponentGroupHolder[] holders = ComponentGroupHelper
757: .getComponentGroupHolders(dcontext);
758: ComponentGroupHelper.populateColorMap(dcontext, holders,
759: colorMap);
760: Form form = (Form) formBean.getInstance();
761: Form.VirtualFormDescriptor[] vforms = form.getVirtualForms();
762: for (int i = 0; vforms != null && i < vforms.length; i++) {
763: String vformName = vforms[i].getName();
764: Form.VirtualFormDescriptor vformCopy = new Form.VirtualFormDescriptor(
765: vformName);
766: //it so happens we never modify the individual members of the participating or submitting arrays
767: //i.e., in setParticipates and setSubmits we use vform.setParticipatingIds(pids) and vform.setSubmittingIds(sids)
768: //so a deep copy of the individual members of these arrays is not necessary
769: vformCopy.setParticipatingIds(vforms[i]
770: .getParticipatingIds());
771: vformCopy.setSubmittingIds(vforms[i].getSubmittingIds());
772: vformsList.add(vformCopy);
773: }
774:
775: vformsTable.setModel(vformsTableModel);
776: vformsTable
777: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
778:
779: TableColumn colorCol = vformsTable.getColumnModel()
780: .getColumn(0);
781: colorCol.setCellRenderer(new ColorCellRenderer());
782: colorCol.setCellEditor(new DefaultCellEditor(
783: new ColorComboBox()));
784: // Takes two click to have the list popup
785: ((DefaultCellEditor) colorCol.getCellEditor())
786: .setClickCountToStart(2);
787:
788: TableColumn psCol = vformsTable.getColumnModel().getColumn(2);
789: psCol
790: .setCellEditor(new DefaultCellEditor(new PSComboBox(
791: true)));
792: // Takes two click to have the list popup
793: ((DefaultCellEditor) psCol.getCellEditor())
794: .setClickCountToStart(2);
795:
796: psCol = vformsTable.getColumnModel().getColumn(3);
797: psCol
798: .setCellEditor(new DefaultCellEditor(new PSComboBox(
799: false)));
800: // Takes two click to have the list popup
801: ((DefaultCellEditor) psCol.getCellEditor())
802: .setClickCountToStart(2);
803:
804: // Have the first row selected by default
805: if (vformsTableModel.getRowCount() > 0)
806: vformsTable.changeSelection(0, 0, false, false);
807: }
808:
809: private String[] getOptions(boolean participate) {
810: if (participate) {
811: boolean atLeastOneEvh = false;
812: for (int i = 0; i < beans.length; i++) {
813: Object instance = beans[i].getInstance();
814: if (instance instanceof EditableValueHolder) {
815: atLeastOneEvh = true;
816: break;
817: }
818: }
819: if (!atLeastOneEvh) {
820: return NO_ONLY;
821: }
822: }
823: return YES_NO;
824: }
825:
826: private String getSelectedComponentsDisplayText() {
827: StringBuffer sb = new StringBuffer();
828: for (int i = 0; this .beans != null && i < this .beans.length; i++) {
829: sb.append(beans[i].getInstanceName());
830: sb.append("<br>"); //NOI18N;
831: }
832: return sb.toString();
833: }
834:
835: private String getColorKey(String vfName) {
836: String formBeanId = formBean.getInstanceName();
837: String formIdDotVfName = formBeanId + "." + vfName;
838: return ComponentGroupHelper.getComponentGroupColorKey(
839: FormDesignInfo.VIRTUAL_FORM_HOLDER_NAME,
840: formIdDotVfName);
841: }
842:
843: class ColorCellRenderer extends DefaultTableCellRenderer {
844:
845: Color SELECTION_BACKGROUND = UIManager.getDefaults().getColor(
846: "TextField.selectionBackground");
847:
848: Color SELECTION_FOREGROUND = UIManager.getDefaults().getColor(
849: "TextField.selectionForeground");
850:
851: Color BACKGROUND = UIManager.getDefaults().getColor(
852: "TextField.background");
853:
854: Color FOREGROUND = UIManager.getDefaults().getColor(
855: "TextField.foreground");
856:
857: public Component getTableCellRendererComponent(JTable table,
858: Object value, boolean isSelected, boolean hasFocus,
859: int row, int column) {
860:
861: super .getTableCellRendererComponent(table, value,
862: isSelected, hasFocus, row, column);
863:
864: Component c = super .getTableCellRendererComponent(table,
865: value, isSelected, hasFocus, row, column);
866:
867: if (isSelected) {
868: c.setBackground(SELECTION_BACKGROUND);
869: c.setForeground(SELECTION_FOREGROUND);
870: } else {
871: c.setBackground(BACKGROUND);
872: c.setForeground(FOREGROUND);
873: }
874:
875: Color color = (Color) value;
876: setIcon(new ColorIcon(color));
877: setText(null);
878:
879: return this ;
880: }
881: }
882:
883: class ColorIcon implements Icon {
884: private Color color;
885:
886: public ColorIcon(Color color) {
887: this .color = color;
888: }
889:
890: public void paintIcon(Component c, Graphics g, int x, int y) {
891: g.setColor(color);
892: g.fillRect(x, y, 8, 8);
893: g.setColor(Color.black);
894: g.drawRect(x, y, 8, 8);
895: }
896:
897: public int getIconWidth() {
898: return 8;
899: }
900:
901: public int getIconHeight() {
902: return 8;
903: }
904: }
905:
906: class ColorComboBox extends JComboBox {
907: public ColorComboBox() {
908: super ();
909: DefaultComboBoxModel cbm = new DefaultComboBoxModel();
910: for (int i = 0; i < ComponentGroupHelper.DEFAULT_COLOR_SET.length; i++) {
911: Color c = ComponentGroupHelper.DEFAULT_COLOR_SET[i];
912: cbm.addElement(c);
913: }
914: setModel(cbm);
915: setRenderer(new ColorListRenderer());
916: getAccessibleContext()
917: .setAccessibleName(
918: ResourceBundle
919: .getBundle(
920: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
921: .getString(
922: "colorComboAccessibleName")); // NOI18N
923: getAccessibleContext()
924: .setAccessibleDescription(
925: ResourceBundle
926: .getBundle(
927: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
928: .getString(
929: "colorComboAccessibleDescription")); // NOI18N
930: }
931: }
932:
933: class PSComboBox extends JComboBox {
934: public PSComboBox(boolean participates) {
935: super ();
936: setOptions(getOptions(participates));
937: }
938:
939: private void setOptions(String[] options) {
940: DefaultComboBoxModel cbm = new DefaultComboBoxModel();
941: for (int i = 0; i < options.length; i++) {
942: cbm.addElement(options[i]);
943: }
944: setModel(cbm);
945: }
946: }
947:
948: class ColorListRenderer extends DefaultListCellRenderer {
949: public Component getListCellRendererComponent(JList list,
950: Object value, int index, boolean isSelected,
951: boolean hasFocus) {
952:
953: super .getListCellRendererComponent(list, value, index,
954: isSelected, hasFocus);
955:
956: Color c = (Color) value;
957: setIcon(new ColorIcon(c));
958: setText(null);
959:
960: return this ;
961: }
962: }
963:
964: // Variables declaration - do not modify//GEN-BEGIN:variables
965: private javax.swing.JButton btnDelete;
966: private javax.swing.JButton btnNew;
967: private javax.swing.JScrollPane jScrollPane1;
968: private javax.swing.JLabel tableLabel;
969: private javax.swing.JTable vformsTable;
970: // End of variables declaration//GEN-END:variables
971:
972: }
|