Skip to content
Snippets Groups Projects
Commit d0143c52 authored by Andreas Bayha's avatar Andreas Bayha
Browse files

Added AdvancedTreeViewer from SFIT

parent 14d7d66e
No related branches found
No related tags found
No related merge requests found
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2017 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.ui.editor;
import static org.eclipse.jface.viewers.AbstractTreeViewer.ALL_LEVELS;
import static org.eclipse.swt.SWT.NONE;
import static org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils.getEObjectElements;
import static org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils.getFirstElement;
import java.util.Collection;
import java.util.List;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.fortiss.tooling.base.ui.dnd.jface.ViewerElementCompositionDropAdapter;
import org.fortiss.tooling.kernel.ui.extension.base.EditorBase;
import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
import org.fortiss.tooling.kernel.ui.service.IActionService;
import org.fortiss.tooling.kernel.ui.service.IContextMenuService;
/**
* Customization of {@link TreeViewerEditorBase} which provide keyboard shortcuts on the tree.
*
* @author aravantinos
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 7FCEFA520787C423AE1873FB95B5A107
*/
public class AdvancedTreeViewerEditorBase<T extends EObject> extends EditorBase<T> implements
ContextMenuContextProvider {
/** Stores the {@link TreeViewer}. */
protected TreeViewer treeViewer;
/** Menu manager for an element in tree viewer */
protected MenuManager menuManager;
/** The root composite for the editor controls. */
protected Composite rootComposite;
/** The adapter used for dealing with changes of the content. */
protected final Adapter editedObjectChanged = new EContentAdapter() {
@Override
public void notifyChanged(Notification notification) {
// super.notifyChanged(notification);
if(notification.getEventType() != Notification.REMOVING_ADAPTER &&
!treeViewer.getTree().isDisposed()) {
treeViewer.refresh(true);
}
}
};
/** {@inheritDoc} */
@Override
public void createPartControl(Composite parent) {
rootComposite = new Composite(parent, NONE);
rootComposite.setLayout(new GridLayout(1, false));
rootComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
treeViewer = createTreeViewer(rootComposite);
treeViewer.getTree().setHeaderVisible(true);
treeViewer.getTree().setLinesVisible(true);
treeViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
treeViewer.setAutoExpandLevel(ALL_LEVELS);
setupTreeViewer(treeViewer);
ViewerElementCompositionDropAdapter dndAdapter =
new ViewerElementCompositionDropAdapter(treeViewer);
treeViewer.addDropSupport(dndAdapter.getSupportedDNDOperations(),
dndAdapter.getPreferredTransfers(), dndAdapter);
setTreeViewerInput();
getEditorSite().setSelectionProvider(treeViewer);
ColumnViewerToolTipSupport.enableFor(treeViewer);
getEditorSite().setSelectionProvider(treeViewer);
}
/** Sets the input and adapter for {@link #treeViewer}. */
protected void setTreeViewerInput() {
treeViewer.setInput(editedObject);
editedObject.eAdapters().add(editedObjectChanged);
}
/**
* Creates the {@link TreeViewer} instance.
*
* @param parent
* The parent, in which the {@link TreeViewer} is supposed to be embedded in.
* @return The new {@link TreeViewer}.
*/
protected TreeViewer createTreeViewer(Composite parent) {
return new TreeViewer(parent);
}
/** {@inheritDoc} */
@Override
public void dispose() {
editedObject.eAdapters().remove(editedObjectChanged);
super.dispose();
}
/** Returns the elements currently selected in the tree. */
protected Collection<EObject> getTreeSelection() {
List<EObject> result = getEObjectElements(treeViewer.getSelection());
if(result.isEmpty()) {
result.add(editedObject);
}
return result;
}
/** {@inheritDoc} */
@Override
public void setFocus() {
treeViewer.getTree().setFocus();
}
/** Returns treeViewer. */
public TreeViewer getTreeViewer() {
return treeViewer;
}
/**
* This method is used to configure the tree viewer, e.g. set the content
* provider, and the label provider.
*/
protected void setupTreeViewer(TreeViewer treeViewer) {
menuManager = IContextMenuService.getInstance().createDefaultContextMenu(this);
Menu contextMenu = menuManager.createContextMenu(treeViewer.getControl());
treeViewer.getControl().setMenu(contextMenu);
getSite().registerContextMenu(menuManager, treeViewer);
treeViewer.getControl().addKeyListener(new ViewerKeyAdapter());
treeViewer.expandAll();
}
/** Key adapter to deal with copy/paste/delete/... done through the keyboard. */
private class ViewerKeyAdapter extends KeyAdapter {
/** Constructor. */
public ViewerKeyAdapter() {
}
/** {@inheritDoc} */
@Override
public void keyReleased(KeyEvent e) {
if((e.stateMask & SWT.CTRL) != 0 && e.keyCode == 0x63) {
IActionService.getInstance().runGlobalCopyAction();
} else if((e.stateMask & SWT.CTRL) != 0 && e.keyCode == 0x76) {
IActionService.getInstance().runGlobalPasteAction();
} else if(e.keyCode == SWT.DEL) {
IActionService.getInstance().runGlobalDeleteAction();
} else if((e.stateMask & SWT.CTRL) != 0 && e.keyCode == 0x78) {
IActionService.getInstance().runGlobalCutAction();
} else if((e.stateMask & SWT.CTRL) != 0 && e.keyCode == 0x7A) {
IActionService.getInstance().runGlobalUndoAction();
} else if((e.stateMask & SWT.CTRL) != 0 && e.keyCode == 0x79) {
IActionService.getInstance().runGlobalRedoAction();
}
}
}
/** {@inheritDoc} */
@Override
public EObject getSelectedModelElement() {
if(getSite().getSelectionProvider() != null) {
return getFirstElement(getSite().getSelectionProvider().getSelection());
}
return null;
}
/** {@inheritDoc} */
@Override
public List<EObject> getSelectedModelElementList() {
if(getSite().getSelectionProvider() != null) {
return getEObjectElements(getSite().getSelectionProvider().getSelection());
}
return null;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.ui.utils;
import static org.eclipse.jface.dialogs.MessageDialog.openInformation;
import static org.eclipse.ui.PlatformUI.getWorkbench;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.fortiss.tooling.kernel.model.INamedElement;
import org.fortiss.tooling.kernel.service.ICommandStackService;
/**
* {@link EditingSupport} for name of {@link INamedElement}.
*
* @author bayha
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 0217C5A98B4291687DF698C27F5000F1
*/
public abstract class AbstractNameEditingSupport extends EditingSupport {
/** Constructor. */
public AbstractNameEditingSupport(ColumnViewer viewer) {
super(viewer);
}
/**
* For a given {@link INamedElement} 'element', retrieves all siblings, among which 'element' is
* supposed to have a unique name.
*
* @param element
* {@link INamedElement} which shall have a unique name.
* @return An array of {@link INamedElement}, which contains all siblings for the
* unique name contraint.
*/
protected abstract INamedElement[] getSiblingsForUniqueNameCheck(INamedElement element);
/** {@inheritDoc} */
@Override
protected void setValue(Object element, final Object value) {
final INamedElement elt = (INamedElement)element;
if(value != null && !(value.toString().equals(elt.getName()))) {
for(INamedElement child : getSiblingsForUniqueNameCheck(elt)) {
if(child != element && value.equals(child.getName())) {
Shell shell = getWorkbench().getActiveWorkbenchWindow().getShell();
openInformation(shell, "Information",
"This name already exists, please choose another one.");
return;
}
}
ICommandStackService.getInstance().runAsCommand(elt, new Runnable() {
@Override
public void run() {
setValueInternal(value, elt);
}
});
}
}
/** Performs the actual setName(...) call. */
protected void setValueInternal(final Object value, final INamedElement elt) {
elt.setName(value.toString());
}
/** {@inheritDoc} */
@Override
protected Object getValue(Object element) {
return ((INamedElement)element).getName();
}
/** {@inheritDoc} */
@Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor((Composite)getViewer().getControl());
}
/** {@inheritDoc} */
@Override
protected boolean canEdit(Object element) {
return element instanceof INamedElement;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment