Source Code Cross Referenced for EnumConverter.java in  » Swing-Library » jide-common » com » jidesoft » converter » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » jide common » com.jidesoft.converter 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)IntegerEnumConverter.java 4/1/2005
003:         *
004:         * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005:         */
006:        package com.jidesoft.converter;
007:
008:        /**
009:         * A typical way to define a constant is to use int as the value type. For example, in SwingConstants, the following values
010:         * are defined.
011:         * <code><pre>
012:         * public static final int CENTER  = 0;
013:         * public static final int TOP     = 1;
014:         * public static final int LEFT    = 2;
015:         * public static final int BOTTOM  = 3;
016:         * public static final int RIGHT   = 4;
017:         * </pre></code>
018:         * Before JDK1.5, there is no enum type, so this is one way to define enumeration.
019:         * When you use it, you just need to define a int field say _locaton and the
020:         * valid value for _location is one of the values above. If you want to display
021:         * it in UI and allow user to specify the value of _location, problem comes.
022:         * You don't want to use 0, 1, 2, 3, 4 as the value doesn't mean anything from user point of view.
023:         * You want user to be able to use meanful names such as "Center", "Top", "Left", "Bottom", "Right".
024:         * Obviously you need a converter here to convert from integer in an enum to string, such as converting from 0 to "Center" and vice versa.
025:         * That's what <tt>EnumConverter</tt> for.
026:         * <p/>
027:         * Combining with EnumCellConverter, EnumCellEditor,
028:         * you can easily use combobox to choose value for _location like the example above using
029:         * meanful strings.
030:         */
031:        public class EnumConverter implements  ObjectConverter {
032:            private String _name;
033:            private Object _default;
034:            private Class<?> _type;
035:            private Object[] _objects;
036:            private String[] _strings;
037:
038:            public EnumConverter(String name, Object[] values, String[] strings) {
039:                this (name, values[0].getClass(), values, strings);
040:            }
041:
042:            public EnumConverter(String name, Class<?> type, Object[] values,
043:                    String[] strings) {
044:                this (name, type, values, strings, null);
045:            }
046:
047:            /**
048:             * Creates an EnumConverter.
049:             *
050:             * @param name         the name of the converter. The name is used to create ConverterContext and later on the EditorContext.
051:             * @param type         the type of the element in <code>objects</code> array.
052:             * @param objects      the <code>objects</code> array. All elements in the <code>objects</code> array should have the same type.
053:             * @param strings      the <code>strings</code> array. It contains the meanful names for the elements in <code>objects</code> array.
054:             *                     They should one to one match with each other. The length of <code>strings</code>
055:             *                     array should be the same as that of <code>objects</code> array. Otherwise IllegalArgumentExceptio will be thrown.
056:             * @param defaultValue the default value
057:             */
058:            public EnumConverter(String name, Class<?> type, Object[] objects,
059:                    String[] strings, Object defaultValue) {
060:                if (name == null || name.trim().length() == 0) {
061:                    throw new IllegalArgumentException(
062:                            "The \"name\" parameter cannot be null or empty. Please use a unique string to represent the name of the converter.");
063:                }
064:                _name = name;
065:                if (objects == null) {
066:                    throw new IllegalArgumentException(
067:                            "The \"objects\" parameter cannot be null.");
068:                }
069:                if (strings == null) {
070:                    throw new IllegalArgumentException(
071:                            "The \"strings\" parameter cannot be null.");
072:                }
073:                if (strings.length != objects.length) {
074:                    throw new IllegalArgumentException(
075:                            "The \"objects\" and \"strings\" parameters should have the same length.");
076:                }
077:                _type = type;
078:                _objects = objects;
079:                _strings = strings;
080:                _default = defaultValue;
081:            }
082:
083:            transient private ConverterContext _conext;
084:
085:            /**
086:             * Gets the converter context of this converter. The name of the context
087:             * is the name of the converter where you pass in to EnumConverter's constructor.
088:             *
089:             * @return the converter context of this converter.
090:             */
091:            public ConverterContext getContext() {
092:                if (_conext == null) {
093:                    _conext = new ConverterContext(_name);
094:                }
095:                return _conext;
096:            }
097:
098:            /**
099:             * Converts the object to string. It will find the object from the <code>objects</code> array and find the matching string
100:             * from <code>strings</code> array.
101:             *
102:             * @param object  the object to be converted.
103:             * @param context the converter context.
104:             * @return the string for the object.
105:             */
106:            public String toString(Object object, ConverterContext context) {
107:                for (int i = 0; i < _objects.length; i++) {
108:                    if ((_objects[i] == null && object == null)
109:                            || (_objects[i] != null && _objects[i]
110:                                    .equals(object))) {
111:                        if (i < _strings.length) {
112:                            return _strings[i];
113:                        }
114:                    }
115:                }
116:                return null;
117:            }
118:
119:            public boolean supportToString(Object object,
120:                    ConverterContext context) {
121:                return true;
122:            }
123:
124:            /**
125:             * Converts the string to the object. It will find the string from the <code>strings</code> array and find the matching
126:             * object from <code>objects</code> array.
127:             *
128:             * @param string  the string to be converted
129:             * @param context the converter context.
130:             * @return the object of the string.
131:             */
132:            public Object fromString(String string, ConverterContext context) {
133:                for (int i = 0; i < _strings.length; i++) {
134:                    if (_strings[i].equals(string)) {
135:                        if (i < _objects.length) {
136:                            return _objects[i];
137:                        }
138:                    }
139:                }
140:                return _default;
141:            }
142:
143:            public boolean supportFromString(String string,
144:                    ConverterContext context) {
145:                return true;
146:            }
147:
148:            /**
149:             * Gets the name of the converter.
150:             *
151:             * @return the name of the converter.
152:             */
153:            public String getName() {
154:                return _name;
155:            }
156:
157:            /**
158:             * Gets the type of the converter.
159:             *
160:             * @return the type of the converter.
161:             */
162:            public Class<?> getType() {
163:                return _type;
164:            }
165:
166:            /**
167:             * Gets the default value of the converter if it failed to find the matching object for a particular string.
168:             *
169:             * @return the default value.
170:             */
171:            public Object getDefault() {
172:                return _default;
173:            }
174:
175:            /**
176:             * Gets the <code>objects</code> array.
177:             *
178:             * @return the <code>objects</code> array.
179:             */
180:            public Object[] getObjects() {
181:                return _objects;
182:            }
183:
184:            /**
185:             * Gets the <code>strings</code> array.
186:             *
187:             * @return the <code>strings</code> array.
188:             */
189:            public String[] getStrings() {
190:                return _strings;
191:            }
192:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.