sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/properties/PropertiesPage.java
changeset 1 1050670c6980
child 6 f65f740e69f9
equal deleted inserted replaced
0:5ad7ad99af01 1:1050670c6980
       
     1 /*
       
     2  * Copyright (c) 2008-2009 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:  Definitions for the class PropertiesPage
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.s60tools.analyzetool.properties;
       
    19 
       
    20 import org.eclipse.cdt.internal.core.model.CProject;
       
    21 import org.eclipse.core.resources.IProject;
       
    22 import org.eclipse.core.runtime.CoreException;
       
    23 import org.eclipse.core.runtime.IAdaptable;
       
    24 import org.eclipse.core.runtime.QualifiedName;
       
    25 import org.eclipse.swt.SWT;
       
    26 import org.eclipse.swt.layout.GridData;
       
    27 import org.eclipse.swt.layout.GridLayout;
       
    28 import org.eclipse.swt.widgets.Button;
       
    29 import org.eclipse.swt.widgets.Composite;
       
    30 import org.eclipse.swt.widgets.Control;
       
    31 import org.eclipse.swt.widgets.Event;
       
    32 import org.eclipse.swt.widgets.FileDialog;
       
    33 import org.eclipse.swt.widgets.Group;
       
    34 import org.eclipse.swt.widgets.List;
       
    35 import org.eclipse.swt.widgets.Listener;
       
    36 import org.eclipse.ui.dialogs.PropertyPage;
       
    37 
       
    38 import com.nokia.s60tools.analyzetool.global.Constants;
       
    39 
       
    40 /**
       
    41  * Implements AnalyzeTool properties page
       
    42  * 
       
    43  * @author kihe
       
    44  * 
       
    45  */
       
    46 @SuppressWarnings("restriction")
       
    47 public class PropertiesPage extends PropertyPage implements Listener {
       
    48 	/** Add button to open file selection dialog. */
       
    49 	private Button addSymbolFile;
       
    50 
       
    51 	/** Remove button to remove items from the list. */
       
    52 	private Button removeSymbolFile;
       
    53 
       
    54 	/** Rom symbol file. */
       
    55 	private List romSymbolDirText;
       
    56 
       
    57 	QualifiedName romSymbols;
       
    58 
       
    59 	QualifiedName useRom;
       
    60 	/**
       
    61 	 * Button to use default rom symbol file.
       
    62 	 */
       
    63 	private Button useRomSymbol;
       
    64 
       
    65 	/**
       
    66 	 * Constructor.
       
    67 	 */
       
    68 	public PropertiesPage() {
       
    69 		super();
       
    70 		useRom = new QualifiedName(Constants.USE_ROM_SYMBOL, Constants.USE_ROM);
       
    71 		romSymbols = new QualifiedName(Constants.USE_ROM_SYMBOL_LOCATION,
       
    72 				Constants.ROM_LOC);
       
    73 	}
       
    74 
       
    75 	/**
       
    76 	 * Checks that is selected symbol already in the list. If symbol file is not
       
    77 	 * added => add it to list
       
    78 	 * 
       
    79 	 * @param selectedFile
       
    80 	 *            Select symbol file name and location
       
    81 	 */
       
    82 	private void checkGivenSymbolFile(String selectedFile) {
       
    83 		String[] items = romSymbolDirText.getItems();
       
    84 		boolean found = false;
       
    85 		for (int i = 0; i < items.length; i++) {
       
    86 			if (items[i].equals(selectedFile)) {
       
    87 				found = true;
       
    88 				break;
       
    89 			}
       
    90 		}
       
    91 		if (!found) {
       
    92 			romSymbolDirText.add(selectedFile);
       
    93 		}
       
    94 	}
       
    95 
       
    96 	/**
       
    97 	 * Updates this preference page values to match stored values
       
    98 	 */
       
    99 	private final void checkInitValues() {
       
   100 		try {
       
   101 			IProject project = getProject();
       
   102 			if (project == null) {
       
   103 				useRomSymbol.setEnabled(false);
       
   104 				romSymbolDirText.setEnabled(false);
       
   105 				return;
       
   106 			}
       
   107 
       
   108 			String useRomSymbolProperty = project.getPersistentProperty(useRom);
       
   109 			if (useRomSymbolProperty == null
       
   110 					|| useRomSymbolProperty.equalsIgnoreCase("false")) {
       
   111 				useRomSymbol.setSelection(false);
       
   112 			} else {
       
   113 				useRomSymbol.setSelection(true);
       
   114 			}
       
   115 
       
   116 			// get defined rom symbol(s)
       
   117 			// if user has definend more than one rom symbol
       
   118 			// parse those symbol files and add to list
       
   119 			// each symbol file name is divided by ";" char
       
   120 			// e.g. c:\temp\symbol.symbol;c:\temp\secondsymbol.symbol
       
   121 			String symbolLoc = project.getPersistentProperty(romSymbols);
       
   122 			if (symbolLoc != null && !("").equals(symbolLoc)) {
       
   123 				String[] split = symbolLoc.split(";");
       
   124 				if (split != null) {
       
   125 					for (int i = 0; i < split.length; i++) {
       
   126 						romSymbolDirText.add(split[i]);
       
   127 					}
       
   128 				}
       
   129 			}
       
   130 			handleRomSymbolState();
       
   131 		} catch (CoreException ce) {
       
   132 			ce.printStackTrace();
       
   133 		}
       
   134 
       
   135 	}
       
   136 
       
   137 	/**
       
   138 	 * Creates this preference page content.
       
   139 	 * 
       
   140 	 * @param parent
       
   141 	 *            This preference page parent
       
   142 	 */
       
   143 	@Override
       
   144 	protected final Control createContents(final Composite parent) {
       
   145 
       
   146 		// create new composite
       
   147 		final Composite composite = new Composite(parent, SWT.TOP);
       
   148 
       
   149 		// create griddata for view
       
   150 		final GridData gridData = new GridData();
       
   151 		composite.setLayoutData(gridData);
       
   152 		final GridLayout gridLayout = new GridLayout();
       
   153 		gridLayout.horizontalSpacing = 0;
       
   154 		gridLayout.verticalSpacing = 0;
       
   155 		gridLayout.marginWidth = 0;
       
   156 		gridLayout.marginHeight = 0;
       
   157 		gridLayout.numColumns = 1;
       
   158 
       
   159 		// set layoyt of this view
       
   160 		composite.setLayout(gridLayout);
       
   161 
       
   162 		// new group for symbol file definition
       
   163 		final Group groupUseRomSymbol = new Group(composite, SWT.NONE);
       
   164 		groupUseRomSymbol.setText(Constants.PREFS_ADVANCED);
       
   165 
       
   166 		// layout for useRomSymbol button
       
   167 		final GridLayout layoutSymbol = new GridLayout();
       
   168 		layoutSymbol.numColumns = 1;
       
   169 		groupUseRomSymbol.setLayout(layoutSymbol);
       
   170 
       
   171 		// checkbox item to enable rom symbol selection
       
   172 		useRomSymbol = new Button(groupUseRomSymbol, SWT.CHECK);
       
   173 		useRomSymbol.setText(Constants.PREFS_USE_ROM_SYMBOL);
       
   174 		useRomSymbol.addListener(SWT.Selection, this);
       
   175 
       
   176 		// grid data for use useRomSymbol
       
   177 		final GridData gridDataAtool = new GridData();
       
   178 		gridDataAtool.horizontalSpan = 3;
       
   179 		groupUseRomSymbol.setLayoutData(gridDataAtool);
       
   180 
       
   181 		// new composite for rom symbol location
       
   182 		final Composite romSymbolLocation = new Composite(groupUseRomSymbol,
       
   183 				SWT.TOP);
       
   184 
       
   185 		// romSymbolLocation layout
       
   186 		final GridLayout dirSettingsLayout = new GridLayout();
       
   187 		dirSettingsLayout.numColumns = 2;
       
   188 		romSymbolLocation.setLayout(dirSettingsLayout);
       
   189 
       
   190 		// grid data for romSymbolLocation
       
   191 		final GridData dirGridData = new GridData();
       
   192 		dirGridData.horizontalSpan = 3;
       
   193 		romSymbolLocation.setLayoutData(dirGridData);
       
   194 
       
   195 		// new composite for symbol file list
       
   196 		Composite listComposite = new Composite(romSymbolLocation, SWT.NULL);
       
   197 		GridLayout listCompLayout = new GridLayout();
       
   198 		listCompLayout.numColumns = 1;
       
   199 		listComposite.setLayout(listCompLayout);
       
   200 
       
   201 		GridData listData = new GridData(250, SWT.DEFAULT);
       
   202 		listData.minimumHeight = 350;
       
   203 		listData.heightHint = 150;
       
   204 		romSymbolDirText = new List(listComposite, SWT.BORDER | SWT.V_SCROLL
       
   205 				| SWT.H_SCROLL);
       
   206 		romSymbolDirText.setLayoutData(listData);
       
   207 
       
   208 		Composite buttonComposite = new Composite(romSymbolLocation, SWT.SIMPLE);
       
   209 		GridLayout buttonLayoyt = new GridLayout();
       
   210 		buttonLayoyt.numColumns = 1;
       
   211 		buttonComposite.setLayout(buttonLayoyt);
       
   212 
       
   213 		GridData buttonGridData = new GridData();
       
   214 		buttonGridData.horizontalAlignment = SWT.FILL;
       
   215 		buttonGridData.grabExcessVerticalSpace = true;
       
   216 
       
   217 		// button which opens the folder selection dialog
       
   218 		addSymbolFile = new Button(buttonComposite, SWT.NONE);
       
   219 		addSymbolFile.setToolTipText(Constants.PREFS_SELECT_ROM_SYMBOL);
       
   220 		addSymbolFile.setText("Add");
       
   221 		addSymbolFile.setLayoutData(buttonGridData);
       
   222 		addSymbolFile.addListener(SWT.Selection, this);
       
   223 
       
   224 		removeSymbolFile = new Button(buttonComposite, SWT.NONE);
       
   225 		removeSymbolFile.setToolTipText(Constants.PREFS_SELECT_ROM_SYMBOL);
       
   226 		removeSymbolFile.setText("Remove");
       
   227 		removeSymbolFile.setLayoutData(buttonGridData);
       
   228 		removeSymbolFile.addListener(SWT.Selection, this);
       
   229 
       
   230 		// UI elements are created => adjust right values
       
   231 		checkInitValues();
       
   232 		return composite;
       
   233 	}
       
   234 
       
   235 	/**
       
   236 	 * Returns selected project reference
       
   237 	 * 
       
   238 	 * @return Project reference
       
   239 	 */
       
   240 	private IProject getProject() {
       
   241 		// get project
       
   242 		
       
   243 		IAdaptable adap = super.getElement();
       
   244 		
       
   245 		IProject project = null;
       
   246 		if( adap instanceof IProject ) {
       
   247 			project = (IProject)adap;
       
   248 		}
       
   249 		else if( adap instanceof CProject ) {
       
   250 			CProject cproj = (CProject)adap;
       
   251 			project = cproj.getProject();
       
   252 		}
       
   253 			
       
   254 		if (project != null && project.exists() && project.isOpen()) {
       
   255 			return project;
       
   256 		}
       
   257 		return null;
       
   258 	}
       
   259 
       
   260 	/**
       
   261 	 * Handles events.
       
   262 	 * 
       
   263 	 * @param event
       
   264 	 *            Preference page event
       
   265 	 */
       
   266 	public final void handleEvent(final Event event) {
       
   267 		if (event.widget == addSymbolFile) {
       
   268 			openFolderDialog();
       
   269 		} else if (event.widget == useRomSymbol) {
       
   270 			handleRomSymbolState();
       
   271 		} else if (event.widget == removeSymbolFile) {
       
   272 			romSymbolDirText.remove(romSymbolDirText.getSelectionIndex());
       
   273 		}
       
   274 	}
       
   275 
       
   276 	/**
       
   277 	 * Updates rom symbol file selection state
       
   278 	 */
       
   279 	private final void handleRomSymbolState() {
       
   280 		if (useRomSymbol.getSelection()) {
       
   281 			romSymbolDirText.setEnabled(true);
       
   282 			addSymbolFile.setEnabled(true);
       
   283 			removeSymbolFile.setEnabled(true);
       
   284 		} else {
       
   285 			romSymbolDirText.setEnabled(false);
       
   286 			addSymbolFile.setEnabled(false);
       
   287 			removeSymbolFile.setEnabled(false);
       
   288 		}
       
   289 	}
       
   290 
       
   291 	/**
       
   292 	 * Opens file selection dialog.
       
   293 	 */
       
   294 	private final void openFolderDialog() {
       
   295 		final FileDialog fileDialog = new FileDialog(romSymbolDirText
       
   296 				.getShell(), SWT.MULTI);
       
   297 		fileDialog.setText(Constants.PREFS_SELECT_ROM_SYMBOL);
       
   298 		String[] extensions = { "*.symbol", "*.*" };
       
   299 		fileDialog.setFilterExtensions(extensions);
       
   300 		final String fileLocation = fileDialog.open();
       
   301 		String[] fileNames = fileDialog.getFileNames();
       
   302 
       
   303 		// if user has pressed "cancel"
       
   304 		if (fileLocation == null || ("").equals(fileLocation)) {
       
   305 			return;
       
   306 		}
       
   307 		// if user has selected only one file
       
   308 		else if (fileNames != null && fileNames.length == 1) {
       
   309 			checkGivenSymbolFile(fileLocation);
       
   310 		}
       
   311 		// user has selected multiple file
       
   312 		else if (fileNames != null && fileNames.length > 1) {
       
   313 
       
   314 			// get selected folder.
       
   315 			String folder = fileDialog.getFilterPath();
       
   316 
       
   317 			// go thru selected items
       
   318 			for (int i = 0; i < fileNames.length; i++) {
       
   319 				// get one selected file
       
   320 				String fileName = fileNames[i];
       
   321 
       
   322 				// check selected file
       
   323 				checkGivenSymbolFile(folder + "\\" + fileName);
       
   324 			}
       
   325 		}
       
   326 	}
       
   327 
       
   328 	/**
       
   329 	 * Stores selected values When user press "Ok" or "apply" button this method
       
   330 	 * is called
       
   331 	 */
       
   332 	@Override
       
   333 	public final boolean performOk() {
       
   334 		try {
       
   335 
       
   336 			// get selected project
       
   337 			IProject project = getProject();
       
   338 			if (project == null) {
       
   339 				return false;
       
   340 			}
       
   341 
       
   342 			// get useRomSymbel selection
       
   343 			if (useRomSymbol.getSelection()) {
       
   344 				project.setPersistentProperty(useRom, "true");
       
   345 
       
   346 			} else {
       
   347 				project.setPersistentProperty(useRom, "false");
       
   348 			}
       
   349 
       
   350 			// store user defined rom symbols
       
   351 			// divided by ";" char
       
   352 			StringBuffer symbolFiles = new StringBuffer();
       
   353 			String[] items = romSymbolDirText.getItems();
       
   354 			for (int i = 0; i < items.length; i++) {
       
   355 				symbolFiles.append(items[i]);
       
   356 				symbolFiles.append(';');
       
   357 			}
       
   358 
       
   359 			// store symbol files
       
   360 			project.setPersistentProperty(romSymbols, symbolFiles.toString());
       
   361 		} catch (CoreException ce) {
       
   362 			ce.printStackTrace();
       
   363 		}
       
   364 		return super.performOk();
       
   365 	}
       
   366 
       
   367 }