I would like to put it here after generalizing confidential parts.
All you have to do is to create your own inherited handler by overriding newTableElement() and editTableElement() methods and set the table and the buttons to your handler.
package com.example.dialog.handler;
import java.util.ArrayList;
import java.util.List;
import metamodeling.generic.kernel.UMLNamedElement;
import metamodeling.generic.kernel.UMLNamespace;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;
public abstract class TableEventHandler {
private Button newButton;
private Button editButton;
private Button removeButton;
private Button upButton;
private Button downButton;
IStructuredSelection selection;
TableViewer viewer;
List<Integer> indexes = new ArrayList<Integer>();
public void setNewButton(Button newButton) {
this.newButton = newButton;
this.newButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
newTableElement();
}
});
}
public void setEditButton(Button editButton) {
this.editButton = editButton;
this.editButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
editTableElement();
}
});
}
public void setRemoveButton(Button removeButton) {
this.removeButton = removeButton;
this.removeButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
removeTableElement();
}
});
}
public void setUpButton(Button upButton) {
this.upButton = upButton;
this.upButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i <= indexes.size() - 1; i++) {
getElements().move(indexes.get(i) - 1, indexes.get(i));
}
viewer.setSelection(selection, true);
}
});
}
public void setDownButton(Button downButton) {
this.downButton = downButton;
this.downButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
for (int i = indexes.size() - 1; i >= 0; i--) {
getElements().move(indexes.get(i) + 1, indexes.get(i));
}
viewer.setSelection(selection, true);
}
});
}
private EList<UMLNamedElement> getElements() {
return ((UMLNamespace) viewer.getInput()).getTheContainedElements();
}
public void setViewer(TableViewer viewer) {
this.viewer = viewer;
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
if (event.getSelection() instanceof IStructuredSelection) {
selection = (IStructuredSelection) event.getSelection();
if (selection.size() < 1) {
if (editButton != null)
editButton.setEnabled(false);
if (removeButton != null)
removeButton.setEnabled(false);
if (upButton != null)
upButton.setEnabled(false);
if (downButton != null)
downButton.setEnabled(false);
} else {
if (upButton != null && downButton != null) {
indexes.clear();
for (Object each : selection.toList()) {
indexes.add(getElements().indexOf(each));
}
if (indexes.get(0) > 0) {
upButton.setEnabled(true);
} else {
upButton.setEnabled(false);
}
if (indexes.get(indexes.size() - 1) < getElements()
.size() - 1) {
downButton.setEnabled(true);
} else {
downButton.setEnabled(false);
}
}
if (selection.size() == 1) {
if (editButton != null)
editButton.setEnabled(true);
if (removeButton != null)
removeButton.setEnabled(true);
} else if (selection.size() > 1) {
if (editButton != null)
editButton.setEnabled(false);
if (removeButton != null)
removeButton.setEnabled(false);
}
}
}
}
});
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
if (event.getSelection() instanceof IStructuredSelection) {
selection = (IStructuredSelection) event.getSelection();
editTableElement();
}
}
});
}
protected Object getFirstSelectedElement() {
return selection.getFirstElement();
}
protected abstract void newTableElement();
protected abstract void editTableElement();
protected void removeTableElement() {
getElements().remove(getFirstSelectedElement());
}
}

No comments:
Post a Comment