aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem loading updated input while expanding in Treeviewer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem loading updated input while expanding in Treeviewer" Watch "Problem loading updated input while expanding in Treeviewer" New topic
Author

Problem loading updated input while expanding in Treeviewer

Subash Murugan
Greenhorn

Joined: Mar 26, 2008
Posts: 21
Hi Folks,

I am new to JFace and creating an application in TreeViewer. While expanding in the tree I wanted to update teh input field by reading the file again. I was trying to set the input bu the UI is still showing with teh old object. In Sys out I see the object is updated though, but the UI is not getting updated. I tried using the viewer.setInput again in the treeExpanded(TreeExpansionEvent e) listner to call the inputChanged method but the UI is refreshed and it shows the initial UI.

Can someone please help me out where am missing something?

I attached the java pgm here,

and the input file profileDetails.txt is having values like,

1#1#0#com.TestMe$<init>#0
1#2#0#com.TestMe$<init>#0
1#0#-1#com.TestMe$main#0
10#2#1#com.TestMe$t2#5000
11#2#1#com.TestMe$t2#5000
1#2#2#com.TestMe$<init>#0
1#0#1#com.TestMe$main#0
10#3#1#com.TestMe$t3#0
10#1#0#com.TestMe$t1#5000
10#0#-1#com.TestMe$run#5000
1#2#3#com.TestMe$<init>#0
1#0#2#com.TestMe$main#0
11#3#1#com.TestMe$t3#0
11#1#0#com.TestMe$t1#5000
11#0#-1#com.TestMe$run#5000


Please help me out in this..

Thanks a lot

Regards
Subash
Subash Murugan
Greenhorn

Joined: Mar 26, 2008
Posts: 21
Hi am not able to attach my file am giving my code here, please bear with the size and have a look, its a stand alone java pgm, can anyone please try in eclipse and figure out the mistakes am doing here?

package com.ebay.swat.jprofiler.view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ITreeViewerListener;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeExpansionEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;



