trace/tracebuilder/com.nokia.tracebuilder.view/src/com/nokia/tracebuilder/view/PropertyDialogUIID.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2 * Copyright (c) 2007 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 * Composite for ID label, field and hex checkbox
       
    17 *
       
    18 */
       
    19 package com.nokia.tracebuilder.view;
       
    20 
       
    21 import org.eclipse.swt.SWT;
       
    22 import org.eclipse.swt.events.ModifyEvent;
       
    23 import org.eclipse.swt.events.ModifyListener;
       
    24 import org.eclipse.swt.events.SelectionEvent;
       
    25 import org.eclipse.swt.events.SelectionListener;
       
    26 import org.eclipse.swt.widgets.Button;
       
    27 import org.eclipse.swt.widgets.Composite;
       
    28 import org.eclipse.swt.widgets.Label;
       
    29 import org.eclipse.swt.widgets.Text;
       
    30 
       
    31 import com.nokia.tracebuilder.engine.TraceBuilderGlobals;
       
    32 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialog;
       
    33 
       
    34 /**
       
    35  * Composite for ID label, field and hex checkbox
       
    36  * 
       
    37  */
       
    38 class PropertyDialogUIID extends PropertyDialogComposite {
       
    39 
       
    40 	/**
       
    41 	 * Configuration name for hex checkbox state
       
    42 	 */
       
    43 	private static final String HEX_CHECK_CONFIG = "PropertyDialogUIID.hexCheck"; //$NON-NLS-1$
       
    44 
       
    45 	/**
       
    46 	 * Hex checkbox listener
       
    47 	 * 
       
    48 	 */
       
    49 	private final class HexCheckSelectionListener implements SelectionListener {
       
    50 
       
    51 		/*
       
    52 		 * (non-Javadoc)
       
    53 		 * 
       
    54 		 * @see org.eclipse.swt.events.SelectionListener#
       
    55 		 *      widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
       
    56 		 */
       
    57 		public void widgetDefaultSelected(SelectionEvent e) {
       
    58 			widgetSelected(e);
       
    59 		}
       
    60 
       
    61 		/*
       
    62 		 * (non-Javadoc)
       
    63 		 * 
       
    64 		 * @see org.eclipse.swt.events.SelectionListener#
       
    65 		 *      widgetSelected(org.eclipse.swt.events.SelectionEvent)
       
    66 		 */
       
    67 		public void widgetSelected(SelectionEvent e) {
       
    68 			try {
       
    69 				TraceBuilderGlobals.getConfiguration().setFlag(
       
    70 						HEX_CHECK_CONFIG, hexCheck.getSelection());
       
    71 				// Converts the ID field to new format. The old value of the
       
    72 				// hex checkbox is used, since that represents the value
       
    73 				// before the checkbox was changed
       
    74 				setID(idFromString(idField.getText(), !hexCheck.getSelection()));
       
    75 			} catch (NumberFormatException ex) {
       
    76 			}
       
    77 		}
       
    78 	}
       
    79 
       
    80 	/**
       
    81 	 * Modification listener for the ID field
       
    82 	 * 
       
    83 	 */
       
    84 	private final class IDFieldModifyListener implements ModifyListener {
       
    85 
       
    86 		/*
       
    87 		 * (non-Javadoc)
       
    88 		 * 
       
    89 		 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
       
    90 		 */
       
    91 		public void modifyText(ModifyEvent e) {
       
    92 			// If the hex field is not checked, a hex character checks it
       
    93 			if (!hexCheck.getSelection()) {
       
    94 				String text = idField.getText();
       
    95 				if (hasHexPrefix(text) || hasHexChars(text)) {
       
    96 					hexCheck.setSelection(true);
       
    97 				}
       
    98 			}
       
    99 		}
       
   100 	}
       
   101 
       
   102 	/**
       
   103 	 * Radix for hex values
       
   104 	 */
       
   105 	private static final int HEX_RADIX = 16; // CodForChk_Dis_Magic
       
   106 
       
   107 	/**
       
   108 	 * ID label
       
   109 	 */
       
   110 	private Label idLabel;
       
   111 
       
   112 	/**
       
   113 	 * ID field is mapped to trace object ID.
       
   114 	 * 
       
   115 	 * @see com.nokia.tracebuilder.model.TraceObject#setID(int)
       
   116 	 */
       
   117 	private Text idField;
       
   118 
       
   119 	/**
       
   120 	 * Hex ID checkbox
       
   121 	 */
       
   122 	private Button hexCheck;
       
   123 
       
   124 	/**
       
   125 	 * Constructor
       
   126 	 * 
       
   127 	 * @param parent
       
   128 	 *            parent composite
       
   129 	 * @param uiType
       
   130 	 *            type of UI
       
   131 	 * @param modifyListener
       
   132 	 *            the modify listener for ID field
       
   133 	 */
       
   134 	PropertyDialogUIID(Composite parent, int uiType,
       
   135 			ModifyListener modifyListener) {
       
   136 		super(parent);
       
   137 		create(uiType, modifyListener);
       
   138 	}
       
   139 
       
   140 	/**
       
   141 	 * Creates the ID text field and associated label
       
   142 	 * 
       
   143 	 * @param uiType
       
   144 	 *            the UI type
       
   145 	 * @param modifyListener
       
   146 	 *            listener for text field changes
       
   147 	 */
       
   148 	private void create(int uiType, ModifyListener modifyListener) {
       
   149 		// Label is added to this composite
       
   150 		idLabel = new Label(getParent(), SWT.NONE);
       
   151 		idLabel.setText(getIDLabel(uiType));
       
   152 		// Field and button are added to sub-composite
       
   153 		Composite composite = createFieldButtonComposite();
       
   154 		idField = new Text(composite, SWT.BORDER);
       
   155 		idField.addModifyListener(modifyListener);
       
   156 		idField.addModifyListener(new IDFieldModifyListener());
       
   157 		hexCheck = new Button(composite, SWT.CHECK);
       
   158 		hexCheck.setSelection(TraceBuilderGlobals.getConfiguration().getFlag(
       
   159 				HEX_CHECK_CONFIG));
       
   160 		hexCheck.setText(Messages.getString("PropertyDialogUI.HexButtonTitle")); //$NON-NLS-1$
       
   161 		hexCheck.addSelectionListener(new HexCheckSelectionListener());
       
   162 		setFieldButtonLayoutData(idField, hexCheck);
       
   163 	}
       
   164 
       
   165 	/**
       
   166 	 * Gets the label for ID field
       
   167 	 * 
       
   168 	 * @param uiType
       
   169 	 *            the UI type
       
   170 	 * @return the label
       
   171 	 */
       
   172 	private String getIDLabel(int uiType) {
       
   173 		String id;
       
   174 		switch (uiType) {
       
   175 		case TraceObjectPropertyDialog.ADD_CONSTANT:
       
   176 			id = Messages.getString("PropertyDialogUI.AddConstantIDLabel"); //$NON-NLS-1$
       
   177 			break;
       
   178 		case TraceObjectPropertyDialog.EDIT_CONSTANT:
       
   179 			id = Messages.getString("PropertyDialogUI.EditConstantIDLabel"); //$NON-NLS-1$
       
   180 			break;
       
   181 		default:
       
   182 			id = ""; //$NON-NLS-1$
       
   183 			break;
       
   184 		}
       
   185 		return id;
       
   186 	}
       
   187 
       
   188 	/**
       
   189 	 * Checks if given string has hex characters
       
   190 	 * 
       
   191 	 * @param text
       
   192 	 *            the text
       
   193 	 * @return true if there are hex characters
       
   194 	 */
       
   195 	private boolean hasHexChars(String text) {
       
   196 		boolean isHex = false;
       
   197 		for (int i = 0; i < text.length() && !isHex; i++) {
       
   198 			char c = text.charAt(i);
       
   199 			if (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e'
       
   200 					|| c == 'f' || c == 'A' || c == 'B' || c == 'C' || c == 'D'
       
   201 					|| c == 'E' || c == 'F') {
       
   202 				isHex = true;
       
   203 			}
       
   204 		}
       
   205 		return isHex;
       
   206 	}
       
   207 
       
   208 	/**
       
   209 	 * Checks if the text has hex prefix
       
   210 	 * 
       
   211 	 * @param text
       
   212 	 *            the text
       
   213 	 * @return true if there is hex prefix
       
   214 	 */
       
   215 	private boolean hasHexPrefix(String text) {
       
   216 		boolean isHex = false;
       
   217 		String pref = Messages.getString("PropertyDialogUI.HexPrefix"); //$NON-NLS-1$
       
   218 		if (text.length() > pref.length()) {
       
   219 			if (text.substring(0, pref.length()).equalsIgnoreCase(pref)) {
       
   220 				isHex = true;
       
   221 			}
       
   222 		}
       
   223 		return isHex;
       
   224 	}
       
   225 
       
   226 	/**
       
   227 	 * Tries to convert an ID string to number
       
   228 	 * 
       
   229 	 * @param text
       
   230 	 *            the ID string
       
   231 	 * @param isHex
       
   232 	 *            true if interpreted as hex
       
   233 	 * @return the ID
       
   234 	 * @throws NumberFormatException
       
   235 	 *             if ID is not valid
       
   236 	 */
       
   237 	int idFromString(String text, boolean isHex) throws NumberFormatException {
       
   238 		if (!isHex) {
       
   239 			// If hex checkbox is not selected, the content is still checked
       
   240 			// If there is hex prefix or hex characters, it is interpreted as
       
   241 			// hex
       
   242 			isHex = hasHexPrefix(text);
       
   243 			if (!isHex) {
       
   244 				isHex = hasHexChars(text);
       
   245 			} else {
       
   246 				text = text.substring(Messages.getString(
       
   247 						"PropertyDialogUI.HexPrefix").length()); //$NON-NLS-1$
       
   248 			}
       
   249 		} else {
       
   250 			// If checkbox is selected, the prefix needs to be removed if
       
   251 			// it exists
       
   252 			if (hasHexPrefix(text)) {
       
   253 				text = text.substring(Messages.getString(
       
   254 						"PropertyDialogUI.HexPrefix").length()); //$NON-NLS-1$
       
   255 			}
       
   256 		}
       
   257 		int id;
       
   258 		if (isHex) {
       
   259 			id = Long.valueOf(text, HEX_RADIX).intValue();
       
   260 		} else {
       
   261 			id = Integer.valueOf(text).intValue();
       
   262 		}
       
   263 		return id;
       
   264 	}
       
   265 
       
   266 	/**
       
   267 	 * Sets the ID field value
       
   268 	 * 
       
   269 	 * @param id
       
   270 	 *            the new ID value
       
   271 	 */
       
   272 	void setID(int id) {
       
   273 		if (hexCheck.getSelection()) {
       
   274 			idField.setText(Messages.getString("PropertyDialogUI.HexPrefix") //$NON-NLS-1$ 
       
   275 					+ Integer.toHexString(id));
       
   276 		} else {
       
   277 			idField.setText(Integer.toString(id));
       
   278 		}
       
   279 	}
       
   280 
       
   281 	/**
       
   282 	 * Returns the value from the ID field. Throws NumberFormatException if the
       
   283 	 * ID field does not contain a number
       
   284 	 * 
       
   285 	 * @return id field value
       
   286 	 * @throws NumberFormatException
       
   287 	 *             if the ID field does not contain valid data
       
   288 	 */
       
   289 	int getID() throws NumberFormatException {
       
   290 		return idFromString(idField.getText(), hexCheck.getSelection());
       
   291 	}
       
   292 
       
   293 	/**
       
   294 	 * Enables or disables the ID field and hex button
       
   295 	 * 
       
   296 	 * @param flag
       
   297 	 *            the enabled flag
       
   298 	 */
       
   299 	void setEnabled(boolean flag) {
       
   300 		idField.setEnabled(flag);
       
   301 		hexCheck.setEnabled(flag);
       
   302 	}
       
   303 
       
   304 }