trace/tracebuilder/com.nokia.tracebuilder.view/src/com/nokia/tracebuilder/view/PropertyDialogUIType.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2  * Copyright (c) 2009-2010 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 parameter type label, combo box and new type button
       
    17  *
       
    18  */
       
    19 package com.nokia.tracebuilder.view;
       
    20 
       
    21 import java.util.Iterator;
       
    22 
       
    23 import org.eclipse.swt.SWT;
       
    24 import org.eclipse.swt.events.SelectionListener;
       
    25 import org.eclipse.swt.widgets.Combo;
       
    26 import org.eclipse.swt.widgets.Composite;
       
    27 import org.eclipse.swt.widgets.Label;
       
    28 
       
    29 import com.nokia.tracebuilder.engine.TraceBuilderGlobals;
       
    30 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialog;
       
    31 import com.nokia.tracebuilder.model.Trace;
       
    32 import com.nokia.tracebuilder.model.TraceConstantTable;
       
    33 import com.nokia.tracebuilder.model.TraceObject;
       
    34 import com.nokia.tracebuilder.project.GroupNameHandlerBase;
       
    35 
       
    36 /**
       
    37  * Composite for parameter type label, combo box and new type button
       
    38  * 
       
    39  */
       
    40 final class PropertyDialogUIType extends PropertyDialogComposite {
       
    41 
       
    42 	/**
       
    43 	 * Parameter type label
       
    44 	 */
       
    45 	private Label typeLabel = null;
       
    46 
       
    47 	/**
       
    48 	 * Parameter type selector
       
    49 	 */
       
    50 	private Combo typeCombo = null;
       
    51 
       
    52 	/**
       
    53 	 * Limited Trace visible type item count
       
    54 	 */
       
    55 	private final int limitedTraceVisibleTypeItemCnt = 2; // CodForChk_Dis_Magic
       
    56 
       
    57 	/**
       
    58 	 * Constructor
       
    59 	 * 
       
    60 	 * @param parent
       
    61 	 *            the parent composite
       
    62 	 * @param uiType
       
    63 	 *            the UI type
       
    64 	 * @param listener
       
    65 	 *            listener for type selection
       
    66 	 */
       
    67 	PropertyDialogUIType(Composite parent, int uiType,
       
    68 			SelectionListener listener) {
       
    69 		super(parent);
       
    70 		create(uiType, listener);
       
    71 	}
       
    72 
       
    73 	/**
       
    74 	 * Creates the parameter type combobox and associated label
       
    75 	 * 
       
    76 	 * @param uiType
       
    77 	 *            the type of UI
       
    78 	 * @param listener
       
    79 	 *            selection listener for combobox
       
    80 	 */
       
    81 	private void create(int uiType, SelectionListener listener) {
       
    82 		// Label is added to this composite
       
    83 		typeLabel = new Label(getParent(), SWT.NONE);
       
    84 		typeLabel.setText(getTypeLabel(uiType));
       
    85 		typeCombo = new Combo(getParent(), SWT.READ_ONLY);
       
    86 
       
    87 		// Adds parameter types to the selector
       
    88 		for (int i = 0; i < TraceViewMessages.PARAMETER_LABEL_MAP.length; i++) {
       
    89 			typeCombo.add(TraceViewMessages.PARAMETER_LABEL_MAP[i][1]);
       
    90 		}
       
    91 		// Adds constant table names after parameter types
       
    92 		Iterator<TraceConstantTable> tables = TraceBuilderGlobals
       
    93 				.getTraceModel().getConstantTables();
       
    94 		while (tables.hasNext()) {
       
    95 			typeCombo.add(tables.next().getName());
       
    96 		}
       
    97 
       
    98 		// If selected object is Performance or State trace, only Signed32 and
       
    99 		// Unsigned32 types are allowed -> First two types in
       
   100 		// TraceViewMessages.PARAMETER_LABEL_MAP. Also, disable the use of the
       
   101 		// "Array Parameter" checkbox
       
   102 		if (isPerformaceTraceSelected() || isStateTraceSelected()) {
       
   103 			typeCombo.remove(limitedTraceVisibleTypeItemCnt, typeCombo
       
   104 					.getItemCount() - 1);
       
   105 			typeCombo.setVisibleItemCount(limitedTraceVisibleTypeItemCnt);
       
   106 		} else {
       
   107 			typeCombo.setVisibleItemCount(typeCombo.getItemCount());
       
   108 		}
       
   109 
       
   110 		typeCombo.addSelectionListener(listener);
       
   111 		setFieldButtonLayoutData(typeCombo, null);
       
   112 	}
       
   113 
       
   114 	/**
       
   115 	 * Gets the label for type field
       
   116 	 * 
       
   117 	 * @param uiType
       
   118 	 *            the UI type
       
   119 	 * @return the label
       
   120 	 */
       
   121 	private String getTypeLabel(int uiType) {
       
   122 		String type;
       
   123 		switch (uiType) {
       
   124 		case TraceObjectPropertyDialog.ADD_PARAMETER:
       
   125 			type = Messages.getString("PropertyDialogUI.AddParameterTypeLabel"); //$NON-NLS-1$
       
   126 			break;
       
   127 		default:
       
   128 			type = ""; //$NON-NLS-1$
       
   129 			break;
       
   130 		}
       
   131 		return type;
       
   132 	}
       
   133 
       
   134 	/**
       
   135 	 * Check is Performance Event trace selected
       
   136 	 * 
       
   137 	 * @return true if Performance Event Trace is selected, otherwise false
       
   138 	 */
       
   139 	private boolean isPerformaceTraceSelected() {
       
   140 		boolean retval = false;
       
   141 
       
   142 		TraceObject selectedObject = TraceBuilderGlobals.getTraceBuilder()
       
   143 				.getSelectedObject();
       
   144 
       
   145 		if (selectedObject instanceof Trace) {
       
   146 			Trace trace = (Trace) selectedObject;
       
   147 			GroupNameHandlerBase groupNameHandler = TraceBuilderGlobals
       
   148 					.getGroupNameHandler();
       
   149 			if (trace.getGroup().getName().equals(
       
   150 					groupNameHandler.getDefaultGroups()[groupNameHandler
       
   151 							.getPerformanceGroupIdIndex()])) {
       
   152 				retval = true;
       
   153 			}
       
   154 		}
       
   155 		return retval;
       
   156 	}
       
   157 
       
   158 	/**
       
   159 	 * Check is State trace selected
       
   160 	 * 
       
   161 	 * @return true if State Trace is selected, otherwise false
       
   162 	 */
       
   163 	private boolean isStateTraceSelected() {
       
   164 		boolean retval = false;
       
   165 
       
   166 		TraceObject selectedObject = TraceBuilderGlobals.getTraceBuilder()
       
   167 				.getSelectedObject();
       
   168 
       
   169 		if (selectedObject instanceof Trace) {
       
   170 			Trace trace = (Trace) selectedObject;
       
   171 			GroupNameHandlerBase groupNameHandler = TraceBuilderGlobals
       
   172 					.getGroupNameHandler();
       
   173 			if (trace.getGroup().getName().equals(
       
   174 					groupNameHandler.getDefaultGroups()[groupNameHandler
       
   175 							.getStateGroupIdIndex()])) {
       
   176 				retval = true;
       
   177 			}
       
   178 		}
       
   179 		return retval;
       
   180 	}
       
   181 
       
   182 	/**
       
   183 	 * Gets the type selected from the combo box
       
   184 	 * 
       
   185 	 * @return the selected type
       
   186 	 */
       
   187 	String getSelectedType() {
       
   188 		String type;
       
   189 		int index = typeCombo.getSelectionIndex();
       
   190 		if (index >= TraceViewMessages.PARAMETER_LABEL_MAP.length) {
       
   191 			type = typeCombo.getText();
       
   192 		} else {
       
   193 			type = TraceViewMessages.PARAMETER_LABEL_MAP[index][0];
       
   194 		}
       
   195 		return type;
       
   196 	}
       
   197 
       
   198 	/**
       
   199 	 * Selects the type
       
   200 	 * 
       
   201 	 * @param type
       
   202 	 *            the type
       
   203 	 */
       
   204 	void setType(String type) {
       
   205 		boolean found = false;
       
   206 
       
   207 		// If selected object is Performance or State trace, only Signed32 and
       
   208 		// Unsigned32 types are allowed, so because it could be that previous
       
   209 		// type is not allowed, we always select Signed32 type as default in
       
   210 		// that case and previous type check is skipped.
       
   211 		if (!isPerformaceTraceSelected() && !isStateTraceSelected()) {
       
   212 			if (type != null && type.length() > 0) {
       
   213 				for (int i = 0; i < TraceViewMessages.PARAMETER_LABEL_MAP.length; i++) {
       
   214 					if (TraceViewMessages.PARAMETER_LABEL_MAP[i][0]
       
   215 							.equals(type)) {
       
   216 						typeCombo.select(i);
       
   217 						i = typeCombo.getItemCount();
       
   218 						found = true;
       
   219 						break;
       
   220 					}
       
   221 				}
       
   222 			}
       
   223 		}
       
   224 		if (!found) {
       
   225 			typeCombo.select(0);
       
   226 		}
       
   227 	}
       
   228 
       
   229 	/**
       
   230 	 * Enables / disables the type combo box
       
   231 	 * 
       
   232 	 * @param flag
       
   233 	 *            new enabled flag
       
   234 	 */
       
   235 	void setEnabled(boolean flag) {
       
   236 		typeCombo.setEnabled(flag);
       
   237 	}
       
   238 
       
   239 }