uidesigner/com.nokia.carbide.cpp.uiq.components/components/implLibrary.js
author tzelaw
Tue, 14 Apr 2009 15:03:19 -0500
changeset 94 d74b720418db
parent 2 d760517a8095
permissions -rw-r--r--
Test framework support: Ask debugger to remember DebugTarget so test framework can use it to setup test framework related utility. With this we can use the DebugUI way of launching while keeping test framework functionality
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
// This file has routines which initialize component implementations
cawthron
parents:
diff changeset
    20
// according to patterns.
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
include ("messageLibrary.js")
cawthron
parents:
diff changeset
    23
cawthron
parents:
diff changeset
    24
implLibraryStrings = getLocalizedStrings("implLibrary");
cawthron
parents:
diff changeset
    25
cawthron
parents:
diff changeset
    26
/**
cawthron
parents:
diff changeset
    27
 *	Set up direct label editing implementation for a component with
cawthron
parents:
diff changeset
    28
 *	one editable label
cawthron
parents:
diff changeset
    29
 *	@param prototype the impl prototype to update
cawthron
parents:
diff changeset
    30
 *	@param property the name of the edited property
cawthron
parents:
diff changeset
    31
 *	@param areafunction a function, taking an instance and laf, which returns the
cawthron
parents:
diff changeset
    32
 *		editable area of the label (or null).  If null, the default behavior is
cawthron
parents:
diff changeset
    33
 *	to use the entire rendered area.
cawthron
parents:
diff changeset
    34
 *	@param fontfunction a function, taking an instance and laf, which returns the
cawthron
parents:
diff changeset
    35
 *		IFont to edit with (or null).  If null, the default behavior is to return
cawthron
parents:
diff changeset
    36
 *		null, indicating a default system font.
cawthron
parents:
diff changeset
    37
 */
cawthron
parents:
diff changeset
    38
function setupCommonDirectLabelEditing(prototype, property, areafunction, fontfunction) {
cawthron
parents:
diff changeset
    39
cawthron
parents:
diff changeset
    40
	prototype.getPropertyPaths = function(instance) {
cawthron
parents:
diff changeset
    41
		return [ property ];
cawthron
parents:
diff changeset
    42
	}
cawthron
parents:
diff changeset
    43
cawthron
parents:
diff changeset
    44
	prototype.getLabelBounds = function(instance, propertyPath, laf) {
cawthron
parents:
diff changeset
    45
		if (areafunction)
cawthron
parents:
diff changeset
    46
			return areafunction(instance, laf);
cawthron
parents:
diff changeset
    47
		var size = instance.properties.size;
cawthron
parents:
diff changeset
    48
	    return new Rectangle(0, 0, size.width, size.height);
cawthron
parents:
diff changeset
    49
	}
cawthron
parents:
diff changeset
    50
cawthron
parents:
diff changeset
    51
	prototype.getLabelFont = function(instance, propertyPath, laf) {
cawthron
parents:
diff changeset
    52
		if (fontfunction)
cawthron
parents:
diff changeset
    53
			return fontfunction(instance, laf)
cawthron
parents:
diff changeset
    54
		return null;
cawthron
parents:
diff changeset
    55
	}
cawthron
parents:
diff changeset
    56
cawthron
parents:
diff changeset
    57
}
cawthron
parents:
diff changeset
    58
cawthron
parents:
diff changeset
    59
function areaWithParentWidth(instance, laf){
cawthron
parents:
diff changeset
    60
	var size = instance.properties.size;
cawthron
parents:
diff changeset
    61
	if (instance.parent != null) {
cawthron
parents:
diff changeset
    62
		var sizeParent = instance.parent.properties.size;
cawthron
parents:
diff changeset
    63
		var x = instance.properties.location.x;
cawthron
parents:
diff changeset
    64
		return new Rectangle(-x, 0, sizeParent.width, size.height);
cawthron
parents:
diff changeset
    65
	}
cawthron
parents:
diff changeset
    66
	return new Rectangle(0, 0, size.width, size.height);
cawthron
parents:
diff changeset
    67
}
cawthron
parents:
diff changeset
    68
