001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.generator.javafx;
022
023 import java.util.HashSet;
024 import java.util.Set;
025
026 import org.granite.generator.as3.ClientType;
027 import org.granite.generator.as3.PackageTranslator;
028
029 /**
030 * @author Franck WOLFF
031 */
032 public class JavaFXType implements ClientType {
033
034 ///////////////////////////////////////////////////////////////////////////
035 // Fields.
036
037 public static final JavaFXType BOOLEAN = new JavaFXType(null, "boolean", false);
038 public static final JavaFXType INT = new JavaFXType(null, "int", Integer.valueOf(0));
039 public static final JavaFXType LONG = new JavaFXType(null, "long", Long.valueOf(0));
040 public static final JavaFXType FLOAT = new JavaFXType(null, "float", Float.valueOf(0.0f));
041 public static final JavaFXType DOUBLE = new JavaFXType(null, "double", Double.valueOf(0.0));
042 public static final JavaFXType STRING = new JavaFXType(null, "String", null);
043
044 public static final JavaFXType PAGE_INFO = new JavaFXType("org.granite.tide.data.model", "PageInfo", null);
045 public static final JavaFXType SORT_INFO = new JavaFXType("org.granite.tide.data.model", "SortInfo", null);
046
047 public static final JavaFXType BOOLEAN_PROPERTY = new JavaFXType(null, "boolean", "javafx.beans.property.BooleanProperty", "javafx.beans.property.SimpleBooleanProperty", Boolean.FALSE);
048 public static final JavaFXType INT_PROPERTY = new JavaFXType(null, "int", "javafx.beans.property.IntegerProperty", "javafx.beans.property.SimpleIntegerProperty", Integer.valueOf(0));
049 public static final JavaFXType LONG_PROPERTY = new JavaFXType(null, "long", "javafx.beans.property.LongProperty", "javafx.beans.property.SimpleLongProperty", Long.valueOf(0));
050 public static final JavaFXType FLOAT_PROPERTY = new JavaFXType(null, "float", "javafx.beans.property.FloatProperty", "javafx.beans.property.SimpleFloatProperty", Float.valueOf(0.0f));
051 public static final JavaFXType DOUBLE_PROPERTY = new JavaFXType(null, "double", "javafx.beans.property.DoubleProperty", "javafx.beans.property.SimpleDoubleProperty", Double.valueOf(0.0));
052 public static final JavaFXType STRING_PROPERTY = new JavaFXType(null, "String", "javafx.beans.property.StringProperty", "javafx.beans.property.SimpleStringProperty", null);
053
054 private final String packageName;
055 private final String name;
056 private final String qualifiedName;
057 private final String propertyTypeName;
058 private final String propertyImplTypeName;
059 private final Object nullValue;
060
061 ///////////////////////////////////////////////////////////////////////////
062 // Constructors.
063
064 public JavaFXType(String packageName, String simpleName) {
065 this(packageName, simpleName, null);
066 }
067 public JavaFXType(String packageName, String name, Object nullValue) {
068 this(packageName, name, null, null, nullValue);
069 }
070 public JavaFXType(String packageName, String name, String propertyTypeName, String propertyImplTypeName, Object nullValue) {
071 this.packageName = (packageName != null ? packageName : "");
072 this.name = name;
073 this.qualifiedName = (hasPackage() ? (packageName + '.' + name) : name);
074 this.nullValue = nullValue;
075 this.propertyTypeName = propertyTypeName;
076 this.propertyImplTypeName = propertyImplTypeName;
077 }
078
079 ///////////////////////////////////////////////////////////////////////////
080 // Properties.
081
082 @Override
083 public boolean hasPackage() {
084 return packageName.length() > 0;
085 }
086
087 @Override
088 public String getPackageName() {
089 return packageName;
090 }
091
092 @Override
093 public String getName() {
094 return name;
095 }
096
097 @Override
098 public String getQualifiedName() {
099 return qualifiedName;
100 }
101
102 public String getPropertyTypeName() {
103 return propertyTypeName;
104 }
105
106 public String getSimplePropertyTypeName() {
107 return propertyTypeName != null && propertyTypeName.indexOf(".") >= 0
108 ? propertyTypeName.substring(propertyTypeName.lastIndexOf(".")+1) : propertyTypeName;
109 }
110
111 public String getPropertyImplTypeName() {
112 return propertyImplTypeName;
113 }
114
115 public String getSimplePropertyImplTypeName() {
116 return propertyImplTypeName != null && propertyImplTypeName.indexOf(".") >= 0
117 ? propertyImplTypeName.substring(propertyImplTypeName.lastIndexOf(".")+1) : propertyImplTypeName;
118 }
119
120 @Override
121 public Object getNullValue() {
122 return nullValue;
123 }
124
125 public boolean isNumber() {
126 return false;
127 }
128
129 ///////////////////////////////////////////////////////////////////////////
130 // Methods
131
132 @Override
133 public Set<String> getImports() {
134 Set<String> imports = new HashSet<String>();
135 if (hasPackage())
136 imports.add(ungenerify(qualifiedName));
137 if (propertyTypeName != null)
138 imports.add(ungenerify(propertyTypeName));
139 if (propertyImplTypeName != null)
140 imports.add(ungenerify(propertyImplTypeName));
141 return imports;
142 }
143
144 private String ungenerify(String className) {
145 if (className.indexOf("<") >= 0)
146 return className.substring(0, className.indexOf("<"));
147 return className;
148 }
149
150 @Override
151 public JavaFXType toArrayType() {
152 return new JavaFXType(packageName, name + "[]", null);
153 }
154
155 @Override
156 public JavaFXType translatePackage(PackageTranslator translator) {
157 return new JavaFXType(translator.translate(packageName), getName(), getPropertyTypeName(), getPropertyImplTypeName(), getNullValue());
158 }
159
160 ///////////////////////////////////////////////////////////////////////////
161 // Utilities.
162
163 @Override
164 public int hashCode() {
165 return qualifiedName.hashCode();
166 }
167
168 @Override
169 public boolean equals(Object obj) {
170 if (obj == this)
171 return true;
172 if (!(obj instanceof JavaFXType))
173 return false;
174 return qualifiedName.equals(((JavaFXType)obj).qualifiedName);
175 }
176
177 @Override
178 public String toString() {
179 return qualifiedName;
180 }
181 }