使用TreeViewer取代TableTreeViewer

项目需要做一个TableTree形式的内容浏览器,以前开发的时候是基于Eclipse3.0,现在Eclipse3.2对于TableTree控件已经是deprecated了(但是TableTreeViewer还没有注明,会在Eclipse3.3中注明),所以不打算再使用TableTreeViewer了。TableTreeViewe的源码中说明是要从Eclipse3.1后,要是用 Tree, TreeItem 和 TreeColumn。以下的代码是使用Tree来实现TableTree的效果。
package org.talend.sqlbuilder;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableTreeViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

class Connection ... {

Stringname;

Stringreso;

Listchildren
=newArrayList();

}


class Table ... {

Stringname;

Stringreso;

}


class TestProvider extends LabelProvider implements ITableLabelProvider,ITreeContentProvider ... {

publicImagegetColumnImage(Objectelement,intcolumnIndex)...{

returnnull;
}


publicStringgetColumnText(Objectelement,intcolumnIndex)...{
switch(columnIndex)...{
case0:
if(elementinstanceofConnection)...{
Connectionc
=(Connection)element;
returnc.name;
}

if(elementinstanceofTable)...{
Tablet
=(Table)element;
returnt.name;
}

break;
case1:
if(elementinstanceofConnection)...{
Connectionc
=(Connection)element;
returnc.reso;
}

if(elementinstanceofTable)...{
Tablet
=(Table)element;
returnt.reso;
}


break;
default:
break;
}

return"--";
}


publicvoidinputChanged(Viewerviewer,ObjectoldInput,ObjectnewInput)...{
//TODOAuto-generatedmethodstub

}


publicObject[]getElements(ObjectinputElement)...{
Listlist
=(List)inputElement;
returnlist.toArray();
}


publicObject[]getChildren(ObjectparentElement)...{
if(parentElementinstanceofConnection)...{
Connectionc
=(Connection)parentElement;
returnc.children.toArray();
}

returnnewObject[0];
}


publicObjectgetParent(Objectelement)...{
//TODOAuto-generatedmethodstub
returnnull;
}


publicbooleanhasChildren(Objectelement)...{
returntrue;
}


}


public class TestTreeTable ... {

publicstaticvoidmain(String[]args)...{
Displaydisplay
=newDisplay();
finalShellshell=newShell(display);
shell.setLayout(
newFillLayout());

TableTreeViewerv
=newTableTreeViewer(shell);

TreeViewerviewer
=newTreeViewer(shell);
Treetree
=viewer.getTree();
tree.setHeaderVisible(
true);
tree.setLinesVisible(
true);

TreeColumncolumn1
=newTreeColumn(tree,SWT.CENTER);
column1.setText(
"Database");
column1.setWidth(
200);
TreeColumncolumn2
=newTreeColumn(tree,SWT.CENTER);
column2.setText(
"Repository");
column2.setWidth(
200);

TestProviderpro
=newTestProvider();
viewer.setLabelProvider(pro);
viewer.setContentProvider(pro);

viewer.setInput(getInput());

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

}

display.dispose();
}


privatestaticObjectgetInput()...{
Connectionc1
=newConnection();
c1.name
="MySql";
c1.reso
="mysql'srepository";
Tablet1
=newTable();
t1.name
="User";
t1.reso
="Txxx";
c1.children.add(t1);

Connectionc2
=newConnection();
c2.name
="MySql";
c2.reso
="mysql'srepository";
Tablet2
=newTable();
t2.name
="Userxxcv";
t2.reso
="Txxxsdfsf";
c2.children.add(t2);
Tablet3
=newTable();
t3.name
="User";
t3.reso
="Txxx";
c2.children.add(t3);

Listlist
=newArrayList();
list.add(c1);
list.add(c2);
returnlist;
}

}

你可能感兴趣的:(eclipse,C++,c,mysql,C#)