cawthron
parents:
diff changeset
    69
//	Set up direct image editing implementation for a component
cawthron
parents:
diff changeset
    70
//	with one image property.  
cawthron
parents:
diff changeset
    71
//
cawthron
parents:
diff changeset
    72
//	@param prototype the impl prototype to update
cawthron
parents:
diff changeset
    73
//	@param property the name of the edited property
cawthron
parents:
diff changeset
    74
//	@param areafunction a function, taking an instance, laf, and propertyPath, or null.
cawthron
parents:
diff changeset
    75
//		If not null, the function which returns a rectangle for the clickable area 
cawthron
parents:
diff changeset
    76
//		of the rendered area of the component to respond to.
cawthron
parents:
diff changeset
    77
//		If null, the default behavior is to use the entire rendered area.
cawthron
parents:
diff changeset
    78
cawthron
parents:
diff changeset
    79
function setupCommonDirectImageEditing(prototype, property, areafunction, sizefunction, isScalingFunction) {
cawthron
parents:
diff changeset
    80
cawthron
parents:
diff changeset
    81
	// implementation for IDirectImageEdit
cawthron
parents:
diff changeset
    82
	prototype.getImagePropertyPaths = function(instance) {
cawthron
parents:
diff changeset
    83
		return [ property ];
cawthron
parents:
diff changeset
    84
	}
cawthron
parents:
diff changeset
    85
cawthron
parents:
diff changeset
    86
	// implementation for IDirectImageEdit
cawthron
parents:
diff changeset
    87
	prototype.getImageBounds = function(instance, propertyPath, laf) {
cawthron
parents:
diff changeset
    88
		//println( "image: " + instance.properties[propertyPath].bmpfile + "/" 
cawthron
parents:
diff changeset
    89
		//		+instance.properties[propertyPath].bmpid + "/" 
cawthron
parents:
diff changeset
    90
		//		+instance.properties[propertyPath].bmpmask );
cawthron
parents:
diff changeset
    91
				
cawthron
parents:
diff changeset
    92
		if (areafunction) 
cawthron
parents:
diff changeset
    93
			return areafunction(instance, laf, property);
cawthron
parents:
diff changeset
    94
		else {
cawthron
parents:
diff changeset
    95
			var size = instance.properties.size;
cawthron
parents:
diff changeset
    96
		    return new Rectangle(0, 0, size.width, size.height);
cawthron
parents:
diff changeset
    97
		}
cawthron
parents:
diff changeset
    98
	}
cawthron
parents:
diff changeset
    99
}
cawthron
parents:
diff changeset
   100
cawthron
parents:
diff changeset
   101
/**	Create a simple IModelMessage that refers to an error on a property. */
cawthron
parents:
diff changeset
   102
function createSimpleModelError(instance, propertyId, messageFormat, argList) {
cawthron
parents:
diff changeset
   103
	var modelMessage = newModelMessage(IStatus.ERROR, 
cawthron
parents:
diff changeset
   104
			formatString(messageFormat, argList), 
cawthron
parents:
diff changeset
   105
			instance, propertyId, null);
cawthron
parents:
diff changeset
   106
	return modelMessage;
cawthron
parents:
diff changeset
   107
}
cawthron
parents:
diff changeset
   108
cawthron
parents:
diff changeset
   109
/**
cawthron
parents:
diff changeset
   110
 *	Set up common IComponentValidator script for an editor that has
cawthron
parents:
diff changeset
   111
 *	minimum, maximum, and current value properties.
cawthron
parents:
diff changeset
   112
 *	@param prototype to modify
cawthron
parents:
diff changeset
   113
 *	@param noun localized string for the unit being edited (i.e. "duration")
cawthron
parents:
diff changeset
   114
 *	@param nouns localized string for the plural unit being edited (i.e. "durations")
cawthron
parents:
diff changeset
   115
 *	@param minProperty, maxProperty, valueProperty, maxLengthProperty properties to check
cawthron
parents:
diff changeset
   116
 *	@param converter a function taking a property value used to convert a property to a comparable value;
cawthron
parents:
diff changeset
   117
 *		if null, the property is treated as an integer
cawthron
parents:
diff changeset
   118
 */
