trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/dialog/LineCountDialog.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-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 "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  * Line counting Dialog class
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.dialog;
       
    20 
       
    21 import java.util.List;
       
    22 
       
    23 import org.eclipse.jface.dialogs.InputDialog;
       
    24 import org.eclipse.jface.viewers.StructuredSelection;
       
    25 import org.eclipse.jface.window.Window;
       
    26 import org.eclipse.swt.widgets.Shell;
       
    27 
       
    28 import com.nokia.traceviewer.TraceViewerPlugin;
       
    29 import com.nokia.traceviewer.action.TraceViewerActionUtils;
       
    30 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeBaseItem;
       
    31 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeComponentItem;
       
    32 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeItem;
       
    33 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeTextItem;
       
    34 import com.nokia.traceviewer.dialog.treeitem.TreeItem;
       
    35 import com.nokia.traceviewer.dialog.treeitem.TreeItemContentProvider;
       
    36 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    37 import com.nokia.traceviewer.engine.TraceViewerPropertyViewInterface;
       
    38 import com.nokia.traceviewer.engine.TraceViewerDialogInterface.Dialog;
       
    39 import com.nokia.traceviewer.engine.dataprocessor.LineCountItem;
       
    40 import com.nokia.traceviewer.engine.preferences.PreferenceConstants;
       
    41 import com.nokia.traceviewer.engine.preferences.XMLLineCountConfigurationExporter;
       
    42 
       
    43 /**
       
    44  * Line counting Dialog class
       
    45  */
       
    46 public final class LineCountDialog extends BaseTreeDialog {
       
    47 
       
    48 	/**
       
    49 	 * Processing reason to give to progressBar dialog
       
    50 	 */
       
    51 	private static final String processReason = Messages
       
    52 			.getString("LineCountDialog.ProcessReason"); //$NON-NLS-1$
       
    53 
       
    54 	/**
       
    55 	 * Add item image
       
    56 	 */
       
    57 	private static final String itemAddImageLocation = "/icons/filteradd.gif"; //$NON-NLS-1$
       
    58 
       
    59 	/**
       
    60 	 * Edit item image
       
    61 	 */
       
    62 	private static final String itemEditImageLocation = "/icons/filteredit.gif"; //$NON-NLS-1$
       
    63 
       
    64 	/**
       
    65 	 * Remove item image
       
    66 	 */
       
    67 	private static final String itemRemoveImageLocation = "/icons/filterremove.gif"; //$NON-NLS-1$
       
    68 
       
    69 	/**
       
    70 	 * Dialog name
       
    71 	 */
       
    72 	private static final String dialogName = "Line Count Rules"; //$NON-NLS-1$
       
    73 
       
    74 	/**
       
    75 	 * Constructor
       
    76 	 * 
       
    77 	 * @param parent
       
    78 	 *            parent shell
       
    79 	 * @param contentProvider
       
    80 	 *            contentprovider for the tree
       
    81 	 * @param treeRoot
       
    82 	 *            tree root
       
    83 	 */
       
    84 	public LineCountDialog(Shell parent,
       
    85 			TreeItemContentProvider contentProvider, TreeItem treeRoot) {
       
    86 		super(parent, contentProvider, treeRoot);
       
    87 	}
       
    88 
       
    89 	/*
       
    90 	 * (non-Javadoc)
       
    91 	 * 
       
    92 	 * @see com.nokia.traceviewer.dialog.BaseTreeDialog#export()
       
    93 	 */
       
    94 	@Override
       
    95 	public void export() {
       
    96 
       
    97 		// Export rules to XML file
       
    98 		XMLLineCountConfigurationExporter exporter;
       
    99 
       
   100 		// Default configuration file
       
   101 		if (!TraceViewerPlugin.getDefault().getPreferenceStore().getString(
       
   102 				PreferenceConstants.CONFIGURATION_FILE).equals(
       
   103 				PreferenceConstants.DEFAULT_CONFIGURATION_FILE)) {
       
   104 			exporter = new XMLLineCountConfigurationExporter(root,
       
   105 					TraceViewerPlugin.getDefault().getPreferenceStore()
       
   106 							.getString(PreferenceConstants.CONFIGURATION_FILE),
       
   107 					false);
       
   108 		} else {
       
   109 			exporter = new XMLLineCountConfigurationExporter(root,
       
   110 					PreferenceConstants.DEFAULT_CONFIGURATION_FILE, true);
       
   111 		}
       
   112 
       
   113 		exporter.export();
       
   114 
       
   115 	}
       
   116 
       
   117 	/*
       
   118 	 * (non-Javadoc)
       
   119 	 * 
       
   120 	 * @see com.nokia.traceviewer.dialog.BaseTreeDialog#saveSettings()
       
   121 	 */
       
   122 	@Override
       
   123 	public void saveSettings() {
       
   124 		super.saveSettings();
       
   125 
       
   126 		// Save LineCountItems if something changed
       
   127 		if (somethingChanged && !viewer.getControl().isDisposed()) {
       
   128 			Object[] arr = viewer.getCheckedElements();
       
   129 
       
   130 			// Get and clear all old rules and items
       
   131 			List<LineCountItem> lineCountItems = TraceViewerGlobals
       
   132 					.getTraceViewer().getDataProcessorAccess()
       
   133 					.getLineCountProcessor().getLineCountItems();
       
   134 			List<LineCountTreeTextItem> textRules = TraceViewerGlobals
       
   135 					.getTraceViewer().getDataProcessorAccess()
       
   136 					.getLineCountProcessor().getTextRules();
       
   137 			List<LineCountTreeComponentItem> componentRules = TraceViewerGlobals
       
   138 					.getTraceViewer().getDataProcessorAccess()
       
   139 					.getLineCountProcessor().getComponentRules();
       
   140 			lineCountItems.clear();
       
   141 			textRules.clear();
       
   142 			componentRules.clear();
       
   143 
       
   144 			// Save rules
       
   145 			for (int i = 0; i < arr.length; i++) {
       
   146 				LineCountTreeItem listItem = (LineCountTreeItem) arr[i];
       
   147 				// Text rules
       
   148 				if (listItem.getRule() == LineCountTreeItem.Rule.TEXT_RULE) {
       
   149 					textRules.add((LineCountTreeTextItem) listItem);
       
   150 					// Component rules
       
   151 				} else if (listItem.getRule() == LineCountTreeItem.Rule.COMPONENT_RULE) {
       
   152 					componentRules.add((LineCountTreeComponentItem) listItem);
       
   153 				}
       
   154 			}
       
   155 
       
   156 			// Insert rules to LineCountItem lists starting from component rules
       
   157 			for (int i = 0; i < componentRules.size(); i++) {
       
   158 				LineCountItem item = new LineCountItem(componentRules.get(i)
       
   159 						.getName(), 0);
       
   160 				lineCountItems.add(item);
       
   161 			}
       
   162 			// Text rules
       
   163 			for (int i = 0; i < textRules.size(); i++) {
       
   164 				LineCountItem item = new LineCountItem(textRules.get(i)
       
   165 						.getName(), 0);
       
   166 				lineCountItems.add(item);
       
   167 			}
       
   168 
       
   169 		}
       
   170 	}
       
   171 
       
   172 	/*
       
   173 	 * (non-Javadoc)
       
   174 	 * 
       
   175 	 * @see com.nokia.traceviewer.dialog.BaseTreeDialog#restoreSettings()
       
   176 	 */
       
   177 	@Override
       
   178 	protected void restoreSettings() {
       
   179 		super.restoreSettings();
       
   180 
       
   181 		// Get rule arrays
       
   182 		List<LineCountTreeTextItem> textRules = TraceViewerGlobals
       
   183 				.getTraceViewer().getDataProcessorAccess()
       
   184 				.getLineCountProcessor().getTextRules();
       
   185 		List<LineCountTreeComponentItem> componentRules = TraceViewerGlobals
       
   186 				.getTraceViewer().getDataProcessorAccess()
       
   187 				.getLineCountProcessor().getComponentRules();
       
   188 
       
   189 		// Tree has to be re-checked
       
   190 		for (int i = 0; i < textRules.size(); i++) {
       
   191 			LineCountTreeTextItem item = textRules.get(i);
       
   192 			viewer.setChecked(item, true);
       
   193 			checkboxStateListener.checkStateChanged(item);
       
   194 		}
       
   195 		for (int i = 0; i < componentRules.size(); i++) {
       
   196 			LineCountTreeComponentItem item = componentRules.get(i);
       
   197 			viewer.setChecked(item, true);
       
   198 			checkboxStateListener.checkStateChanged(item);
       
   199 		}
       
   200 	}
       
   201 
       
   202 	/*
       
   203 	 * (non-Javadoc)
       
   204 	 * 
       
   205 	 * @see com.nokia.traceviewer.dialog.BaseDialog#createDialogContents()
       
   206 	 */
       
   207 	@Override
       
   208 	protected void createDialogContents() {
       
   209 		super.createDialogContents(dialogName);
       
   210 	}
       
   211 
       
   212 	/*
       
   213 	 * (non-Javadoc)
       
   214 	 * 
       
   215 	 * @see com.nokia.traceviewer.dialog.BaseTreeDialog#createToolBar()
       
   216 	 */
       
   217 	@Override
       
   218 	public void createToolBar() {
       
   219 		super.createToolBar(itemAddImageLocation, itemEditImageLocation,
       
   220 				itemRemoveImageLocation);
       
   221 	}
       
   222 
       
   223 	/*
       
   224 	 * (non-Javadoc)
       
   225 	 * 
       
   226 	 * @see com.nokia.traceviewer.dialog.BaseTreeDialog#processAddGroupAction()
       
   227 	 */
       
   228 	@Override
       
   229 	protected void processAddGroupAction() {
       
   230 		String name = Messages.getString("LineCountDialog.NewGroupText"); //$NON-NLS-1$
       
   231 		InputDialog dialog = new InputDialog(getShell(), name, Messages
       
   232 				.getString("LineCountDialog.NewGroupDialogInfo"), name, null); //$NON-NLS-1$
       
   233 		int ret = dialog.open();
       
   234 		if (ret == Window.OK) {
       
   235 			name = dialog.getValue();
       
   236 			// Get parent node
       
   237 
       
   238 			Object selection = getSelectedGroup();
       
   239 			LineCountTreeItem item = new LineCountTreeBaseItem(contentProvider,
       
   240 					selection, name, LineCountTreeItem.Rule.GROUP);
       
   241 			((LineCountTreeItem) selection).addChild(item);
       
   242 			viewer.expandToLevel(item, 0);
       
   243 
       
   244 			somethingChanged = true;
       
   245 		}
       
   246 	}
       
   247 
       
   248 	/*
       
   249 	 * (non-Javadoc)
       
   250 	 * 
       
   251 	 * @see
       
   252 	 * com.nokia.traceviewer.dialog.BaseTreeDialog#processApplyButtonAction()
       
   253 	 */
       
   254 	@Override
       
   255 	protected void processApplyButtonAction() {
       
   256 		saveSettings();
       
   257 
       
   258 		List<LineCountItem> items = TraceViewerGlobals.getTraceViewer()
       
   259 				.getDataProcessorAccess().getLineCountProcessor()
       
   260 				.getLineCountItems();
       
   261 
       
   262 		// Start counting lines from beginning
       
   263 		if (!items.isEmpty() && somethingChanged) {
       
   264 			TraceViewerPropertyViewInterface propView = TraceViewerGlobals
       
   265 					.getTraceViewer().getPropertyView();
       
   266 
       
   267 			if (propView != null && !propView.isDisposed()) {
       
   268 				propView.clearAll();
       
   269 				propView.createNewPropertyTableItems();
       
   270 				// View is not open, empty items from processors
       
   271 			} else {
       
   272 				TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   273 						.getLineCountProcessor().emptyLineCountItems();
       
   274 
       
   275 				TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   276 						.getVariableTracingProcessor()
       
   277 						.emptyVariableTracingItems();
       
   278 			}
       
   279 
       
   280 			// Set traces not to be shown in the view
       
   281 			TraceViewerGlobals.getTraceViewer().getDataReaderAccess()
       
   282 					.getCurrentDataReader().getTraceConfiguration()
       
   283 					.setShowInView(false);
       
   284 
       
   285 			int maxLines = TraceViewerGlobals.getTraceViewer()
       
   286 					.getDataReaderAccess().getCurrentDataReader()
       
   287 					.getTraceCount();
       
   288 
       
   289 			TraceViewerGlobals.getTraceViewer().getDataReaderAccess()
       
   290 					.getCurrentDataReader().setFilePosition(0);
       
   291 
       
   292 			// Open property view
       
   293 			TraceViewerActionUtils.openPropertyView();
       
   294 
       
   295 			// Hide dialog and open progressbar only if there is at least one
       
   296 			// trace
       
   297 			if (maxLines > 0) {
       
   298 				getShell().setVisible(false);
       
   299 				progressBarDialog = (ProgressBarDialog) TraceViewerGlobals
       
   300 						.getTraceViewer().getDialogs().createDialog(
       
   301 								Dialog.PROGRESSBAR);
       
   302 				progressBarDialog.open(maxLines, processReason);
       
   303 			}
       
   304 
       
   305 			// Set traces back to be shown in the view
       
   306 			TraceViewerGlobals.getTraceViewer().getDataReaderAccess()
       
   307 					.getCurrentDataReader().getTraceConfiguration()
       
   308 					.setShowInView(true);
       
   309 
       
   310 			// Removed all count line rules
       
   311 		} else if (somethingChanged) {
       
   312 			TraceViewerPropertyViewInterface view = TraceViewerGlobals
       
   313 					.getTraceViewer().getPropertyView();
       
   314 
       
   315 			if (view != null && !view.isDisposed()) {
       
   316 				view.createNewPropertyTableItems();
       
   317 			}
       
   318 		}
       
   319 
       
   320 		// Insert this to NOT run saveSettings again
       
   321 		somethingChanged = false;
       
   322 	}
       
   323 
       
   324 	/*
       
   325 	 * (non-Javadoc)
       
   326 	 * 
       
   327 	 * @see com.nokia.traceviewer.dialog.BaseTreeDialog#okPressed()
       
   328 	 */
       
   329 	@Override
       
   330 	protected void okPressed() {
       
   331 		TraceViewerGlobals.postUiEvent("OkButton", "1"); //$NON-NLS-1$ //$NON-NLS-2$
       
   332 		saveSettings();
       
   333 		super.okPressed();
       
   334 		TraceViewerGlobals.postUiEvent("OkButton", "0"); //$NON-NLS-1$ //$NON-NLS-2$
       
   335 	}
       
   336 
       
   337 	/*
       
   338 	 * (non-Javadoc)
       
   339 	 * 
       
   340 	 * @see
       
   341 	 * com.nokia.traceviewer.dialog.BaseTreeDialog#getPropertyDialog(java.lang
       
   342 	 * .Object, boolean)
       
   343 	 */
       
   344 	@Override
       
   345 	protected BasePropertyDialog getPropertyDialog(Object selection,
       
   346 			boolean editOldItem) {
       
   347 		LineCountTreeItem oldItem = null;
       
   348 		if (editOldItem) {
       
   349 			oldItem = (LineCountTreeItem) selection;
       
   350 		}
       
   351 		LineCountPropertyDialog dialog = new LineCountPropertyDialog(
       
   352 				getShell(), oldItem, contentProvider, selection);
       
   353 		return dialog;
       
   354 	}
       
   355 
       
   356 	/**
       
   357 	 * Edits existing item or adds new one
       
   358 	 * 
       
   359 	 * @param itemIndex
       
   360 	 *            item index to be edited. If -1, create new one
       
   361 	 */
       
   362 	public void editOrAddItem(int itemIndex) {
       
   363 		TreeItem item = null;
       
   364 
       
   365 		// Create the dialog
       
   366 		create();
       
   367 
       
   368 		// Restore selected items to the tree
       
   369 		restoreSettings();
       
   370 
       
   371 		// Create new item
       
   372 		if (itemIndex == -1) {
       
   373 			item = processAddItemAction();
       
   374 
       
   375 			// Edit old one
       
   376 		} else {
       
   377 
       
   378 			List<LineCountTreeTextItem> textRules = TraceViewerGlobals
       
   379 					.getTraceViewer().getDataProcessorAccess()
       
   380 					.getLineCountProcessor().getTextRules();
       
   381 			List<LineCountTreeComponentItem> componentRules = TraceViewerGlobals
       
   382 					.getTraceViewer().getDataProcessorAccess()
       
   383 					.getLineCountProcessor().getComponentRules();
       
   384 			if (itemIndex < componentRules.size()) {
       
   385 				viewer.setSelection(new StructuredSelection(componentRules
       
   386 						.get(itemIndex)));
       
   387 			} else {
       
   388 				itemIndex -= componentRules.size();
       
   389 				viewer.setSelection(new StructuredSelection(textRules
       
   390 						.get(itemIndex)));
       
   391 			}
       
   392 
       
   393 			item = processEditItemAction();
       
   394 		}
       
   395 
       
   396 		if (item != null) {
       
   397 			// Apply the changes
       
   398 			somethingChanged = true;
       
   399 			okPressed();
       
   400 		} else {
       
   401 			cancelPressed();
       
   402 		}
       
   403 	}
       
   404 }