src/main/java/xelfi/debugger/CallStackView.java
author Jaroslav Tulach <jaroslav.tulach@xelfi.cz>
Tue, 17 Jan 2017 21:12:37 +0100
branchDirtyFix
changeset 10 fe294d0f1297
parent 0 189280700bc7
permissions -rw-r--r--
Making the project compilable
     1 /**
     2  * CallStackView
     3  *
     4  * @author Roman Blazevic
     5  * @version 970208
     6  */
     7 
     8 package xelfi.debugger;
     9 
    10 import java.awt.*;
    11 import xelfi.top.TopLevel;
    12 import xelfi.top.TopDialog;
    13 
    14 public class CallStackView extends TopDialog
    15 {
    16 	private Debugger document;
    17 	private List list;
    18 	private RemoteStackFrame[] csf = null; // used for call stack choosing
    19 
    20 	static final int KEY_ENTER = 10;
    21 
    22 	static final int HINT_COMPLETE_UPDATE = 0;
    23 
    24 	/**
    25 	 * CallStackView constructor.
    26 	 *
    27 	 * debugger - the document for this view
    28 	 */
    29 	CallStackView(Debugger debugger)
    30 	{
    31 		super(TopLevel.getMenu(), false);
    32 		setTitle("Call Stack");
    33 		//super("Call Stack");
    34 		setBackground(Color.lightGray);
    35 
    36 		document = debugger;
    37 		list = new List();
    38 		add("Center", list);
    39 	}
    40 
    41 	/**
    42 	 * My handleEvent() implementation.
    43 	 */
    44 	public boolean handleEvent(Event event)
    45 	{
    46 		// items with index csf.length-i-2 contains info about stack frame with index i (variable csf[i])
    47 		int selectedIndex = list.getSelectedIndex();
    48 
    49 		if ((event.id == Event.KEY_PRESS && event.key == KEY_ENTER)
    50 			|| (event.id == Event.ACTION_EVENT))
    51 		{
    52 			if (selectedIndex != -1)
    53 			{
    54 				int stackFrameIndex = csf.length-selectedIndex-2;
    55 
    56 				document.stackFrameChosen(csf[stackFrameIndex]);
    57 			}
    58 			return true;
    59 		}
    60 
    61 		return super.handleEvent(event);
    62 	}
    63 
    64 	/**
    65 	 * This method is called when the content of the view must be
    66 	 * updated.
    67 	 */
    68 	void update(int hint, int index)
    69 	{
    70 		if (!isVisible())
    71 			return;
    72 
    73 		if (hint != HINT_COMPLETE_UPDATE)
    74 		{
    75 			System.out.println("unsupported hint");
    76 			return;
    77 		}
    78 
    79 		csf = document.getCurrentStackFrames();
    80 
    81 		if (csf == null)
    82 		{
    83 			list.clear();
    84 			return;
    85 		}
    86 
    87 		int indexOfSelected = list.getSelectedIndex();
    88 		if (indexOfSelected != -1)
    89 			list.deselect(indexOfSelected);
    90 
    91 		int currentStackFrameIndex = document.getCurrentStackFrameIndex();
    92 		int numberOfLines = csf.length-currentStackFrameIndex-1; // without "system" frame
    93 		// int numberOfLines = csf.length-currentStackFrameIndex;
    94 
    95 		if (numberOfLines < list.countItems())
    96 			list.delItems(numberOfLines, list.countItems()-1);
    97 
    98 		for(int i = csf.length-2; i >= currentStackFrameIndex; i--) // without "system" frame
    99 	{
   100 			// for(int i = csf.length-1; i >= currentStackFrameIndex; i--)
   101 			// "system" frame is that one with index csf.length-1
   102 
   103 			try
   104 			{
   105         		RemoteStackVariable lv[] = csf[i].getLocalVariables();
   106 				String string = new String();
   107 				int lastParameterIndex = -1;
   108 
   109 				for(int j = lv.length-1; j >= 0; j--)
   110 					if (lv[j].methodArgument())
   111 					{
   112 						lastParameterIndex = j; // the last method argument is here
   113 						break;
   114 					}
   115 
   116 				for(int j = 0; j <= lastParameterIndex; j++)
   117 					if (lv[j].methodArgument())
   118 					{
   119 						String name = lv[j].getName();
   120 
   121 						string += name+": "+document.getCurrentValue(name /*, i*/);
   122 						if (j != lastParameterIndex)
   123 							string += ", ";
   124 					}
   125 
   126 				// put one call stack line into the 'string' variable
   127 				string = " ["+(csf.length-i-1)+"] "+csf[i].getRemoteClass().getName()+"."
   128 					+csf[i].getMethodName()+"("+string+")";
   129 
   130 				if (csf.length-i-1 > list.countItems())
   131 					list.addItem(string);
   132 				else
   133 					if (list.getItem(csf.length-i-2).compareTo(string) != 0)
   134 						list.replaceItem(string, csf.length-i-2);
   135 			}
   136 			catch(Exception e)
   137 			{
   138 				System.out.println(e);
   139 			}
   140 		}
   141 	}
   142 
   143 	/**
   144 	 * My show() implementation.
   145 	 */
   146 	public void show()
   147 	{
   148 		super.show();
   149 		list.requestFocus();
   150 		document.callStackViewShown();
   151 	}
   152 }