Commit def101f5 authored by Adrien's avatar Adrien Committed by GitHub

feat(project explorer) test connector actions from explorer (#1432)

parent 33c4c36d
......@@ -5,14 +5,12 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.common.jface;
......@@ -26,13 +24,9 @@ import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
......@@ -41,8 +35,6 @@ import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
......@@ -52,126 +44,103 @@ import org.eclipse.swt.widgets.TreeColumn;
/**
* @author Romain Bioteau
*
*/
public class TreeExplorer extends Composite implements SWTBotConstants{
private TreeViewer leftTree;
private TableViewer rightTable;
private ITreeContentProvider contentProvider;
private ILabelProvider labelProvider;
private Composite additionalComposite;
public TreeExplorer(final Composite parent, final int style) {
super(parent, style);
setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create());
final Text searchField = new Text(this, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL);
searchField.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
searchField.setMessage(Messages.filterLabel);
additionalComposite = new Composite(this, SWT.NONE);
additionalComposite.setLayoutData(GridDataFactory.fillDefaults().grab(false, false).hint(0, 0).create());
additionalComposite.setLayout(GridLayoutFactory.fillDefaults().numColumns(1).margins(0, 0).create());
final Composite content = new Composite(this, SWT.BORDER | SWT.FLAT);
content.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).spacing(0, 0).margins(0, 0).create());
content.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).span(2, 1).create());
leftTree = new TreeViewer(content, SWT.V_SCROLL );
leftTree.getTree().setData(SWTBOT_WIDGET_ID_KEY, SWTBOT_ID_EXPLORER_LEFT_TREE);
leftTree.getTree().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(180, SWT.DEFAULT).create());
final Label vSeparator = new Label(content, SWT.SEPARATOR | SWT.VERTICAL);
vSeparator.setLayoutData(GridDataFactory.fillDefaults().grab(false, true).create());
rightTable = new TableViewer(content, SWT.NONE);
rightTable.getTable().setData(SWTBOT_WIDGET_ID_KEY, SWTBOT_ID_EXPLORER_RIGHT_TABLE);
rightTable.getTable().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(180,SWT.DEFAULT).create());
rightTable.setSorter(new ViewerSorter());
rightTable.addFilter(new ViewerFilter() {
@Override
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
final Object leftSelection =((IStructuredSelection) leftTree.getSelection()).getFirstElement();
final Set<Object> parents = new HashSet<>();
public class TreeExplorer extends Composite implements SWTBotConstants {
private TreeViewer leftTree;
private TableViewer rightTable;
private ITreeContentProvider contentProvider;
private ILabelProvider labelProvider;
private Composite additionalComposite;
public TreeExplorer(final Composite parent, final int style) {
super(parent, style);
setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create());
final Text searchField = new Text(this, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL);
searchField.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
searchField.setMessage(Messages.filterLabel);
searchField.setData(SWTBotConstants.SWTBOT_WIDGET_ID_KEY,
"org.bonitasoft.studio.common.jface.treeExplorer.searchFieldText");
additionalComposite = new Composite(this, SWT.NONE);
additionalComposite.setLayoutData(GridDataFactory.fillDefaults().grab(false, false).hint(0, 0).create());
additionalComposite.setLayout(GridLayoutFactory.fillDefaults().numColumns(1).margins(0, 0).create());
final Composite content = new Composite(this, SWT.BORDER | SWT.FLAT);
content.setLayout(
GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).spacing(0, 0).margins(0, 0).create());
content.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).span(2, 1).create());
leftTree = new TreeViewer(content, SWT.V_SCROLL);
leftTree.getTree().setData(SWTBOT_WIDGET_ID_KEY, SWTBOT_ID_EXPLORER_LEFT_TREE);
leftTree.getTree().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(180, SWT.DEFAULT).create());
final Label vSeparator = new Label(content, SWT.SEPARATOR | SWT.VERTICAL);
vSeparator.setLayoutData(GridDataFactory.fillDefaults().grab(false, true).create());
rightTable = new TableViewer(content, SWT.NONE);
rightTable.getTable().setData(SWTBOT_WIDGET_ID_KEY, SWTBOT_ID_EXPLORER_RIGHT_TABLE);
rightTable.getTable().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(180, SWT.DEFAULT).create());
rightTable.setSorter(new ViewerSorter());
rightTable.addFilter(new ViewerFilter() {
@Override
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
final Object leftSelection = ((IStructuredSelection) leftTree.getSelection()).getFirstElement();
final Set<Object> parents = new HashSet<>();
final Set<String> candidates = new HashSet<>();
Object p = contentProvider.getParent(element);
if(p != null){
parents.add(p);
Object p = contentProvider.getParent(element);
if (p != null) {
parents.add(p);
candidates.add(labelProvider.getText(p));
}
while (p != null) {
p = contentProvider.getParent(p);
if(p != null){
parents.add(p);
}
while (p != null) {
p = contentProvider.getParent(p);
if (p != null) {
parents.add(p);
candidates.add(labelProvider.getText(p));
}
}
if(searchField.getText().isEmpty()){
return leftSelection == null || parents.contains(leftSelection);
}
final String text = labelProvider.getText(element);
}
}
if (searchField.getText().isEmpty()) {
return leftSelection == null || parents.contains(leftSelection);
}
final String text = labelProvider.getText(element);
candidates.add(text);
return text != null &&
isCandidate(candidates, searchField.getText()) &&
(leftSelection == null || parents.contains(leftSelection));
}
});
leftTree.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(final SelectionChangedEvent event) {
final Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
if(selection != null){
rightTable.refresh(null);
}
}
});
leftTree.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(final DoubleClickEvent event) {
final Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement() ;
if(selection != null){
if(leftTree.getExpandedState(selection)){
leftTree.collapseToLevel(selection, 1) ;
}else{
leftTree.expandToLevel(selection, 1) ;
}
}
}
});
rightTable.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(final SelectionChangedEvent event) {
final Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
if(selection != null){
final Object parent = contentProvider.getParent(selection);
if(parent != null){
leftTree.setSelection(new StructuredSelection(parent),true);
}
}
}
});
searchField.addModifyListener(new ModifyListener() {
}
});
leftTree.addSelectionChangedListener(event -> {
final Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
if (selection != null) {
rightTable.refresh(null);
}
});
@Override
public void modifyText(final ModifyEvent e) {
Display.getDefault().asyncExec(new Runnable() {
leftTree.addDoubleClickListener(event -> {
final Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
if (selection != null) {
if (leftTree.getExpandedState(selection)) {
leftTree.collapseToLevel(selection, 1);
} else {
leftTree.expandToLevel(selection, 1);
}
@Override
public void run() {
rightTable.refresh();
}
});
}
});
rightTable.addSelectionChangedListener(event -> {
final Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
if (selection != null) {
final Object parent1 = contentProvider.getParent(selection);
if (parent1 != null) {
leftTree.setSelection(new StructuredSelection(parent1), true);
}
}
});
}
});
}
searchField.addModifyListener(e -> Display.getDefault().asyncExec(() -> rightTable.refresh()));
}
protected boolean isCandidate(Set<String> candidates, String text) {
for (final String c : candidates) {
......@@ -183,86 +152,84 @@ public class TreeExplorer extends Composite implements SWTBotConstants{
}
protected Object[] getSubtree(final Object selection) {
final Set<Object> result = new HashSet<>();
addChildren(result,selection);
return result.toArray();
}
private void addChildren(final Set<Object> result, final Object element) {
if(contentProvider.hasChildren(element)){
for(final Object c : contentProvider.getChildren(element)){
result.add(c);
addChildren(result, c);
}
}
}
final Set<Object> result = new HashSet<>();
addChildren(result, selection);
return result.toArray();
}
public void setContentProvider(final ITreeContentProvider contentProvider){
this.contentProvider = contentProvider;
}
private void addChildren(final Set<Object> result, final Object element) {
if (contentProvider.hasChildren(element)) {
for (final Object c : contentProvider.getChildren(element)) {
result.add(c);
addChildren(result, c);
}
}
}
public void addLeftTreeFilter(final ViewerFilter filter){
leftTree.addFilter(filter);
}
public void setContentProvider(final ITreeContentProvider contentProvider) {
this.contentProvider = contentProvider;
}
public void addRightTreeFilter(final ViewerFilter filter){
if(!Arrays.asList(rightTable.getFilters()).contains(filter)){
rightTable.addFilter(filter);
}
}
public void addLeftTreeFilter(final ViewerFilter filter) {
leftTree.addFilter(filter);
}
public void removeRightTreeFilter(final ViewerFilter filter) {
rightTable.removeFilter(filter);
}
public void addRightTreeFilter(final ViewerFilter filter) {
if (!Arrays.asList(rightTable.getFilters()).contains(filter)) {
rightTable.addFilter(filter);
}
}
public void removeLeftTreeFilter(final ViewerFilter filter) {
leftTree.removeFilter(filter);
}
public void removeRightTreeFilter(final ViewerFilter filter) {
rightTable.removeFilter(filter);
}
public void removeLeftTreeFilter(final ViewerFilter filter) {
leftTree.removeFilter(filter);
}
public void setLabelProvider(final ILabelProvider labelProvider){
this.labelProvider = labelProvider;
}
public void setLabelProvider(final ILabelProvider labelProvider) {
this.labelProvider = labelProvider;
}
public void setInput(final Object input){
Assert.isNotNull(contentProvider);
Assert.isNotNull(labelProvider);
leftTree.setContentProvider(contentProvider);
leftTree.setLabelProvider(labelProvider);
rightTable.setContentProvider(new ArrayContentProvider());
rightTable.setLabelProvider(labelProvider);
leftTree.setInput(input);
}
public void setInput(final Object input) {
Assert.isNotNull(contentProvider);
Assert.isNotNull(labelProvider);
leftTree.setContentProvider(contentProvider);
leftTree.setLabelProvider(labelProvider);
rightTable.setContentProvider(new ArrayContentProvider());
rightTable.setLabelProvider(labelProvider);
leftTree.setInput(input);
}
public Composite getAdditionalComposite() {
return additionalComposite;
}
public Composite getAdditionalComposite() {
return additionalComposite;
}
public void setLeftHeader(final String title) {
leftTree.getTree().setHeaderVisible(true);
final TreeColumn columnName = new TreeColumn(leftTree.getTree(), SWT.NONE);
columnName.setText(title);
final TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(1));
leftTree.getTree().setLayout(layout);
}
public void setLeftHeader(final String title) {
leftTree.getTree().setHeaderVisible(true);
final TreeColumn columnName = new TreeColumn(leftTree.getTree(), SWT.NONE);
columnName.setText(title);
final TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(1));
leftTree.getTree().setLayout(layout);
}
public void setRightHeader(final String title) {
rightTable.getTable().setHeaderVisible(true);
final TableColumn columnName = new TableColumn(rightTable.getTable(), SWT.NONE);
columnName.setText(title);
final TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(1));
rightTable.getTable().setLayout(layout);
}
public void setRightHeader(final String title) {
rightTable.getTable().setHeaderVisible(true);
final TableColumn columnName = new TableColumn(rightTable.getTable(), SWT.NONE);
columnName.setText(title);
final TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(1));
rightTable.getTable().setLayout(layout);
}
public TableViewer getRightTableViewer() {
return rightTable;
}
public TreeViewer geLeftTreeViewer() {
return leftTree;
}
return rightTable;
}
public TreeViewer geLeftTreeViewer() {
return leftTree;
}
}
......@@ -23,6 +23,7 @@ import java.util.Properties;
import org.bonitasoft.studio.common.FileUtil;
import org.bonitasoft.studio.common.NamingUtils;
import org.bonitasoft.studio.common.jface.SWTBotConstants;
import org.bonitasoft.studio.common.jface.databinding.validator.EmptyInputValidator;
import org.bonitasoft.studio.common.jface.databinding.validator.InputLengthValidator;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
......@@ -38,7 +39,6 @@ import org.bonitasoft.studio.dependencies.ui.dialog.SelectJarsDialog;
import org.bonitasoft.studio.pics.Pics;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.MultiValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
......@@ -72,10 +72,8 @@ import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
/**
......@@ -133,6 +131,8 @@ public class DefinitionInformationWizardPage extends WizardPage implements ISele
final Text idText = new Text(idComposite, SWT.BORDER);
idText.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
idText.setData(SWTBotConstants.SWTBOT_WIDGET_ID_KEY,
"org.bonitasoft.studio.connector.model.definition.wizard.idText");
final Label definitionVersionLabel = new Label(idComposite, SWT.NONE);
definitionVersionLabel.setText(Messages.versionLabel + " *");
......@@ -140,20 +140,18 @@ public class DefinitionInformationWizardPage extends WizardPage implements ISele
final Text versionText = new Text(idComposite, SWT.BORDER);
versionText.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
versionText.setData(SWTBotConstants.SWTBOT_WIDGET_ID_KEY,
"org.bonitasoft.studio.connector.model.definition.wizard.versionText");
final UpdateValueStrategy versionStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
versionStrategy.setAfterGetValidator(new EmptyInputValidator(Messages.versionLabel));
versionStrategy.setBeforeSetValidator(new IValidator() {
@Override
public IStatus validate(Object value) {
if (!FileUtil.isValidName(
NamingUtils.toConnectorDefinitionFilename(definition.getId(), value.toString(), true))) {
return ValidationStatus.error(Messages.invalidFileName);
}
return Status.OK_STATUS;
versionStrategy.setBeforeSetValidator(value -> {
if (!FileUtil.isValidName(
NamingUtils.toConnectorDefinitionFilename(definition.getId(), value.toString(), true))) {
return ValidationStatus.error(Messages.invalidFileName);
}
return Status.OK_STATUS;
});
final ISWTObservableValue observableIdText = SWTObservables.observeText(idText, SWT.Modify);
......@@ -185,16 +183,12 @@ public class DefinitionInformationWizardPage extends WizardPage implements ISele
final UpdateValueStrategy idStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
idStrategy.setAfterGetValidator(new EmptyInputValidator(Messages.definitionIdLabel));
idStrategy.setBeforeSetValidator(new IValidator() {
@Override
public IStatus validate(Object value) {
if (!FileUtil.isValidName(
NamingUtils.toConnectorDefinitionFilename(value.toString(), definition.getVersion(), true))) {
return ValidationStatus.error(Messages.invalidFileName);
}
return Status.OK_STATUS;
idStrategy.setBeforeSetValidator(value -> {
if (!FileUtil.isValidName(
NamingUtils.toConnectorDefinitionFilename(value.toString(), definition.getVersion(), true))) {
return ValidationStatus.error(Messages.invalidFileName);
}
return Status.OK_STATUS;
});
context.bindValue(definitionValidator.observeValidatedValue(observableIdText),
......@@ -210,6 +204,8 @@ public class DefinitionInformationWizardPage extends WizardPage implements ISele
final Text displayNameText = new Text(mainComposite, SWT.BORDER);
displayNameText.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(2, 1).create());
displayNameText.setData(SWTBotConstants.SWTBOT_WIDGET_ID_KEY,
"org.bonitasoft.studio.connector.model.definition.wizard.displayNameText");
context.bindValue(SWTObservables.observeText(displayNameText, SWT.Modify),
PojoProperties.value(DefinitionInformationWizardPage.class, "displayName").observe(this));
......@@ -248,27 +244,23 @@ public class DefinitionInformationWizardPage extends WizardPage implements ISele
final Button iconButton = new Button(iconComposite, SWT.PUSH);
iconButton.setLayoutData(GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).create());
iconButton.setText("...");
iconButton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
FileDialog dialog = new FileDialog(getShell(), SWT.OPEN | SWT.SINGLE);
dialog.setFilterExtensions(new String[] { "*.jpg;*.jpeg;*.gif;*.png;*.bmp;" });
dialog.setFilterPath(System.getProperty("user.home"));
String res = dialog.open();
if (res != null) {
try (FileInputStream is = new FileInputStream(res)) {
iconImageFile = new File(res);
setIconName(iconImageFile.getName());
if (iconImage != null) {
iconImage.dispose();
}
iconImage = new Image(Display.getDefault(), new ImageData(is).scaledTo(16, 16));
icon.setImage(iconImage);
icon.getParent().layout(true, true);
} catch (Exception ex) {
BonitaStudioLog.error(ex);
iconButton.addListener(SWT.Selection, event -> {
FileDialog dialog = new FileDialog(getShell(), SWT.OPEN | SWT.SINGLE);
dialog.setFilterExtensions(new String[] { "*.jpg;*.jpeg;*.gif;*.png;*.bmp;" });
dialog.setFilterPath(System.getProperty("user.home"));
String res = dialog.open();
if (res != null) {
try (FileInputStream is = new FileInputStream(res)) {
iconImageFile = new File(res);
setIconName(iconImageFile.getName());
if (iconImage != null) {
iconImage.dispose();
}
iconImage = new Image(Display.getDefault(), new ImageData(is).scaledTo(16, 16));
icon.setImage(iconImage);