trace/tracebuilder/com.nokia.tracebuilder.view/src/com/nokia/tracebuilder/view/TemplateComboSelectionListener.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     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  * UI for property dialogs
       
    17  *
       
    18  */
       
    19 package com.nokia.tracebuilder.view;
       
    20 
       
    21 import org.eclipse.swt.events.SelectionEvent;
       
    22 import org.eclipse.swt.events.SelectionListener;
       
    23 import org.eclipse.swt.widgets.Combo;
       
    24 
       
    25 import com.nokia.tracebuilder.engine.TraceBuilderGlobals;
       
    26 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialog;
       
    27 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialogTemplate;
       
    28 import com.nokia.tracebuilder.engine.TraceParameterPropertyDialogTemplate;
       
    29 import com.nokia.tracebuilder.engine.TracePropertyDialogTemplate;
       
    30 import com.nokia.tracebuilder.engine.rules.PerformanceEventTemplate;
       
    31 import com.nokia.tracebuilder.engine.utils.TraceUtils;
       
    32 import com.nokia.tracebuilder.model.TraceModel;
       
    33 import com.nokia.tracebuilder.model.TraceObjectUtils;
       
    34 import com.nokia.tracebuilder.source.SourceContext;
       
    35 
       
    36 /**
       
    37  * Selection listener for template combo box
       
    38  * 
       
    39  */
       
    40 final class TemplateComboSelectionListener implements SelectionListener {
       
    41 
       
    42 	/**
       
    43 	 * Dialog UI
       
    44 	 */
       
    45 	private final PropertyDialogUI dialogUI;
       
    46 
       
    47 	/**
       
    48 	 * Temporary storage for name
       
    49 	 */
       
    50 	private String tmpName;
       
    51 
       
    52 	/**
       
    53 	 * Temporary storage for value
       
    54 	 */
       
    55 	private String tmpValue;
       
    56 
       
    57 	/**
       
    58 	 * Temporary storage for target object
       
    59 	 */
       
    60 	private String tmpTarget;
       
    61 
       
    62 	/**
       
    63 	 * Temporary storage for type
       
    64 	 */
       
    65 	private String tmpType;
       
    66 
       
    67 	/**
       
    68 	 * Current selection
       
    69 	 */
       
    70 	private int selection = -1;
       
    71 
       
    72 	/**
       
    73 	 * Constructor
       
    74 	 * 
       
    75 	 * @param dialogUI
       
    76 	 *            the dialog UI
       
    77 	 */
       
    78 	TemplateComboSelectionListener(PropertyDialogUI dialogUI) {
       
    79 		this.dialogUI = dialogUI;
       
    80 	}
       
    81 
       
    82 	/*
       
    83 	 * (non-Javadoc)
       
    84 	 * 
       
    85 	 * @see org.eclipse.swt.events.SelectionListener#
       
    86 	 *      widgetSelected(org.eclipse.swt.events.SelectionEvent)
       
    87 	 */
       
    88 	public void widgetSelected(SelectionEvent e) {
       
    89 		templateSelected(((Combo) e.widget).getSelectionIndex());
       
    90 	}
       
    91 
       
    92 	/*
       
    93 	 * (non-Javadoc)
       
    94 	 * 
       
    95 	 * @see org.eclipse.swt.events.SelectionListener#
       
    96 	 *      widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
       
    97 	 */
       
    98 	public void widgetDefaultSelected(SelectionEvent e) {
       
    99 		widgetSelected(e);
       
   100 	}
       
   101 
       
   102 	/**
       
   103 	 * Selection event handler
       
   104 	 * 
       
   105 	 * @param newSelection
       
   106 	 *            the selected template index
       
   107 	 */
       
   108 	void templateSelected(int newSelection) {
       
   109 		int oldSelection = selection;
       
   110 		selection = newSelection;
       
   111 		if (selection != oldSelection) {
       
   112 			// User-defined values are stored
       
   113 			if (oldSelection == 0) {
       
   114 				storeSelection();
       
   115 			}
       
   116 			if (selection == 0) {
       
   117 				int dialogType = dialogUI.getUiType();
       
   118 				if (dialogType == TraceObjectPropertyDialog.ADD_TRACE) {
       
   119 					dialogUI.valueComposite.setLabel(Messages
       
   120 							.getString("PropertyDialogUI.AddTraceValueLabel")); //$NON-NLS-1$
       
   121 					dialogUI.valueComposite.getValueLabel().pack();
       
   122 				}
       
   123 				if (oldSelection >= 0) {
       
   124 					loadSelection();
       
   125 					dialogUI.notifyTemplateChanged(null);
       
   126 				}
       
   127 			} else {
       
   128 				TraceObjectPropertyDialogTemplate template = setFromTemplate();
       
   129 				dialogUI.notifyTemplateChanged(template);
       
   130 			}
       
   131 		} else {
       
   132 			// If same template is 'selected' the values are reset
       
   133 			if (selection > 0) {
       
   134 				setFromTemplate();
       
   135 			}
       
   136 		}
       
   137 	}
       
   138 
       
   139 	/**
       
   140 	 * Sets the values from selected template into the dialog
       
   141 	 * 
       
   142 	 * @return the selected template
       
   143 	 */
       
   144 	private TraceObjectPropertyDialogTemplate setFromTemplate() {
       
   145 		TraceObjectPropertyDialogTemplate template = dialogUI
       
   146 				.getTemplate(selection);
       
   147 		String name = template.getName();
       
   148 		if (name != null) {
       
   149 			dialogUI.setNameField(name);
       
   150 		}
       
   151 		int dialogType = dialogUI.getUiType();
       
   152 		if (dialogType == TraceObjectPropertyDialog.ADD_TRACE) {
       
   153 			if (template instanceof PerformanceEventTemplate) {
       
   154 				dialogUI.valueComposite.setLabel(Messages
       
   155 						.getString("PropertyDialogUI.AddTraceEventNameLabel")); //$NON-NLS-1$
       
   156 			} else {
       
   157 				dialogUI.valueComposite.setLabel(Messages
       
   158 						.getString("PropertyDialogUI.AddTraceValueLabel")); //$NON-NLS-1$
       
   159 			}
       
   160 			dialogUI.valueComposite.getValueLabel().pack();
       
   161 		}
       
   162 		if (template instanceof TraceParameterPropertyDialogTemplate) {
       
   163 			dialogUI.setType(((TraceParameterPropertyDialogTemplate) template)
       
   164 					.getType());
       
   165 		}
       
   166 		if (template instanceof TracePropertyDialogTemplate) {
       
   167 			// If the template is a trace property template, the target group
       
   168 			// name is passed to the template. The trace name is adjusted based
       
   169 			// on the target group
       
   170 			String groupName = ((TracePropertyDialogTemplate) template)
       
   171 					.getGroupName();
       
   172 			if (groupName == null) {
       
   173 				groupName = dialogUI.getTarget();
       
   174 			} else {
       
   175 				dialogUI.setTarget(groupName);
       
   176 			}
       
   177 			if (groupName != null) {
       
   178 				dialogUI.setValueField(((TracePropertyDialogTemplate) template)
       
   179 						.getText(groupName));
       
   180 			} else {
       
   181 				dialogUI.setValueField(template.getValue());
       
   182 			}
       
   183 		} else {
       
   184 			String value = template.getValue();
       
   185 			if (value != null) {
       
   186 				dialogUI.setValueField(value);
       
   187 			}
       
   188 		}
       
   189 		return template;
       
   190 	}
       
   191 
       
   192 	/**
       
   193 	 * Sets the values from the stored selection into the dialog
       
   194 	 */
       
   195 	private void loadSelection() {
       
   196 		String name = null;
       
   197 		String value = null;
       
   198 
       
   199 		// If tmpName or tmpValue is null and context is valid, the name and
       
   200 		// value proposal are based on context
       
   201 		if ((tmpName == null || tmpName == "") //$NON-NLS-1$
       
   202 				|| (tmpValue == null || tmpValue == "")) { //$NON-NLS-1$
       
   203 			SourceContext context = TraceBuilderGlobals
       
   204 					.getSourceContextManager().getContext();
       
   205 			if (context != null) {
       
   206 				String cname = context.getClassName();
       
   207 				String fname = context.getFunctionName();
       
   208 				name = TraceUtils.formatTrace(
       
   209 						TraceUtils.getDefaultNameFormat(), cname, fname);
       
   210 				TraceModel model = TraceBuilderGlobals.getTraceModel();
       
   211 				name = TraceObjectUtils.modifyDuplicateTraceName(model,
       
   212 						TraceUtils.convertName(name)).getData();
       
   213 				value = TraceUtils.formatTrace(TraceUtils
       
   214 						.getDefaultTraceFormat(), cname, fname);
       
   215 			}
       
   216 		}
       
   217 
       
   218 		if (tmpName != null && tmpName != "") { //$NON-NLS-1$
       
   219 			dialogUI.setNameField(tmpName);
       
   220 		} else {
       
   221 			if (name != null) {
       
   222 				dialogUI.setNameField(name);
       
   223 			} else {
       
   224 				dialogUI.setNameField(""); //$NON-NLS-1$
       
   225 			}
       
   226 		}
       
   227 		if (tmpValue != null && tmpValue != "") { //$NON-NLS-1$
       
   228 			dialogUI.setValueField(tmpValue);
       
   229 		} else {
       
   230 			if (value != null) {
       
   231 				dialogUI.setValueField(value);
       
   232 			} else {
       
   233 				dialogUI.setValueField(""); //$NON-NLS-1$
       
   234 			}
       
   235 		}
       
   236 		if (tmpType != null) {
       
   237 			dialogUI.setType(tmpType);
       
   238 		} else {
       
   239 			dialogUI.setType(""); //$NON-NLS-1$
       
   240 		}
       
   241 		if (tmpTarget != null) {
       
   242 			dialogUI.setTarget(tmpTarget);
       
   243 		} else {
       
   244 			dialogUI.setTarget(""); //$NON-NLS-1$
       
   245 		}
       
   246 	}
       
   247 
       
   248 	/**
       
   249 	 * Stores the current dialog values
       
   250 	 */
       
   251 	private void storeSelection() {
       
   252 		tmpTarget = dialogUI.getTarget();
       
   253 		tmpName = dialogUI.getNameField();
       
   254 		tmpValue = dialogUI.getValueField();
       
   255 		tmpType = dialogUI.getType();
       
   256 	}
       
   257 
       
   258 }