如何把java SWT程序中的匿名内部类改写成命名内部类? 20
1个回答
展开全部
//请看最后一个private class ,其实命名内部类跟普通的java程序一样写,只要知道你自己的监听器要继承于哪个类,实现什么方法
public class Level4Class extends ViewPart {
private TableViewer tv;
public static int num=0;
private List<NavigatorEntry> files;
public int getNum() {
return num;
}
public List<NavigatorEntry> getFiles() {
return files;
}
public void setFiles(List<NavigatorEntry> files) {
this.files = files;
}
public void setNum(int num) {
this.num = num;
}
@Override
public void createPartControl(Composite parent) {
Composite topComp = new Composite(parent, SWT.NONE);
topComp.setLayout(new FillLayout());
createTableViewer(topComp);// 自定义方法:创建一个TableViewer表格
tv.setContentProvider(new SmsContentProvider());// 内容器
tv.setLabelProvider(new TableViewerLabelProvider());// 标签器
tv.addDoubleClickListener(new MyDoubleClickListener());
files = getCourse();
tv.setInput(files);
}
@Override
public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
// TODO 自动生成方法存根
super.setInitializationData(cfig, propertyName, getCourse());
}
// 创建表格
private void createTableViewer(Composite parent) {
tv = new TableViewer(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
Table table = tv.getTable();
table.setHeaderVisible(true); // 显示表头
table.setLinesVisible(true); // 显示表格线
table.setLayout(new TableLayout());// 专用于表格的布局
// 建立TableViewer中的列,createColumn是自定义方法
createColumn(10, "编号");
createColumn(70, "域名");
createColumn(30, "截止日期");
createColumn(30, "版本状态");
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
// 创建表格的列
private TableColumn createColumn(int weight, String name) {
Table table = tv.getTable();
TableLayout layout = (TableLayout) table.getLayout();
layout.addColumnData(new ColumnWeightData(weight));
TableColumn col = new TableColumn(table, SWT.NONE);
if (name.equals("编号")){
col.addSelectionListener(new SelectionAdapter() {
boolean asc = true; // 记录上一次的排序方式,默认为升序
public void widgetSelected(SelectionEvent e) {
Level4Class.num=0;
// asc=true则ID的升序排序器,否则用降序
tv.setSorter(asc ? MySorter.ID_ASC : MySorter.ID_DESC);
asc = !asc;// 得到下一次排序方式
}
});
}else if(name.equals("截止日期")){
col.addSelectionListener(new SelectionAdapter() {
boolean asc = true; // 记录上一次的排序方式,默认为升序
public void widgetSelected(SelectionEvent e) {
Level4Class.num=0;
// asc=true则ID的升序排序器,否则用降序
tv.setSorter(asc ? MySorter.TIME_ASC : MySorter.TIME_DESC);
asc = !asc;// 得到下一次排序方式
}
});
}else if(name.equals("版本状态")){
col.addSelectionListener(new SelectionAdapter() {
boolean asc = true; // 记录上一次的排序方式,默认为升序
public void widgetSelected(SelectionEvent e) {
Level4Class.num=0;
// asc=true则ID的升序排序器,否则用降序
tv.setSorter(asc ? MySorter.STATE_ASC : MySorter.STATE_DESC);
asc = !asc;// 得到下一次排序方式
}
});
}
col.setText(name);
return col;
}
// 标签器
private static final class TableViewerLabelProvider extends TableLabelProviderAdapter implements ITableColorProvider{
public String getColumnText(Object element, int col) {
// String result = "";
NavigatorEntry o = (NavigatorEntry) element;
switch (col) {
case 0:
return String.valueOf(++num);
case 1:
return o.getDomain();
case 2:
String time = o.getTime();
if (null != time !time.equals("")time.length()>=10){
time = time.substring(0, 10);
}else if(null != time !time.equals("")time.length()<10){
time =time;
}else{
time ="";
}
return time;
case 3:
return o.getLevel().getMeg();
default:
return "";
}
}
public Color getBackground(Object element, int columnIndex) {
// NavigatorEntry o = (NavigatorEntry) element;
// if (o.getLevel().getMeg().equals(IAppConstants.LEVEL_2)){
// return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
// }else if(o.getLevel().getMeg().equals(IAppConstants.LEVEL_3)){
// return Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
// }else if(o.getLevel().getMeg().equals(IAppConstants.LEVEL_1)){
// return Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
// }else if(o.getLevel().getMeg().equals(IAppConstants.LEVEL_4)){
// return Display.getDefault().getSystemColor(SWT.COLOR_RED);
// }else if (o.getLevel().getMeg().equals(IAppConstants.LEVEL_5)) {
// return Display.getDefault().getSystemColor(SWT.COLOR_YELLOW);
// }
return null;
}
public Color getForeground(Object element, int columnIndex) {
NavigatorEntry o = (NavigatorEntry) element;
if (true){
return Display.getDefault().getSystemColor(SWT.COLOR_RED);
}
return null;
}
}
/**
* 得到所有正常的版本数据
* @return
*/
private List getCourse(){
List<NavigatorEntry> list = new ArrayList<NavigatorEntry>();
Session session = HibernateSessionFactory.getSession("hibernate_mysql.cfg.xml");
Query query = session.getNamedQuery("org.b2c.framework.sys.Version.getNotNormal");
for (Iterator iter = query.iterate(); iter.hasNext();) {
Version version = (Version)iter.next();
if (null != version){
NavigatorEntry o = new NavigatorEntry();
o.setDomain(version.getDomain());
o.setId(version.getId());
Level l = new Level();
l.setLevel(version.getState());
switch(version.getState()){
case 0:
l.setMeg("未注册");
break;
case 4:
l.setMeg("已过期");
break;
}
o.setLevel(l);
o.setPublickey(version.getPublickey());
o.setPrivatekey(version.getPrivatekey());
o.setCiphertext(version.getCiphertext());
o.setEnshrined(version.getEnshrined());
if (null != version.getVtime()){
o.setTime(version.getVtime().toString().substring(0,10));
}else{
o.setTime("-----");
}
list.add(o);
}
}
HibernateSessionFactory.closeSession();
return list;
}
public TableViewer getTv() {
return tv;
}
public void setTv(TableViewer tv) {
this.tv = tv;
}
private class MyDoubleClickListener implements IDoubleClickListener {
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
NavigatorEntry entry = (NavigatorEntry) sel.getFirstElement();
if(entry!=null){
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IViewPart view1 = window.getActivePage().findView("org.b2c.framework.ui.teachunitname");
Text temp1 = ((TeachUnitName)view1).getLable1();
Text temp2 = ((TeachUnitName)view1).getLable2();
temp1.setText(entry.getPrivatekey());
temp2.setText(entry.getCiphertext());
}
}
}
}
public class Level4Class extends ViewPart {
private TableViewer tv;
public static int num=0;
private List<NavigatorEntry> files;
public int getNum() {
return num;
}
public List<NavigatorEntry> getFiles() {
return files;
}
public void setFiles(List<NavigatorEntry> files) {
this.files = files;
}
public void setNum(int num) {
this.num = num;
}
@Override
public void createPartControl(Composite parent) {
Composite topComp = new Composite(parent, SWT.NONE);
topComp.setLayout(new FillLayout());
createTableViewer(topComp);// 自定义方法:创建一个TableViewer表格
tv.setContentProvider(new SmsContentProvider());// 内容器
tv.setLabelProvider(new TableViewerLabelProvider());// 标签器
tv.addDoubleClickListener(new MyDoubleClickListener());
files = getCourse();
tv.setInput(files);
}
@Override
public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
// TODO 自动生成方法存根
super.setInitializationData(cfig, propertyName, getCourse());
}
// 创建表格
private void createTableViewer(Composite parent) {
tv = new TableViewer(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
Table table = tv.getTable();
table.setHeaderVisible(true); // 显示表头
table.setLinesVisible(true); // 显示表格线
table.setLayout(new TableLayout());// 专用于表格的布局
// 建立TableViewer中的列,createColumn是自定义方法
createColumn(10, "编号");
createColumn(70, "域名");
createColumn(30, "截止日期");
createColumn(30, "版本状态");
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
// 创建表格的列
private TableColumn createColumn(int weight, String name) {
Table table = tv.getTable();
TableLayout layout = (TableLayout) table.getLayout();
layout.addColumnData(new ColumnWeightData(weight));
TableColumn col = new TableColumn(table, SWT.NONE);
if (name.equals("编号")){
col.addSelectionListener(new SelectionAdapter() {
boolean asc = true; // 记录上一次的排序方式,默认为升序
public void widgetSelected(SelectionEvent e) {
Level4Class.num=0;
// asc=true则ID的升序排序器,否则用降序
tv.setSorter(asc ? MySorter.ID_ASC : MySorter.ID_DESC);
asc = !asc;// 得到下一次排序方式
}
});
}else if(name.equals("截止日期")){
col.addSelectionListener(new SelectionAdapter() {
boolean asc = true; // 记录上一次的排序方式,默认为升序
public void widgetSelected(SelectionEvent e) {
Level4Class.num=0;
// asc=true则ID的升序排序器,否则用降序
tv.setSorter(asc ? MySorter.TIME_ASC : MySorter.TIME_DESC);
asc = !asc;// 得到下一次排序方式
}
});
}else if(name.equals("版本状态")){
col.addSelectionListener(new SelectionAdapter() {
boolean asc = true; // 记录上一次的排序方式,默认为升序
public void widgetSelected(SelectionEvent e) {
Level4Class.num=0;
// asc=true则ID的升序排序器,否则用降序
tv.setSorter(asc ? MySorter.STATE_ASC : MySorter.STATE_DESC);
asc = !asc;// 得到下一次排序方式
}
});
}
col.setText(name);
return col;
}
// 标签器
private static final class TableViewerLabelProvider extends TableLabelProviderAdapter implements ITableColorProvider{
public String getColumnText(Object element, int col) {
// String result = "";
NavigatorEntry o = (NavigatorEntry) element;
switch (col) {
case 0:
return String.valueOf(++num);
case 1:
return o.getDomain();
case 2:
String time = o.getTime();
if (null != time !time.equals("")time.length()>=10){
time = time.substring(0, 10);
}else if(null != time !time.equals("")time.length()<10){
time =time;
}else{
time ="";
}
return time;
case 3:
return o.getLevel().getMeg();
default:
return "";
}
}
public Color getBackground(Object element, int columnIndex) {
// NavigatorEntry o = (NavigatorEntry) element;
// if (o.getLevel().getMeg().equals(IAppConstants.LEVEL_2)){
// return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
// }else if(o.getLevel().getMeg().equals(IAppConstants.LEVEL_3)){
// return Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
// }else if(o.getLevel().getMeg().equals(IAppConstants.LEVEL_1)){
// return Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
// }else if(o.getLevel().getMeg().equals(IAppConstants.LEVEL_4)){
// return Display.getDefault().getSystemColor(SWT.COLOR_RED);
// }else if (o.getLevel().getMeg().equals(IAppConstants.LEVEL_5)) {
// return Display.getDefault().getSystemColor(SWT.COLOR_YELLOW);
// }
return null;
}
public Color getForeground(Object element, int columnIndex) {
NavigatorEntry o = (NavigatorEntry) element;
if (true){
return Display.getDefault().getSystemColor(SWT.COLOR_RED);
}
return null;
}
}
/**
* 得到所有正常的版本数据
* @return
*/
private List getCourse(){
List<NavigatorEntry> list = new ArrayList<NavigatorEntry>();
Session session = HibernateSessionFactory.getSession("hibernate_mysql.cfg.xml");
Query query = session.getNamedQuery("org.b2c.framework.sys.Version.getNotNormal");
for (Iterator iter = query.iterate(); iter.hasNext();) {
Version version = (Version)iter.next();
if (null != version){
NavigatorEntry o = new NavigatorEntry();
o.setDomain(version.getDomain());
o.setId(version.getId());
Level l = new Level();
l.setLevel(version.getState());
switch(version.getState()){
case 0:
l.setMeg("未注册");
break;
case 4:
l.setMeg("已过期");
break;
}
o.setLevel(l);
o.setPublickey(version.getPublickey());
o.setPrivatekey(version.getPrivatekey());
o.setCiphertext(version.getCiphertext());
o.setEnshrined(version.getEnshrined());
if (null != version.getVtime()){
o.setTime(version.getVtime().toString().substring(0,10));
}else{
o.setTime("-----");
}
list.add(o);
}
}
HibernateSessionFactory.closeSession();
return list;
}
public TableViewer getTv() {
return tv;
}
public void setTv(TableViewer tv) {
this.tv = tv;
}
private class MyDoubleClickListener implements IDoubleClickListener {
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
NavigatorEntry entry = (NavigatorEntry) sel.getFirstElement();
if(entry!=null){
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IViewPart view1 = window.getActivePage().findView("org.b2c.framework.ui.teachunitname");
Text temp1 = ((TeachUnitName)view1).getLable1();
Text temp2 = ((TeachUnitName)view1).getLable2();
temp1.setText(entry.getPrivatekey());
temp2.setText(entry.getCiphertext());
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询