Skip to main content
summaryrefslogtreecommitdiffstats
blob: 30382bd94179520b94dd2b26981c5c7ccd69061d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *         Florian Pirchner - Initial implementation
 */

package org.eclipse.osbp.ecview.dsl.extensions

import com.google.inject.Inject
import org.eclipse.osbp.ecview.semantic.uimodel.UiBeanReferenceField
import org.eclipse.osbp.ecview.semantic.uimodel.UiColumn
import org.eclipse.osbp.ecview.semantic.uimodel.UiComboBox
import org.eclipse.osbp.ecview.semantic.uimodel.UiEmbeddable
import org.eclipse.osbp.ecview.semantic.uimodel.UiErrorCode
import org.eclipse.osbp.ecview.semantic.uimodel.UiExposedAction
import org.eclipse.osbp.ecview.semantic.uimodel.UiField
import org.eclipse.osbp.ecview.semantic.uimodel.UiList
import org.eclipse.osbp.ecview.semantic.uimodel.UiNestedProperty
import org.eclipse.osbp.ecview.semantic.uimodel.UiOptionsGroup
import org.eclipse.osbp.ecview.semantic.uimodel.UiSearchField
import org.eclipse.osbp.ecview.semantic.uimodel.UiSearchPanel
import org.eclipse.osbp.ecview.semantic.uimodel.UiTabAssignment
import org.eclipse.osbp.ecview.semantic.uimodel.UiTable
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.common.types.JvmType
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.osbp.ecview.core.common.model.core.util.II18nConstants

class I18nKeyProvider {

	@Inject extension IQualifiedNameProvider
	@Inject BindingInfoHelper bindingInfoHelper;

	def dispatch String toI18nKey(UiEmbeddable embeddable) {
		if(embeddable.i18nInfo !== null && embeddable.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		
		if (embeddable.i18nInfo !== null && embeddable.i18nInfo.key !== null) {
			return embeddable.i18nInfo.key;
		}

		if (embeddable instanceof UiField) {
			val String i18nKey = toI18nKeyByBindings(embeddable)
			if (i18nKey !== null && !i18nKey.equals("")) {
				return i18nKey;
			}
		}
		return if(embeddable.name !== null) embeddable.name else ""
	}

	def dispatch String toI18nKey(UiTable element) {
		if(element.i18nInfo !== null && element.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		
		if (element.i18nInfo !== null && element.i18nInfo.key !== null) {
			return element.i18nInfo.key;
		} else if (element.jvmType !== null) {
			return element.jvmType.simpleName
		}
		return if(element.name !== null) element.name else ""
	}

	def dispatch String toI18nKey(UiList element) {
		if(element.i18nInfo !== null && element.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		
		if (element.i18nInfo !== null && element.i18nInfo.key !== null) {
			return element.i18nInfo.key;
		} else if (element.jvmType !== null) {
			return element.jvmType.simpleName
		}
		return if(element.name !== null) element.name else ""
	}

	def dispatch String toI18nKey(UiOptionsGroup element) {
		if(element.i18nInfo !== null && element.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		
		if (element.i18nInfo !== null && element.i18nInfo.key !== null) {
			return element.i18nInfo.key;
		} else if (element.jvmType !== null) {
			return element.jvmType.simpleName
		}
		return if(element.name !== null) element.name else ""
	}

	def dispatch String toI18nKey(UiComboBox element) {
		if(element.i18nInfo !== null && element.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		if (element.i18nInfo !== null && element.i18nInfo.key !== null) {
			return element.i18nInfo.key;
		} else if (element.jvmType !== null) {
			return element.jvmType.simpleName
		}
		return if(element.name !== null) element.name else ""
	}

	def dispatch String toI18nKey(UiSearchPanel element) {
		if(element.i18nInfo !== null && element.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		if (element.i18nInfo !== null && element.i18nInfo.key !== null) {
			return element.i18nInfo.key;
		} else if (element.jvmType !== null) {
			return element.jvmType.simpleName
		}
		return if(element.name !== null) element.name else ""
	}

	def dispatch String toI18nKey(UiBeanReferenceField element) {
		
		if(element.i18nInfo !== null && element.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		
		if (element.i18nInfo !== null && element.i18nInfo.key !== null) {
			return element.i18nInfo.key;
		} else if (element.jvmType !== null) {
			return element.jvmType.simpleName
		}
		return if(element.name !== null) element.name else ""
	}

	def dispatch String toI18nKey(UiColumn column) {
		val UiTable table = column.eContainer.eContainer as UiTable

		if (column.i18nInfo !== null && column.i18nInfo.key !== null) {
			return column.i18nInfo.key;
		} else if (table.jvmType !== null) {
			val UiNestedProperty property = column.property
			if (property !== null) {
				val path = property.toPathString
				if (path === null) {
					return table.jvmType.simpleName
				} else if (path.contains(".")) {
					return property.simpleGetterNameOfLastSegment
				} else {
					return property.simpleGetterName
				}
			}
		} else {
			val UiNestedProperty property = column.property
			if (property !== null) {
				val parent = column.eContainer().eContainer() as UiEmbeddable
				return parent.name + "." + property.toPathString
			}
		}
		return column.name
	}

	def dispatch String toI18nKey(UiExposedAction action) {
		if (action.actionReference !== null) {
			return action.actionReference.name
		} else {
			return action.actionID
		}
	}

	def dispatch String toI18nKey(UiSearchField embeddable) {
		if(embeddable.i18nInfo !== null && embeddable.i18nInfo.noCaption){
			return II18nConstants.NO_CAPTION
		} 
		
		if (embeddable.i18nInfo !== null && embeddable.i18nInfo.key !== null) {
			return embeddable.i18nInfo.key;
		} else {
			val UiNestedProperty property = embeddable.property
			if (property !== null) {
				val String i18nKey = toI18nKeyByBindings(property)
				if (i18nKey !== null && !i18nKey.equals("")) {
					return i18nKey;
				}
				return "search." + property.toPathString
			}
		}
		return embeddable.name
	}

	def dispatch String toI18nKey(UiTabAssignment embeddable) {
		if (embeddable.i18nInfo !== null && embeddable.i18nInfo.key !== null) {
			return embeddable.i18nInfo.key;
		}
		return embeddable.name
	}

	def dispatch String toI18nKey(UiErrorCode code) {
		val fqn = code.fullyQualifiedName
		return fqn.skipLast(1).toString
	}

	def String toI18nKeyByBindings(UiEmbeddable embeddable) {
		if (embeddable instanceof UiField) {
			val ops = bindingInfoHelper.findBoundOppositeJvmOperations(embeddable, "value")

			if (!ops.empty) {
				val JvmOperation op = ops.get(0)
				if(op === null){
					return null
				}
				val JvmType type = op.declaringType
				if (type === null) {
					return null
				}
				return ops.get(0).simpleName.toPropertyName
			}
		}
	}

	def String toI18nKeyByBindings(UiNestedProperty property) {
		if (property !== null) {
			val JvmOperation op = property.operationofLastSegment
			if (op !== null) {
				val JvmType type = op.declaringType
				if (type === null) {
					return null
				}
				return op.simpleName.toPropertyName
			}
		}
	}

	def static getToPropertyName(String string) {
		if (string.startsWith("get")) {
			return string.replaceFirst("get", "").toFirstLower
		} else if (string.startsWith("is")) {
			return string.replaceFirst("is", "").toFirstLower
		}
	}
}

Back to the top