Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / meta / internal / CellEditorFactoryRegistry.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.meta.internal;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.HashMap;\r
16 import java.util.List;\r
17 import java.util.Map;\r
18 \r
19 import org.eclipse.jface.viewers.CellEditor;\r
20 import org.eclipse.jst.jsf.common.ui.internal.dialogfield.DialogField;\r
21 import org.eclipse.jst.jsf.common.ui.internal.dialogfield.ISupportTextValue;\r
22 import org.eclipse.jst.jsf.common.ui.internal.dialogfield.StringDialogField;\r
23 import org.eclipse.jst.pagedesigner.editors.properties.IPropertyPageDescriptor;\r
24 import org.eclipse.jst.pagedesigner.meta.ITagAttributeCellEditorFactory;\r
25 import org.eclipse.jst.pagedesigner.properties.celleditors.CellEditorFactory;\r
26 import org.eclipse.swt.widgets.Composite;\r
27 import org.w3c.dom.Element;\r
28 \r
29 /**\r
30  * CellEditorFactoryRegistry also read information from plugin.xml extension to\r
31  * allow other plugins to contribute new kinds of cell editors.\r
32  * \r
33  */\r
34 public class CellEditorFactoryRegistry {\r
35 //      private static final Logger _log = PDPlugin\r
36 //                      .getLogger(CellEditorFactoryRegistry.class);\r
37 \r
38         private static CellEditorFactoryRegistry _instance;\r
39 \r
40         private Map _factoryMap = new HashMap();\r
41 \r
42         private List _defaultFactories = new ArrayList();\r
43 \r
44         /**\r
45          * @return singleton CellEditorFactoryRegistry\r
46          */\r
47         public static CellEditorFactoryRegistry getInstance() {\r
48                 if (_instance == null) {\r
49                         _instance = new CellEditorFactoryRegistry();\r
50                 }\r
51                 return _instance;\r
52         }\r
53 \r
54         private CellEditorFactoryRegistry() {\r
55                 ITagAttributeCellEditorFactory[] facs = CellEditorFacRegistryReader\r
56                                 .getAllFactories();\r
57                 if (facs != null) {\r
58                         for (int i = 0; i < facs.length; i++) {\r
59                                 addCellEditorFactory(facs[i]);\r
60                         }\r
61                 }\r
62                 addCellEditorFactory(new CellEditorFactory());\r
63         }\r
64 \r
65         /**\r
66          * @param fac\r
67          */\r
68         public void addCellEditorFactory(ITagAttributeCellEditorFactory fac) {\r
69                 String[] types = fac.getSupportedValueTypes();\r
70                 if (types == null || types.length == 0) {\r
71                         _defaultFactories.add(fac);\r
72                 } else {\r
73                         for (int i = 0; i < types.length; i++) {\r
74                                 _factoryMap.put(types[i], fac);\r
75                         }\r
76                 }\r
77         }\r
78 \r
79         /**\r
80          * Return cell editor for attribute based upon runtime value type\r
81          * @param parent\r
82          * @param attr\r
83          * @param element\r
84          * @return CellEditor\r
85          */\r
86         public CellEditor createCellEditor(Composite parent,\r
87                         IPropertyPageDescriptor attr, Element element) {\r
88                 String type = attr.getValueType();\r
89                 if (type == null || type.length() == 0)\r
90                         return null;\r
91 \r
92                 CellEditor result = null;\r
93                 ITagAttributeCellEditorFactory fac = (ITagAttributeCellEditorFactory) _factoryMap\r
94                                 .get(type);\r
95                 \r
96                 if (fac != null) {\r
97                         result = fac.createCellEditor(parent, attr, element);\r
98                 }\r
99                 if (result == null) {\r
100                         for (int i = 0, size = _defaultFactories.size(); i < size; i++) {\r
101                                 result = ((ITagAttributeCellEditorFactory) _defaultFactories\r
102                                                 .get(i)).createCellEditor(parent, attr, element);\r
103                                 if (result != null)\r
104                                         break;\r
105                         }\r
106                 }\r
107                 return result;\r
108         }\r
109 \r
110         /**\r
111          * @param attr\r
112          * @return DialogField\r
113          */\r
114         public DialogField createDialogField(IPropertyPageDescriptor attr) {\r
115                 String type = attr.getValueType();\r
116                 if (type == null || type.length() == 0) {\r
117                         DialogField result = createTextDialogField(attr);\r
118                         result.setLabelText(attr.getLabel()); //labelProvider???\r
119                         return result;\r
120                 }\r
121 //              type = type.toUpperCase();\r
122 \r
123                 DialogField result = null;\r
124                 ITagAttributeCellEditorFactory fac = (ITagAttributeCellEditorFactory) _factoryMap\r
125                                 .get(type);\r
126                 if (fac != null) {\r
127                         result = fac.createDialogField(attr);\r
128                 }\r
129                 if (result == null) {\r
130                         for (int i = 0, size = _defaultFactories.size(); i < size; i++) {\r
131                                 result = ((ITagAttributeCellEditorFactory) _defaultFactories\r
132                                                 .get(i)).createDialogField(attr);\r
133                                 if (result != null) {\r
134                                         break;\r
135                                 }\r
136                         }\r
137                 }\r
138                 if (result == null) {\r
139                         result = createTextDialogField(attr);\r
140                 }\r
141                 if (!(result instanceof ISupportTextValue)) {\r
142                         result = createTextDialogField(attr);\r
143                 }\r
144 //              result.setLabelText(attr.getLabel() + ":"); //$NON-NLS-1$\r
145                 return result;\r
146         }\r
147 \r
148         /**\r
149          * @param attr\r
150          * @return DialogField\r
151          */\r
152         public DialogField createTextDialogField(IPropertyPageDescriptor attr) {\r
153                 StringDialogField field = new StringDialogField();\r
154                 field.setLabelText(attr.getLabel());\r
155                 field.setRequired(attr.isRequired());\r
156                 field.setToolTip(attr.getDescription());\r
157                 return field;\r
158         }\r
159 \r
160 }\r