sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi/src/com/nokia/carbide/cpp/internal/pi/actions/TimeSetAction.java
changeset 2 b9ab3b238396
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     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 the License "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  * 
       
    20  */
       
    21 package com.nokia.carbide.cpp.internal.pi.actions;
       
    22 
       
    23 import java.text.DecimalFormat;
       
    24 import java.util.Enumeration;
       
    25 
       
    26 import org.eclipse.swt.SWT;
       
    27 import org.eclipse.swt.events.FocusEvent;
       
    28 import org.eclipse.swt.events.FocusListener;
       
    29 import org.eclipse.swt.events.SelectionEvent;
       
    30 import org.eclipse.swt.events.SelectionListener;
       
    31 import org.eclipse.swt.layout.FillLayout;
       
    32 import org.eclipse.swt.widgets.Composite;
       
    33 import org.eclipse.swt.widgets.Control;
       
    34 import org.eclipse.swt.widgets.Event;
       
    35 import org.eclipse.swt.widgets.Label;
       
    36 import org.eclipse.swt.widgets.Text;
       
    37 
       
    38 import com.nokia.carbide.cpp.internal.pi.analyser.PIChangeEvent;
       
    39 import com.nokia.carbide.cpp.internal.pi.manager.PluginInitialiser;
       
    40 import com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener;
       
    41 
       
    42 
       
    43 public class TimeSetAction extends org.eclipse.jface.action.ControlContribution {
       
    44 
       
    45 	double startTime = 0;
       
    46 	double endTime   = 0;
       
    47 	
       
    48 	private Text  startTimeText;
       
    49 	private Text  endTimeText;
       
    50 	
       
    51 	private DecimalFormat timeFormat = new DecimalFormat(Messages.getString("TimeSetAction.decimalFormat")); //$NON-NLS-1$
       
    52 	
       
    53 	public TimeSetAction(String id) {
       
    54 		super(id);
       
    55 	}
       
    56 	
       
    57 	public void setTime(double start, double end) {
       
    58 		if (start < 0f)
       
    59 			start = 0f;
       
    60 		
       
    61 		if (end < 0f)
       
    62 			end = 0f;
       
    63 		
       
    64 		if (start <= end) {
       
    65 			this.startTime = start;
       
    66 			this.endTime   = end;
       
    67 		} else {
       
    68 			// swap start and end
       
    69 			double temp = start;
       
    70 			this.startTime = end;
       
    71 			this.endTime   = temp;
       
    72 		}
       
    73 
       
    74 		if ((startTimeText == null) || (endTimeText == null))
       
    75 			return;
       
    76 
       
    77 		startTimeText.setText(timeFormat.format(startTime));
       
    78 		endTimeText.setText(timeFormat.format(endTime));
       
    79 	}
       
    80 	
       
    81 	public void setStartTime(double start) {
       
    82 		setTime(start, this.endTime);
       
    83 	}
       
    84 	
       
    85 	public void setEndTime(double end) {
       
    86 		setTime(this.startTime, end);
       
    87 	}
       
    88 	
       
    89 	public double getStartTime() {
       
    90 		return this.startTime;
       
    91 	}
       
    92 
       
    93 	public double getEndTime() {
       
    94 		return this.endTime;
       
    95 	}
       
    96 
       
    97 	protected Control createControl(Composite parent) {
       
    98 		Composite control;
       
    99 		Label label;
       
   100 
       
   101 		// create a composite where the user can enter a start time and an end time
       
   102 		control = new Composite(parent, SWT.BORDER);
       
   103 		parent.setLayout(new FillLayout(SWT.HORIZONTAL));
       
   104 		control.setLayout(new FillLayout(SWT.HORIZONTAL));
       
   105 
       
   106 		label = new Label(control, SWT.RIGHT);
       
   107 		label.setText(Messages.getString("TimeSetAction.startTime")); //$NON-NLS-1$
       
   108 		label.pack();
       
   109 
       
   110 		startTimeText = new Text(control, SWT.BORDER);
       
   111 		
       
   112 		if (startTime >= 0.0f)
       
   113 			startTimeText.setText(timeFormat.format(startTime));
       
   114 		else
       
   115 			startTimeText.setText(""); //$NON-NLS-1$
       
   116 
       
   117 		startTimeText.addFocusListener(new FocusListener() {
       
   118 
       
   119 			double initialStartTime = -1;
       
   120 			
       
   121 			public void focusGained(FocusEvent e) {
       
   122 				initialStartTime = startTime;
       
   123 			}
       
   124 
       
   125 			public void focusLost(FocusEvent e) {
       
   126 				double new_double;
       
   127 				
       
   128 				// convert, catch (NumberFormatException e1)
       
   129 				try {
       
   130 					new_double = Double.parseDouble(startTimeText.getText().replace(',','.'));
       
   131 					if (new_double >= 0)
       
   132 						startTime = new_double;
       
   133 				} catch (NumberFormatException exc) {
       
   134 					// just keep the old value
       
   135 				}					
       
   136 
       
   137 				// redisplay the time
       
   138 				setTime(startTime, endTime);
       
   139 				
       
   140 				if (startTime != initialStartTime) {
       
   141 					PIChangeEvent.action("changeSelection"); //$NON-NLS-1$
       
   142 					
       
   143 					// after the graphs have been updated, notify plugins that might have tables but no graphs
       
   144             		Enumeration enu = PluginInitialiser.getPluginInstances(
       
   145             									"com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener"); //$NON-NLS-1$
       
   146             		if (enu != null) {
       
   147             			Event event = new Event();
       
   148             			event.start = (int) startTime;
       
   149             			event.end   = (int) endTime;
       
   150             			
       
   151 	            		while (enu.hasMoreElements())
       
   152 	            		{
       
   153 	            			IEventListener plugin = (IEventListener)enu.nextElement();
       
   154 	            			plugin.receiveEvent("changeSelection", event); //$NON-NLS-1$
       
   155 	            		}
       
   156             		}
       
   157 				}
       
   158 			}
       
   159 
       
   160 		});
       
   161 		
       
   162 		startTimeText.addSelectionListener(new SelectionListener() {
       
   163 
       
   164 			public void widgetSelected(SelectionEvent e) {
       
   165 			}
       
   166 
       
   167 			public void widgetDefaultSelected(SelectionEvent e) {
       
   168 				double new_double;
       
   169 
       
   170 				// convert, catch (NumberFormatException e1)
       
   171 				try {
       
   172 					new_double = Double.parseDouble(startTimeText.getText().replace(',','.'));
       
   173 					if (new_double >= 0)
       
   174 						startTime = new_double;
       
   175 				} catch (NumberFormatException exc) {
       
   176 					// just keep the old value
       
   177 				}					
       
   178 
       
   179 				// redisplay the time
       
   180 				setTime(startTime, endTime);
       
   181 			}
       
   182 
       
   183 		});
       
   184 
       
   185 		label = new Label(control, SWT.RIGHT);
       
   186 		label.setText(Messages.getString("TimeSetAction.endTime")); //$NON-NLS-1$
       
   187 		label.pack();
       
   188 
       
   189 		endTimeText   = new Text(control, SWT.BORDER);
       
   190 
       
   191 		if (endTime >= 0.0f)
       
   192 			endTimeText.setText(timeFormat.format(endTime));
       
   193 		else
       
   194 			endTimeText.setText(""); //$NON-NLS-1$
       
   195 
       
   196 		endTimeText.addFocusListener(new FocusListener() {
       
   197 			
       
   198 			double initialEndTime   = -1;
       
   199 
       
   200 			public void focusGained(FocusEvent e) {
       
   201 				initialEndTime = endTime;
       
   202 			}
       
   203 
       
   204 			public void focusLost(FocusEvent e) {
       
   205 				double new_double;
       
   206 				
       
   207 				// convert, catch (NumberFormatException e1)
       
   208 				try {
       
   209 					new_double = Double.parseDouble(endTimeText.getText().replace(',','.'));
       
   210 					if (new_double >= 0)
       
   211 						endTime = new_double;
       
   212 				} catch (NumberFormatException exc) {
       
   213 					// just keep the old value
       
   214 				}					
       
   215 
       
   216 				// redisplay the time
       
   217 				setTime(startTime, endTime);
       
   218 				if (endTime != initialEndTime) {
       
   219 					PIChangeEvent.action("changeSelection"); //$NON-NLS-1$
       
   220 					
       
   221 					// after the graphs have been updated, notify plugins that might have tables but no graphs
       
   222             		Enumeration enu = PluginInitialiser.getPluginInstances(
       
   223             									"com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener"); //$NON-NLS-1$
       
   224             		if (enu != null) {
       
   225             			Event event = new Event();
       
   226             			event.start = (int) startTime;
       
   227             			event.end   = (int) endTime;
       
   228             			
       
   229 	            		while (enu.hasMoreElements())
       
   230 	            		{
       
   231 	            			IEventListener plugin = (IEventListener)enu.nextElement();
       
   232 	            			plugin.receiveEvent("changeSelection", event); //$NON-NLS-1$
       
   233 	            		}
       
   234             		}
       
   235 				}
       
   236 			}
       
   237 
       
   238 		});
       
   239 		
       
   240 		endTimeText.addSelectionListener(new SelectionListener() {
       
   241 
       
   242 			public void widgetSelected(SelectionEvent e) {
       
   243 			}
       
   244 
       
   245 			public void widgetDefaultSelected(SelectionEvent e) {
       
   246 				double new_double;
       
   247 			
       
   248 				// convert, catch (NumberFormatException e1)
       
   249 				try {
       
   250 					new_double = Double.parseDouble(endTimeText.getText().replace(',','.'));
       
   251 					if (new_double >= 0)
       
   252 						endTime = new_double;
       
   253 				} catch (NumberFormatException exc) {
       
   254 					// just keep the old value
       
   255 				}					
       
   256 
       
   257 				// redisplay the time
       
   258 				setTime(startTime, endTime);
       
   259 			}
       
   260 			
       
   261 		});
       
   262 
       
   263 		control.addFocusListener(new FocusListener() {
       
   264 
       
   265 			public void focusGained(FocusEvent e) {
       
   266 			}
       
   267 
       
   268 			public void focusLost(FocusEvent e) {
       
   269 				if (startTime >= 0.0f)
       
   270 					startTimeText.setText(timeFormat.format(startTime));
       
   271 				else
       
   272 					startTimeText.setText(""); //$NON-NLS-1$
       
   273 				
       
   274 				if (endTime >= 0.0f)
       
   275 					endTimeText.setText(timeFormat.format(endTime));
       
   276 				else
       
   277 					endTimeText.setText(""); //$NON-NLS-1$
       
   278 			}
       
   279 			
       
   280 		});
       
   281 
       
   282 		return control;
       
   283 	}
       
   284 	
       
   285 	public void dispose() {
       
   286 	}
       
   287 }
       
   288