package com.jtang.compare.dialog;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
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.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
/**
* 文件比较对话框
* @author lww
*
*/
public class FileCompareDialog extends Dialog{
public Text leftText;
public Text rightText;
public Text filterText;
public String leftFile;
public String rightFile;
public FileCompareDialog(Shell parentShell) {
super(parentShell);
// TODO Auto-generated constructor stub
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
// 设置对话框的title
newShell.setText("选择文件");
}
@Override
protected Control createDialogArea(Composite parent){
Composite composite = new Composite(parent, SWT.FILL);
GridLayout layout = new GridLayout();
layout.verticalSpacing = 10;
layout.horizontalSpacing = 10;
layout.numColumns = 3;
Group group=new Group(composite,SWT.NONE);
group.setText("要对比的文件"); //设置分组框说明信息
group.setForeground(ColorConstants.blue);
group.setLayout(layout);
group.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
Label leftLabel = new Label(group, SWT.NONE);
leftLabel.setText("左侧(L):");
leftLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
leftText = new Text(group, SWT.BORDER);
leftText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button leftButton = new Button(group, SWT.NONE);
leftButton.setText("浏览(B)..");
leftButton.addSelectionListener(left);
Label rightLabel = new Label(group, SWT.NONE);
rightLabel.setText("右侧(R):");
rightLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
rightText = new Text(group, SWT.BORDER);
rightText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button rightButton = new Button(group, SWT.NONE);
rightButton.setText("浏览(W)..");
rightButton.addSelectionListener(right);
Label filterLabel = new Label(group, SWT.NONE);
filterLabel.setText("过滤器(F):");
filterLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
filterText = new Text(group, SWT.BORDER);
filterText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
GridLayout comLayout = new GridLayout();
comLayout.marginLeft = 30;
comLayout.marginRight =30;
comLayout.numColumns = 1;
composite.setLayout(comLayout);//设置容器组件与容器边缘的水平距离
composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
composite.setFont(parent.getFont());
return composite;
}
public SelectionListener left = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog fd = new FileDialog(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(), SWT.OPEN);
fd.setText("选择左边文件");
if(!filterText.getText().equals("")){
String[] filterExt = filterText.getText().split(";");
fd.setFilterExtensions(filterExt);
}
String leftLabel = fd.open();
if(leftLabel != null){
leftText.setText(leftLabel);
leftFile = leftLabel;
} else{
leftText.setText("");
leftFile = "";
}
}
};
public SelectionListener right = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//选择左边的文件
FileDialog fd = new FileDialog(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(), SWT.OPEN);
fd.setText("选择右边文件");
if(!filterText.getText().equals("")){
String[] filterExt = filterText.getText().split(";");
fd.setFilterExtensions(filterExt);
}
String rightLabel = fd.open();
if(rightLabel != null){
rightText.setText(rightLabel);
rightFile = rightLabel;
} else{
rightText.setText("");
rightFile = "";
}
}
};
/**
* 重载按钮OK事件
*/
@Override
protected void okPressed() {
if(!rightFile.equals("") && !leftFile.equals("")){
super.okPressed();
}else{
MessageDialog.openWarning(getShell(), "提示", "查看比较文件是否配置好!");
return;
}
}
/**
* 设置对话框初始大小
*/
protected Point getInitialSize() {
return new Point(500, 300);
}
public String getLeftFile() {
return leftFile;
}
public String getRightFile() {
return rightFile;
}
}