cawthron
parents:
diff changeset
   119
function setupCommonAndMaxLengthRangeCheckingValidation(prototype, noun, nouns,
cawthron
parents:
diff changeset
   120
		minProperty, maxProperty, valueProperty, converter, maxLengthProperty) {
cawthron
parents:
diff changeset
   121
cawthron
parents:
diff changeset
   122
	if (converter == null)
cawthron
parents:
diff changeset
   123
		converter = function(value) { return value; };
cawthron
parents:
diff changeset
   124
cawthron
parents:
diff changeset
   125
			// note that laf will be null if a display model was not created
cawthron
parents:
diff changeset
   126
	prototype.validate = function(instance, laf) {
cawthron
parents:
diff changeset
   127
cawthron
parents:
diff changeset
   128
		var properties = instance.properties;
cawthron
parents:
diff changeset
   129
		var messages = new java.util.ArrayList();
cawthron
parents:
diff changeset
   130
cawthron
parents:
diff changeset
   131
		var min = converter(properties[minProperty]);
cawthron
parents:
diff changeset
   132
		var max = converter(properties[maxProperty]);
cawthron
parents:
diff changeset
   133
		var value = converter(properties[valueProperty]);
cawthron
parents:
diff changeset
   134
cawthron
parents:
diff changeset
   135
		if (converter(properties[maxLengthProperty])!= null) {
cawthron
parents:
diff changeset
   136
			var maxLength = converter(properties[maxLengthProperty]);
cawthron
parents:
diff changeset
   137
			if ( maxLength < 3 || maxLength > 32 ){
cawthron
parents:
diff changeset
   138
				messages.add(createSimpleModelError(instance, maxLengthProperty, 
cawthron
parents:
diff changeset
   139
				implLibraryStrings.getString("maxLengthValueError"), 
cawthron
parents:
diff changeset
   140
				[ maxLength ]));
cawthron
parents:
diff changeset
   141
			} else {
cawthron
parents:
diff changeset
   142
				var textValue = value.toString();
cawthron
parents:
diff changeset
   143
				if (textValue.length > maxLength){
cawthron
parents:
diff changeset
   144
					messages.add(createSimpleModelError(instance, valueProperty,
cawthron
parents:
diff changeset
   145
					implLibraryStrings.getString("maxLengthConstraint"), 
cawthron
parents:
diff changeset
   146
					[noun, maxLength ]));
cawthron
parents:
diff changeset
   147
				}	
cawthron
parents:
diff changeset
   148
			}
cawthron
parents:
diff changeset
   149
		} 
cawthron
parents:
diff changeset
   150
		
cawthron
parents:
diff changeset
   151
		if (min > max) {
cawthron
parents:
diff changeset
   152
			messages.add(createSimpleModelError(instance, 
cawthron
parents:
diff changeset
   153
				minProperty, 
cawthron
parents:
diff changeset
   154
				implLibraryStrings.getString("minMaxValueError"), 
cawthron
parents:
diff changeset
   155
				[ noun, nouns, instance.name, min, max ]));
cawthron
parents:
diff changeset
   156
		} 
cawthron
parents:
diff changeset
   157
		if (value < min || value > max ) {
cawthron
parents:
diff changeset
   158
			messages.add(createSimpleModelError(instance, 
cawthron
parents:
diff changeset
   159
				valueProperty, 
cawthron
parents:
diff changeset
   160
				implLibraryStrings.getString("valueRangeError"), 
cawthron
parents:
diff changeset
   161
				[ noun, nouns, instance.name, min, max, value ]));
cawthron
parents:
diff changeset
   162
		}	
cawthron
parents:
diff changeset
   163
		return messages;
cawthron
parents:
diff changeset
   164
	}
cawthron
parents:
diff changeset
   165
	
cawthron
parents:
diff changeset
   166
		// note that laf will be null if a display model was not created
cawthron
parents:
diff changeset
   167
	prototype.queryPropertyChange = function(instance, propertyPath,
cawthron
parents:
diff changeset
   168
					newVal, laf) {
cawthron
parents:
diff changeset
   169
		var properties = instance.properties;
cawthron
parents:
diff changeset
   170
		var message = null;
cawthron
parents:
diff changeset
   171
cawthron
parents:
diff changeset
   172
		newValue = converter(newVal);
cawthron
parents:
diff changeset
   173
		if (propertyPath == minProperty) {
cawthron
parents:
diff changeset
   174
			if (newValue > converter(properties[valueProperty]) ||
cawthron
parents:
diff changeset
   175
				newValue >= converter(properties[maxProperty])) {
cawthron
parents:
diff changeset
   176
				message = formatString(implLibraryStrings.getString("minValueConstraint"), noun, nouns );
cawthron
parents:
diff changeset
   177
			}
cawthron
parents:
diff changeset
   178
		}
cawthron
parents:
diff changeset
   179
		else if (propertyPath == valueProperty) {
cawthron
parents:
diff changeset
   180
			if (newValue < converter(properties[minProperty]) ||
cawthron
parents:
diff changeset
   181
			    newValue > converter(properties[maxProperty])) {
cawthron
parents:
diff changeset
   182
				message = formatString(implLibraryStrings.getString("valueConstraint"), noun, nouns );
cawthron
parents:
diff changeset
   183
			}else {
cawthron
parents:
diff changeset
   184
				if (converter(properties[maxLengthProperty])!= null){
cawthron
parents:
diff changeset
   185
					if (newValue.toString().length() > converter(properties[maxLengthProperty])) {
cawthron
parents:
diff changeset
   186
						message = formatString(implLibraryStrings.getString("maxLengthConstraint"), noun );
cawthron
parents:
diff changeset
   187
					}
cawthron
parents:
diff changeset
   188
				}
cawthron
parents:
diff changeset
   189
			}
cawthron
parents:
diff changeset
   190
		}
cawthron
parents:
diff changeset
   191
		else if (propertyPath == maxProperty) {
cawthron
parents:
diff changeset
   192
			if (newValue <= converter(properties[minProperty]) ||
cawthron
parents:
diff changeset
   193
				newValue < converter(properties[valueProperty]))
cawthron
parents:
diff changeset
   194
				message = formatString(implLibraryStrings.getString("maxValueConstraint"), noun, nouns );
cawthron
parents:
diff changeset
   195
		}
cawthron
parents:
diff changeset
   196
		return message;		
cawthron
parents:
diff changeset
   197
	}
cawthron
parents:
diff changeset
   198
cawthron
parents:
diff changeset
   199
}
cawthron
parents:
diff changeset
   200
