sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/ui/AbstractBaseGroup.java
changeset 5 844b047e260d
child 12 ae255c9aa552
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 package com.nokia.carbide.cpp.internal.pi.wizards.ui;
       
    18 
       
    19 import java.io.File;
       
    20 import java.io.FileFilter;
       
    21 import java.io.IOException;
       
    22 import java.text.MessageFormat;
       
    23 import java.util.ArrayList;
       
    24 import java.util.List;
       
    25 
       
    26 import org.eclipse.core.runtime.IPath;
       
    27 import org.eclipse.core.runtime.Path;
       
    28 import org.eclipse.jface.viewers.ITableLabelProvider;
       
    29 import org.eclipse.jface.viewers.LabelProvider;
       
    30 import org.eclipse.jface.viewers.TableViewer;
       
    31 import org.eclipse.jface.viewers.Viewer;
       
    32 import org.eclipse.jface.viewers.ViewerSorter;
       
    33 import org.eclipse.swt.SWT;
       
    34 import org.eclipse.swt.graphics.Image;
       
    35 import org.eclipse.swt.layout.GridData;
       
    36 import org.eclipse.swt.layout.GridLayout;
       
    37 import org.eclipse.swt.widgets.Composite;
       
    38 import org.eclipse.swt.widgets.Group;
       
    39 import org.eclipse.swt.widgets.Table;
       
    40 import org.eclipse.swt.widgets.TableColumn;
       
    41 import org.eclipse.swt.widgets.TableItem;
       
    42 
       
    43 import com.nokia.carbide.cpp.internal.pi.analyser.StreamFileParser;
       
    44 import com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace;
       
    45 import com.nokia.carbide.cpp.internal.pi.utils.PIUtilities;
       
    46 
       
    47 /**
       
    48  * Base group for the profiler data importers
       
    49  * 
       
    50  */
       
    51 public abstract class AbstractBaseGroup extends Group {
       
    52 
       
    53 	abstract class AbstractBaseSorter extends ViewerSorter {
       
    54 		Table table;
       
    55 		int column = 0;
       
    56 		boolean sortAscending;
       
    57 
       
    58 		public AbstractBaseSorter(Table table, int defaultColumn) {
       
    59 			this.table = table;
       
    60 			doSort(defaultColumn);
       
    61 		}
       
    62 
       
    63 		public void doSort(int column) {
       
    64 			sortAscending = !sortAscending;
       
    65 
       
    66 			// find the TableColumn corresponding to column, and give it a
       
    67 			// column direction
       
    68 			TableColumn sortByColumn = table.getColumn(column);
       
    69 			if (sortByColumn != null) {
       
    70 				table.setSortColumn(sortByColumn);
       
    71 				table.setSortDirection(sortAscending ? SWT.UP : SWT.DOWN);
       
    72 			}
       
    73 			this.column = column;
       
    74 		}
       
    75 
       
    76 		@Override
       
    77 		abstract public int compare(Viewer viewer, Object e1, Object e2);
       
    78 
       
    79 	}
       
    80 
       
    81 	abstract class AbstractLabelProvider extends LabelProvider implements
       
    82 			ITableLabelProvider {
       
    83 		public Image getColumnImage(Object element, int columnIndex) {
       
    84 			return null;
       
    85 		}
       
    86 	}
       
    87 
       
    88 	private List<ProfilerDataPlugins> profilerDataFiles = new ArrayList<ProfilerDataPlugins>();
       
    89 	protected INewPIWizardSettings wizardSettings;
       
    90 
       
    91 	/**
       
    92 	 * Constructor
       
    93 	 * 
       
    94 	 * @param parent
       
    95 	 *            instance of parent Composite
       
    96 	 * @param wizardSettings
       
    97 	 *            instance of the INewPIWizardSettings
       
    98 	 */
       
    99 	public AbstractBaseGroup(Composite parent,
       
   100 			INewPIWizardSettings wizardSettings) {
       
   101 		super(parent, SWT.NONE);
       
   102 		this.wizardSettings = wizardSettings;
       
   103 		// set default layout
       
   104 		this.setLayout(new GridLayout(1, false));
       
   105 		this.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   106 		createContent();
       
   107 
       
   108 	}
       
   109 
       
   110 	// allow subclassing
       
   111 	protected void checkSubclass() {
       
   112 	}
       
   113 
       
   114 	/**
       
   115 	 * Implement content for the group
       
   116 	 */
       
   117 	protected abstract void createContent();
       
   118 
       
   119 	protected abstract Table getTable();
       
   120 
       
   121 	/**
       
   122 	 * Set this visible and hide given composite
       
   123 	 * 
       
   124 	 * @param hideComposite
       
   125 	 *            to hide
       
   126 	 */
       
   127 	public void setVisible(Composite hideComposite) {
       
   128 		setVisible(true);
       
   129 		setLocation(hideComposite.getLocation());
       
   130 		setSize(hideComposite.getSize());
       
   131 		hideComposite.setVisible(false);
       
   132 	}
       
   133 
       
   134 	/**
       
   135 	 * Add given profile data file into store
       
   136 	 * 
       
   137 	 * @param path
       
   138 	 *            selected profile data file
       
   139 	 * @throws IllegalArgumentException
       
   140 	 *             if given file is not valid profile data file
       
   141 	 */
       
   142 	public void addProfileDataFile(IPath path) throws IllegalArgumentException {
       
   143 		boolean exists = false;
       
   144 		for (ProfilerDataPlugins pdp : profilerDataFiles) {
       
   145 			if (pdp.getProfilerDataPath().equals(path)) {
       
   146 				exists = true;
       
   147 			}
       
   148 		}
       
   149 		if (!exists) {
       
   150 			// check whether selected file a valid profile data file or not
       
   151 			try {
       
   152 				File file = path.toFile();
       
   153 				if (!file.isFile() || file.length() <= 0) {
       
   154 					throw new IllegalArgumentException();
       
   155 				}
       
   156 				new StreamFileParser(file).allTraceType();
       
   157 				profilerDataFiles.add(new ProfilerDataPlugins(path,
       
   158 						getPluginsForTraceFile(path)));
       
   159 			} catch (Exception e) {
       
   160 				throw new IllegalArgumentException(MessageFormat.format(
       
   161 						Messages.getString("AbstractBaseGroup.isNotValidProfilerFile"), path //$NON-NLS-1$
       
   162 								.lastSegment()));
       
   163 			}
       
   164 		} else {
       
   165 			throw new IllegalArgumentException(MessageFormat.format(
       
   166 					Messages.getString("AbstractBaseGroup.profilerFileIsExisted"), path.lastSegment())); //$NON-NLS-1$
       
   167 		}
       
   168 	}
       
   169 
       
   170 	/**
       
   171 	 * Add all valid profile data file from given directory's path
       
   172 	 * 
       
   173 	 * @param path
       
   174 	 *            directory
       
   175 	 * @return
       
   176 	 */
       
   177 	public void addDirectory(IPath path) throws IllegalArgumentException {
       
   178 		if (path != null && path.toFile().isDirectory()) {
       
   179 			boolean addedValidFile = false;
       
   180 			try {
       
   181 				File directory = path.toFile();
       
   182 				if (!directory.isDirectory()) {
       
   183 					throw new IllegalArgumentException();
       
   184 				}
       
   185 				File[] fileArray = directory.listFiles(new FileFilter() {
       
   186 					public boolean accept(File file) {
       
   187 						if (file.isFile()) {
       
   188 							if (file.getPath().endsWith(".dat")) { //$NON-NLS-1$
       
   189 								return true;
       
   190 							}
       
   191 						}
       
   192 						return false;
       
   193 					}
       
   194 				});
       
   195 
       
   196 				for (File file : fileArray) {
       
   197 					try {
       
   198 						addProfileDataFile(new Path(file.toString()));
       
   199 						addedValidFile = true;
       
   200 					} catch (Exception e) {
       
   201 						// do nothing
       
   202 					}
       
   203 				}
       
   204 
       
   205 			} catch (Exception e) {
       
   206 				throw new IllegalArgumentException(MessageFormat.format(
       
   207 						Messages.getString("AbstractBaseGroup.failedToImportFromFolder"), path //$NON-NLS-1$
       
   208 								.toOSString()));
       
   209 			}
       
   210 			if (!addedValidFile) {
       
   211 				throw new IllegalArgumentException(MessageFormat.format(
       
   212 						Messages.getString("AbstractBaseGroup.notFoundProfilerDataFiles"), path //$NON-NLS-1$
       
   213 								.toOSString()));
       
   214 			}
       
   215 		}
       
   216 	}
       
   217 
       
   218 	/**
       
   219 	 * Remove selected item form given TableViewer
       
   220 	 * 
       
   221 	 * @param tableViewer
       
   222 	 *            instance of the TableViewer
       
   223 	 */
       
   224 	public void removeSelectedItem(TableViewer tableViewer) {
       
   225 		for (TableItem item : tableViewer.getTable().getSelection()) {
       
   226 			if (item.getData() instanceof IPath) {
       
   227 				IPath path = (IPath) item.getData();
       
   228 				for (ProfilerDataPlugins pdp : profilerDataFiles) {
       
   229 					if (pdp.getProfilerDataPath().equals(path)) {
       
   230 						profilerDataFiles.remove(pdp);
       
   231 						break;
       
   232 					}
       
   233 				}
       
   234 			}
       
   235 		}
       
   236 	}
       
   237 
       
   238 	/**
       
   239 	 * Remove all item
       
   240 	 */
       
   241 	public void removeAll() {
       
   242 		profilerDataFiles.clear();
       
   243 	}
       
   244 
       
   245 	/**
       
   246 	 * Get list of the ProfilerDataPlugins
       
   247 	 * 
       
   248 	 * @return list of the ProfilerDataPlugins
       
   249 	 */
       
   250 	public List<ProfilerDataPlugins> getProfilerDataFiles() {
       
   251 		return profilerDataFiles;
       
   252 	}
       
   253 
       
   254 	/**
       
   255 	 * Get selected item from the table
       
   256 	 * 
       
   257 	 * @return instance of the ProfilerDataPlugins if found otherwise null is
       
   258 	 *         returned
       
   259 	 */
       
   260 	public ProfilerDataPlugins getSelectedItem() {
       
   261 		Table table = getTable();
       
   262 		if (table.getSelectionCount() == 1) {
       
   263 			IPath path = (IPath) table.getSelection()[0].getData();
       
   264 			return getProfilerDataPlugins(path);
       
   265 		}
       
   266 		return null;
       
   267 	}
       
   268 
       
   269 	/**
       
   270 	 * Updates given TableViewer so that it contains same data that
       
   271 	 * ProfilerDataPlugins list.
       
   272 	 * 
       
   273 	 * @param tableViewer
       
   274 	 *            instance of the TableViewer
       
   275 	 */
       
   276 	public void refreshTable(TableViewer tableViewer) {
       
   277 		Table table = tableViewer.getTable();
       
   278 		List<IPath> pathList = new ArrayList<IPath>();
       
   279 		for (ProfilerDataPlugins pdp : profilerDataFiles) {
       
   280 			pathList.add(pdp.getProfilerDataPath());
       
   281 		}
       
   282 		tableViewer.setInput(pathList);
       
   283 		table.setSelection(0);
       
   284 		tableViewer.refresh();
       
   285 		wizardSettings.validatePage();
       
   286 	}
       
   287 
       
   288 	/**
       
   289 	 * Get plugins list from given file
       
   290 	 * 
       
   291 	 * @param profilerPath profiler data file
       
   292 	 * @return available plugins list from given file
       
   293 	 * @throws IOException
       
   294 	 */
       
   295 	private List<ITrace> getPluginsForTraceFile(IPath profilerPath)
       
   296 			throws IOException {
       
   297 		return PIUtilities.getPluginsForTraceFile(profilerPath.toString());
       
   298 	}
       
   299 
       
   300 	/**
       
   301 	 * Get ProfilerDataPlugins by given profiler data file
       
   302 	 * 
       
   303 	 * @param path 
       
   304 	 * @return instance of the ProfilerDataPlugins if found otherwise null is
       
   305 	 *         returned
       
   306 	 */
       
   307 	private ProfilerDataPlugins getProfilerDataPlugins(IPath path) {
       
   308 		for (ProfilerDataPlugins pdp : profilerDataFiles) {
       
   309 			if (pdp.getProfilerDataPath().equals(path)) {
       
   310 				return pdp;
       
   311 			}
       
   312 		}
       
   313 		return null;
       
   314 	}
       
   315 }