/**
* A simple TreeViewer to demonstrate usage
*
* @author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class TestPgm2 {

private int listIndex = -1;
private boolean positionChildFlag = false;
private ArrayList<ProfilerInputModel> threadDetailsList = new ArrayList();
private String profilerInputFile = "";
File profilerFileObj = null;
BufferedReader readBuffer = null;
private TreeViewer v = null;

public TestPgm2(final Shell shell) {
//Button b = new Button(shell,SWT.PUSH);
//b.setText("Remove column");
v = new TreeViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
v.getTree().setLinesVisible(true);
v.getTree().setHeaderVisible(true);


//
// TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(v,new FocusCellOwnerDrawHighlighter(v));
// ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
// protected boolean isEditorActivationEvent(
// ColumnViewerEditorActivationEvent event) {
// return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
// || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
// || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
// || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
// }
// };
//
// TreeViewerEditor.create(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
// | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
// | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

final TextCellEditor textCellEditor = new TextCellEditor(v.getTree());

TreeViewerColumn column = new TreeViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Thread Name");

column.setLabelProvider(new ColumnLabelProvider() {

public String getText(Object element) {
return ((ProfilerInputModel) element).threadDetails.getThreadName();
}

});
column.setEditingSupport(new EditingSupport(v) {
protected boolean canEdit(Object element) {
return true;
}

protected CellEditor getCellEditor(Object element) {
return textCellEditor;

}

protected Object getValue(Object element) {
return ((ProfilerInputModel) element).threadDetails.getThreadName() + "";
}

protected void setValue(Object element, Object value) {
((ProfilerInputModel) element).threadDetails.setThreadName(value.toString());
v.update(element, null);
}
});

column = new TreeViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Method id");
column.setLabelProvider(new ColumnLabelProvider() {

public String getText(Object element) {
return ((ProfilerInputModel) element).threadDetails.getThreadId();
}

});
column.setEditingSupport(new EditingSupport(v) {
protected boolean canEdit(Object element) {
return true;
}

protected CellEditor getCellEditor(Object element) {
return textCellEditor;
}

protected Object getValue(Object element) {
return ((ProfilerInputModel) element).threadDetails.getThreadId() + "";
}

protected void setValue(Object element, Object value) {
((ProfilerInputModel) element).threadDetails.setThreadId(value.toString());
v.update(element, null);
}
});

column = new TreeViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Parent Id");
column.setLabelProvider(new ColumnLabelProvider() {

public String getText(Object element) {
return ((ProfilerInputModel) element).threadDetails.getParentId();
}

});
column.setEditingSupport(new EditingSupport(v) {
protected boolean canEdit(Object element) {
return true;
}
protected CellEditor getCellEditor(Object element) {
return textCellEditor;
}

protected Object getValue(Object element) {
return ((ProfilerInputModel) element).threadDetails.getParentId() + "";
}

protected void setValue(Object element, Object value) {
((ProfilerInputModel) element).threadDetails.setParentId(value.toString());
v.update(element, null);
}
});



column = new TreeViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Method Name");
column.setLabelProvider(new ColumnLabelProvider() {

public String getText(Object element) {
return ((ProfilerInputModel) element).threadDetails.getMethodName();
}

});
column.setEditingSupport(new EditingSupport(v) {
protected boolean canEdit(Object element) {
return true;
}

protected CellEditor getCellEditor(Object element) {
return textCellEditor;
}

protected Object getValue(Object element) {
return ((ProfilerInputModel) element).threadDetails.getMethodName() + "";
}

protected void setValue(Object element, Object value) {
((ProfilerInputModel) element).threadDetails.setMethodName(value.toString());
v.update(element, null);
}
});

column = new TreeViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Execution Time");
column.setLabelProvider(new ColumnLabelProvider() {

public String getText(Object element) {
return ((ProfilerInputModel) element).threadDetails.getExecutionTime();
}

});
column.setEditingSupport(new EditingSupport(v) {
protected boolean canEdit(Object element) {
return true;
}

protected CellEditor getCellEditor(Object element) {
return textCellEditor;
}

protected Object getValue(Object element) {
return ((ProfilerInputModel) element).threadDetails.getExecutionTime() + "";
}

protected void setValue(Object element, Object value) {
((ProfilerInputModel) element).threadDetails.setExecutionTime(value.toString());
v.update(element, null);
}
});


v.setContentProvider(new MyProfileContentProvider());
v.addTreeListener(new ITreeViewerListener(){


public void treeExpanded(TreeExpansionEvent e) {
String strLine ="";
readBuffer = null;
try {
readBuffer = new BufferedReader(new FileReader(profilerFileObj));

System.out.println("Element "+((ProfilerInputModel)e.getElement()).threadName);

String threadName = "";
String threadMethodId = "";
String threadParentId = "";
int threadIndex = -1;
ProfilerInputModel element = new ProfilerInputModel();
ProfilerInputModel element1 = (ProfilerInputModel)e.getElement();
boolean entryFlag = false;

threadName = ((ProfilerInputModel)e.getElement()).threadName;
threadMethodId = ((ProfilerInputModel)e.getElement()).threadDetails.getThreadId();
threadParentId = ((ProfilerInputModel)e.getElement()).threadDetails.getParentId();
if(threadParentId.equals("-1"))
{
threadParentId = "0";
entryFlag = true;
}

System.out.println("test 1");
for(int i=0;i<threadDetailsList.size();i++)
{
System.out.println("test 2");
if(threadDetailsList.get(i).threadName.equals(threadName))
{
threadIndex = i;
break;
}
}

while((strLine = readBuffer.readLine())!=null)
{

String[] threadInfo = null;
threadInfo = strLine.split("#");
if(threadName.equals(threadInfo[0])&& threadParentId.equals(threadInfo[2]))
{
System.out.println("test 3"+threadParentId);
ProfilerInputModel profilerInputModel = new ProfilerInputModel();
ThreadDetails td = new ThreadDetails();
td.setThreadName(threadInfo[0]);
profilerInputModel.threadName=threadInfo[0];
td.setThreadId(threadInfo[1]);
td.setParentId(threadInfo[2]);
td.setMethodName(threadInfo[3]);
td.setExecutionTime(threadInfo[4]);
profilerInputModel.threadDetails = td;
if(entryFlag)
{
entryFlag = false;
threadDetailsList.get(threadIndex).profileInputModelChildList.remove(0);
}
threadDetailsList.get(threadIndex).profileInputModelChildList.add(profilerInputModel);
//element1.profileInputModelChildList.add(profilerInputModel);
element1.updateChildProfile(profilerInputModel);
}

}

//element.profileInputModelChildList = threadDetailsList;
//v.setInput(element);
//v.expandAll();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

public void treeCollapsed(TreeExpansionEvent e) {
System.out.println("Testing doulbe click");
}

});

v.setUseHashlookup(true);
v.setInput(createProfileInputModel());
}


public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new TestPgm2(shell);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();
}


private class MyProfileContentProvider implements ITreeContentProvider, ITreeViewerListener
{
protected TreeViewer viewer;
public Object[] getElements(Object inputElement) {
System.out.println("calling..1");
return ((ProfilerInputModel) inputElement).profileInputModelChildList.toArray();
}

public void dispose() {
}

public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

if (newInput != null)
((ProfilerInputModel)newInput).addListener(this);
if (oldInput != null)
((ProfilerInputModel) oldInput).removeListener(this);

System.out.println("Input changed: old=" + oldInput + ", new=" + newInput);
// this.getElements(newInput);
this.viewer = (TreeViewer)viewer;
//viewer.refresh();
// viewer.addTreeListener(

}

public Object[] getChildren(Object parentElement) {
System.out.println("calling..2");
return getElements(parentElement);
}

public Object getParent(Object element) {
System.out.println("calling..3");
if (element == null) {

return null;
}
return ((ProfilerInputModel) element);
}

public boolean hasChildren(Object element) {
System.out.println("calling..");
return (((ProfilerInputModel) element).profileInputModelChildList.size()>0) ;
}

@Override
public void treeCollapsed(TreeExpansionEvent event) {


// TODO Auto-generated method stub

}

@Override
public void treeExpanded(TreeExpansionEvent event) {
System.out.println("*********** Called here..");
Object profilerInputModel = ((ProfilerInputModel)event.getElement());
viewer.refresh(profilerInputModel, true);

// TODO Auto-generated method stub

}

}

public class ProfilerInputModel
{
////public ProfilerInputModel rootProfilerInputModel;
//public ProfilerInputModel childProfilerInputModel;

//public ArrayList child = new ArrayList();
ArrayList<ProfilerInputModel> profileInputModelChildList = new ArrayList();
ThreadDetails threadDetails ;
protected ITreeViewerListener listener = null;

public ProfilerInputModel(String threadName,String methodName, String childThread)
{
this.threadName = threadName;
this.methodName = methodName;
this.childThread = childThread;
}
public ProfilerInputModel(String profilerFile)
{
this.profilerFile = profilerFile;
}
public ProfilerInputModel(ProfilerInputModel rootProfilerInputModel)
{
//this.rootProfilerInputModel = rootProfilerInputModel;
}
public ProfilerInputModel()
{

}
public void addListener(ITreeViewerListener listener)
{
this.listener = listener;
}

/*
* @see IDeltaListener#remove(DeltaEvent)
*/
public void removeListener(ITreeViewerListener listener)
{
this.listener = null;
}

