package org.jbpm.ui.editor;
import java.beans.PropertyChangeEvent;
import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.gef.ui.actions.Clipboard;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Display;
import org.eclipse.ui.ide.IDE;
import org.jbpm.ui.DesignerLogger;
import org.jbpm.ui.ParContentProvider;
import org.jbpm.ui.common.command.ProcessDefinitionRemoveVariablesCommand;
import org.jbpm.ui.common.model.FormNode;
import org.jbpm.ui.common.model.NotificationMessages;
import org.jbpm.ui.common.model.ProcessDefinition;
import org.jbpm.ui.common.model.Variable;
import org.jbpm.ui.dialog.CreateVariableDialog;
import org.jbpm.ui.dialog.UpdateVariableNameDialog;
import org.jbpm.ui.editor.ltk.PortabilityRefactoring;
import org.jbpm.ui.editor.ltk.RenameRefactoringWizard;
import org.jbpm.ui.editor.search.GPDSearchQuery;
import org.jbpm.ui.resource.Messages;
import ru.runa.wf.web.forms.format.StringFormat;
public class DesignerVariableEditorPage extends EditorPartBase {
private ListViewer viewer;
private Button searchButton;
private Button moveUpButton;
private Button moveDownButton;
private Button renameButton;
private Button changeButton;
private Button deleteButton;
private Button copyButton;
private Button pasteButton;
public DesignerVariableEditorPage(DesignerEditor editor) {
super(editor);
}
@Override
public void setFocus() {
super.setFocus();
updateButtons();
}
@Override
public void createPartControl(Composite parent) {
SashForm sashForm = createToolkit(parent, "DesignerVariableEditorPage.label.variables");
Composite allVariablesComposite = createSection(sashForm, "DesignerVariableEditorPage.label.all_variables");
viewer = new ListViewer(allVariablesComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
toolkit.adapt(viewer.getControl(), false, false);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.minimumWidth = 100;
viewer.getControl().setLayoutData(gridData);
viewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
return ((Variable) element).getName();
}
});
viewer.setContentProvider(new ArrayContentProvider());
createContextMenu(viewer.getControl());
getSite().setSelectionProvider(viewer);
Composite buttonsBar = toolkit.createComposite(allVariablesComposite);
buttonsBar.setLayout(new GridLayout(1, false));
gridData = new GridData();
gridData.horizontalAlignment = SWT.LEFT;
gridData.verticalAlignment = SWT.TOP;
buttonsBar.setLayoutData(gridData);
addButton(buttonsBar, "button.create", new CreateVariableSelectionListener(), false);
renameButton = addButton(buttonsBar, "button.rename", new RenameVariableSelectionListener(), true);
changeButton = addButton(buttonsBar, "button.change", new ChangeVariableSelectionListener(), true);
copyButton = addButton(buttonsBar, "button.copy", new CopyVariableSelectionListener(), true);
pasteButton = addButton(buttonsBar, "button.paste", new PasteVariableSelectionListener(), true);
searchButton = addButton(buttonsBar, "button.search", new SearchVariableUsageSelectionListener(), true);
moveUpButton = addButton(buttonsBar, "button.up", new MoveVariableSelectionListener(true), true);
moveDownButton = addButton(buttonsBar, "button.down", new MoveVariableSelectionListener(false), true);
deleteButton = addButton(buttonsBar, "button.delete", new RemoveVariableSelectionListener(), true);
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
updateButtons();
}
});
fillViewer();
updateButtons();
}
private void updateButtons() {
List<Variable> variables = (List<Variable>) viewer.getInput();
List selected = ((IStructuredSelection) viewer.getSelection()).toList();
enableAction(searchButton, selected.size() == 1);
enableAction(changeButton, selected.size() == 1);
enableAction(moveUpButton, selected.size() == 1 && variables.indexOf(selected.get(0)) > 0);
enableAction(moveDownButton, selected.size() == 1 && variables.indexOf(selected.get(0)) < variables.size() - 1);
enableAction(deleteButton, selected.size() > 0);
enableAction(renameButton, selected.size() == 1);
enableAction(copyButton, selected.size() > 0);
boolean pasteEnabled = false;
if (Clipboard.getDefault().getContents() instanceof List) {
List list = (List) Clipboard.getDefault().getContents();
if (list.size() > 0 && list.get(0) instanceof Variable) {
pasteEnabled = true;
}
}
enableAction(pasteButton, pasteEnabled);
}
public void select(Variable variable) {
viewer.setSelection(new StructuredSelection(variable));
}
public void propertyChange(PropertyChangeEvent evt) {
String type = evt.getPropertyName();
if (NotificationMessages.NODE_CHILDS_CHANGED.equals(type)) {
fillViewer();
} else if (ProcessDefinition.PROPERTY_NAME.equals(type) && evt.getSource() instanceof Variable) {
viewer.refresh(evt.getSource());
}
}
private void fillViewer() {
List<Variable> variables = getDefinition().getVariablesList();
viewer.setInput(variables);
for (Variable var : variables) {
var.addPropertyChangeListener(this);
}
updateButtons();
}
@Override
public void dispose() {
for (Variable var : getDefinition().getVariablesList()) {
var.removePropertyChangeListener(this);
}
super.dispose();
}
private class MoveVariableSelectionListener extends SelectionAdapter {
private final boolean up;
public MoveVariableSelectionListener(boolean up) {
this.up = up;
}
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Variable variable = (Variable) selection.getFirstElement();
List<Variable> children = getDefinition().getVariablesList();
int index = children.indexOf(variable);
getDefinition().swapChilds(variable, up ? children.get(index-1) : children.get(index+1));
viewer.setSelection(selection);
//updateButtons();
}
}
private void delete(Variable variable) {
List<FormNode> nodesWithVar = ParContentProvider.getFormsWhereVariableUsed(editor.getDefinitionFile(), getDefinition(), variable);
StringBuffer formNames = new StringBuffer(Messages.getString("Variable.ExistInForms")).append("\n");
if (nodesWithVar.size() > 0) {
for (FormNode node : nodesWithVar) {
formNames.append(" - ").append(node.getName()).append("\n");
}
formNames.append(Messages.getString("Variable.WillBeRemovedFromFormAuto"));
} else {
formNames.append(Messages.getString("Variable.NoFormsUsed"));
}
if (MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), Messages.getString("ConfirmDelete"), formNames.toString())) {
// remove variable from form validations
ParContentProvider.rewriteFormValidationsRemoveVariable(editor.getDefinitionFile(), nodesWithVar, variable);
// remove variable from definition
ProcessDefinitionRemoveVariablesCommand command = new ProcessDefinitionRemoveVariablesCommand();
command.setProcessDefinition(getDefinition());
command.setVariable(variable);
editor.getCommandStack().execute(command);
}
}
private class SearchVariableUsageSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
try {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Variable variable = (Variable) selection.getFirstElement();
GPDSearchQuery query = new GPDSearchQuery(editor.getDefinitionFile(), getDefinition(), variable.getName());
NewSearchUI.runQueryInBackground(query);
} catch (Exception ex) {
DesignerLogger.logError(ex);
}
}
}
private class RenameVariableSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Variable variable = (Variable) selection.getFirstElement();
UpdateVariableNameDialog dialog = new UpdateVariableNameDialog(editor.getDefinition());
dialog.setName(variable.getName());
int result = dialog.open();
if (result != IDialogConstants.OK_ID) {
return;
}
String replacement = dialog.getName();
IResource projectRoot = editor.getDefinitionFile().getParent();
PortabilityRefactoring ref = new PortabilityRefactoring(
editor.getDefinitionFile(), editor.getDefinition(),
variable.getName(), replacement);
boolean useLtk = ref.isUserInteractionNeeded();
if (useLtk) {
RenameRefactoringWizard wizard = new RenameRefactoringWizard(ref);
wizard.setDefaultPageTitle(Messages.getString("Refactoring.variable.name"));
RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
try {
result = op.run(Display.getCurrent().getActiveShell(), "");
if (result != IDialogConstants.OK_ID) {
return;
}
} catch (InterruptedException ex) {
// operation was canceled
}
}
variable.setName(replacement);
if (useLtk) {
IDE.saveAllEditors(new IResource[] { projectRoot }, false);
}
}
}
private class RemoveVariableSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
List<Variable> variables = (List<Variable>) selection.toList();
for (Variable variable : variables) {
try {
delete(variable);
} catch (Exception e1) {
DesignerLogger.logError(e1);
}
}
}
}
private class CreateVariableSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
CreateVariableDialog dialog = new CreateVariableDialog(getDefinition(), StringFormat.class.getName(), true);
if (dialog.open() == IDialogConstants.OK_ID) {
Variable variable = new Variable(dialog.getName(), dialog.getType(), dialog.isPublicVisibility());
getDefinition().addVariable(variable);
IStructuredSelection selection = new StructuredSelection(variable);
viewer.setSelection(selection);
}
}
}
private class ChangeVariableSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Variable variable = (Variable) selection.getFirstElement();
CreateVariableDialog dialog = new CreateVariableDialog(getDefinition(), variable.getFormat(), false);
if (dialog.open() == IDialogConstants.OK_ID) {
variable.setFormat(dialog.getType());
variable.setPublicVisibility(dialog.isPublicVisibility());
viewer.setSelection(selection);
}
}
}
private class CopyVariableSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Clipboard.getDefault().setContents(selection.toList());
}
}
private class PasteVariableSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
List<Variable> newVariables = (List<Variable>) Clipboard.getDefault().getContents();
for (Variable variable : newVariables) {
Variable newVariable = getDefinition().getVariablesMap().get(variable.getName());
if (newVariable == null) {
newVariable = new Variable(variable.getName(), variable.getFormat(), variable.isPublicVisibility());
getDefinition().addVariable(newVariable);
} else {
newVariable.setFormat(variable.getFormat());
}
}
}
}
}