📄 profiledialog.java
字号:
package net.sf.p2pim.profile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.component.table.BeanBindedTableModel;
import net.sf.component.table.BindedTableColumn;
import net.sf.component.table.BindedTableViewer;
import net.sf.component.table.IBindedTableModel;
import net.sf.component.table.TextBindedTableColumn;
import net.sf.p2pim.P2PActivator;
import net.sf.util.persistence.DMFactory;
import net.sf.util.persistence.DataException;
import net.sf.util.persistence.IEntry;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
* @author levin
* @since 2007-11-17 下午04:06:38
* 可以和联系人进行关联
*/
public class ProfileDialog extends Dialog {
private String p2pid; //传入的p2p
private String showName; //该p2p的名字
private BindedTableViewer btv; //联系人列表
private Map<String,Label> detail; //详表的控件集
private Button contactButton; //显示联系人列表的开发按钮
private Contract contact; //最后选中的联系人
private List<IEntry> list;
public ProfileDialog(Shell shell, String p2pid,String showName) {
super(shell);
this.p2pid=p2pid;
this.showName=showName;
detail=new HashMap<String, Label>();
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("人员信息");
Image image = P2PActivator.getImageDescriptor("icons/profile.gif").createImage(shell.getDisplay());
shell.setImage(image);
image.dispose();
}
protected Control createDialogArea(Composite parent) {
Composite composite=new Composite(parent,SWT.NULL);
GridLayout gl=new GridLayout();
gl.numColumns=2;
composite.setLayout(gl);
Font boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);
Contract contract = new Contract();
contract.setProperty("p2pid", p2pid);
try {
List<IEntry> list=DMFactory.getDataManager(Contract.class).readList("p2pid", p2pid);
if(list.size() > 0)
contract=(Contract) list.get(0);
} catch (DataException e) {
e.printStackTrace();
}
String[] descs=contract.getFieldDescs();
String[] values=contract.getContent();
Label label;
{
label=new Label(composite,SWT.NULL);
label.setText("显示名:");
label.setFont(boldFont);
label=new Label(composite,SWT.NULL);
label.setText(showName);
}
for(int i=0;i<descs.length;i++){
label=new Label(composite,SWT.NULL);
label.setText(descs[i]+":");
label.setFont(boldFont);
label=new Label(composite,SWT.NULL);
label.setText(values[i]);
detail.put(descs[i], label);
}
return composite;
}
@Override
protected void buttonPressed(int buttonId) {
if(buttonId == IDialogConstants.CLIENT_ID+1){
Shell shell2 = getShell();
if(btv == null){
Composite listComposite=new Composite((Composite)getDialogArea().getParent(),SWT.NULL);
listComposite.setLayout(new GridLayout());
initTableViewer(listComposite);
GridDataFactory.fillDefaults().grab(true, true).applyTo(btv);
Point size = shell2.getSize();
Rectangle bounds = getButtonBar().getBounds();
shell2.setSize(size.x, size.y+240);
listComposite.setSize(size.x, 240);
((Composite)getButtonBar()).layout();
getButtonBar().setBounds(bounds); //重设button区
contactButton.setText("隐藏联系人列表");
shell2.layout();
}else{
btv.getParent().dispose();
btv=null;
contactButton.setText("显示联系人列表");
shell2.pack();
}
}
//保存关联关系
if(buttonId == IDialogConstants.OK_ID && contact != null){
try {
contact.setProperty("p2pid", p2pid);
DMFactory.getDataManager(Contract.class).createList(list);
} catch (DataException e) {
e.printStackTrace();
}
}
super.buttonPressed(buttonId);
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
contactButton = createButton(parent, IDialogConstants.CLIENT_ID+1, "显示联系人列表",false);
}
private void initTableViewer(Composite parent) {
try {
list = DMFactory.getDataManager(Contract.class).readList();
} catch (DataException e) {
e.printStackTrace();
}
IBindedTableModel model = new BeanBindedTableModel(list){
public Object createRow() {
return new Contract();
}};
model.setEditable(false);
IEntry entry=new Contract();
String[] fieldNames = entry.getFieldNames();
String[] fieldDescs = entry.getFieldDescs();
for(int i=0;i<fieldNames.length;i++){
BindedTableColumn btc=new TextBindedTableColumn(fieldNames[i]);
btc.setEditable(false);
btc.setEditor(null);
btc.setShowName(fieldDescs[i]);
model.addColumn(btc);
}
btv = new BindedTableViewer(parent,SWT.NULL);
btv.setModel(model);
btv.setRowCreateRemovable( false);
btv.setRowsOrderable( false);
btv.setColsOrderable( false);
btv.create();
btv.getTableViewer().addSelectionChangedListener(new ISelectionChangedListener(){
public void selectionChanged(SelectionChangedEvent event) {
StructuredSelection ss=(StructuredSelection)event.getSelection();
contact = (Contract)ss.getFirstElement();
final String[] descs=contact.getFieldDescs();
final String[] values=contact.getContent();
for(int i=0;i<descs.length-1;i++){
Label label = detail.get(descs[i]);
label.setText(values[i]);
label.setSize(label.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
}});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -