trace/traceanalyser/com.nokia.s60tools.traceanalyser.timingrule/src/com/nokia/s60tools/traceanalyser/timingrule/rule/TimingRuleType.java
changeset 9 14dc2103a631
equal deleted inserted replaced
8:15296fd0af4a 9:14dc2103a631
       
     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 
       
    19 package com.nokia.s60tools.traceanalyser.timingrule.rule;
       
    20 
       
    21 import java.io.File;
       
    22 import java.util.ArrayList;
       
    23 
       
    24 import org.eclipse.core.runtime.IPath;
       
    25 import org.eclipse.core.runtime.Platform;
       
    26 import org.eclipse.swt.SWT;
       
    27 import org.eclipse.swt.events.ModifyEvent;
       
    28 import org.eclipse.swt.events.ModifyListener;
       
    29 import org.eclipse.swt.layout.GridData;
       
    30 import org.eclipse.swt.layout.GridLayout;
       
    31 import org.eclipse.swt.widgets.Composite;
       
    32 import org.eclipse.swt.widgets.Event;
       
    33 import org.eclipse.swt.widgets.Label;
       
    34 import org.eclipse.swt.widgets.Listener;
       
    35 import org.eclipse.swt.widgets.Text;
       
    36 
       
    37 import com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRuleType;
       
    38 import com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRuleTypeListener;
       
    39 import com.nokia.s60tools.traceanalyser.export.ITraceSelectionCompositeListener;
       
    40 import com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule;
       
    41 import com.nokia.s60tools.traceanalyser.export.TraceInfo;
       
    42 import com.nokia.s60tools.traceanalyser.export.TraceSelectionComposite;
       
    43 import com.nokia.s60tools.traceanalyser.timingrule.plugin.TraceAnalyserTimingRulePlugin;
       
    44 import com.nokia.traceviewer.engine.TraceInformation;
       
    45 
       
    46 /**
       
    47  * class Timing Rule.
       
    48  * "Parameter Rule"-rule type for Trace Analyser
       
    49  */
       
    50 
       
    51 public class TimingRuleType implements ITraceAnalyserRuleType, ITraceSelectionCompositeListener, ModifyListener{
       
    52 
       
    53 	/* UI components */
       
    54 	private GridData gridDataComposite;
       
    55 	private Composite compositeTimingRule;
       
    56 	private Text textLimitTraceA;
       
    57 	private Text textLimitTraceB;
       
    58 		
       
    59 	/* Trace Selection composites */
       
    60 	TraceSelectionComposite traceSelectionCompositeA;
       
    61 	TraceSelectionComposite traceSelectionCompositeB;
       
    62 	
       
    63 	/* Edit rule-window */
       
    64 	ITraceAnalyserRuleTypeListener listener;
       
    65 	
       
    66 
       
    67 	
       
    68 	/**
       
    69 	 * TimingRule.
       
    70 	 * constructor
       
    71 	 */
       
    72 	public TimingRuleType(){
       
    73 	}
       
    74 	
       
    75 	/*
       
    76 	 * (non-Javadoc)
       
    77 	 * @see com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRule#createUIComponents(org.eclipse.swt.widgets.Composite)
       
    78 	 */
       
    79 	public void createUIComponents(Composite composite, ITraceAnalyserRuleTypeListener listener) {
       
    80 	
       
    81 		this.listener = listener;
       
    82 		
       
    83 		// Create composite for rule
       
    84 		compositeTimingRule = new Composite(composite, SWT.NONE);
       
    85  		GridLayout layoutTraceA = new GridLayout();
       
    86  		layoutTraceA.numColumns = 1;
       
    87  		compositeTimingRule.setLayout(layoutTraceA);
       
    88  		gridDataComposite = new GridData(GridData.FILL_HORIZONTAL);
       
    89  		compositeTimingRule.setLayoutData(gridDataComposite);
       
    90  		
       
    91  		// create trace selection components.
       
    92 		traceSelectionCompositeA = new TraceSelectionComposite(compositeTimingRule, "Trace A:", this);
       
    93 		traceSelectionCompositeB = new TraceSelectionComposite(compositeTimingRule, "Trace B:", this);
       
    94 
       
    95 		// create composite for time limits
       
    96 		Composite compositeTimeLimits = new Composite(compositeTimingRule, SWT.NONE);
       
    97  		GridLayout layoutTimeLimitA = new GridLayout();
       
    98  		layoutTimeLimitA.numColumns = 2;
       
    99  		compositeTimeLimits.setLayout(layoutTimeLimitA);
       
   100  		GridData gridDataTimeLimits = new GridData(GridData.FILL_HORIZONTAL);
       
   101  		compositeTimeLimits.setLayoutData(gridDataTimeLimits);
       
   102  		
       
   103  		// create time limit components.
       
   104  		textLimitTraceA = this.createTimeLimitControls(compositeTimeLimits, "Time Limit A(Trace B must arrive after this):");
       
   105  		textLimitTraceB = this.createTimeLimitControls(compositeTimeLimits, "Time Limit B(Trace B must arrive before this):");
       
   106 
       
   107 		
       
   108 	}
       
   109 	
       
   110 	/**
       
   111 	 * createTimeLimitControls.
       
   112 	 * Method that creates time limit related ui-components.
       
   113 	 * @param composite composite where components are placed
       
   114 	 * @param labelText text of the label
       
   115 	 * @return textBox where time limit is written.
       
   116 	 */
       
   117 	private Text createTimeLimitControls(Composite composite, String labelText){
       
   118 		
       
   119 		// create composite for trace limit components
       
   120 		Composite compositeTimeLimits = new Composite(composite, SWT.NONE);
       
   121  		GridLayout layoutTimeLimitA = new GridLayout();
       
   122  		layoutTimeLimitA.numColumns = 2;
       
   123  		compositeTimeLimits.setLayout(layoutTimeLimitA);
       
   124  		GridData gridDataTimeLimits = new GridData(GridData.FILL_HORIZONTAL);
       
   125  		compositeTimeLimits.setLayoutData(gridDataTimeLimits);
       
   126  		
       
   127 		// Create time limit label
       
   128 		Label labelTimeLimit = new Label(compositeTimeLimits, SWT.NONE);
       
   129 		labelTimeLimit.setText(labelText);
       
   130 		GridData gridDataLabelTimeLimit = new GridData(GridData.BEGINNING);
       
   131 		gridDataLabelTimeLimit.horizontalSpan = 2;
       
   132 		labelTimeLimit.setLayoutData(gridDataLabelTimeLimit);
       
   133 		
       
   134 		// Create time limit text box.
       
   135 		Text textBox = new Text(compositeTimeLimits, SWT.BORDER);
       
   136 		GridData gridDataTextBox = new GridData(GridData.BEGINNING);
       
   137 		gridDataTextBox.widthHint = 30;
       
   138 		textBox.setTextLimit(5);
       
   139 		textBox.setToolTipText("Define time limit for trace.");
       
   140 		textBox.setLayoutData(gridDataTextBox);
       
   141 		textBox.setText("0");
       
   142 		
       
   143 		// Create ms-label
       
   144 		Label labelMS = new Label(compositeTimeLimits, SWT.NONE);
       
   145 		labelMS.setText("ms");
       
   146 		
       
   147 		// Set text box to accept only numbers.
       
   148 		textBox.addListener(SWT.Verify, new Listener() {
       
   149 			public void handleEvent(Event event) {
       
   150 				String string = event.text;
       
   151 				char[] chars = new char[string.length()];
       
   152 				string.getChars(0, chars.length, chars, 0);
       
   153 				for (int i = 0; i < chars.length; i++) {
       
   154 					if (!('0' <= chars[i] && chars[i] <= '9')) {
       
   155 						event.doit = false;
       
   156 						return;
       
   157 					}
       
   158 				}
       
   159 			}
       
   160 
       
   161 		});
       
   162 		
       
   163 		textBox.addModifyListener(this);
       
   164 		
       
   165 		return textBox;
       
   166 		
       
   167 	}
       
   168 	
       
   169 	/*
       
   170 	 * (non-Javadoc)
       
   171 	 * @see com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRule#setVisible(boolean)
       
   172 	 */
       
   173 	public void setVisible(boolean value){
       
   174 		compositeTimingRule.setVisible(value);
       
   175 		gridDataComposite.exclude = !value;
       
   176 	}
       
   177 	
       
   178 	/*
       
   179 	 * (non-Javadoc)
       
   180 	 * @see com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRuleType#saveRule(java.lang.String, java.lang.String)
       
   181 	 */
       
   182 	public TraceAnalyserRule getRule(String name, String description){
       
   183 		
       
   184 		// Create new timing rule.
       
   185 		TimingRule newRule = new TimingRule();
       
   186 		
       
   187 		// set basic info.
       
   188 		newRule.setName(name);
       
   189 		newRule.setDescription(description);
       
   190 		
       
   191 		
       
   192 		// set directory for rule.
       
   193 		newRule.setWorkingDirectory(getPluginWorkingLocation());
       
   194 		
       
   195 		// set trace items for rule.
       
   196 		newRule.setTraceItemA(traceSelectionCompositeA.getTraceInformation());
       
   197 		newRule.setTraceItemB(traceSelectionCompositeB.getTraceInformation());
       
   198 		
       
   199 		
       
   200 		// set time limits for rule.
       
   201 		
       
   202 		// if empty, set to zero.
       
   203 		if(textLimitTraceA.getText() == ""){
       
   204 			textLimitTraceA.setText("0");
       
   205 		}
       
   206 		if(textLimitTraceB.getText() == ""){
       
   207 			textLimitTraceB.setText("0");
       
   208 		}
       
   209 		
       
   210 		int limitA = 0;
       
   211 		int limitB = 0;
       
   212 		try{
       
   213 			limitA = Integer.parseInt(textLimitTraceA.getText());
       
   214 			limitB = Integer.parseInt(textLimitTraceB.getText());
       
   215 		}
       
   216 		catch (Exception e) {
       
   217 			e.printStackTrace();
       
   218 		}
       
   219 		newRule.setTimeLimitA(limitA);
       
   220 		newRule.setTimeLimitB(limitB);
       
   221 
       
   222 		// write rule into xml-file.
       
   223 	//	if(newRule.writeXML()){
       
   224 			return newRule;
       
   225 		//}
       
   226 		//else{
       
   227 			//return null;
       
   228 	//	}
       
   229 	}
       
   230 	
       
   231 		
       
   232 	/**
       
   233 	 * getPluginWorkingLocation.
       
   234 	 * Returns a path where Rule plugin can do various tasks (located under workspace).
       
   235 	 */	
       
   236 	public static String getPluginWorkingLocation() {
       
   237 		IPath location = Platform.getStateLocation( TraceAnalyserTimingRulePlugin.getDefault().getBundle());
       
   238 		return location.toOSString();		
       
   239 	}
       
   240 
       
   241 	/*
       
   242 	 * (non-Javadoc)
       
   243 	 * @see com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRuleType#getRules()
       
   244 	 */
       
   245 	public ArrayList<TraceAnalyserRule> getRules(){
       
   246 		
       
   247 		ArrayList<TraceAnalyserRule> rules = new ArrayList<TraceAnalyserRule>();
       
   248 		File rootFolder = new File(getPluginWorkingLocation());
       
   249 		File[] rootFiles = rootFolder.listFiles();
       
   250 		int i = 0;
       
   251 		
       
   252 		while( i < rootFiles.length ){
       
   253 			if(rootFiles[i].isDirectory()){
       
   254 				// Create new rule.
       
   255 				TimingRule rule = new TimingRule();
       
   256 				rule.setRulePath(rootFiles[i].toString());
       
   257 				
       
   258 				// read XML, if successful add object to arraylist.
       
   259 				if( rule.readXML() ){
       
   260 					rules.add(rule);
       
   261 				}
       
   262 			}
       
   263 			i++;
       
   264 			
       
   265 		}
       
   266 		
       
   267 		return rules;
       
   268 		
       
   269 	}
       
   270 
       
   271 	/**
       
   272 	 * getRuleType.
       
   273 	 * @param this rule types name.
       
   274 	 */
       
   275 	public String getRuleType() {
       
   276 		return "Timing Rule";
       
   277 	}
       
   278 
       
   279 	/*
       
   280 	 * (non-Javadoc)
       
   281 	 * @see com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRuleType#formatRuleDefinitions(com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule)
       
   282 	 */
       
   283 	public boolean formatRuleDefinitions(TraceAnalyserRule rule) {
       
   284 		
       
   285 		if(TimingRule.class == rule.getClass()){
       
   286 			TimingRule timingRule = (TimingRule) rule;
       
   287 			
       
   288 			traceSelectionCompositeA.setTraceItem(timingRule.getTraceItemA());
       
   289 			traceSelectionCompositeB.setTraceItem(timingRule.getTraceItemB());
       
   290 			
       
   291 			textLimitTraceA.setText(Integer.toString( timingRule.getTimeLimitA()));
       
   292 			textLimitTraceB.setText(Integer.toString( timingRule.getTimeLimitB()));
       
   293 			return true;
       
   294 		}
       
   295 		return false;
       
   296 	}
       
   297 
       
   298 	/*
       
   299 	 * (non-Javadoc)
       
   300 	 * @see com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRuleType#canFinish()
       
   301 	 */
       
   302 	public String canFinish() {
       
   303 		if(traceSelectionCompositeA.getTraceInformation() == null ){
       
   304 			return "Trace A must be defined";
       
   305 		}
       
   306 		if(traceSelectionCompositeB.getTraceInformation() == null){
       
   307 			return "Trace B must be defined";
       
   308 		}
       
   309 	
       
   310 		int limit1 = Integer.parseInt(textLimitTraceA.getText());
       
   311 		int limit2 = Integer.parseInt(textLimitTraceB.getText());
       
   312 
       
   313 		if(limit1 >= limit2 && !(limit1 == 0 && limit2 == 0) ){
       
   314 			return "Limit B must be greater that limit A";
       
   315 		}
       
   316 		
       
   317 		return null;
       
   318 
       
   319 	}
       
   320 
       
   321 	/*
       
   322 	 * (non-Javadoc)
       
   323 	 * @see com.nokia.s60tools.traceanalyser.export.ITraceSelectionCompositeListener#traceInfoUpdated()
       
   324 	 */
       
   325 	public void traceInfoUpdated() {
       
   326 		listener.canFinish();
       
   327 	}
       
   328 	
       
   329 	static public TimingRule createDummyRule(String ruleName){
       
   330 		// Create new timing rule.
       
   331 
       
   332 		TimingRule newRule = new TimingRule();
       
   333 		
       
   334 		// set basic info.
       
   335 		newRule.setName(ruleName);
       
   336 		newRule.setDescription("testDescription");
       
   337 		
       
   338 		// set directory for rule.
       
   339 		newRule.setWorkingDirectory(getPluginWorkingLocation());
       
   340 		
       
   341 		// set trace items for rule.
       
   342 		TraceInfo info1 = new TraceInfo();
       
   343 		TraceInformation information = new TraceInformation();
       
   344 		information.setTraceId(1);
       
   345 		info1.setIdNumbers(information);
       
   346 		info1.setIdNumbers(information);
       
   347 		
       
   348 
       
   349 		TraceInfo info2 = new TraceInfo();
       
   350 		TraceInformation information2 = new TraceInformation();
       
   351 		information2.setTraceId(2);
       
   352 		info2.setIdNumbers(information2);
       
   353 		
       
   354 		newRule.setTraceItemA(info1);
       
   355 		newRule.setTraceItemB(info2);
       
   356 		
       
   357 		
       
   358 			
       
   359 		int limitA = 0;
       
   360 		int limitB = 0;
       
   361 		try{
       
   362 			limitA = Integer.parseInt("10");
       
   363 			limitB = Integer.parseInt("100");
       
   364 		}
       
   365 		catch (Exception e) {
       
   366 			e.printStackTrace();
       
   367 		}
       
   368 		newRule.setTimeLimitA(limitA);
       
   369 		newRule.setTimeLimitB(limitB);
       
   370 
       
   371 		// write rule into xml-file.
       
   372 			return newRule;
       
   373 		
       
   374 	}
       
   375 
       
   376 	public void modifyText(ModifyEvent arg0) {
       
   377 		listener.canFinish();
       
   378 	}
       
   379 
       
   380 
       
   381 
       
   382 }