1. 定义扩展
代码
<view
allowMultiple="false"
category="com.sg.intellifmea.views"
class="com.sg.intellifmea.view.DocumentView"
icon="icons/fldr_unrec.gif"
id="com.sg.intellifmea.view.DocumentView"
name="文档"
restorable="true">
</view>
allowMultiple="false"
category="com.sg.intellifmea.views"
class="com.sg.intellifmea.view.DocumentView"
icon="icons/fldr_unrec.gif"
id="com.sg.intellifmea.view.DocumentView"
name="文档"
restorable="true">
</view>
2. 创建TableViewer
文档视图上是以列表的形式显示文档的,因此,我们不需要定义column
@Override
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent,SWT.BORDER);
viewer.setLabelProvider(new LazyDTOStyledCellLabelProvider());
viewer.setContentProvider(ArrayContentProvider.getInstance());
}
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent,SWT.BORDER);
viewer.setLabelProvider(new LazyDTOStyledCellLabelProvider());
viewer.setContentProvider(ArrayContentProvider.getInstance());
}
3. LabelProvider
代码
package com.sg.hyperobject.ui.viewers;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.graphics.Image;
import com.sg.hyperobject.datamanager.dt.LazyDTO;
public class LazyDTOStyledCellLabelProvider extends StyledCellLabelProvider{
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd");
@Override
public void update(ViewerCell cell) {
LazyDTO ldto = (LazyDTO)cell.getElement();
StyledString styledString= getStyleString(ldto);
cell.setText(styledString.toString());
cell.setStyleRanges(styledString.getStyleRanges());
cell.setImage(getImage(ldto));
super.update(cell);
}
protected StyledString getStyleString(LazyDTO ldto){
StyledString sString = ldto.getStyleString();
String user = ldto.getText("md$user");
sString.append(user, StyledString.COUNTER_STYLER);
String cdate = ldto.getText("md$cdate");
if(!"".equals(cdate)){
try {
cdate = sdf1.format(sdf.parse(cdate));
sString.append(" "+cdate,StyledString.COUNTER_STYLER);
} catch (ParseException e) {
}
}
return sString;
}
protected Image getImage(LazyDTO ldto){
return ldto.getIcon();
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.graphics.Image;
import com.sg.hyperobject.datamanager.dt.LazyDTO;
public class LazyDTOStyledCellLabelProvider extends StyledCellLabelProvider{
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd");
@Override
public void update(ViewerCell cell) {
LazyDTO ldto = (LazyDTO)cell.getElement();
StyledString styledString= getStyleString(ldto);
cell.setText(styledString.toString());
cell.setStyleRanges(styledString.getStyleRanges());
cell.setImage(getImage(ldto));
super.update(cell);
}
protected StyledString getStyleString(LazyDTO ldto){
StyledString sString = ldto.getStyleString();
String user = ldto.getText("md$user");
sString.append(user, StyledString.COUNTER_STYLER);
String cdate = ldto.getText("md$cdate");
if(!"".equals(cdate)){
try {
cdate = sdf1.format(sdf.parse(cdate));
sString.append(" "+cdate,StyledString.COUNTER_STYLER);
} catch (ParseException e) {
}
}
return sString;
}
protected Image getImage(LazyDTO ldto){
return ldto.getIcon();
}
}
注意,这里我们不为这个viewer setInput()
我们需要监听当前选择的项目来设置为viewer的input
所以接下来设置view侦听Page的Selection
4. 侦听Page Selection
代码
@Override
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.BORDER);
viewer.setLabelProvider(new LazyDTOStyledCellLabelProvider());
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(new ArrayList<LazyDTO>());
getSite().getPage().addPostSelectionListener(
IntelliFmeaViews.PROJECT_NAVIGATOR, this);
}
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.BORDER);
viewer.setLabelProvider(new LazyDTOStyledCellLabelProvider());
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(new ArrayList<LazyDTO>());
getSite().getPage().addPostSelectionListener(
IntelliFmeaViews.PROJECT_NAVIGATOR, this);
}
当然,前提是你所侦听的部件已经把Selection注册到了Page
5. 实现selectionChanged
代码
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (selection.isEmpty())
return;
LazyDTO element = (LazyDTO) ((StructuredSelection) selection).getFirstElement();
String ouid = element.getOuid();
Assert.isNotNull(ouid);
if(ouid.equals(master)){
return;//没有改变
}
setPartName("文档 -"+element);
HashMap<String, String> condition = new HashMap<String,String>();
condition.put("project", ouid);
ArrayList<LazyDTO> input = new ArrayList<LazyDTO>();
try {
ArrayList<ArrayList<String>> result = DMModel.getDMCbyName("Document").listData(condition);
if(result!=null){
for(ArrayList<String> row:result){
LazyDTO ldto = new LazyDTO(row.get(0));
input.add(ldto);
}
}
} catch (Exception e) {
}
viewer.setInput(input);
}
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (selection.isEmpty())
return;
LazyDTO element = (LazyDTO) ((StructuredSelection) selection).getFirstElement();
String ouid = element.getOuid();
Assert.isNotNull(ouid);
if(ouid.equals(master)){
return;//没有改变
}
setPartName("文档 -"+element);
HashMap<String, String> condition = new HashMap<String,String>();
condition.put("project", ouid);
ArrayList<LazyDTO> input = new ArrayList<LazyDTO>();
try {
ArrayList<ArrayList<String>> result = DMModel.getDMCbyName("Document").listData(condition);
if(result!=null){
for(ArrayList<String> row:result){
LazyDTO ldto = new LazyDTO(row.get(0));
input.add(ldto);
}
}
} catch (Exception e) {
}
viewer.setInput(input);
}
现在看不到效果。等我做完了注册文档后,自然就可以看到了。