cawthron
parents:
diff changeset
   201
/**
cawthron
parents:
diff changeset
   202
 *	Set up common IComponentValidator script for an editor that has
cawthron
parents:
diff changeset
   203
 *	minimum, maximum, and current value properties.
cawthron
parents:
diff changeset
   204
 *	@param prototype to modify
cawthron
parents:
diff changeset
   205
 *	@param noun localized string for the unit being edited (i.e. "duration")
cawthron
parents:
diff changeset
   206
 *	@param nouns localized string for the plural unit being edited (i.e. "durations")
cawthron
parents:
diff changeset
   207
 *	@param minProperty, maxProperty, valueProperty properties to check
cawthron
parents:
diff changeset
   208
 *	@param converter a function taking a property value used to convert a property to a comparable value;
cawthron
parents:
diff changeset
   209
 *		if null, the property is treated as an integer
cawthron
parents:
diff changeset
   210
 */
cawthron
parents:
diff changeset
   211
function setupCommonRangeCheckingValidation(prototype, noun, nouns,
cawthron
parents:
diff changeset
   212
		minProperty, maxProperty, valueProperty, converter) {
cawthron
parents:
diff changeset
   213
	setupCommonAndMaxLengthRangeCheckingValidation(prototype, noun, nouns,
cawthron
parents:
diff changeset
   214
		minProperty, maxProperty, valueProperty, converter, null);
cawthron
parents:
diff changeset
   215
}