testdev/ite/src/com.nokia.testfw.codegen.ui/src/com/nokia/testfw/codegen/ui/preferences/TESTFWPropertiesPreferencePage.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     1 /*
       
     2 * Copyright (c) 2009 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 "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 package com.nokia.testfw.codegen.ui.preferences;
       
    19 
       
    20 import org.eclipse.jface.preference.FieldEditorPreferencePage;
       
    21 import org.eclipse.jface.preference.StringFieldEditor;
       
    22 import org.eclipse.swt.events.FocusAdapter;
       
    23 import org.eclipse.swt.events.FocusEvent;
       
    24 import org.eclipse.swt.widgets.Composite;
       
    25 import org.eclipse.swt.widgets.Text;
       
    26 import org.eclipse.ui.IWorkbench;
       
    27 import org.eclipse.ui.IWorkbenchPreferencePage;
       
    28 
       
    29 import com.nokia.testfw.codegen.ui.CodegenUIPlugin;
       
    30 import com.nokia.testfw.codegen.ui.preferences.PreferenceConstants;
       
    31 import com.nokia.testfw.codegen.ui.preferences.PreferenceUtil;
       
    32 import com.nokia.testfw.codegen.ui.Messages;
       
    33 
       
    34 public class TESTFWPropertiesPreferencePage extends FieldEditorPreferencePage
       
    35 		implements IWorkbenchPreferencePage {
       
    36 
       
    37 	public StringFieldEditor iTestFolderName;
       
    38 	public HexFieldEditor iUID3MaxValue;
       
    39 	public HexFieldEditor iUID3MinValue;
       
    40 	public StringFieldEditor iAutherName;
       
    41 
       
    42 	public TESTFWPropertiesPreferencePage() {
       
    43 		super(GRID);
       
    44 		setPreferenceStore(CodegenUIPlugin.getDefault().getPreferenceStore());
       
    45 		setDescription(Messages
       
    46 				.getString("TESTFWPropertiesPreferencePage.Description"));
       
    47 	}
       
    48 
       
    49 	public void init(IWorkbench workbench) {
       
    50 	}
       
    51 
       
    52 	@Override
       
    53 	protected void createFieldEditors() {
       
    54 
       
    55 		iTestFolderName = new StringFieldEditor(
       
    56 				PreferenceConstants.TEST_FOLDER_NAME,
       
    57 				Messages
       
    58 						.getString("TESTFWPropertiesPreferencePage.TestFolder.Name"),
       
    59 				getFieldEditorParent()) {
       
    60 			protected boolean doCheckState() {
       
    61 				String testFolderName = getTextControl().getText();
       
    62 				if (!testFolderName.matches("[\\w]*")) {
       
    63 					setErrorMessage(Messages
       
    64 							.getString("TESTFWPropertiesPreferencePage.TestFolder.Error"));
       
    65 					return false;
       
    66 				}
       
    67 				return true;
       
    68 			}
       
    69 		};
       
    70 
       
    71 		iUID3MaxValue = new HexFieldEditor(
       
    72 				PreferenceConstants.UID3_MAX_VALUE,
       
    73 				Messages
       
    74 						.getString("TESTFWPropertiesPreferencePage.UID3.MaxValue"),
       
    75 				getFieldEditorParent()) {
       
    76 			protected boolean doCheckState() {
       
    77 				if (super.doCheckState()) {
       
    78 					if (iUID3MaxValue.getIntValue() < iUID3MinValue
       
    79 							.getIntValue()) {
       
    80 						setErrorMessage(Messages
       
    81 								.getString("TESTFWPropertiesPreferencePage.UID3.Error1"));
       
    82 						return false;
       
    83 					} else {
       
    84 						return true;
       
    85 					}
       
    86 				} else {
       
    87 					Messages.getString(
       
    88 							"TESTFWPropertiesPreferencePage.UIDHexError",
       
    89 							iUID3MaxValue.getStringValue());
       
    90 					return false;
       
    91 				}
       
    92 			}
       
    93 		};
       
    94 
       
    95 		iUID3MinValue = new HexFieldEditor(
       
    96 				PreferenceConstants.UID3_MIN_VALUE,
       
    97 				Messages
       
    98 						.getString("TESTFWPropertiesPreferencePage.UID3.MinValue"),
       
    99 				getFieldEditorParent()) {
       
   100 			protected boolean doCheckState() {
       
   101 				if (super.doCheckState()) {
       
   102 					if (iUID3MinValue.getIntValue() > iUID3MaxValue
       
   103 							.getIntValue()) {
       
   104 						setErrorMessage(Messages
       
   105 								.getString("TESTFWPropertiesPreferencePage.UID3.Error2"));
       
   106 						return false;
       
   107 					} else {
       
   108 						return true;
       
   109 					}
       
   110 				} else {
       
   111 					Messages.getString(
       
   112 							"TESTFWPropertiesPreferencePage.UIDHexError",
       
   113 							iUID3MinValue.getStringValue());
       
   114 					return false;
       
   115 				}
       
   116 			}
       
   117 		};
       
   118 
       
   119 		iAutherName = new StringFieldEditor(PreferenceConstants.AUTHER,
       
   120 				Messages.getString("TESTFWPropertiesPreferencePage.Author"),
       
   121 				getFieldEditorParent());
       
   122 
       
   123 		iTestFolderName.setEmptyStringAllowed(false);
       
   124 		iUID3MaxValue.setEmptyStringAllowed(false);
       
   125 		iUID3MinValue.setEmptyStringAllowed(false);
       
   126 		iAutherName.setEmptyStringAllowed(true);
       
   127 
       
   128 		addField(iTestFolderName);
       
   129 		addField(iUID3MaxValue);
       
   130 		addField(iUID3MinValue);
       
   131 		addField(iAutherName);
       
   132 	}
       
   133 
       
   134 	public boolean performOk() {
       
   135 		//CodegenUIPlugin.getDefault().savePluginPreferences();
       
   136 		return super.performOk();
       
   137 	}
       
   138 
       
   139 	public class HexFieldEditor extends StringFieldEditor {
       
   140 
       
   141 		private int minValidValue = 0;
       
   142 		private int maxValidValue = Integer.MAX_VALUE;
       
   143 		private static final int DEFAULT_TEXT_LIMIT = 10;
       
   144 
       
   145 		protected HexFieldEditor() {
       
   146 		}
       
   147 
       
   148 		public HexFieldEditor(String name, String labelText, Composite parent) {
       
   149 			this(name, labelText, parent, DEFAULT_TEXT_LIMIT);
       
   150 		}
       
   151 
       
   152 		public HexFieldEditor(String name, String labelText, Composite parent,
       
   153 				int textLimit) {
       
   154 			init(name, labelText);
       
   155 			setTextLimit(textLimit);
       
   156 			setEmptyStringAllowed(false);
       
   157 			setErrorMessage(Messages
       
   158 					.getString("TESTFWPropertiesPreferencePage.UIDHexError3"));
       
   159 			createControl(parent);
       
   160 			final Text text = getTextControl();
       
   161 			text.addFocusListener(new FocusAdapter() {
       
   162 				public void focusLost(FocusEvent event) {
       
   163 					String hexString = text.getText();
       
   164 					int number = -1;
       
   165 					try {
       
   166 						number = Integer.decode(hexString).intValue();
       
   167 						hexString = PreferenceUtil
       
   168 								.createCanonicalHexString(number);
       
   169 						text.setText(hexString);
       
   170 					} catch (NumberFormatException _ex) {
       
   171 					}
       
   172 				}
       
   173 			});
       
   174 		}
       
   175 
       
   176 		public void setValidRange(int min, int max) {
       
   177 			minValidValue = min;
       
   178 			maxValidValue = max;
       
   179 			setErrorMessage(Messages.getString(
       
   180 					"TESTFWPropertiesPreferencePage.UIDHexError2",
       
   181 					new Object[] { Integer.toHexString(min),
       
   182 							Integer.toHexString(max) }));
       
   183 		}
       
   184 
       
   185 		protected boolean checkState() {
       
   186 			String hexString;
       
   187 			Text text = getTextControl();
       
   188 			if (text == null)
       
   189 				return false;
       
   190 			hexString = text.getText();
       
   191 			int number = -1;
       
   192 			try {
       
   193 				number = Integer.decode(hexString).intValue();
       
   194 			} catch (NumberFormatException _ex) {
       
   195 				showErrorMessage();
       
   196 				return false;
       
   197 			}
       
   198 			if (number < minValidValue || number > maxValidValue) {
       
   199 				showErrorMessage();
       
   200 				return false;
       
   201 			}
       
   202 			clearErrorMessage();
       
   203 			return true;
       
   204 		}
       
   205 
       
   206 		protected void doLoad() {
       
   207 			Text text = getTextControl();
       
   208 			if (text != null) {
       
   209 				String strHex = getPreferenceStore().getString(
       
   210 						getPreferenceName());
       
   211 				text.setText(strHex);
       
   212 				oldValue = strHex;
       
   213 			}
       
   214 		}
       
   215 
       
   216 		protected void doLoadDefault() {
       
   217 			Text text = getTextControl();
       
   218 			if (text != null) {
       
   219 				String strHex = getPreferenceStore().getString(
       
   220 						getPreferenceName());
       
   221 				text.setText(strHex);
       
   222 			}
       
   223 			valueChanged();
       
   224 		}
       
   225 
       
   226 		protected void doStore() {
       
   227 			Text text = getTextControl();
       
   228 			if (text != null) {
       
   229 				getPreferenceStore().setValue(getPreferenceName(),
       
   230 						text.getText());
       
   231 			}
       
   232 		}
       
   233 
       
   234 		public int getIntValue() throws NumberFormatException {
       
   235 			return Integer.decode(getStringValue()).intValue();
       
   236 		}
       
   237 	}
       
   238 }