public void updateChildProfile(ProfilerInputModel profilerInputModel)
{
profileInputModelChildList.add(profilerInputModel);
}

public String profilerFile = "C:/smurugan/jprofiler/profileDetails.txt";

public String threadName ="thread 1";

public String methodName ="TestPgm.testCall";

public String childThread ="0";


}

public class ThreadDetails
{
public String threadName;
public String getThreadName() {
return threadName;
}
public void setThreadName(String threadName) {
this.threadName = threadName;
}
public String getThreadId() {
return threadId;
}
public void setThreadId(String threadId) {
this.threadId = threadId;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public String getExecutionTime() {
return executionTime;
}
public void setExecutionTime(String executionTime) {
this.executionTime = executionTime;
}
public String threadId;
public String parentId;
public String methodName;
public String executionTime;
}



private ProfilerInputModel createProfileInputModel() {

//ProfilerInputModel root = new ProfilerInputModel("thread 2","Test.test","1");
ProfilerInputModel root = new ProfilerInputModel();
//root.profileInputModelChildList = parseProfilerFile("C:/smurugan/jprofiler/profileDetails.txt");
root.profileInputModelChildList = populateProfilerModel("C:/smurugan/jprofiler/profileDetails.txt");

// for (int i = 1; i < 10; i++) {
// tmp = new ProfilerInputModel("thread "+i,"Test.test",String.valueOf(i));
// root.threadDetailsList.add(tmp);
// for (int j = 1; j < i; j++) {
// subItem = new ProfilerInputModel("thread "+j,"Test.test",String.valueOf(j));
// subItem.threadDetailsList.add(new ProfilerInputModel("thread "+j,"Test.test",String.valueOf(j)));
// tmp.threadDetailsList.add(subItem);
// }
// }

return root;
}

private ArrayList<ProfilerInputModel> populateProfilerModel(String profilerInputFile)
{
this.profilerInputFile = profilerInputFile;

try {
profilerFileObj = new File(profilerInputFile);
readBuffer = new BufferedReader(new FileReader(profilerFileObj));
String strLine;
int tempCounter = 0;
while((strLine = readBuffer.readLine())!=null)
{
System.out.println("tempCounter "+ ++tempCounter);
ProfilerInputModel profilerInputModel = null;



String[] threadInfo = strLine.split("#");
if(threadInfo[2].equals("-1"))
{
profilerInputModel = new ProfilerInputModel();
ThreadDetails td = new ThreadDetails();
td.setThreadName(threadInfo[0]);
profilerInputModel.threadName=threadInfo[0];
td.setThreadId(threadInfo[1]);
td.setParentId(threadInfo[2]);
td.setMethodName(threadInfo[3]);
td.setExecutionTime(threadInfo[4]);
profilerInputModel.threadDetails = td;

ProfilerInputModel tmpProfilerInputModel = new ProfilerInputModel();
ThreadDetails tds = new ThreadDetails();
tds.setThreadName("temp");
tmpProfilerInputModel.threadName="temp";
tds.setThreadId("temp");
tds.setParentId("temp");
tds.setMethodName("temp");
tds.setExecutionTime("temp");
tmpProfilerInputModel.threadDetails = td;

profilerInputModel.profileInputModelChildList.add(0,tmpProfilerInputModel);
threadDetailsList.add(profilerInputModel);
}
}
return threadDetailsList;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

}



}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Please Use Code Tags. You can edit your post to add them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem loading updated input while expanding in Treeviewer