sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/ui/TraceSelectionGroup.java
changeset 12 ae255c9aa552
parent 11 5b9d4d8641ce
child 13 86a2634e903d
equal deleted inserted replaced
11:5b9d4d8641ce 12:ae255c9aa552
     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 package com.nokia.carbide.cpp.internal.pi.wizards.ui;
       
    18 
       
    19 import java.text.MessageFormat;
       
    20 import java.util.List;
       
    21 
       
    22 import org.eclipse.jface.action.Action;
       
    23 import org.eclipse.jface.action.MenuManager;
       
    24 import org.eclipse.jface.layout.TableColumnLayout;
       
    25 import org.eclipse.jface.viewers.CheckStateChangedEvent;
       
    26 import org.eclipse.jface.viewers.CheckboxTableViewer;
       
    27 import org.eclipse.jface.viewers.ColumnLabelProvider;
       
    28 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
       
    29 import org.eclipse.jface.viewers.ColumnWeightData;
       
    30 import org.eclipse.jface.viewers.ICheckStateListener;
       
    31 import org.eclipse.jface.viewers.IStructuredContentProvider;
       
    32 import org.eclipse.jface.viewers.TableViewerColumn;
       
    33 import org.eclipse.jface.viewers.Viewer;
       
    34 import org.eclipse.jface.viewers.ViewerSorter;
       
    35 import org.eclipse.jface.window.ToolTip;
       
    36 import org.eclipse.swt.SWT;
       
    37 import org.eclipse.swt.graphics.Color;
       
    38 import org.eclipse.swt.graphics.Point;
       
    39 import org.eclipse.swt.layout.GridData;
       
    40 import org.eclipse.swt.layout.GridLayout;
       
    41 import org.eclipse.swt.widgets.Composite;
       
    42 import org.eclipse.swt.widgets.Display;
       
    43 import org.eclipse.swt.widgets.Table;
       
    44 
       
    45 import com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace;
       
    46 
       
    47 public class TraceSelectionGroup extends AbstractBaseGroup {
       
    48 
       
    49 	private CheckboxTableViewer viewerTraceSelection;
       
    50 	private ProfilerDataPlugins profilerDataPlugins;
       
    51 
       
    52 	/**
       
    53 	 * Constructor
       
    54 	 * 
       
    55 	 * @param parent
       
    56 	 *            instance of the parent composite
       
    57 	 * @param wizardSettings
       
    58 	 *            instance of the INewPIWizardSettings
       
    59 	 */
       
    60 	public TraceSelectionGroup(Composite parent,
       
    61 			INewPIWizardSettings wizardSettings) {
       
    62 		super(parent, wizardSettings);
       
    63 	}
       
    64 
       
    65 	/*
       
    66 	 * (non-Javadoc)
       
    67 	 * 
       
    68 	 * @see
       
    69 	 * com.nokia.carbide.cpp.internal.pi.wizards.ui.AbstractBaseGroup#createContent
       
    70 	 * ()
       
    71 	 */
       
    72 	public void createContent() {
       
    73 		GridLayout gridLayout = new GridLayout();
       
    74 		gridLayout.numColumns = 2;
       
    75 		this.setLayout(gridLayout);
       
    76 		this.setLayoutData(new GridData(GridData.FILL_BOTH));
       
    77 		this.setText(Messages.getString("TraceSelectionGroup.groupTitle")); //$NON-NLS-1$
       
    78 
       
    79 		final Composite tablePanel = new Composite(this, SWT.NONE);
       
    80 		final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
       
    81 		tablePanel.setLayoutData(layoutData);
       
    82 
       
    83 		viewerTraceSelection = CheckboxTableViewer.newCheckList(tablePanel,
       
    84 				SWT.BORDER | SWT.FULL_SELECTION);
       
    85 		viewerTraceSelection
       
    86 				.setContentProvider(new IStructuredContentProvider() {
       
    87 
       
    88 					public Object[] getElements(Object inputElement) {
       
    89 						if (inputElement instanceof List<?>) {
       
    90 							return ((List<?>) inputElement).toArray();
       
    91 						}
       
    92 						return new Object[0];
       
    93 					}
       
    94 
       
    95 					public void dispose() {
       
    96 					}
       
    97 
       
    98 					public void inputChanged(Viewer viewer, Object oldInput,
       
    99 							Object newInput) {
       
   100 					}
       
   101 
       
   102 				});
       
   103 		viewerTraceSelection.setSorter(new ViewerSorter() {
       
   104 
       
   105 			@Override
       
   106 			public int compare(Viewer viewer, Object e1, Object e2) {
       
   107 				if (profilerDataPlugins == null) {
       
   108 					return 0;
       
   109 				}
       
   110 				ITrace trace1 = (ITrace) e1;
       
   111 				ITrace trace2 = (ITrace) e2;
       
   112 				int returnCode = 0;
       
   113 				if (profilerDataPlugins.isMandatory(trace1) == true
       
   114 						&& profilerDataPlugins.isMandatory(trace2) == true) {
       
   115 					returnCode = 0;
       
   116 				} else if (profilerDataPlugins.isMandatory(trace1) == true
       
   117 						&& profilerDataPlugins.isMandatory(trace2) == false) {
       
   118 					returnCode = -1;
       
   119 				} else if (profilerDataPlugins.isMandatory(trace1) == false
       
   120 						&& profilerDataPlugins.isMandatory(trace2) == true) {
       
   121 					returnCode = 1;
       
   122 				} else {
       
   123 					returnCode = trace1.getTraceTitle().compareTo(
       
   124 							trace2.getTraceTitle());
       
   125 				}
       
   126 				return returnCode;
       
   127 			}
       
   128 
       
   129 		});
       
   130 		Table table = viewerTraceSelection.getTable();
       
   131 		table.setLinesVisible(true);
       
   132 		table.setHeaderVisible(true);
       
   133 		addActions();
       
   134 
       
   135 		ColumnViewerToolTipSupport.enableFor(viewerTraceSelection,
       
   136 				ToolTip.NO_RECREATE);
       
   137 
       
   138 		TableViewerColumn column = new TableViewerColumn(viewerTraceSelection,
       
   139 				SWT.NONE);
       
   140 		column.setLabelProvider(new ColumnLabelProvider() {
       
   141 			@Override
       
   142 			public String getText(final Object element) {
       
   143 				if (element instanceof ITrace) {
       
   144 					return ((ITrace) element).getTraceTitle();
       
   145 				}
       
   146 				return element.toString();
       
   147 			}
       
   148 
       
   149 			@Override
       
   150 			public String getToolTipText(Object element) {
       
   151 				StringBuilder sb = new StringBuilder(((ITrace) element)
       
   152 						.getTraceTitle());
       
   153 				if (((ITrace) element).getTraceId() == 1) {
       
   154 					sb.append(Messages.getString("TraceSelectionGroup.mandatory")); //$NON-NLS-1$
       
   155 				}
       
   156 				return sb.toString();
       
   157 			}
       
   158 
       
   159 			@Override
       
   160 			public Color getForeground(Object element) {
       
   161 				if (((ITrace) element).getTraceId() == 1) {
       
   162 					return Display.getCurrent().getSystemColor(
       
   163 							SWT.COLOR_DARK_GRAY);
       
   164 				}
       
   165 				return null;
       
   166 			}
       
   167 
       
   168 			@Override
       
   169 			public Point getToolTipShift(Object object) {
       
   170 				return new Point(5, 5);
       
   171 			}
       
   172 
       
   173 			@Override
       
   174 			public int getToolTipTimeDisplayed(Object object) {
       
   175 				return 5000;
       
   176 			}
       
   177 		});
       
   178 
       
   179 		column.getColumn().setText(Messages.getString("TraceSelectionGroup.piView")); //$NON-NLS-1$
       
   180 
       
   181 		TableColumnLayout tableColumnLayout = new TableColumnLayout();
       
   182 		tablePanel.setLayout(tableColumnLayout);
       
   183 		tableColumnLayout.setColumnData(column.getColumn(),
       
   184 				new ColumnWeightData(1));// column weight 1 to fill the whole
       
   185 		// table width
       
   186 
       
   187 		viewerTraceSelection.setAllChecked(true);
       
   188 		viewerTraceSelection.addCheckStateListener(new ICheckStateListener() {
       
   189 
       
   190 			/*
       
   191 			 * (non-Javadoc)
       
   192 			 * 
       
   193 			 * @see
       
   194 			 * org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged
       
   195 			 * (org.eclipse.jface.viewers.CheckStateChangedEvent)
       
   196 			 */
       
   197 			public void checkStateChanged(CheckStateChangedEvent event) {
       
   198 				if (event.getChecked() == false
       
   199 						&& viewerTraceSelection.getGrayed(event.getElement())) {
       
   200 					// mandatory view; don't allow to deselect it
       
   201 					viewerTraceSelection.setChecked(event.getElement(), true);
       
   202 				} else {
       
   203 					ITrace plugin = (ITrace) event.getElement();
       
   204 					profilerDataPlugins.setChecked(plugin, event.getChecked());
       
   205 				}
       
   206 			}
       
   207 		});
       
   208 	}
       
   209 
       
   210 	/**
       
   211 	 * Update group's title
       
   212 	 * 
       
   213 	 * @param fileName for the group title
       
   214 	 */
       
   215 	private void updateTitle(String fileName){
       
   216 		if(fileName == null){
       
   217 			this.setText(Messages.getString("TraceSelectionGroup.groupTitle"));
       
   218 		}else{
       
   219 			this.setText(MessageFormat.format(Messages.getString("TraceSelectionGroup.groupTitleFor"), fileName));
       
   220 		}
       
   221 		
       
   222 	}
       
   223 	
       
   224 	/**
       
   225 	 * Add actions 
       
   226 	 */
       
   227 	private void addActions() {
       
   228 		final MenuManager mgr = new MenuManager();
       
   229 		Action checkAllAction = new Action(Messages.getString("TraceSelectionGroup.actionCheckAll")) {  //$NON-NLS-1$
       
   230 			@Override
       
   231 			public void run() {				
       
   232 				if (profilerDataPlugins != null) {
       
   233 					profilerDataPlugins.checkAll();	
       
   234 					viewerTraceSelection.setAllChecked(true);		
       
   235 				}
       
   236 			}
       
   237 		};
       
   238 		checkAllAction.setEnabled(true);
       
   239 
       
   240 		Action uncheckAllAction = new Action(Messages.getString("TraceSelectionGroup.actionUncheckAll")) {  //$NON-NLS-1$
       
   241 			@Override
       
   242 			public void run() {				
       
   243 				if (profilerDataPlugins != null) {
       
   244 					profilerDataPlugins.unCheckAll();	
       
   245 					for(ITrace trace : profilerDataPlugins.getPlugins()){
       
   246 						viewerTraceSelection.setChecked(trace, profilerDataPlugins.isChecked(trace));
       
   247 					}
       
   248 				}
       
   249 			}
       
   250 		};
       
   251 		uncheckAllAction.setEnabled(true);
       
   252 		mgr.add(checkAllAction);
       
   253 		mgr.add(uncheckAllAction);
       
   254 		viewerTraceSelection.getControl().setMenu(
       
   255 				mgr.createContextMenu(viewerTraceSelection.getControl()));
       
   256 	}
       
   257 
       
   258 	/**
       
   259 	 * Update trace ids
       
   260 	 * 
       
   261 	 * @param profilerDataPlugins
       
   262 	 */
       
   263 	public void updateTraceIds(ProfilerDataPlugins profilerDataPlugins) {
       
   264 		this.profilerDataPlugins = profilerDataPlugins;
       
   265 		if(profilerDataPlugins == null){
       
   266 			viewerTraceSelection.setInput(null);
       
   267 			updateTitle(null);
       
   268 		}else{			
       
   269 			viewerTraceSelection.setInput(profilerDataPlugins.getPlugins());
       
   270 			viewerTraceSelection.setAllChecked(true);
       
   271 			for (ITrace trace : profilerDataPlugins.getPlugins()) {
       
   272 				if (profilerDataPlugins.isMandatory(trace)) {
       
   273 					viewerTraceSelection.setGrayed(trace, true);
       
   274 				} else {
       
   275 					viewerTraceSelection.setChecked(trace, profilerDataPlugins
       
   276 							.isChecked(trace));
       
   277 				}
       
   278 			}			
       
   279 			updateTitle(profilerDataPlugins.getProfilerDataPath().lastSegment());
       
   280 		}
       
   281 
       
   282 	}
       
   283 
       
   284 	@Override
       
   285 	public Table getTable() {
       
   286 		return viewerTraceSelection.getTable();
       
   287 	}
       
   288 }