uidesigner/com.nokia.carbide.cpp.uiq.components/components/implLibrary.js
changeset 0 fb279309251b
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     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 function areaWithParentWidth(instance, laf){
       
    60 	var size = instance.properties.size;
       
    61 	if (instance.parent != null) {
       
    62 		var sizeParent = instance.parent.properties.size;
       
    63 		var x = instance.properties.location.x;
       
    64 		return new Rectangle(-x, 0, sizeParent.width, size.height);
       
    65 	}
       
    66 	return new Rectangle(0, 0, size.width, size.height);
       
    67 }
       
    68 
       
    69 //	Set up direct image editing implementation for a component
       
    70 //	with one image property.  
       
    71 //
       
    72 //	@param prototype the impl prototype to update
       
    73 //	@param property the name of the edited property
       
    74 //	@param areafunction a function, taking an instance, laf, and propertyPath, or null.
       
    75 //		If not null, the function which returns a rectangle for the clickable area 
       
    76 //		of the rendered area of the component to respond to.
       
    77 //		If null, the default behavior is to use the entire rendered area.
       
    78 
       
    79 function setupCommonDirectImageEditing(prototype, property, areafunction, sizefunction, isScalingFunction) {
       
    80 
       
    81 	// implementation for IDirectImageEdit
       
    82 	prototype.getImagePropertyPaths = function(instance) {
       
    83 		return [ property ];
       
    84 	}
       
    85 
       
    86 	// implementation for IDirectImageEdit
       
    87 	prototype.getImageBounds = function(instance, propertyPath, laf) {
       
    88 		//println( "image: " + instance.properties[propertyPath].bmpfile + "/" 
       
    89 		//		+instance.properties[propertyPath].bmpid + "/" 
       
    90 		//		+instance.properties[propertyPath].bmpmask );
       
    91 				
       
    92 		if (areafunction) 
       
    93 			return areafunction(instance, laf, property);
       
    94 		else {
       
    95 			var size = instance.properties.size;
       
    96 		    return new Rectangle(0, 0, size.width, size.height);
       
    97 		}
       
    98 	}
       
    99 }
       
   100 
       
   101 /**	Create a simple IModelMessage that refers to an error on a property. */
       
   102 function createSimpleModelError(instance, propertyId, messageFormat, argList) {
       
   103 	var modelMessage = newModelMessage(IStatus.ERROR, 
       
   104 			formatString(messageFormat, argList), 
       
   105 			instance, propertyId, null);
       
   106 	return modelMessage;
       
   107 }
       
   108 
       
   109 /**
       
   110  *	Set up common IComponentValidator script for an editor that has
       
   111  *	minimum, maximum, and current value properties.
       
   112  *	@param prototype to modify
       
   113  *	@param noun localized string for the unit being edited (i.e. "duration")
       
   114  *	@param nouns localized string for the plural unit being edited (i.e. "durations")
       
   115  *	@param minProperty, maxProperty, valueProperty, maxLengthProperty properties to check
       
   116  *	@param converter a function taking a property value used to convert a property to a comparable value;
       
   117  *		if null, the property is treated as an integer
       
   118  */
       
   119 function setupCommonAndMaxLengthRangeCheckingValidation(prototype, noun, nouns,
       
   120 		minProperty, maxProperty, valueProperty, converter, maxLengthProperty) {
       
   121 
       
   122 	if (converter == null)
       
   123 		converter = function(value) { return value; };
       
   124 
       
   125 			// note that laf will be null if a display model was not created
       
   126 	prototype.validate = function(instance, laf) {
       
   127 
       
   128 		var properties = instance.properties;
       
   129 		var messages = new java.util.ArrayList();
       
   130 
       
   131 		var min = converter(properties[minProperty]);
       
   132 		var max = converter(properties[maxProperty]);
       
   133 		var value = converter(properties[valueProperty]);
       
   134 
       
   135 		if (converter(properties[maxLengthProperty])!= null) {
       
   136 			var maxLength = converter(properties[maxLengthProperty]);
       
   137 			if ( maxLength < 3 || maxLength > 32 ){
       
   138 				messages.add(createSimpleModelError(instance, maxLengthProperty, 
       
   139 				implLibraryStrings.getString("maxLengthValueError"), 
       
   140 				[ maxLength ]));
       
   141 			} else {
       
   142 				var textValue = value.toString();
       
   143 				if (textValue.length > maxLength){
       
   144 					messages.add(createSimpleModelError(instance, valueProperty,
       
   145 					implLibraryStrings.getString("maxLengthConstraint"), 
       
   146 					[noun, maxLength ]));
       
   147 				}	
       
   148 			}
       
   149 		} 
       
   150 		
       
   151 		if (min > max) {
       
   152 			messages.add(createSimpleModelError(instance, 
       
   153 				minProperty, 
       
   154 				implLibraryStrings.getString("minMaxValueError"), 
       
   155 				[ noun, nouns, instance.name, min, max ]));
       
   156 		} 
       
   157 		if (value < min || value > max ) {
       
   158 			messages.add(createSimpleModelError(instance, 
       
   159 				valueProperty, 
       
   160 				implLibraryStrings.getString("valueRangeError"), 
       
   161 				[ noun, nouns, instance.name, min, max, value ]));
       
   162 		}	
       
   163 		return messages;
       
   164 	}
       
   165 	
       
   166 		// note that laf will be null if a display model was not created
       
   167 	prototype.queryPropertyChange = function(instance, propertyPath,
       
   168 					newVal, laf) {
       
   169 		var properties = instance.properties;
       
   170 		var message = null;
       
   171 
       
   172 		newValue = converter(newVal);
       
   173 		if (propertyPath == minProperty) {
       
   174 			if (newValue > converter(properties[valueProperty]) ||
       
   175 				newValue >= converter(properties[maxProperty])) {
       
   176 				message = formatString(implLibraryStrings.getString("minValueConstraint"), noun, nouns );
       
   177 			}
       
   178 		}
       
   179 		else if (propertyPath == valueProperty) {
       
   180 			if (newValue < converter(properties[minProperty]) ||
       
   181 			    newValue > converter(properties[maxProperty])) {
       
   182 				message = formatString(implLibraryStrings.getString("valueConstraint"), noun, nouns );
       
   183 			}else {
       
   184 				if (converter(properties[maxLengthProperty])!= null){
       
   185 					if (newValue.toString().length() > converter(properties[maxLengthProperty])) {
       
   186 						message = formatString(implLibraryStrings.getString("maxLengthConstraint"), noun );
       
   187 					}
       
   188 				}
       
   189 			}
       
   190 		}
       
   191 		else if (propertyPath == maxProperty) {
       
   192 			if (newValue <= converter(properties[minProperty]) ||
       
   193 				newValue < converter(properties[valueProperty]))
       
   194 				message = formatString(implLibraryStrings.getString("maxValueConstraint"), noun, nouns );
       
   195 		}
       
   196 		return message;		
       
   197 	}
       
   198 
       
   199 }
       
   200 
       
   201 /**
       
   202  *	Set up common IComponentValidator script for an editor that has
       
   203  *	minimum, maximum, and current value properties.
       
   204  *	@param prototype to modify
       
   205  *	@param noun localized string for the unit being edited (i.e. "duration")
       
   206  *	@param nouns localized string for the plural unit being edited (i.e. "durations")
       
   207  *	@param minProperty, maxProperty, valueProperty properties to check
       
   208  *	@param converter a function taking a property value used to convert a property to a comparable value;
       
   209  *		if null, the property is treated as an integer
       
   210  */
       
   211 function setupCommonRangeCheckingValidation(prototype, noun, nouns,
       
   212 		minProperty, maxProperty, valueProperty, converter) {
       
   213 	setupCommonAndMaxLengthRangeCheckingValidation(prototype, noun, nouns,
       
   214 		minProperty, maxProperty, valueProperty, converter, null);
       
   215 }