import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
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.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import utils.system;
/**
* 扩展属性过滤对话框
*
* @author sunway
*
*/
public class ExtendedAttributeFilterDialog extends WorkflowDialog implements
SelectionListener, ISelectionChangedListener {
private ExtendedAttributes resultExtendedAttributes;
private ExtendedAttributes sourceExtendedAttributes;
private TableViewer tableViewer;
private CheckboxTableViewer ctv;
private Table table;
private GridData gridData;
private Button openBtn;
private String fileFullPath;
private Text fileFullPathText;
private String lastFileFullPath = "";
private String projectPath= "";
/**
* 创建c:\temp\opendDialogPath.xml文件
*
* @return
*/
private File getDialogConfigFile() {
String parent = getProjectPath() + File.separator + ".settings";
File dir = new File(parent);
File file = new File(parent + File.separator
+ "opendDialogPath.xml");
try {
if (!dir.exists()) {
if (dir.mkdir()) {
if (!file.exists()) {
file.createNewFile();
}
}
}
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
public String getLastFileFullPath() {
return this.lastFileFullPath;
}
public void setFileFullPath(String fileFullPath) {
this.fileFullPath = fileFullPath;
}
public ExtendedAttributes getSourceExtendedAttributes() {
return sourceExtendedAttributes;
}
public void setSourceExtendedAttributes(
ExtendedAttributes sourceExtendedAttributes) {
this.sourceExtendedAttributes = sourceExtendedAttributes;
}
public ExtendedAttributes getResultExtendedAttributes() {
return resultExtendedAttributes;
}
public ExtendedAttributeFilterDialog(Shell parentShell) {
super(parentShell);
// TODO Auto-generated constructor stub
}
protected Control createDialogArea(Composite parent) {
setTitle(DialogMessages
.getString("ExtendedAttributeFilterDialog.title")); //$NON-NLS-1$
setMessage(DialogMessages
.getString("ExtendedAttributeFilterDialog.message")); //$NON-NLS-1$
setHelpAvailable(false);
Composite container = (Composite) super.createDialogArea(parent);
createDialogContext(container);
this.initTableViewer();
return container;
}
private void createDialogContext(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
GridData gdGroup = new GridData(GridData.FILL_BOTH);
// gdGroup.widthHint = 600;
container.setLayoutData(gdGroup);
GridLayout gridLayout = new GridLayout();
gridLayout.verticalSpacing = 4;
gridLayout.numColumns = 2;
gridLayout.marginWidth = 10;
gridLayout.marginTop = 10;
container.setLayout(gridLayout);
Group group = new Group(container, SWT.NONE);
GridLayout gl = new GridLayout();
gl.numColumns = 2;
gl.marginWidth = 10;
group.setLayout(gl);
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.setText(DialogMessages
.getString("ExtendedAttributeFilterDialog.xpdlFile"));//$NON-NLS-1$
openBtn = new Button(group, SWT.OPEN);
openBtn.setText(DialogMessages
.getString("ExtendedAttributeFilterDialog.browserButton"));//$NON-NLS-1$
openBtn.addSelectionListener(this);
fileFullPathText = new Text(group, SWT.BORDER);
fileFullPathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// 默认选中上次打开的页面
String fullPath = loadFileFullPath();
if (fullPath == null || fullPath == "" || fullPath.equals("")) {
fileFullPathText.setText(fileFullPath);
} else {
fileFullPathText.setText(fullPath);
}
fileFullPathText.addSelectionListener(this);
tableViewer = new TableViewer(container, SWT.CHECK | SWT.MULTI
| SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
// checkboxViewer
ctv = new CheckboxTableViewer(tableViewer.getTable());
ctv.setContentProvider(new ExtendedAttributeFilterContentProvider());
ctv.setLabelProvider(new ExtendedAttributeFilterLabelProvider());
ctv.addSelectionChangedListener(this);
ctv
.setColumnProperties(new String[] {
DialogMessages
.getString("ExtendedAttributeFilterDialog.nameColumnProperties"), DialogMessages.getString("ExtendedAttributeFilterDialog.valueColumnProperties") }); //$NON-NLS-1$ //$NON-NLS-2$
table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
table.setLayoutData(gridData);
}
private void initTableViewer() {
// TODO Auto-generated method stub
final TableColumn nameCol = new TableColumn(table, SWT.NONE);
nameCol.setText(DialogMessages
.getString("ExtendedAttributeFilterDialog.nameTableColumn")); //$NON-NLS-1$
nameCol.setWidth((getInitialSize().x - 35) / 2);
final TableColumn valueCol = new TableColumn(table, SWT.NONE);
valueCol.setText(DialogMessages
.getString("ExtendedAttributeFilterDialog.valueTableColumn")); //$NON-NLS-1$
valueCol.setWidth((getInitialSize().x - 35) / 2);
CellEditor[] cellEditor = new CellEditor[table.getColumnCount() + 1];
cellEditor[table.getColumnCount() - 1] = new CheckboxCellEditor(
tableViewer.getTable());
this.ctv.setCellEditors(cellEditor);
this.table.setHeaderVisible(true);
this.table.setLinesVisible(true);
this.ctv.addSelectionChangedListener(this);
this.ctv.setColumnProperties(new String[] { "name", "value" });
String filePath = this.fileFullPathText.getText();
if (filePath.endsWith("xml")) {// 简单处理
this.ctv.setInput(getExtendedAttributes(filePath));
} else {
this.ctv
.setInput(filterConfigExtendedAttributes(filterDuplicateExtendedAttributes(getExtendedAttributes(filePath))));
}
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
// TODO Auto-generated method stub
super.createButtonsForButtonBar(parent);
dialogChanged();
setErrorMessage(null);
}
@Override
protected Point getInitialSize() {
return new Point(520, 560);
}
@Override
protected void configureShell(Shell newShell) {
// TODO Auto-generated method stub
super.configureShell(newShell);
newShell.setText(DialogMessages
.getString("ExtendedAttributeFilterDialog.shellText")); //$NON-NLS-1$
}
@Override
protected void buttonPressed(int buttonId) {
// TODO Auto-generated method stub
this.resultExtendedAttributes = (ExtendedAttributes) this.sourceExtendedAttributes
.clone();
this.resultExtendedAttributes.clear();
if (buttonId == IDialogConstants.OK_ID) {
Object[] object = ctv.getCheckedElements();
if (object.length > 0) {
for (int i = 0, n = object.length; i < n; i++) {
ExtendedAttribute bean = (ExtendedAttribute) object[i];
this.resultExtendedAttributes.add(bean);
}
}
}
// 保存上次打开的文件路径
saveFileFullPath();
super.buttonPressed(buttonId);
}
private IDialogSettings getDialogTopSettings() {
IDialogSettings settings = new DialogSettings("root");
try {
File file = getDialogConfigFile();
settings.load(file.getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return settings;
}
/**
* 从c:\temp\dialogPath.xml文件中取出上次打开的文件路径
*
* @return
*/
private String loadFileFullPath() {
// TODO Auto-generated method stub
String result = "";
IDialogSettings topSettings = getDialogTopSettings();
IDialogSettings settings = topSettings.getSection("Dialog");
if (settings == null) {
return result;
} else {
result = settings.get("path");
}
return result;
}
/**
* 保存上次打开的对话框路径到c:\temp\dialogPath.xml文件中
*/
private void saveFileFullPath() {
// TODO Auto-generated method stub
IDialogSettings topSettings = getDialogTopSettings();
IDialogSettings settings = topSettings.getSection("Dialog");
if (settings == null) {
settings = topSettings.addNewSection("Dialog");
}
settings.put("path", this.fileFullPathText.getText());
try {
topSettings.save(getDialogConfigFile().getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
if (e.getSource().equals(this.openBtn)) {
// 打开文件选择对话框
FileDialog fd = new FileDialog(WorkflowProjectUtils
.getActiveShell(), SWT.OPEN);
fd.setFilterExtensions(new String[] { "*.xpdl", "*.xml" });
String fileFullPath = fd.open();
fileFullPathText.setText(fileFullPath);
if (fileFullPath.endsWith("xml")) {// 简单处理
this.ctv.setInput(getExtendedAttributes(fileFullPath));
} else
this.ctv
.setInput(filterConfigExtendedAttributes(filterDuplicateExtendedAttributes(getExtendedAttributes(this.fileFullPathText
.getText()))));
}
if (e.getSource().equals(this.fileFullPathText)) {
// 先过滤掉重复的扩展属性
this.ctv
.setInput(filterConfigExtendedAttributes(filterDuplicateExtendedAttributes(getExtendedAttributes(this.fileFullPathText
.getText()))));
}
}
public void selectionChanged(SelectionChangedEvent event) {
// TODO Auto-generated method stub
}
/**
* 过滤掉配置文件里有的ExtendedAttribute
*/
private ExtendedAttributes filterConfigExtendedAttributes(
ExtendedAttributes parent) {
if (parent != null) {
ExtendedAttributes result = (ExtendedAttributes) parent.clone();
Set<String> set = WorkflowProjectUtils
.getConfigExtendedAttributesName();
Iterator<ExtendedAttribute> extendedAttributeIt = result
.toElements().iterator();
while (extendedAttributeIt.hasNext()) {
ExtendedAttribute extendedAttribute = extendedAttributeIt
.next();
if (set.contains(extendedAttribute.getName())) {
// 删除这个ExtendedAttribute
result.remove(extendedAttribute);
}
}
return result;
}
return null;
}
/**
* 过滤掉一个ExtendedAttributes重复的ExtendedAttribute
*/
private ExtendedAttributes filterDuplicateExtendedAttributes(
ExtendedAttributes parent) {
if (parent != null) {
try {
ArrayList<ExtendedAttribute> extendedAttributeList = parent
.toElements();
Iterator<ExtendedAttribute> extendedAttributeIt = extendedAttributeList
.iterator();
ExtendedAttributes extendedAttributes = (ExtendedAttributes) parent
.clone();
extendedAttributes.clear();
while (extendedAttributeIt.hasNext()) {
ExtendedAttribute extendedAttribute = extendedAttributeIt
.next();
if (!extendedAttributes.contains(extendedAttribute)) {
extendedAttributes.add(extendedAttribute);
}
}
return extendedAttributes;
} catch (Exception ex) {
// TODO: handle exception
ex.printStackTrace();
}
}
return null;
}
/**
* 向一个ExtendedAttributes里添加另一个ExtendedAttributes的所有元素
*/
private ExtendedAttributes addExtendedAttributes(ExtendedAttributes parent,
ExtendedAttributes children) {
if (children != null) {
ArrayList<ExtendedAttribute> extendedAttributeList = children
.toElements();
Iterator<ExtendedAttribute> extendedAttributeIt = extendedAttributeList
.iterator();
while (extendedAttributeIt.hasNext()) {
parent.add(extendedAttributeIt.next());
}
}
return parent;
}
/**
* 根据文件路径得到所有的扩展属性
*
* @param fileFullPath
* @return
*/
public ExtendedAttributes getExtendedAttributes(String fileFullPath) {
ExtendedAttributes parent = null;
if (fileFullPath.endsWith("xpdl")) {
Package pkg = XpdlUtils.loadPageckFromFile(fileFullPath, null);
if (pkg == null || pkg.equals("")) {
return null;
}
// 如果是包的扩展属性
if (getSourceExtendedAttributes().getParent() instanceof Package) {
parent = (ExtendedAttributes) pkg.getExtendedAttributes()
.clone();
}
// 如果是流程的扩展属性
if (getSourceExtendedAttributes().getParent() instanceof WorkflowProcess) {
WorkflowProcesses workflowProcesses = pkg
.getWorkflowProcesses();
ArrayList<WorkflowProcess> arrayList = workflowProcesses
.toElements();
Iterator<WorkflowProcess> it = arrayList.iterator();
ExtendedAttributes workflowProcessParent = null;
while (it.hasNext()) {
WorkflowProcess workflowProcess = it.next();
ExtendedAttributes extendedAttributes = workflowProcess
.getExtendedAttributes();
if (workflowProcessParent == null) {
workflowProcessParent = (ExtendedAttributes) extendedAttributes
.clone();
} else {
// workflowProcessParent.add(extendedAttributes);
addExtendedAttributes(workflowProcessParent,
extendedAttributes);
}
}
if (workflowProcessParent != null) {
parent = (ExtendedAttributes) workflowProcessParent.clone();
}
}
// 如果是活动的扩展属性
if (getSourceExtendedAttributes().getParent() instanceof Activity) {
WorkflowProcesses workflowProcesses = pkg
.getWorkflowProcesses();
ArrayList<WorkflowProcess> arrayList = workflowProcesses
.toElements();
Iterator<WorkflowProcess> it = arrayList.iterator();
ExtendedAttributes workflowProcessParent = null;
while (it.hasNext()) {
WorkflowProcess workflowProcess = it.next();
Activities activities = workflowProcess.getActivities();
ArrayList<Activity> activityArrayList = activities
.toElements();
Iterator<Activity> activityIt = activityArrayList
.iterator();
ExtendedAttributes activityParent = null;
while (activityIt.hasNext()) {
Activity activity = activityIt.next();
ExtendedAttributes extendedAttributes = activity
.getExtendedAttributes();
if (activityParent == null) {
activityParent = (ExtendedAttributes) extendedAttributes
.clone();
} else {
addExtendedAttributes(activityParent,
extendedAttributes);
}
}
if (workflowProcessParent == null) {
if (activityParent != null) {
workflowProcessParent = (ExtendedAttributes) activityParent
.clone();
}
} else {
addExtendedAttributes(workflowProcessParent,
activityParent);
}
}
if (workflowProcessParent != null) {
parent = (ExtendedAttributes) workflowProcessParent.clone();
}
}
// 如果是转移的扩展属性
if (getSourceExtendedAttributes().getParent() instanceof Transition) {
WorkflowProcesses workflowProcesses = pkg
.getWorkflowProcesses();
ArrayList<WorkflowProcess> arrayList = workflowProcesses
.toElements();
Iterator<WorkflowProcess> it = arrayList.iterator();
ExtendedAttributes workflowProcessParent = null;
while (it.hasNext()) {
WorkflowProcess workflowProcess = it.next();
Transitions transitions = workflowProcess.getTransitions();
ArrayList<Transition> transitionArrayList = transitions
.toElements();
Iterator<Transition> transitionIt = transitionArrayList
.iterator();
ExtendedAttributes transitionParent = null;
while (transitionIt.hasNext()) {
Transition transition = transitionIt.next();
ExtendedAttributes extendedAttributes = transition
.getExtendedAttributes();
if (transitionParent == null) {
transitionParent = (ExtendedAttributes) extendedAttributes
.clone();
} else {
addExtendedAttributes(transitionParent,
extendedAttributes);
}
}
if (workflowProcessParent == null) {
if (transitionParent != null) {
workflowProcessParent = (ExtendedAttributes) transitionParent
.clone();
}
} else {
addExtendedAttributes(workflowProcessParent,
transitionParent);
}
}
if (workflowProcessParent != null) {
parent = (ExtendedAttributes) workflowProcessParent.clone();
}
}
// 如果是参与者的扩展属性
if (getSourceExtendedAttributes().getParent() instanceof Participant) {
WorkflowProcesses workflowProcesses = pkg
.getWorkflowProcesses();
ArrayList<WorkflowProcess> arrayList = workflowProcesses
.toElements();
Iterator<WorkflowProcess> it = arrayList.iterator();
ExtendedAttributes workflowProcessParent = null;
while (it.hasNext()) {
WorkflowProcess workflowProcess = it.next();
Participants participants = workflowProcess
.getParticipants();
ArrayList<Participant> participantArrayList = participants
.toElements();
Iterator<Participant> participantIt = participantArrayList
.iterator();
ExtendedAttributes participantParent = null;
while (participantIt.hasNext()) {
Participant participant = participantIt.next();
ExtendedAttributes extendedAttributes = participant
.getExtendedAttributes();
if (participantParent == null) {
participantParent = (ExtendedAttributes) extendedAttributes
.clone();
} else {
addExtendedAttributes(participantParent,
extendedAttributes);
}
}
if (workflowProcessParent == null) {
if (participantParent != null) {
workflowProcessParent = (ExtendedAttributes) participantParent
.clone();
}
} else {
addExtendedAttributes(workflowProcessParent,
participantParent);
}
}
if (workflowProcessParent != null) {
parent = (ExtendedAttributes) workflowProcessParent.clone();
}
}
// 如果是数据类型的扩展属性
if (getSourceExtendedAttributes().getParent() instanceof DataField) {
// 如果是包级的数据类型
if (getSourceExtendedAttributes().getParent().getParent()
.getParent() instanceof Package) {
DataFields dataFields = pkg.getDataFields();
ArrayList<DataField> dataFieldArrayList = dataFields
.toElements();
Iterator<DataField> dataFieldIt = dataFieldArrayList
.iterator();
ExtendedAttributes dataFieldParent = null;
while (dataFieldIt.hasNext()) {
DataField dataField = dataFieldIt.next();
ExtendedAttributes extendedAttributes = dataField
.getExtendedAttributes();
if (dataFieldParent == null) {
dataFieldParent = (ExtendedAttributes) extendedAttributes
.clone();
} else {
addExtendedAttributes(dataFieldParent,
extendedAttributes);
}
}
if (dataFieldParent != null) {
parent = (ExtendedAttributes) dataFieldParent.clone();
}
}
// 如果是流程级的数据类型
if (getSourceExtendedAttributes().getParent().getParent()
.getParent() instanceof WorkflowProcess) {
WorkflowProcesses workflowProcesses = pkg
.getWorkflowProcesses();
ArrayList<WorkflowProcess> arrayList = workflowProcesses
.toElements();
Iterator<WorkflowProcess> it = arrayList.iterator();
ExtendedAttributes workflowProcessParent = null;
while (it.hasNext()) {
WorkflowProcess workflowProcess = it.next();
DataFields dataFields = workflowProcess.getDataFields();
ArrayList<DataField> dataFieldArrayList = dataFields
.toElements();
Iterator<DataField> dataFieldIt = dataFieldArrayList
.iterator();
ExtendedAttributes dataFieldParent = null;
while (dataFieldIt.hasNext()) {
DataField dataField = dataFieldIt.next();
ExtendedAttributes extendedAttributes = dataField
.getExtendedAttributes();
if (dataFieldParent == null) {
dataFieldParent = (ExtendedAttributes) extendedAttributes
.clone();
} else {
addExtendedAttributes(dataFieldParent,
extendedAttributes);
}
}
if (workflowProcessParent == null) {
if (dataFieldParent != null) {
workflowProcessParent = (ExtendedAttributes) dataFieldParent
.clone();
}
} else {
addExtendedAttributes(workflowProcessParent,
dataFieldParent);
}
}
if (workflowProcessParent != null) {
parent = (ExtendedAttributes) workflowProcessParent
.clone();
}
}
}
}
// 如果打开的是xml文件
if (fileFullPath.endsWith("xml")) {
try {
parent = (ExtendedAttributes) this.sourceExtendedAttributes
.clone();
parent.clear();
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileFullPath));
Node root = document
.selectSingleNode("//beans/bean/property/props");
List<Element> list = root.selectNodes(".//prop");
Iterator<Element> it = list.iterator();
while (it.hasNext()) {
Element element = it.next();
Attribute attribute = element.attribute("key");
String value = attribute.getValue();
ExtendedAttribute extendedAttribute = new ExtendedAttribute(
parent);
extendedAttribute.setName("URL");
extendedAttribute.setVValue(value.substring(1));// 去掉开始的斜杠
parent.add(extendedAttribute);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return parent;
}
@Override
protected void dialogChanged() {
// TODO Auto-generated method stub
}
public void setProjectPath(String projectPath) {
// TODO Auto-generated method stub
this.projectPath = projectPath;
}
public String getProjectPath() {
return projectPath;
}
}