crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/ui/editors/RegistersPage.java
changeset 4 615035072f7e
equal deleted inserted replaced
3:431bbaccaec8 4:615035072f7e
       
     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 "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.s60tools.crashanalyser.ui.editors;
       
    19 
       
    20 import java.util.HashMap;
       
    21 import java.util.List;
       
    22 import java.util.Map;
       
    23 
       
    24 import org.eclipse.jface.resource.FontRegistry;
       
    25 import org.eclipse.swt.SWT;
       
    26 import org.eclipse.swt.custom.SashForm;
       
    27 import org.eclipse.swt.events.SelectionEvent;
       
    28 import org.eclipse.swt.events.SelectionListener;
       
    29 import org.eclipse.swt.graphics.FontData;
       
    30 import org.eclipse.swt.layout.FormAttachment;
       
    31 import org.eclipse.swt.layout.FormData;
       
    32 import org.eclipse.swt.layout.FormLayout;
       
    33 import org.eclipse.swt.layout.GridData;
       
    34 import org.eclipse.swt.layout.GridLayout;
       
    35 import org.eclipse.swt.widgets.Combo;
       
    36 import org.eclipse.swt.widgets.Composite;
       
    37 import org.eclipse.swt.widgets.Display;
       
    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 import org.eclipse.swt.widgets.Text;
       
    43 import org.eclipse.ui.PlatformUI;
       
    44 
       
    45 import com.nokia.s60tools.crashanalyser.containers.Process;
       
    46 import com.nokia.s60tools.crashanalyser.containers.Register;
       
    47 import com.nokia.s60tools.crashanalyser.containers.RegisterBit;
       
    48 import com.nokia.s60tools.crashanalyser.containers.RegisterDetails;
       
    49 import com.nokia.s60tools.crashanalyser.containers.RegisterSet;
       
    50 import com.nokia.s60tools.crashanalyser.containers.Thread;
       
    51 import com.nokia.s60tools.crashanalyser.files.CrashFile;
       
    52 import com.nokia.s60tools.crashanalyser.files.SummaryFile;
       
    53 import com.nokia.s60tools.crashanalyser.resources.HelpContextIDs;
       
    54 
       
    55 public class RegistersPage implements SelectionListener {
       
    56 	// registers group UI items
       
    57 	Combo comboRegisters;
       
    58 	Table tableRegisters;
       
    59 
       
    60 	// CPSR details group UI items
       
    61 	Group groupCpsrEmpty;
       
    62 	Combo comboCpsrDetails;
       
    63 	Table tableCpsrDetails;
       
    64 
       
    65 	FontRegistry fontRegistry;
       
    66 	SummaryFile crashFile;
       
    67 	Process selectedProcess = null;
       
    68 	Thread selectedThread = null;
       
    69 
       
    70 	
       
    71 	/**
       
    72 	 * Creates the page
       
    73 	 * @param parent composite
       
    74 	 * @param file summary file
       
    75 	 * @return composite
       
    76 	 */
       
    77 	public Composite createPage(Composite parent, SummaryFile file) {
       
    78 		crashFile = file;
       
    79 		return doCreatePage(parent);
       
    80 	}
       
    81 	
       
    82 	/**
       
    83 	 * Creates the page
       
    84 	 * @param parent composite
       
    85 	 * @return composite
       
    86 	 */
       
    87 	public Composite createPage(Composite parent) {
       
    88 		return doCreatePage(parent);
       
    89 	}
       
    90 	
       
    91 	/**
       
    92 	 * Loads data from given file into UI elements.
       
    93 	 * @param file crash file
       
    94 	 */
       
    95 	public void setFile(CrashFile file) {
       
    96 		if (file != null) {
       
    97 			crashFile = file;
       
    98 			initialUserRegistersTableLoad();
       
    99 			initialCpsrDetailsTableLoad();
       
   100 		}
       
   101 	}
       
   102 	
       
   103 	/**
       
   104 	 * Creates all UI elements to the page
       
   105 	 * @param parent
       
   106 	 * @return composite
       
   107 	 */
       
   108 	Composite doCreatePage(Composite parent) {
       
   109 		GridLayout layout = new GridLayout();
       
   110 		layout.numColumns = 1;
       
   111 		parent.setLayout(layout);
       
   112 		parent.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   113 		
       
   114 		fontRegistry = new FontRegistry(Display.getCurrent());
       
   115 		fontRegistry.put("monospace", new FontData[]{new FontData("Courier", 8, SWT.NORMAL)});
       
   116 
       
   117 		SashForm sashFormMain = new SashForm(parent, SWT.VERTICAL);
       
   118 		sashFormMain.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   119 //		SashForm sashFormTop = new SashForm(sashFormMain, SWT.HORIZONTAL);
       
   120 		createRegistersGroup(sashFormMain);
       
   121 //		createCodeSegmentsGroup(sashFormTop);
       
   122 //		SashForm sashFormBottom = new SashForm(sashFormMain, SWT.HORIZONTAL);
       
   123 //		createEventLogGroup(sashFormBottom);
       
   124 		createCpsrDetailsGroup(sashFormMain);
       
   125 		
       
   126 //		sashFormTop.setWeights(new int[]{3,2});
       
   127 //		sashFormBottom.setWeights(new int[]{3,7});
       
   128 			
       
   129 		setHelps();
       
   130 		
       
   131 		return parent;
       
   132 	}
       
   133 
       
   134 	/**
       
   135 	 * Creates registers group
       
   136 	 * @param parent
       
   137 	 */
       
   138 	void createRegistersGroup(Composite parent) {
       
   139 		Group groupRegisters = new Group(parent, SWT.NONE);
       
   140 		GridLayout layout = new GridLayout();
       
   141 		layout.numColumns = 2;
       
   142 		groupRegisters.setLayout(layout);
       
   143 		groupRegisters.setText("Registers");
       
   144 		GridData groupGD = new GridData(GridData.FILL_HORIZONTAL);
       
   145 		groupRegisters.setLayoutData(groupGD);
       
   146 		
       
   147 		comboRegisters = new Combo(groupRegisters, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
       
   148 		GridData comboGD = new GridData();
       
   149 		comboGD.horizontalSpan = 2;
       
   150 		comboRegisters.setLayoutData(comboGD);
       
   151 		comboRegisters.addSelectionListener(this);
       
   152 		
       
   153 		tableRegisters = new Table(groupRegisters, SWT.BORDER);
       
   154 		tableRegisters.setHeaderVisible(true);
       
   155 		tableRegisters.setLinesVisible(true);
       
   156 		TableColumn col = new TableColumn(tableRegisters, SWT.LEFT);
       
   157 		col.setWidth(100);
       
   158 		col.setText("Register");
       
   159 		col = new TableColumn(tableRegisters, SWT.LEFT);
       
   160 		col.setWidth(100);
       
   161 		col.setText("Value");
       
   162 		col = new TableColumn(tableRegisters, SWT.LEFT);
       
   163 		col.setWidth(100);
       
   164 		col.setText("Symbol");
       
   165 		col = new TableColumn(tableRegisters, SWT.LEFT);
       
   166 		col.setWidth(100);
       
   167 		col.setText("Comment");
       
   168 		tableRegisters.setFont(fontRegistry.get("monospace"));
       
   169 
       
   170 		GridData tableGd = new GridData(GridData.FILL_BOTH);
       
   171 		tableGd.horizontalSpan = 2;
       
   172 		tableRegisters.setLayoutData(tableGd);
       
   173 
       
   174 		initialUserRegistersTableLoad();
       
   175 	}
       
   176 		/**
       
   177 		 * Creates cpsr details group
       
   178 		 * @param parent
       
   179 		 */
       
   180 		void createCpsrDetailsGroup(Composite parent) {
       
   181 			Group groupCpsrDetails = new Group(parent, SWT.NONE);
       
   182 			GridLayout layout = new GridLayout();
       
   183 			layout.numColumns = 1;
       
   184 			groupCpsrDetails.setLayout(layout);
       
   185 			groupCpsrDetails.setText("CPSR Details");
       
   186 			GridData groupGD = new GridData(GridData.FILL_VERTICAL);
       
   187 			groupCpsrDetails.setLayoutData(groupGD);
       
   188 			
       
   189 			comboCpsrDetails = new Combo(groupCpsrDetails, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
       
   190 			comboCpsrDetails.addSelectionListener(this);
       
   191 
       
   192 			// Empty group
       
   193 			groupCpsrEmpty = new Group(groupCpsrDetails, SWT.NONE);
       
   194 			FormLayout fl = new FormLayout();
       
   195 			fl.marginRight = 5;
       
   196 			fl.marginLeft = 5;
       
   197 			fl.marginBottom = 5;
       
   198 			groupCpsrEmpty.setLayout(fl);
       
   199 			GridData groupCpsrEmptyGd = new GridData(GridData.FILL_HORIZONTAL);
       
   200 			groupCpsrEmptyGd.horizontalAlignment = SWT.FILL;
       
   201 			groupCpsrEmptyGd.grabExcessHorizontalSpace = true;
       
   202 			groupCpsrEmpty.setLayoutData(groupCpsrEmptyGd);
       
   203 
       
   204 			// create 32 empty text boxes
       
   205 			for (int i = 0; i < 32; i++) {
       
   206 				Text text = new Text(groupCpsrEmpty, SWT.READ_ONLY | SWT.BORDER | SWT.NO_FOCUS | SWT.CENTER);
       
   207 				text.setFont(fontRegistry.get("monospace"));
       
   208 				FormData textFd = new FormData();
       
   209 				textFd.left = new FormAttachment(0,i*20);
       
   210 				textFd.top = new FormAttachment(8, 15);
       
   211 				textFd.width = 8;
       
   212 				text.setLayoutData(textFd); 			
       
   213 			}
       
   214 
       
   215 			tableCpsrDetails = new Table(groupCpsrDetails, SWT.BORDER);
       
   216 			tableCpsrDetails.setLinesVisible(true);
       
   217 			tableCpsrDetails.setHeaderVisible(true);
       
   218 			TableColumn col = new TableColumn(tableCpsrDetails, SWT.LEFT);
       
   219 			col.setWidth(380);
       
   220 			col.setText("Name");
       
   221 			col = new TableColumn(tableCpsrDetails, SWT.LEFT);
       
   222 			col.setWidth(220);
       
   223 			col.setText("Value");
       
   224 			GridData tableGd = new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL);
       
   225 			tableGd.horizontalAlignment = SWT.FILL;
       
   226 			tableGd.grabExcessHorizontalSpace = true;
       
   227 			tableCpsrDetails.setLayoutData(tableGd);
       
   228 			tableCpsrDetails.setFont(fontRegistry.get("monospace"));
       
   229 			initialCpsrDetailsTableLoad();
       
   230 		}
       
   231 		
       
   232 		
       
   233 		/**
       
   234 		 * Loads register sets to combo, selects the correct set as
       
   235 		 * default set and then loads set's registers to table.
       
   236 		 */
       
   237 		void initialUserRegistersTableLoad() {
       
   238 			if (crashFile == null)
       
   239 				return;
       
   240 
       
   241 			int defaultSelectionIndex = 0;
       
   242 
       
   243 			// show the data of crashed process
       
   244 			if (crashFile.getThread() != null) {
       
   245 				selectedProcess = crashFile.getProcessByThread(crashFile.getThread().getId());
       
   246 			} else {
       
   247 				selectedProcess = crashFile.getCrashedProcess();
       
   248 			}
       
   249 				
       
   250 			if (selectedProcess != null) {
       
   251 				// current UI support only one thread, so show the first thread of first process
       
   252 				if(crashFile.getThread() != null) {
       
   253 					selectedThread = crashFile.getThread();
       
   254 				} else {
       
   255 					selectedThread = crashFile.getCrashedThread();
       
   256 				}
       
   257 				
       
   258 				if (selectedThread != null) {
       
   259 					List<RegisterSet> registerSets = selectedThread.getRegisters();
       
   260 					if (registerSets != null && !registerSets.isEmpty()) {
       
   261 						// load all register sets to combo
       
   262 						for (int i = 0; i < registerSets.size(); i++) {
       
   263 							RegisterSet registerSet = registerSets.get(i);
       
   264 							// default register to show is the one that contains CPSR register
       
   265 							if (registerSet.containsCPSR()) 
       
   266 								defaultSelectionIndex = i;
       
   267 							comboRegisters.add(registerSet.getName());
       
   268 							comboRegisters.setData(registerSet.getName(), registerSet);
       
   269 						}
       
   270 					}
       
   271 				}
       
   272 			}
       
   273 			
       
   274 			List<RegisterSet> standAloneRegisters = crashFile.getStandAloneRegisterSets();
       
   275 			if (standAloneRegisters != null && !standAloneRegisters.isEmpty()) {
       
   276 				for (int i = 0; i < standAloneRegisters.size(); i++) {
       
   277 					RegisterSet regSet = standAloneRegisters.get(i);
       
   278 					if (regSet.containsCPSR())
       
   279 						defaultSelectionIndex = comboRegisters.getItemCount() + i;
       
   280 					comboRegisters.add(regSet.getName());
       
   281 					comboRegisters.setData(regSet.getName(), regSet);
       
   282 				}
       
   283 			}
       
   284 			
       
   285 			if (comboRegisters.getItemCount() > 0) {
       
   286 				comboRegisters.select(defaultSelectionIndex);
       
   287 				loadRegistersTable();
       
   288 			}
       
   289 			
       
   290 		}
       
   291 		
       
   292 		/**
       
   293 		 * Loads user registers table according to which registers set is
       
   294 		 * selected in combo. 
       
   295 		 */
       
   296 		void loadRegistersTable() {
       
   297 			tableRegisters.removeAll();
       
   298 			
       
   299 			if (comboRegisters.getItemCount() < 1)
       
   300 				return;
       
   301 			
       
   302 			try {
       
   303 				RegisterSet registerSet = (RegisterSet)comboRegisters.getData(comboRegisters.getText());
       
   304 				List<Register> registers = registerSet.getRegisters();
       
   305 				if (registers != null && !registers.isEmpty()) {
       
   306 					// show all register values in selected register set
       
   307 					for (int i = 0; i < registers.size(); i++) {
       
   308 						Register register = registers.get(i);
       
   309 						newRegistersTableItem(register);
       
   310 					}
       
   311 					AutoSizeTableCells(tableRegisters);
       
   312 				}
       
   313 			} catch (Exception e) {
       
   314 				e.printStackTrace();
       
   315 			}
       
   316 		}
       
   317 
       
   318 		/**
       
   319 		 * Loads cprs types to combo, selects first type and then loads it's
       
   320 		 * data to table.
       
   321 		 */
       
   322 		void initialCpsrDetailsTableLoad() {
       
   323 			if (crashFile == null)
       
   324 				return;
       
   325 
       
   326 			List<RegisterDetails> registerDetails = crashFile.getRegisterDetails();
       
   327 			if (registerDetails != null && !registerDetails.isEmpty()) {
       
   328 				for (int i = 0; i < registerDetails.size(); i++) {
       
   329 					RegisterDetails regDetails = registerDetails.get(i);
       
   330 					if (regDetails.getDescription().contains("CPSR")) {
       
   331 						comboCpsrDetails.add(regDetails.getDescription());
       
   332 						comboCpsrDetails.setData(regDetails.getDescription(), regDetails);
       
   333 					}
       
   334 				}
       
   335 				comboCpsrDetails.select(0);
       
   336 				loadCpsrDetailsTable();
       
   337 			}		
       
   338 		}
       
   339 		
       
   340 		/**
       
   341 		 * Loads currently selected cprs's details into table.
       
   342 		 */
       
   343 		void loadCpsrDetailsTable() {
       
   344 			try {
       
   345 				tableCpsrDetails.removeAll();
       
   346 
       
   347 				RegisterDetails registerDetails = null;
       
   348 				if (comboRegisters.getSelectionIndex() >= 0)
       
   349 					registerDetails = (RegisterDetails)comboCpsrDetails.getData(comboCpsrDetails.getText());
       
   350 
       
   351 				if (registerDetails != null) {
       
   352 					boolean bitStartFromRight = registerDetails.bitsStartFromRight();
       
   353 					Map<String, String> categories = new HashMap<String, String>();
       
   354 					// go through all 32 text boxes, and see if there is a bit for each box
       
   355 					for (int i = 0, j = 31; i < 32; i++, j--) {
       
   356 						Text box = (Text)groupCpsrEmpty.getChildren().clone()[j];
       
   357 						// bits can start from the right or left (endianess)
       
   358 						if (!bitStartFromRight)
       
   359 							box = (Text)groupCpsrEmpty.getChildren().clone()[i];
       
   360 						RegisterBit bit = registerDetails.getBit(i);
       
   361 						// there's no bit for this box
       
   362 						if (bit == null) {
       
   363 							box.setText("");
       
   364 							box.setToolTipText("");
       
   365 							// there is a bit for this box
       
   366 						} else {
       
   367 							String regChar = bit.getRegisterChar();
       
   368 							String category = bit.getCategory();
       
   369 							String interpretation = bit.getInterpretation();
       
   370 							// if this bit contains a longer description, it should be shown in the table also
       
   371 							if (!"".equals(interpretation) && !categories.containsKey(category)) {
       
   372 								categories.put(category, interpretation);
       
   373 							}
       
   374 							box.setText(regChar);
       
   375 							box.setToolTipText(category);
       
   376 						}
       
   377 					}
       
   378 				
       
   379 					// load table
       
   380 					for (int i = 0; i < categories.size(); i++) {
       
   381 						newCpsrDetailsTableItem(categories.keySet().toArray()[i].toString(),
       
   382 											categories.values().toArray()[i].toString());
       
   383 					}
       
   384 				}
       
   385 			} catch (Exception e) {
       
   386 				e.printStackTrace();
       
   387 			}
       
   388 		}
       
   389 		
       
   390 		/**
       
   391 		 * Adds a new table row for registers table
       
   392 		 * @param register row data
       
   393 		 */
       
   394 		void newRegistersTableItem(Register register) {
       
   395 			TableItem item = new TableItem(tableRegisters, SWT.NONE);
       
   396 			item.setText(new String[] {register.getName(),
       
   397 										register.getValue(),
       
   398 										register.getSymbol(),
       
   399 										register.getComments()});
       
   400 		}	
       
   401 
       
   402 		/**
       
   403 		 * Adds a new table row for cpsr details table
       
   404 		 * @param header header text
       
   405 		 * @param value value text
       
   406 		 */
       
   407 		void newCpsrDetailsTableItem(String header, String value) {
       
   408 			TableItem item = new TableItem(tableCpsrDetails, SWT.NONE);
       
   409 			item.setText(new String[] {header, value});
       
   410 		}
       
   411 
       
   412 		/**
       
   413 		 * Packs all columns for given table
       
   414 		 * @param table table which columns are to be packed
       
   415 		 */
       
   416 		void AutoSizeTableCells(Table table) {
       
   417 			for (int i = 0; i < table.getColumnCount(); i++) {
       
   418 				table.getColumn(i).pack();
       
   419 			}
       
   420 		}
       
   421 		
       
   422 		public void widgetDefaultSelected(SelectionEvent arg0) {
       
   423 			// No implementation needed
       
   424 		}
       
   425 
       
   426 		public void widgetSelected(SelectionEvent event) {
       
   427 			// cpsr was changed in combo
       
   428 			if (event.widget == comboCpsrDetails) {
       
   429 				loadCpsrDetailsTable();
       
   430 			// register set was changed in combo
       
   431 			} else if (event.widget == comboRegisters) {
       
   432 				loadRegistersTable();
       
   433 			}
       
   434 		}
       
   435 		
       
   436 		/**
       
   437 		 * Sets context sensitive help ids to UI elements
       
   438 		 */
       
   439 		void setHelps() {
       
   440 			PlatformUI.getWorkbench().getHelpSystem().setHelp(tableCpsrDetails,
       
   441 					HelpContextIDs.CRASH_ANALYSER_HELP_CRASH_VISUALISER);	
       
   442 			PlatformUI.getWorkbench().getHelpSystem().setHelp(tableRegisters,
       
   443 					HelpContextIDs.CRASH_ANALYSER_HELP_CRASH_VISUALISER);	
       
   444 			PlatformUI.getWorkbench().getHelpSystem().setHelp(tableRegisters,
       
   445 					HelpContextIDs.CRASH_ANALYSER_HELP_CRASH_VISUALISER);	
       
   446 			PlatformUI.getWorkbench().getHelpSystem().setHelp(comboCpsrDetails,
       
   447 					HelpContextIDs.CRASH_ANALYSER_HELP_CRASH_VISUALISER);	
       
   448 			PlatformUI.getWorkbench().getHelpSystem().setHelp(comboRegisters,
       
   449 					HelpContextIDs.CRASH_ANALYSER_HELP_CRASH_VISUALISER);	
       
   450 		}
       
   451 		
       
   452 	}
       
   453 
       
   454