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