sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.irq/src/com/nokia/carbide/cpp/pi/irq/SwiThreadTable.java
changeset 5 844b047e260d
equal deleted inserted replaced
4:615035072f7e 5:844b047e260d
       
     1 /*
       
     2  * Copyright (c) 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 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 package com.nokia.carbide.cpp.pi.irq;
       
    19 
       
    20 import java.awt.event.MouseEvent;
       
    21 import java.util.Vector;
       
    22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
       
    23 import org.eclipse.jface.viewers.CheckboxTableViewer;
       
    24 import org.eclipse.jface.viewers.ICheckStateListener;
       
    25 import org.eclipse.jface.viewers.IStructuredContentProvider;
       
    26 import org.eclipse.jface.viewers.Viewer;
       
    27 import org.eclipse.jface.viewers.ViewerSorter;
       
    28 import org.eclipse.swt.SWT;
       
    29 import org.eclipse.swt.events.MouseListener;
       
    30 import org.eclipse.swt.events.SelectionAdapter;
       
    31 import org.eclipse.swt.events.SelectionEvent;
       
    32 import org.eclipse.swt.widgets.Composite;
       
    33 import org.eclipse.swt.widgets.Menu;
       
    34 import org.eclipse.swt.widgets.MenuItem;
       
    35 import org.eclipse.swt.widgets.TableColumn;
       
    36 import org.eclipse.swt.widgets.TableItem;
       
    37 import com.nokia.carbide.cpp.internal.pi.visual.GenericTable;
       
    38 
       
    39 /**
       
    40  *	Software thread interrupt table 
       
    41  */
       
    42 public class SwiThreadTable extends GenericTable implements ICheckStateListener {
       
    43 	
       
    44 	private IrqTraceGraph myGraph;
       
    45 	private Composite parent;
       
    46 	
       
    47     protected Vector<SwiThreadWrapper> tableItemData;
       
    48 
       
    49 	// sort direction
       
    50 	private boolean sortAscending = true;
       
    51 	
       
    52 	/**
       
    53 	 * Constructor
       
    54 	 * @param myGraph irq graph
       
    55 	 * @param parent parent where table is placed
       
    56 	 */
       
    57 	public SwiThreadTable(IrqTraceGraph myGraph, Composite parent){
       
    58 		this.myGraph = myGraph;
       
    59 		this.parent  = parent;
       
    60 
       
    61 		this.tableViewer = CheckboxTableViewer.newCheckList(parent,
       
    62   				SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
       
    63 		this.table = this.tableViewer.getTable();
       
    64 		
       
    65 		// add the check state handler, label provider and content provider
       
    66 		this.tableViewer.addCheckStateListener(this);
       
    67 		this.tableViewer.setLabelProvider(new SwiThreadLabelProvider(this.table));
       
    68 		this.tableViewer.setContentProvider(new TableContentProvider());
       
    69 		this.tableViewer.setSorter(new SharedSorter());
       
    70 
       
    71 		// create the columns
       
    72 		TableColumn column;
       
    73 
       
    74 		// data associated with the TableViewer will note which columns contain hex values
       
    75 		// Keep this in the order in which columns have been created
       
    76 		boolean[] isHex = {false, false, false, false};
       
    77 		this.table.setData("isHex", isHex); //$NON-NLS-1$
       
    78 
       
    79 		// Check column
       
    80 		column = new TableColumn(this.table, SWT.CENTER);
       
    81 		column.setText(Messages.SwiThreadTable_0);
       
    82 		column.setWidth(20);
       
    83 		column.setData(COLUMN_ID_SWI_CHECK);
       
    84 		column.setMoveable(true);
       
    85 		column.setResizable(true);
       
    86 		column.addSelectionListener(new ColumnSelectionHandler());
       
    87 		
       
    88 		// Thread Name column
       
    89 		column = new TableColumn(this.table, SWT.LEFT);
       
    90 		column.setText(Messages.SwiThreadTable_1);
       
    91 		column.setWidth(COLUMN_WIDTH_THREAD_IRQ_LINE);
       
    92 		column.setData(COLUMN_ID_SWI_THREAD);
       
    93 		column.setMoveable(true);
       
    94 		column.setResizable(true);
       
    95 		column.addSelectionListener(new ColumnSelectionHandler());
       
    96 
       
    97 		// Thread address
       
    98 		column = new TableColumn(tableViewer.getTable(), SWT.RIGHT);
       
    99 		column.setText(Messages.SwiThreadTable_2);
       
   100 		column.setWidth(COLUMN_WIDTH_ADDRESS_COUNT);
       
   101 		column.setData(COLUMN_ID_ADDRESS);
       
   102 		column.setMoveable(true);
       
   103 		column.setResizable(true);
       
   104 		column.addSelectionListener(new ColumnSelectionHandler());
       
   105 
       
   106 		// add mouse listener and set other table settings
       
   107 		this.table.addMouseListener(new TableMouseListener());
       
   108 		this.table.setHeaderVisible(true);
       
   109 		this.table.setLinesVisible(true);
       
   110 		this.table.setRedraw(true);
       
   111 		
       
   112 		// format data into table
       
   113 		this.updateItemData(true);
       
   114 		((SharedSorter) tableViewer.getSorter()).doSort(COLUMN_ID_SWI_THREAD);
       
   115 
       
   116 		// Select initially no lines
       
   117 		this.tableViewer.setAllChecked(false);
       
   118 		
       
   119 		// listen for key sequences such as Ctrl-A and Ctrl-C
       
   120 		table.addKeyListener(new TableKeyListener());
       
   121 		
       
   122 		tableViewer.refresh();
       
   123 		table.redraw();
       
   124 	}
       
   125 	
       
   126 	
       
   127 	/**
       
   128 	 * Content provider for table
       
   129 	 */
       
   130 	private static class TableContentProvider implements IStructuredContentProvider {
       
   131 		
       
   132 		/**
       
   133 		 * Constructor
       
   134 		 */
       
   135 		public TableContentProvider() {
       
   136 			super();
       
   137 		}
       
   138 
       
   139 		/*
       
   140 		 * (non-Javadoc)
       
   141 		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
       
   142 		 */
       
   143 		@SuppressWarnings("unchecked")
       
   144 		public Object[] getElements(Object inputElement) {
       
   145 			return ((Vector<SwiThreadWrapper>) inputElement).toArray();
       
   146 		}
       
   147 
       
   148 		/*
       
   149 		 * (non-Javadoc)
       
   150 		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
       
   151 		 */
       
   152 		public void dispose() {
       
   153 		}
       
   154 
       
   155 		/*
       
   156 		 * (non-Javadoc)
       
   157 		 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
       
   158 		 */
       
   159 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
       
   160 		}
       
   161 	}
       
   162 	
       
   163 	/*
       
   164 	 * (non-Javadoc)
       
   165 	 * @see com.nokia.carbide.cpp.internal.pi.visual.GenericTable#action(java.lang.String)
       
   166 	 */
       
   167 	public void action(String actionString)
       
   168 	{
       
   169 		if (actionString.equals("add")){ //$NON-NLS-1$
       
   170 			checkOrUncheckSelectedItems(true);
       
   171 		}
       
   172 		else if (actionString.equals("remove")){ //$NON-NLS-1$
       
   173 			checkOrUncheckSelectedItems(false);
       
   174 		}
       
   175 		else if (actionString.equals("addall")){ //$NON-NLS-1$
       
   176 			checkOrUncheckAllItems(true);
       
   177 		}
       
   178 		else if (actionString.equals("removeall")){ //$NON-NLS-1$
       
   179 			checkOrUncheckAllItems(false);
       
   180 		}
       
   181 	    else if (actionString.equals("copy")) //$NON-NLS-1$
       
   182 	    {
       
   183 	    	actionCopyOrSave(true, this.table, CHECKBOX_TEXT, false, "\t", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
       
   184 	        return; // no redraw needed
       
   185 	    }
       
   186 	    else if (actionString.equals("copyTable")) //$NON-NLS-1$
       
   187 	    {
       
   188 	    	actionCopyOrSave(true, this.table, CHECKBOX_TEXT, true, "\t", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
       
   189 	        return; // no redraw needed
       
   190 	    }
       
   191 		else if (actionString.equals("selectAll")) //$NON-NLS-1$
       
   192 	    {
       
   193 	    	actionSelectAll();
       
   194 	        return;
       
   195 	    }
       
   196 		
       
   197 	    else if (actionString.equals("saveTable")) //$NON-NLS-1$
       
   198 	    {
       
   199 	    	actionCopyOrSave(false, this.table, CHECKBOX_TEXT, true, ",", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
       
   200 	        return; // no redraw needed
       
   201 	    }
       
   202 	}
       
   203 	
       
   204 	/**
       
   205 	 * Checks or unchecks all selected threads from table
       
   206 	 * @param value true if items are checked 
       
   207 	 */
       
   208 	private void checkOrUncheckSelectedItems(boolean value)
       
   209 	{
       
   210 		
       
   211 		TableItem[] selectedItems = this.table.getSelection();
       
   212 		for (int i = 0; i < selectedItems.length; i++)
       
   213 		{
       
   214 			if(selectedItems[i].getData().getClass() == SwiThreadWrapper.class){
       
   215 				selectedItems[i].setChecked(value);
       
   216 				SwiThreadWrapper wrapper = (SwiThreadWrapper)selectedItems[i].getData();
       
   217 				if(value){
       
   218 					myGraph.threadChecked(wrapper.threadName);
       
   219 				}
       
   220 				else{
       
   221 					myGraph.threadUnchecked(wrapper.threadName);
       
   222 
       
   223 				}
       
   224 			}
       
   225 		}
       
   226 		myGraph.updateIrqCountsInLegendsAsynch(IrqTraceGraph.TYPE_SWI);
       
   227 		myGraph.repaint();
       
   228 
       
   229 	}
       
   230 	
       
   231 	/**
       
   232 	 * Checks or unchecks all table items
       
   233 	 * @param value true if all items are checked
       
   234 	 */
       
   235 	private void checkOrUncheckAllItems(boolean value)
       
   236 	{
       
   237 		
       
   238 		TableItem[] allItems = this.table.getItems();
       
   239 		for (int i = 0; i < allItems.length; i++)
       
   240 		{
       
   241 			if(allItems[i].getData().getClass() == SwiThreadWrapper.class){
       
   242 				allItems[i].setChecked(value);
       
   243 				SwiThreadWrapper wrapper = (SwiThreadWrapper)allItems[i].getData();
       
   244 				if(value){
       
   245 					myGraph.threadChecked(wrapper.threadName);
       
   246 				}
       
   247 				else{
       
   248 					myGraph.threadUnchecked(wrapper.threadName);
       
   249 
       
   250 				}
       
   251 			}
       
   252 
       
   253 			
       
   254 		}
       
   255 		myGraph.repaint();
       
   256 		myGraph.updateIrqCountsInLegendsAsynch(IrqTraceGraph.TYPE_SWI);
       
   257 
       
   258 	}
       
   259 	
       
   260 	/**
       
   261 	 * Formats item data into table
       
   262 	 * @param setInput true if table needs to be refreshed
       
   263 	 */
       
   264 	public void updateItemData(boolean setInput)
       
   265 	{
       
   266 		tableItemData = ((IrqTrace)this.myGraph.getTrace()).getAllThreadWrappers();
       
   267 
       
   268 		// refresh the table, if needed
       
   269 		if (setInput)
       
   270 			refreshTableViewer();
       
   271 	}
       
   272 	
       
   273 	/**
       
   274 	 * Refreshes table viewer
       
   275 	 */
       
   276 	public void refreshTableViewer()
       
   277 	{
       
   278 		this.tableViewer.setInput(tableItemData);
       
   279 	}
       
   280 
       
   281 	/**
       
   282 	 * @return tableviewer of the thread table
       
   283 	 */
       
   284 	public CheckboxTableViewer getTableViewer(){
       
   285 		return this.tableViewer;
       
   286 	}
       
   287 
       
   288 	/*
       
   289 	 * (non-Javadoc)
       
   290 	 * @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)
       
   291 	 */
       
   292 	public void checkStateChanged(CheckStateChangedEvent event) {
       
   293 		if(event.getElement().getClass() == SwiThreadWrapper.class){
       
   294 			SwiThreadWrapper wrapper = (SwiThreadWrapper) event.getElement();
       
   295 			if(event.getChecked()){
       
   296 				myGraph.threadChecked(wrapper.threadName);
       
   297 			}
       
   298 			else{
       
   299 				myGraph.threadUnchecked(wrapper.threadName);
       
   300 			}
       
   301 			myGraph.repaint();
       
   302 			myGraph.updateIrqCountsInLegendsAsynch(IrqTraceGraph.TYPE_SWI);
       
   303 			
       
   304 		}
       
   305 		
       
   306 	}
       
   307 	
       
   308 	/**
       
   309 	 * Mouse listener of the table
       
   310 	 */
       
   311 	private class TableMouseListener implements MouseListener
       
   312 	{
       
   313 		/*
       
   314 		 * (non-Javadoc)
       
   315 		 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
       
   316 		 */
       
   317 		public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
       
   318 			
       
   319 		}
       
   320 		
       
   321 		/*
       
   322 		 * (non-Javadoc)
       
   323 		 * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
       
   324 		 */
       
   325 		public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
       
   326 		}
       
   327 		
       
   328 		/*
       
   329 		 * (non-Javadoc)
       
   330 		 * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
       
   331 		 */
       
   332 		public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
       
   333 
       
   334 			if (e.button == MouseEvent.BUTTON3) {
       
   335 				// get rid of last Menu created so we don't have double menu
       
   336 				// on click
       
   337 				if (contextMenu != null) {
       
   338 					contextMenu.dispose();
       
   339 				}
       
   340 
       
   341 				contextMenu = new Menu(table.getShell(), SWT.POP_UP);
       
   342 				getCheckRows(contextMenu, table.getSelectionCount() > 0);
       
   343 				
       
   344 				
       
   345 				// select all, copy, and copy all
       
   346 				new MenuItem(contextMenu, SWT.SEPARATOR);
       
   347 				getSelectAllItem(contextMenu, table.getItemCount() > 0);
       
   348 				getCopyItem(contextMenu, table.getSelectionCount() > 0);
       
   349 				getCopyTableItem(contextMenu, table.getItemCount() > 0);
       
   350 
       
   351 				// save all
       
   352 				new MenuItem(contextMenu, SWT.SEPARATOR);
       
   353 				getSaveTableItem(contextMenu, table.getItemCount() > 0);
       
   354 				
       
   355 				// Recolor highlighted items
       
   356 				new MenuItem(contextMenu, SWT.SEPARATOR);
       
   357 				getRecolorItem(contextMenu, Messages.SwiThreadTable_3, table.getSelectionCount() > 0);
       
   358 
       
   359 				contextMenu.setLocation(parent.toDisplay(e.x + table.getLocation().x, e.y + table.getLocation().y));
       
   360 			    contextMenu.setVisible(true);
       
   361 
       
   362 			    table.setMenu(contextMenu);
       
   363 			}
       
   364 		}
       
   365 	}
       
   366 	
       
   367 	/**
       
   368 	 * Column selection handler for the table
       
   369 	 */
       
   370 	private class ColumnSelectionHandler extends SelectionAdapter
       
   371 	{
       
   372 		public void widgetSelected(SelectionEvent e)
       
   373         {
       
   374         	if (!(e.widget instanceof TableColumn))
       
   375         		return;
       
   376         	
       
   377         	sortOnColumnSelection((TableColumn) e.widget);
       
   378         }
       
   379 	}
       
   380 	
       
   381 	/**
       
   382 	 * @param tableColumn which column is sorted
       
   383 	 */
       
   384 	public void sortOnColumnSelection(TableColumn tableColumn) {
       
   385     	int columnID = ((Integer) tableColumn.getData()).intValue();
       
   386     	((SharedSorter) tableViewer.getSorter()).doSort(columnID);
       
   387 
       
   388 		this.refreshTableViewer();
       
   389 		this.table.redraw();
       
   390 	}
       
   391 	
       
   392 	/**
       
   393 	 * Sorter for the table
       
   394 	 */
       
   395 	private class SharedSorter extends ViewerSorter {
       
   396 		// last column sorted
       
   397 		private int column = -1;
       
   398 		
       
   399 		/* 
       
   400 		 * decide on which column to sort by, and the sort ordering
       
   401 		 */
       
   402 		public void doSort(int column) {
       
   403 			// ignore the column passed in and use the id set by the column selection handler
       
   404 			if (column == this.column) {
       
   405 				// sort in other order
       
   406 				sortAscending = !sortAscending;
       
   407 			} else {
       
   408 				// changed columns, so sort in the default order
       
   409 				switch (column) {
       
   410 					case COLUMN_ID_SWI_CHECK:
       
   411 					{
       
   412 		            	// sort in ascending order
       
   413 		            	sortAscending = true;
       
   414 		                break;
       
   415 					}
       
   416 					case COLUMN_ID_SWI_THREAD:
       
   417 					case COLUMN_ID_ADDRESS:
       
   418 					{
       
   419 		            	// sort in descending order
       
   420 		            	sortAscending = false;
       
   421 		                break;
       
   422 					}
       
   423 					default:
       
   424 					{
       
   425 						// ignore the column
       
   426 						return;
       
   427 					}
       
   428 				}
       
   429 				this.column = column;
       
   430 			}
       
   431 
       
   432 			// find the TableColumn corresponding to column, and give it a column direction
       
   433 			TableColumn sortByColumn = null;
       
   434 			for (int i = 0; i < table.getColumnCount(); i++) {
       
   435 				if (table.getColumn(i).getData() instanceof Integer) {
       
   436 					if (((Integer)table.getColumn(i).getData()) == column) {
       
   437 						sortByColumn = table.getColumn(i);
       
   438 						break;
       
   439 					}
       
   440 				}
       
   441 			}
       
   442 
       
   443 			if (sortByColumn != null) {
       
   444 				table.setSortColumn(sortByColumn);
       
   445 				table.setSortDirection(sortAscending ? SWT.UP : SWT.DOWN);
       
   446 			}
       
   447 		}
       
   448 		
       
   449 		/*
       
   450 		 * compare two items from a table column
       
   451 		 */
       
   452 		public int compare(Viewer viewer, Object e1, Object e2) {
       
   453 			int returnCode = 0;
       
   454 			
       
   455 			SwiThreadWrapper elem1 = (SwiThreadWrapper)e1;
       
   456 			SwiThreadWrapper elem2 = (SwiThreadWrapper)e2;
       
   457 
       
   458 			// find the memory information for the two threads
       
   459 			
       
   460 			// compare based on the memory information
       
   461 			switch (column) {
       
   462 			case COLUMN_ID_SWI_CHECK:
       
   463 				if(tableViewer.getChecked(e1) == true && tableViewer.getChecked(e2) == false){
       
   464 					returnCode = -1;
       
   465 				}
       
   466 				else{
       
   467 					returnCode = 1;
       
   468 				}
       
   469 				break;
       
   470 			case COLUMN_ID_SWI_THREAD:
       
   471 				returnCode = elem1.threadName.compareToIgnoreCase(elem2.threadName);
       
   472 				break;
       
   473 			case COLUMN_ID_ADDRESS:
       
   474 				returnCode = elem1.threadAddress > elem2.threadAddress ? 1 : -1;
       
   475 				break;
       
   476 			default:
       
   477 				break;
       
   478 			}
       
   479 
       
   480 			// for descending order, reverse the sense of the compare
       
   481 			if (!sortAscending)
       
   482 				returnCode = -returnCode;
       
   483 
       
   484 			return returnCode;
       
   485 		}
       
   486 	}
       
   487 	
       
   488 	
       
   489 	/**
       
   490 	 * sets sort direction
       
   491 	 * @param sortAscending new sort direction
       
   492 	 */
       
   493 	public void setSortAscending(boolean sortAscending) {
       
   494 		this.sortAscending = sortAscending;
       
   495 	}
       
   496 	
       
   497 }