|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // This file has routines which initialize component implementations |
|
20 // according to patterns. |
|
21 |
|
22 include ("messageLibrary.js") |
|
23 |
|
24 implLibraryStrings = getLocalizedStrings("implLibrary"); |
|
25 |
|
26 /** |
|
27 * Set up direct label editing implementation for a component with |
|
28 * one editable label |
|
29 * @param prototype the impl prototype to update |
|
30 * @param property the name of the edited property |
|
31 * @param areafunction a function, taking an instance and laf, which returns the |
|
32 * editable area of the label (or null). If null, the default behavior is |
|
33 * to use the entire rendered area. |
|
34 * @param fontfunction a function, taking an instance and laf, which returns the |
|
35 * IFont to edit with (or null). If null, the default behavior is to return |
|
36 * null, indicating a default system font. |
|
37 */ |
|
38 function setupCommonDirectLabelEditing(prototype, property, areafunction, fontfunction) { |
|
39 |
|
40 prototype.getPropertyPaths = function(instance) { |
|
41 return [ property ]; |
|
42 } |
|
43 |
|
44 prototype.getLabelBounds = function(instance, propertyPath, laf) { |
|
45 if (areafunction) |
|
46 return areafunction(instance, laf); |
|
47 var size = instance.properties.size; |
|
48 return new Rectangle(0, 0, size.width, size.height); |
|
49 } |
|
50 |
|
51 prototype.getLabelFont = function(instance, propertyPath, laf) { |
|
52 if (fontfunction) |
|
53 return fontfunction(instance, laf) |
|
54 return null; |
|
55 } |
|
56 |
|
57 } |
|
58 |
|
59 |
|
60 // Set up direct image editing implementation for a component |
|
61 // with one image property. |
|
62 // |
|
63 // @param prototype the impl prototype to update |
|
64 // @param property the name of the edited property |
|
65 // @param areafunction a function, taking an instance, laf, and propertyPath, or null. |
|
66 // If not null, the function which returns a rectangle for the clickable area |
|
67 // of the rendered area of the component to respond to. |
|
68 // If null, the default behavior is to use the entire rendered area. |
|
69 |
|
70 function setupCommonDirectImageEditing(prototype, property, areafunction, sizefunction, isScalingFunction) { |
|
71 |
|
72 // implementation for IDirectImageEdit |
|
73 prototype.getImagePropertyPaths = function(instance) { |
|
74 return [ property ]; |
|
75 } |
|
76 |
|
77 // implementation for IDirectImageEdit |
|
78 prototype.getImageBounds = function(instance, propertyPath, laf) { |
|
79 //println( "image: " + instance.properties[propertyPath].bmpfile + "/" |
|
80 // +instance.properties[propertyPath].bmpid + "/" |
|
81 // +instance.properties[propertyPath].bmpmask ); |
|
82 |
|
83 if (areafunction) |
|
84 return areafunction(instance, laf, property); |
|
85 else { |
|
86 var size = instance.properties.size; |
|
87 return new Rectangle(0, 0, size.width, size.height); |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 /** Create a simple IModelMessage that refers to an error on a property. */ |
|
93 function createSimpleModelError(instance, propertyId, messageFormat, argList) { |
|
94 var modelMessage = newModelMessage(IStatus.ERROR, |
|
95 formatString(messageFormat, argList), |
|
96 instance, propertyId, null); |
|
97 return modelMessage; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Set up common IComponentValidator script for an editor that has |
|
102 * minimum, maximum, and current value properties. |
|
103 * @param prototype to modify |
|
104 * @param noun localized string for the unit being edited (i.e. "duration") |
|
105 * @param nouns localized string for the plural unit being edited (i.e. "durations") |
|
106 * @param minProperty, maxProperty, valueProperty properties to check |
|
107 * @param converter a function taking a property value used to convert a property to a comparable value; |
|
108 * if null, the property is treated as an integer |
|
109 */ |
|
110 function setupCommonRangeCheckingValidation(prototype, noun, nouns, |
|
111 minProperty, maxProperty, valueProperty, converter) { |
|
112 |
|
113 if (converter == null) |
|
114 converter = function(value) { return value; }; |
|
115 |
|
116 // note that laf will be null if a display model was not created |
|
117 prototype.validate = function(instance, laf) { |
|
118 var properties = instance.properties; |
|
119 |
|
120 var min = converter(properties[minProperty]); |
|
121 var max = converter(properties[maxProperty]); |
|
122 var value = converter(properties[valueProperty]); |
|
123 |
|
124 var messages = new java.util.ArrayList(); |
|
125 |
|
126 if (min > max) { |
|
127 messages.add(createSimpleModelError(instance, |
|
128 minProperty, |
|
129 implLibraryStrings.getString("minMaxValueError"), |
|
130 [ noun, nouns, instance.name, min, max ])); |
|
131 } |
|
132 if (value < min || value > max) { |
|
133 messages.add(createSimpleModelError(instance, |
|
134 valueProperty, |
|
135 implLibraryStrings.getString("valueRangeError"), |
|
136 [ noun, nouns, instance.name, min, max, value ])); |
|
137 } |
|
138 return messages; |
|
139 } |
|
140 |
|
141 // note that laf will be null if a display model was not created |
|
142 prototype.queryPropertyChange = function(instance, propertyPath, |
|
143 newVal, laf) { |
|
144 |
|
145 var properties = instance.properties; |
|
146 var message = null; |
|
147 |
|
148 newValue = converter(newVal); |
|
149 if (propertyPath == minProperty) { |
|
150 if (newValue > converter(properties[valueProperty]) || |
|
151 newValue >= converter(properties[maxProperty])) { |
|
152 message = formatString(implLibraryStrings.getString("minValueConstraint"), noun, nouns ); |
|
153 } |
|
154 } |
|
155 else if (propertyPath == valueProperty) { |
|
156 if (newValue < converter(properties[minProperty]) || |
|
157 newValue > converter(properties[maxProperty])) { |
|
158 message = formatString(implLibraryStrings.getString("valueConstraint"), noun, nouns ); |
|
159 } |
|
160 } |
|
161 else if (propertyPath == maxProperty) { |
|
162 if (newValue <= converter(properties[minProperty]) || |
|
163 newValue < converter(properties[valueProperty])) |
|
164 message = formatString(implLibraryStrings.getString("maxValueConstraint"), noun, nouns ); |
|
165 } |
|
166 |
|
167 return message; |
|
168 } |
|
169 |
|
170 } |
|
171 |