KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > FieldComponentFactory


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import java.text.DecimalFormat JavaDoc;
23 import java.text.Format JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25
26 import javax.swing.ComboBoxModel JavaDoc;
27 import javax.swing.DefaultComboBoxModel JavaDoc;
28 import javax.swing.JCheckBox JavaDoc;
29 import javax.swing.JComboBox JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JFormattedTextField JavaDoc;
32 import javax.swing.JTextField JavaDoc;
33 import javax.swing.ListCellRenderer JavaDoc;
34
35 public class FieldComponentFactory {
36
37   public FieldComponentFactory() {
38   }
39
40   public JComponent JavaDoc createFieldEditComponent(ObjEntityViewField field) {
41     CellRenderers cellRenderers = new CellRenderers();
42     JComponent JavaDoc editor = null;
43     Format JavaDoc format = field.getEditFormat();
44     int dataType = field.getDataType().getValue();
45     boolean lookup = field.isLookup();
46     int alignment;
47
48     switch (dataType) {
49       case DataTypeEnum.INTEGER_TYPE_VALUE:
50       case DataTypeEnum.DOUBLE_TYPE_VALUE:
51       case DataTypeEnum.MONEY_TYPE_VALUE:
52       case DataTypeEnum.PERCENT_TYPE_VALUE:
53         alignment = JTextField.RIGHT;
54         break;
55       default:
56         alignment = JTextField.LEFT;
57         break;
58     }
59
60     if (lookup) {
61       ComboBoxModel JavaDoc comboData =
62           new DefaultComboBoxModel JavaDoc(field.getLookupValues());
63       ListCellRenderer JavaDoc comboRenderer =
64           cellRenderers.createListCellRenderer(field);
65       JComboBox JavaDoc comboBox = new JComboBox JavaDoc(comboData);
66       comboBox.setRenderer(comboRenderer);
67       editor = comboBox;
68     } else if (format != null) {
69       if (format instanceof MapFormat) {
70         MapFormat mapFormat = (MapFormat)format;
71         ComboBoxModel JavaDoc comboData =
72           new DefaultComboBoxModel JavaDoc((mapFormat).getValues());
73         ListCellRenderer JavaDoc comboRenderer =
74           cellRenderers.createFormatListCellRenderer(
75           mapFormat, mapFormat.getNullFormat(), null, -1);
76         JComboBox JavaDoc comboBox = new JComboBox JavaDoc(comboData);
77         comboBox.setRenderer(comboRenderer);
78         editor = comboBox;
79       } else {
80         JFormattedTextField JavaDoc textField = new JFormattedTextField JavaDoc(format);
81         if (alignment >= 0)
82           textField.setHorizontalAlignment(alignment);
83         if (format instanceof DecimalFormat JavaDoc)
84           textField.setToolTipText(((DecimalFormat JavaDoc)format).toPattern());
85         else if (format instanceof SimpleDateFormat JavaDoc)
86           textField.setToolTipText(((SimpleDateFormat JavaDoc)format).toPattern());
87         editor = textField;
88       }
89     } else {
90       if (dataType == DataTypeEnum.BOOLEAN_TYPE_VALUE) {
91         JCheckBox JavaDoc checkBox = new JCheckBox JavaDoc();
92         editor = checkBox;
93       } else {
94         JTextField JavaDoc textField = new JTextField JavaDoc();
95         if (alignment >= 0)
96           textField.setHorizontalAlignment(alignment);
97         editor = textField;
98       }
99     }
100
101     return editor;
102   }
103 }
104
Popular Tags