KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > properties > PropertyElementsEditor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * PropertyElementsEditor.java
26  *
27  * Created on March 13, 2002, 4:22 PM
28  */

29
30 package com.sun.enterprise.tools.common.properties;
31
32 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
33
34 import java.awt.Component JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.awt.Rectangle JavaDoc;
37
38 import javax.swing.JTable JavaDoc;
39 import javax.swing.table.TableCellEditor JavaDoc;
40 import javax.swing.JScrollPane JavaDoc;
41 import javax.swing.JOptionPane JavaDoc;
42 import java.beans.*;
43 import java.util.*;
44 import java.util.ResourceBundle JavaDoc;
45
46 //import com.sun.enterprise.tools.common.ui.GenericTable;
47

48 /**
49  *
50  * @author vkraemer
51  * @version
52  */

53 public class PropertyElementsEditor extends PropertyEditorSupport {
54     
55     private PropertyElements propElements;
56     private static ResourceBundle JavaDoc bundle =
57         ResourceBundle.getBundle("com.sun.enterprise.tools.common.properties.Bundle"); // NOI18N
58
private JTable JavaDoc table = null;
59
60     /** Creates new PropertyElementsEditor */
61     public PropertyElementsEditor() {
62     }
63
64     /** The String is not editable in the sheet.
65      */

66     public String JavaDoc getAsText() {
67         return null;
68     }
69     
70     /** Does this editor use painting to illuminate the property state in the
71      * property sheet
72      */

73     public boolean isPaintable() {
74         return true;
75     }
76     
77     /** return the UI components to edit this object */
78     public Component JavaDoc getCustomEditor() {
79         //System.out.println("PropElEd.getCustomEditor(" + propElements.toString() +") " + //NOI18N
80
//propElements.hashCode());
81
table = new JTable JavaDoc(new PropertyElementsTableModel(propElements));
82         table.setDefaultEditor(Object JavaDoc.class, acell);
83         if (table.getModel().getRowCount() > 0)
84             table.editCellAt(0,0);
85         //JTable tab = new JTable(propElements);
86
JScrollPane JavaDoc sp = new JScrollPane JavaDoc(table);
87         return sp;
88     }
89     
90     public Object JavaDoc getValue() {
91 // Reporter.info(propElements.toString() + ", " + propElements.hashCode()); //NOI18N
92
return new PropertyElements((PropertyElements) propElements);//*/ propElements;
93
}
94     
95     public void paintValue(Graphics JavaDoc gfx, Rectangle JavaDoc box) {
96         //System.out.println("PropElEd.paintValue(" + propElements.toString() +") " + //NOI18N
97
//propElements.hashCode());
98
int num = propElements.getLength();
99         String JavaDoc s = "" + num + " "; // NOI18N
100

101         String JavaDoc format = null;
102         if(num == 1)
103             format = bundle.getString("PROP_TEXT_ONE_PROPERTY");//NOI18N
104
else
105             format = bundle.getString("PROP_TEXT_N_PROPERTIES");//NOI18N
106

107         s = java.text.MessageFormat.format(format, new Object JavaDoc[] { s });
108         
109         java.awt.FontMetrics JavaDoc fm = gfx.getFontMetrics();
110         
111         gfx.drawString(s, 4, (box.height - fm.getHeight()) / 2 + 1 + fm.getMaxAscent());
112     }
113     
114     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
115         throw new IllegalArgumentException JavaDoc(bundle.getString("ERR_SET_AS_TEXT_ILLEGAL"));//NOI18N
116
}
117     
118     public List isValueValid(PropertyElements value) {
119         String JavaDoc propName = null;
120         String JavaDoc propValue = null;
121         Vector errors = new Vector();
122         HashSet propNames = new HashSet();
123         try {
124             for (int row = 0; row < value.getLength(); row++) {
125                 if ((propName = (String JavaDoc)value.getAttributeDetail(row, 0)) == null || propName.trim().length() == 0 || //NOI18N
126
(propValue = (String JavaDoc)value.getAttributeDetail(row, 1)) == null || propValue.trim().length() == 0) { //NOI18N
127
String JavaDoc format = bundle.getString("ERR_InvalidEntry"); //NOI18N
128
errors.add(java.text.MessageFormat.format(format, new Object JavaDoc[] { new Integer JavaDoc(row + 1) }));
129                 }
130                 else if (propNames.contains(propName)) {
131                     String JavaDoc format = bundle.getString("ERR_DuplicateProperty"); //NOI18N
132
errors.add(java.text.MessageFormat.format(format, new Object JavaDoc[] { propName, new Integer JavaDoc(row + 1) }));
133                 }
134                 else
135                     propNames.add(propName);
136             }
137         } catch (Exception JavaDoc ex) {
138             errors.add(ex.getLocalizedMessage());
139         }
140         Reporter.info(new Integer JavaDoc(errors.size())); //NOI18N
141
return errors;
142     }
143         
144     public void setValue(Object JavaDoc value) {
145 // Reporter.info(value.toString() + ", " + value.hashCode()); //NOI18N
146
List errors = null;
147         if (table != null) {
148             Reporter.info("table"); //NOI18N
149
TableCellEditor JavaDoc cell = table.getCellEditor();
150             if (null != cell) {
151                 Reporter.info("There is a cell: " + cell);//NOI18N
152
Reporter.info(" this is the value of the cell: " + cell.getCellEditorValue());//NOI18N
153
int r = table.getEditingRow();
154                 int c = table.getEditingColumn();
155                 if (r > -1 && c > -1) {
156                     table.setValueAt(cell.getCellEditorValue(), r, c);
157                 }
158             }
159             if (value instanceof PropertyElements) {
160                 errors = isValueValid((PropertyElements) value);
161                 if (!errors.isEmpty()) {
162                     StringBuffer JavaDoc str = new StringBuffer JavaDoc(bundle.getString("ERR_Properties")); //NOI18N
163
Iterator iter = errors.iterator();
164                     while (iter.hasNext()) {
165                         str.append("\n\t -" + (String JavaDoc)iter.next()); // NOI18N
166
}
167                     JOptionPane.showMessageDialog(null, str.toString(), bundle.getString ("Error"), JOptionPane.ERROR_MESSAGE);//NOI18N
168
}
169             }
170         }
171         if (value instanceof PropertyElements) {
172             if (errors == null || errors.isEmpty()) {
173                 Reporter.info(""); //NOI18N
174
propElements = (PropertyElements) value; //*/ new PropertyElements((PropertyElements) value);
175
}
176         }
177         //firePropertyChange();
178
}
179     
180     public boolean supportsCustomEditor() {
181         return true;
182     }
183     
184     ////
185

186     TableCellEditor JavaDoc acell = new javax.swing.DefaultCellEditor JavaDoc(new javax.swing.JTextField JavaDoc());
187     
188 }
189
Popular Tags