sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/ui/NewPIWizardPageRomSubTask.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 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 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 
       
    18 package com.nokia.carbide.cpp.internal.pi.wizards.ui;
       
    19 
       
    20 import java.io.File;
       
    21 import java.util.ArrayList;
       
    22 
       
    23 import org.eclipse.jface.dialogs.Dialog;
       
    24 import org.eclipse.jface.dialogs.MessageDialog;
       
    25 import org.eclipse.swt.SWT;
       
    26 import org.eclipse.swt.events.ModifyEvent;
       
    27 import org.eclipse.swt.events.ModifyListener;
       
    28 import org.eclipse.swt.events.SelectionEvent;
       
    29 import org.eclipse.swt.events.SelectionListener;
       
    30 import org.eclipse.swt.graphics.Point;
       
    31 import org.eclipse.swt.layout.GridData;
       
    32 import org.eclipse.swt.layout.GridLayout;
       
    33 import org.eclipse.swt.widgets.Button;
       
    34 import org.eclipse.swt.widgets.Composite;
       
    35 import org.eclipse.swt.widgets.Event;
       
    36 import org.eclipse.swt.widgets.FileDialog;
       
    37 import org.eclipse.swt.widgets.Label;
       
    38 import org.eclipse.swt.widgets.Listener;
       
    39 import org.eclipse.swt.widgets.Table;
       
    40 import org.eclipse.swt.widgets.TableItem;
       
    41 import org.eclipse.swt.widgets.Text;
       
    42 import org.eclipse.ui.PlatformUI;
       
    43 
       
    44 import com.nokia.carbide.cpp.internal.pi.wizards.ui.util.CarbidePiWizardHelpIds;
       
    45 import com.nokia.carbide.cpp.internal.pi.wizards.ui.util.RofsObySymbolPair;
       
    46 import com.nokia.carbide.cpp.internal.pi.wizards.ui.util.SdkChooserBase;
       
    47 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    48 
       
    49 public class NewPIWizardPageRomSubTask extends NewPIWizardPage implements INewPIWizardSettings
       
    50 {
       
    51 	private boolean visableBefore = false;
       
    52 	
       
    53 	protected NewPIWizardPageRomSubTask() {
       
    54 		super(""); //$NON-NLS-1$
       
    55 		setTitle(Messages.getString("NewPIWizardPageRomSubTask.title")); //$NON-NLS-1$
       
    56 	    setDescription(Messages.getString("NewPIWizardPageRomSubTask.description")); //$NON-NLS-1$
       
    57 	}
       
    58 	
       
    59 	// use base SDK chooser object for layout
       
    60 	private SdkChooserBase sdkCommon = new SdkChooserBase();
       
    61 	private static final String FILTER_EXT_KEY = "extension"; //$NON-NLS-1$
       
    62 	private static final String FILTER_NAME_KEY = "name"; //$NON-NLS-1$
       
    63 	private static final String MY_WIGET_KEY = "myWiget"; //$NON-NLS-1$
       
    64 	private static final String ROFS_ITEM_PAIR_KEY = "ROFS_ITEM_PAIR_KEY";	//$NON-NLS-1$
       
    65 	private static final int TEXT_MARGIN = 2;
       
    66 	private static final int ARC_WIDTH = 2;
       
    67 	private static String lastGoodDir = null;
       
    68 	private ModifyListener textModifyListener = new ModifyListener () {
       
    69 
       
    70 		public void modifyText(ModifyEvent arg0) {
       
    71 			Object source = arg0.getSource();
       
    72 			if (source instanceof Text) {
       
    73 				String pathString = ((Text)source).getText();
       
    74 				java.io.File file = new java.io.File(pathString);
       
    75 				if (file.exists()) {
       
    76 					if (file.isFile()) {
       
    77 						lastGoodDir = file.getParent();
       
    78 					} else if (file.isDirectory()){
       
    79 						lastGoodDir = pathString;
       
    80 					} else {
       
    81 						lastGoodDir = null;
       
    82 					}
       
    83 				}
       
    84 			}
       
    85 			if (source == symbolText) {
       
    86 				NewPIWizardSettings.getInstance().romSymbolFile = symbolText.getText();
       
    87 			}
       
    88 			if (source == obyText) {
       
    89 				NewPIWizardSettings.getInstance().romObyFile = obyText.getText();
       
    90 			}
       
    91 			validatePage();
       
    92 		}
       
    93 		
       
    94 	};
       
    95 	private SelectionListener browseSelectionListener = new SelectionListener() {
       
    96 
       
    97 		public void widgetDefaultSelected(SelectionEvent arg0) {
       
    98 		}
       
    99 
       
   100 		public void widgetSelected(SelectionEvent arg0) {
       
   101 			Button button = (Button) arg0.getSource();
       
   102 			String selectedFilePath;
       
   103 			FileDialog dialog = new FileDialog(getShell());
       
   104 			ArrayList<String> pkgExtensions = new ArrayList<String>();
       
   105 			ArrayList<String> pkgNames = new ArrayList<String>();
       
   106 			pkgExtensions.add((String)button.getData(FILTER_EXT_KEY));
       
   107 			pkgNames.add((String)button.getData(FILTER_NAME_KEY));
       
   108 			pkgExtensions.add("*.*"); //$NON-NLS-1$"
       
   109 			pkgNames.add(Messages.getString("NewPIWizardPageRomSubTask.name.filter")); //$NON-NLS-1$
       
   110 			dialog.setFilterExtensions(pkgExtensions.toArray(new String[pkgExtensions.size()]));
       
   111 			dialog.setFilterNames(pkgNames.toArray(new String[pkgNames.size()]));
       
   112 			ISymbianSDK sdk = sdkCommon.getSelectedSdk();
       
   113 			if (sdk != null) {
       
   114 				String epocroot = sdk.getEPOCROOT();
       
   115 				if (lastGoodDir != null) {
       
   116 					// pick last known good before EPOCROOT
       
   117 					dialog.setFilterPath(lastGoodDir);
       
   118 				} else if (epocroot != null) {
       
   119 					if (epocroot != "") { //$NON-NLS-1$
       
   120 						String romDir = epocroot;
       
   121 						if (romDir.endsWith(File.separator))
       
   122 							romDir += "epoc32" + File.separator + "rom";	//$NON-NLS-1$" //$NON-NLS-2$"
       
   123 						else
       
   124 							romDir += File.separator + "epoc32" + File.separator + "rom";	//$NON-NLS-1$" //$NON-NLS-2$"
       
   125 						// S60 and Techview symbol locations
       
   126 						dialog.setFilterPath(romDir);
       
   127 					}
       
   128 				}
       
   129 			}
       
   130 			
       
   131 			selectedFilePath = dialog.open();
       
   132 			
       
   133 			if (selectedFilePath != null) {
       
   134 				((Text)button.getData(MY_WIGET_KEY)).setText(selectedFilePath);
       
   135 			}
       
   136 		}
       
   137 		
       
   138 	};
       
   139 
       
   140 	//	 controls
       
   141 	private Composite composite = null;
       
   142 //	private Label headerLabel = null;
       
   143 	private Label symbolLabel = null;
       
   144 	private Composite symbolComposite = null;
       
   145 	private Text symbolText = null;
       
   146 	private Button symbolButton = null;
       
   147 	private Label obyLabel = null;
       
   148 	private Composite obyComposite = null;
       
   149 	private Text obyText = null;
       
   150 	private Button obyButton = null;
       
   151 	private Label rofsObySymbolLabel = null;
       
   152 	private Composite rofsObySymbolComposite = null;
       
   153 	private Table rofsObySymbolTable = null;
       
   154 	private Composite rofsObySymbolButtonComposite = null;
       
   155 	private Button rofsAddButton = null;
       
   156 	private Button rofsRemoveButton = null;
       
   157 //	private Label detailLabel = null;
       
   158 	
       
   159 	public void validatePage() {
       
   160 		if (symbolText.getText().length() > 1 ||
       
   161 				obyText.getText().length() > 1 ||
       
   162 				rofsObySymbolTable.getItemCount() > 1) {
       
   163 				if (sdkCommon.getSelectedSdk() == null) {
       
   164 					setErrorMessage(Messages.getString("NewPIWizardPageRomSubTask.error.1")); //$NON-NLS-1$
       
   165 					setPageComplete(false);
       
   166 					return;
       
   167 				}
       
   168 		}
       
   169 		if (symbolText.getText().length() > 0 ) {
       
   170 			java.io.File symFile = new java.io.File(symbolText.getText());
       
   171 			if (!symFile.exists() || !symFile.isFile()) {
       
   172 				setErrorMessage(Dialog.shortenText(Messages.getString("NewPIWizardPageRomSubTask.symbol.label") +  " " + symbolText.getText() + " " + Messages.getString("NewPIWizardPageRomSubTask.error.2"), symbolComposite)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
       
   173 				setPageComplete(false);
       
   174 				return;
       
   175 			}
       
   176 		}
       
   177 		if (obyText.getText().length() > 0 ) {
       
   178 			java.io.File obyFile = new java.io.File(obyText.getText());
       
   179 			if (!obyFile.exists() || !obyFile.isFile()) {
       
   180 				setErrorMessage(Dialog.shortenText(Messages.getString("NewPIWizardPageRomSubTask.oby.label") +  " " + obyText.getText() + " " + Messages.getString("NewPIWizardPageRomSubTask.error.2"), obyComposite)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
       
   181 				setPageComplete(false);
       
   182 				return;
       
   183 			}
       
   184 		}
       
   185 		
       
   186 		setErrorMessage(null);
       
   187 		setPageComplete(true);
       
   188 		return;
       
   189 	}
       
   190 	
       
   191 	private void writeRofsTableToStatus() {
       
   192 		NewPIWizardSettings npws = NewPIWizardSettings.getInstance();
       
   193 		npws.rofsObySymbolPairList.clear();
       
   194 		TableItem[] items = rofsObySymbolTable.getItems();
       
   195 		for (TableItem item : items) {
       
   196 			RofsObySymbolPair pair = (RofsObySymbolPair) item.getData(ROFS_ITEM_PAIR_KEY);
       
   197 			if (pair.haveObyFile() || pair.haveSymbolFile()) {
       
   198 				npws.rofsObySymbolPairList.add(pair);
       
   199 			}
       
   200 		}
       
   201 	}
       
   202 
       
   203 	public void createControl(Composite parent) {
       
   204 		super.createControl(parent);
       
   205 		GridLayout gridLayout = new GridLayout();
       
   206 		gridLayout.numColumns = 1;
       
   207 		composite = new Composite(parent, SWT.NONE);
       
   208 		composite.setLayout(gridLayout);
       
   209 		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   210 		
       
   211 //		headerLabel = new Label(composite, SWT.NONE);
       
   212 //		headerLabel.setText(Messages.getString("NewPIWizardPageRomSubTask.header.label")); //$NON-NLS-1$
       
   213 
       
   214 		// the SDK top half of the page
       
   215 		sdkCommon.layout(composite);
       
   216 		sdkCommon.sdkTable.addSelectionListener(new SelectionListener() {
       
   217 			public void widgetDefaultSelected(SelectionEvent arg0) {
       
   218 			}
       
   219 
       
   220 			public void widgetSelected(SelectionEvent arg0) {
       
   221 				// write back our status
       
   222 				NewPIWizardSettings.getInstance().romSdk = sdkCommon.getSelectedSdk();
       
   223 				validatePage();
       
   224 			}
       
   225 		});
       
   226 
       
   227 //		detailLabel = new Label(composite, SWT.NONE);
       
   228 //		detailLabel.setText(Messages.getString("NewPIWizardPageRomSubTask.detail.label")); //$NON-NLS-1$
       
   229 		obyLabel = new Label(composite, SWT.NONE);
       
   230 		obyLabel.setText(Messages.getString("NewPIWizardPageRomSubTask.oby.label")); //$NON-NLS-1$
       
   231 		createObyControl();
       
   232 		symbolLabel = new Label(composite, SWT.NONE);
       
   233 		symbolLabel.setText(Messages.getString("NewPIWizardPageRomSubTask.symbol.label")); //$NON-NLS-1$
       
   234 		createSymbolControl();
       
   235 		rofsObySymbolLabel = new Label(composite, SWT.NONE);
       
   236 		rofsObySymbolLabel.setText(Messages.getString("NewPIWizardPageRomSubTask.rofs.pairs")); //$NON-NLS-1$
       
   237 		createRofsObySymbolControl();
       
   238 		validatePage();
       
   239 		
       
   240 		// top tap get focus
       
   241 		sdkCommon.sdkTable.setFocus();
       
   242 		
       
   243 		setControl(composite);
       
   244 		PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), CarbidePiWizardHelpIds.PI_IMPORT_WIZARD_ROM);
       
   245 	}
       
   246 	
       
   247 	public void createSymbolControl() {
       
   248 		GridLayout gridLayout = new GridLayout();
       
   249 		gridLayout.numColumns = 2;
       
   250 		symbolComposite = new Composite(composite, SWT.NONE);
       
   251 		symbolComposite.setLayout(gridLayout);
       
   252 		symbolComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
       
   253 		symbolText = new Text(symbolComposite, SWT.BORDER | SWT.SINGLE);
       
   254 		symbolText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
       
   255 		symbolText.addModifyListener(textModifyListener);
       
   256 		symbolButton = new Button(symbolComposite, SWT.NONE);
       
   257 		symbolButton.setText(Messages.getString("NewPIWizardPageRomSubTask.symbol.button")); //$NON-NLS-1$
       
   258 		symbolButton.setData(FILTER_EXT_KEY, Messages.getString("NewPIWizardPageRomSubTask.symbol.filter")); //$NON-NLS-1$
       
   259 		symbolButton.setData(FILTER_NAME_KEY, Messages.getString("NewPIWizardPageRomSubTask.symbol.filter.text")); //$NON-NLS-1$
       
   260 		symbolButton.setData(MY_WIGET_KEY, symbolText);
       
   261 		symbolButton.addSelectionListener(browseSelectionListener);
       
   262 	}
       
   263 
       
   264 	public void createObyControl() {
       
   265 		GridLayout gridLayout = new GridLayout();
       
   266 		gridLayout.numColumns = 2;
       
   267 		obyComposite = new Composite(composite, SWT.NONE);
       
   268 		obyComposite.setLayout(gridLayout);
       
   269 		obyComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
       
   270 		obyText = new Text(obyComposite, SWT.BORDER | SWT.SINGLE);
       
   271 		obyText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
       
   272 		obyText.addModifyListener(textModifyListener);
       
   273 		obyButton = new Button(obyComposite, SWT.NONE);
       
   274 		obyButton.setText(Messages.getString("NewPIWizardPageRomSubTask.oby.button")); //$NON-NLS-1$
       
   275 		obyButton.setData(FILTER_EXT_KEY, Messages.getString("NewPIWizardPageRomSubTask.oby.filter")); //$NON-NLS-1$
       
   276 		obyButton.setData(FILTER_NAME_KEY, Messages.getString("NewPIWizardPageRomSubTask.oby.filter.text")); //$NON-NLS-1$
       
   277 		obyButton.setData(MY_WIGET_KEY, obyText);
       
   278 		obyButton.addSelectionListener(browseSelectionListener);
       
   279 	}
       
   280 	
       
   281 	public void createRofsObySymbolControl() {
       
   282 		GridLayout gridLayout = new GridLayout();
       
   283 		gridLayout.numColumns = 2;
       
   284 		rofsObySymbolComposite = new Composite(composite, SWT.NONE);
       
   285 		rofsObySymbolComposite.setLayout(gridLayout);
       
   286 		rofsObySymbolComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   287 		rofsObySymbolTable = new Table(rofsObySymbolComposite, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.MULTI );
       
   288 		rofsObySymbolTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   289 		GridLayout buttonGridLayout = new GridLayout();
       
   290 		buttonGridLayout.numColumns = 1;
       
   291 		// custom widget for multiple line
       
   292 		rofsObySymbolTable.addListener(SWT.MeasureItem, new Listener() {
       
   293 			public void handleEvent(Event event) {
       
   294 				TableItem item = (TableItem)event.item;
       
   295 				String text = item.getText(event.index);
       
   296 				Point size = event.gc.textExtent(text);
       
   297 				event.width = Math.max(event.width, size.x + 2 * TEXT_MARGIN);
       
   298 				event.height = Math.max(event.height, size.y + 2 * TEXT_MARGIN);
       
   299 			}});
       
   300 		rofsObySymbolTable.addListener(SWT.EraseItem, new Listener() {
       
   301 			public void handleEvent(Event event) {
       
   302 				event.detail &= ~SWT.FOREGROUND;
       
   303 			}
       
   304 		});
       
   305 		rofsObySymbolTable.addListener(SWT.PaintItem, new Listener() {
       
   306 
       
   307 			public void handleEvent(Event event) {
       
   308 				TableItem item = (TableItem)event.item;
       
   309 				String text = item.getText(event.index);
       
   310 				event.gc.drawText(text, event.x, event.y, true);
       
   311 				event.gc.drawLine(event.x , event.y + event.height - TEXT_MARGIN, event.x + rofsObySymbolTable.getSize().x , event.y + event.height - TEXT_MARGIN);
       
   312 			}});
       
   313 		
       
   314 		rofsObySymbolTable.pack();
       
   315 		rofsObySymbolButtonComposite = new Composite(rofsObySymbolComposite, SWT.NONE);
       
   316 		rofsObySymbolButtonComposite.setLayout(buttonGridLayout);
       
   317 		rofsObySymbolButtonComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));
       
   318 		rofsAddButton = new Button(rofsObySymbolButtonComposite, SWT.NONE);
       
   319 		rofsAddButton.setText(Messages.getString("NewPIWizardPageRomSubTask.add")); //$NON-NLS-1$
       
   320 		rofsAddButton.addSelectionListener(new SelectionListener() {
       
   321 
       
   322 			public void widgetDefaultSelected(SelectionEvent arg0) {
       
   323 			}
       
   324 
       
   325 			public void widgetSelected(SelectionEvent arg0) {
       
   326 				NewPIWizardObySymbolPairDialog dialog = new NewPIWizardObySymbolPairDialog(getShell());
       
   327 				dialog.open();
       
   328 				RofsObySymbolPair pair = dialog.getPair();
       
   329 				if (pair.haveObyFile() || pair.haveSymbolFile() ) {
       
   330 					boolean found = false;
       
   331 					TableItem[] items = rofsObySymbolTable.getItems();
       
   332 					for (TableItem item : items) {
       
   333 						if (((RofsObySymbolPair)item.getData(ROFS_ITEM_PAIR_KEY)).equals(pair)) {
       
   334 							found = true;
       
   335 							break;
       
   336 						}
       
   337 					}
       
   338 					// don't add new entry if it's already there
       
   339 					if (found == false) {
       
   340 						TableItem newItem = new TableItem(rofsObySymbolTable, SWT.NONE);
       
   341 						newItem.setData(ROFS_ITEM_PAIR_KEY, pair);
       
   342 						newItem.setText(pair.getDisplayString());	
       
   343 					} else {
       
   344 						MessageDialog.openInformation(getShell(), Messages.getString("NewPIWizardPageRomSubTask.add.rofs.pair"), Messages.getString("NewPIWizardPageRomSubTask.entry.already.exisits")); //$NON-NLS-1$ //$NON-NLS-2$
       
   345 					}
       
   346 				}
       
   347 				writeRofsTableToStatus();
       
   348 			}
       
   349 		
       
   350 		});
       
   351 		rofsRemoveButton = new Button(rofsObySymbolButtonComposite, SWT.NONE);
       
   352 		rofsRemoveButton.setText(Messages.getString("NewPIWizardPageRomSubTask.remove")); //$NON-NLS-1$
       
   353 		rofsRemoveButton.addSelectionListener(new SelectionListener() {
       
   354 
       
   355 			public void widgetDefaultSelected(SelectionEvent arg0) {
       
   356 			}
       
   357 
       
   358 			public void widgetSelected(SelectionEvent arg0) {
       
   359 				rofsObySymbolTable.remove(rofsObySymbolTable.getSelectionIndices());
       
   360 				writeRofsTableToStatus();
       
   361 			}
       
   362 			
       
   363 		});
       
   364 		
       
   365 		GridData buttonWidthGridData = new GridData();
       
   366 		buttonWidthGridData.widthHint = Math.max(rofsAddButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x, rofsRemoveButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
       
   367 		rofsAddButton.setLayoutData(buttonWidthGridData);
       
   368 		rofsRemoveButton.setLayoutData(buttonWidthGridData);
       
   369 	}
       
   370 		
       
   371 	public void setupPageFromFromNewPIWizardSettings() {
       
   372 		NewPIWizardSettings npws = NewPIWizardSettings.getInstance();
       
   373 		sdkCommon.setSelectedSdk(npws.romSdk);
       
   374 		symbolText.setText(npws.romSymbolFile);
       
   375 		obyText.setText(npws.romObyFile);
       
   376 		rofsObySymbolTable.removeAll();
       
   377 		for(RofsObySymbolPair pair:npws.rofsObySymbolPairList) {
       
   378 			TableItem newItem = new TableItem(rofsObySymbolTable, SWT.NONE);
       
   379 			newItem.setData(ROFS_ITEM_PAIR_KEY, pair);
       
   380 			newItem.setText(pair.getDisplayString());
       
   381 		}
       
   382 	}
       
   383 
       
   384 	public void setVisible(boolean visable) {
       
   385 		super.setVisible(visable);
       
   386 		if (visable) {
       
   387 //			 block the text if it's wrong on startup, so user know what's wrong
       
   388 			if (visableBefore == false) {
       
   389 				String errorMessage = getErrorMessage();
       
   390 				if (errorMessage != null) {
       
   391 					if (errorMessage.startsWith(Messages.getString("NewPIWizardPageRomSubTask.symbol.label"))) { //$NON-NLS-1$
       
   392 						symbolText.selectAll();
       
   393 						symbolText.setFocus();
       
   394 					} else if (errorMessage.startsWith(Messages.getString("NewPIWizardPageRomSubTask.oby.label"))) { //$NON-NLS-1$
       
   395 						obyText.selectAll();
       
   396 						symbolText.setFocus();
       
   397 					}
       
   398 				}
       
   399 			}
       
   400 			lastGoodDir = null;
       
   401 		}
       
   402 		visableBefore = visable;
       
   403 	}
       
   404 }