trace/tracebuilder/com.nokia.tracebuilder.view/src/com/nokia/tracebuilder/view/PropertyDialogUITemplate.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2  * Copyright (c) 2007-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 template label and combo box
       
    17  *
       
    18  */
       
    19 package com.nokia.tracebuilder.view;
       
    20 
       
    21 import java.util.List;
       
    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.engine.TraceObjectPropertyDialogTemplate;
       
    32 import com.nokia.tracebuilder.model.Trace;
       
    33 import com.nokia.tracebuilder.model.TraceObject;
       
    34 import com.nokia.tracebuilder.project.GroupNameHandlerBase;
       
    35 
       
    36 /**
       
    37  * Composite for template label and combo box
       
    38  * 
       
    39  */
       
    40 final class PropertyDialogUITemplate extends PropertyDialogComposite {
       
    41 
       
    42 	/**
       
    43 	 * Number of visible templates when opening the combo box
       
    44 	 */
       
    45 	private static final int VISIBLE_TEMPLATES_COUNT = 8; // CodForChk_Dis_Magic
       
    46 
       
    47 	/**
       
    48 	 * Template label
       
    49 	 */
       
    50 	private Label templateLabel;
       
    51 
       
    52 	/**
       
    53 	 * Template selector
       
    54 	 */
       
    55 	private Combo templateCombo;
       
    56 
       
    57 	/**
       
    58 	 * List of templates
       
    59 	 */
       
    60 	private List<TraceObjectPropertyDialogTemplate> templates;
       
    61 
       
    62 	/**
       
    63 	 * Constructor
       
    64 	 * 
       
    65 	 * @param parent
       
    66 	 *            the parent composite
       
    67 	 * @param uiType
       
    68 	 *            the UI type
       
    69 	 * @param templateListener
       
    70 	 *            listener for template changes
       
    71 	 * @param templates
       
    72 	 *            the list of templates
       
    73 	 */
       
    74 	PropertyDialogUITemplate(Composite parent, int uiType,
       
    75 			SelectionListener templateListener,
       
    76 			List<TraceObjectPropertyDialogTemplate> templates) {
       
    77 		super(parent);
       
    78 		this.templates = templates;
       
    79 		create(uiType, templateListener);
       
    80 	}
       
    81 
       
    82 	/**
       
    83 	 * Creates the template combo box
       
    84 	 * 
       
    85 	 * @param uiType
       
    86 	 *            type of UI
       
    87 	 * @param templateListener
       
    88 	 *            listener for template changes
       
    89 	 */
       
    90 	private void create(int uiType, SelectionListener templateListener) {
       
    91 		templateLabel = new Label(getParent(), SWT.NONE);
       
    92 		templateLabel.setText(Messages
       
    93 				.getString("PropertyDialogUI.TemplatesLabel")); //$NON-NLS-1$
       
    94 		templateCombo = new Combo(getParent(), SWT.READ_ONLY);
       
    95 		templateCombo.add(Messages.getString("PropertyDialogUI.NoTemplate")); //$NON-NLS-1$
       
    96 		templateCombo.select(0);
       
    97 
       
    98 		// If selected trace is Performance trace and UI type is "Add parameter"
       
    99 		// no templates can be selected
       
   100 		if (templates != null
       
   101 				&& !(uiType == TraceObjectPropertyDialog.ADD_PARAMETER && isPerformaceTraceSelected())) {
       
   102 			for (int i = 0; i < templates.size(); i++) {
       
   103 				templateCombo.add(templates.get(i).getTitle());
       
   104 			}
       
   105 		}
       
   106 		templateCombo.addSelectionListener(templateListener);
       
   107 		templateCombo.setVisibleItemCount(VISIBLE_TEMPLATES_COUNT);
       
   108 		setFieldButtonLayoutData(templateCombo, null);
       
   109 	}
       
   110 
       
   111 	/**
       
   112 	 * Gets the index of the selected template. Note that index 0 means "No
       
   113 	 * template" and actual templates start from index 1
       
   114 	 * 
       
   115 	 * @return the index
       
   116 	 */
       
   117 	int getTemplateIndex() {
       
   118 		return templateCombo.getSelectionIndex();
       
   119 	}
       
   120 
       
   121 	/**
       
   122 	 * Gets the template at given index. Note that index 0 means "No template"
       
   123 	 * and actual templates start from index 1
       
   124 	 * 
       
   125 	 * @param index
       
   126 	 *            the index
       
   127 	 * @return the template
       
   128 	 */
       
   129 	TraceObjectPropertyDialogTemplate getTemplateAt(int index) {
       
   130 		return templates.get(index - 1);
       
   131 	}
       
   132 
       
   133 	/**
       
   134 	 * Selects the given template. Retuns the index to the selected template.
       
   135 	 * Note that index 0 means "No template" and actual templates start from
       
   136 	 * index 1
       
   137 	 * 
       
   138 	 * @param template
       
   139 	 *            the template to be selected
       
   140 	 * @return index of the selected template
       
   141 	 */
       
   142 	int selectTemplate(TraceObjectPropertyDialogTemplate template) {
       
   143 		int retval = -1;
       
   144 		if (template != null && templates != null) {
       
   145 			for (int i = 0; i < templates.size(); i++) {
       
   146 				if (templates.get(i) == template) {
       
   147 					templateCombo.select(i + 1);
       
   148 					retval = i + 1;
       
   149 					i = templates.size();
       
   150 				}
       
   151 			}
       
   152 		}
       
   153 		return retval;
       
   154 	}
       
   155 
       
   156 	/**
       
   157 	 * Check is Performance Event trace selected
       
   158 	 * 
       
   159 	 * @return true if Performance Event Trace is selected, otherwise false
       
   160 	 */
       
   161 	private boolean isPerformaceTraceSelected() {
       
   162 		boolean retval = false;
       
   163 
       
   164 		TraceObject selectedObject = TraceBuilderGlobals.getTraceBuilder()
       
   165 				.getSelectedObject();
       
   166 
       
   167 		if (selectedObject instanceof Trace) {
       
   168 			Trace trace = (Trace) selectedObject;
       
   169 			GroupNameHandlerBase groupNameHandler = TraceBuilderGlobals.getGroupNameHandler();
       
   170 			if (trace.getGroup().getName().equals(
       
   171 					groupNameHandler.getDefaultGroups()[groupNameHandler.getPerformanceGroupIdIndex()])) {
       
   172 				retval = true;
       
   173 			}
       
   174 		}
       
   175 		return retval;
       
   176 	}
       
   177 }