Search in sources :

Example 1 with MetadataActionExtension

use of eu.esdihumboldt.hale.common.instance.extension.metadata.MetadataActionExtension in project hale by halestudio.

the class MetadataActionProvider method setup.

/**
 * Adds the action to the certain TreeViewer cell
 */
public void setup() {
    final TreeEditor metaEditor = new TreeEditor(treeViewer.getTree());
    metaEditor.horizontalAlignment = SWT.LEFT;
    metaEditor.verticalAlignment = SWT.TOP;
    treeViewer.getTree().addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent e) {
            final ViewerCell cell = treeViewer.getCell(new Point(e.x, e.y));
            // Selected cell changed?
            if (cell == null || metaEditor.getItem() != cell.getItem() || metaEditor.getColumn() != cell.getColumnIndex()) {
                // Clean up any previous editor control
                Control oldmetaEditor = metaEditor.getEditor();
                if (oldmetaEditor != null) {
                    oldmetaEditor.dispose();
                    metaEditor.setEditor(null, null, 0);
                }
            }
            // No selected cell or selected cell didn't change.
            if (cell == null || cell.getColumnIndex() == 0 || (metaEditor.getItem() == cell.getItem() && metaEditor.getColumn() == cell.getColumnIndex())) {
                return;
            }
            // Initiate the extension-point
            MetadataActionExtension mae = MetadataActionExtension.getInstance();
            final Pair<Object, Object> data = retrieveMetadata(cell);
            if (data == null) {
                return;
            }
            // get all defined actions
            final List<MetadataActionFactory> actionSources = mae.getMetadataActions(data.getFirst().toString());
            if (actionSources == null || actionSources.isEmpty()) {
                return;
            }
            // Tool-bar used to view and trigger the different possible
            // actions
            ToolBar tooli = new ToolBar(treeViewer.getTree(), SWT.NONE);
            for (final MetadataActionFactory source : actionSources) {
                ToolItem actionItem = new ToolItem(tooli, SWT.PUSH);
                // get the Icon of the action
                URL iconURL = source.getIconURL();
                Image image = ImageDescriptor.createFromURL(iconURL).createImage();
                actionItem.setImage(image);
                actionItem.setToolTipText(source.getDisplayName());
                actionItem.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        try {
                            source.createExtensionObject().execute(data.getFirst(), data.getSecond());
                        } catch (Exception e1) {
                            _log.userError("error creating metadata action", e1);
                        }
                    }
                });
            }
            metaEditor.setEditor(tooli, (TreeItem) cell.getItem(), cell.getColumnIndex());
            tooli.pack();
        }
    });
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) TreeEditor(org.eclipse.swt.custom.TreeEditor) MetadataActionExtension(eu.esdihumboldt.hale.common.instance.extension.metadata.MetadataActionExtension) TreeItem(org.eclipse.swt.widgets.TreeItem) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) ViewerCell(org.eclipse.jface.viewers.ViewerCell) URL(java.net.URL) MouseMoveListener(org.eclipse.swt.events.MouseMoveListener) MetadataActionFactory(eu.esdihumboldt.hale.common.instance.extension.metadata.MetadataActionFactory) Control(org.eclipse.swt.widgets.Control) ToolBar(org.eclipse.swt.widgets.ToolBar) SelectionEvent(org.eclipse.swt.events.SelectionEvent) List(java.util.List) ToolItem(org.eclipse.swt.widgets.ToolItem) Pair(eu.esdihumboldt.util.Pair)

Aggregations

MetadataActionExtension (eu.esdihumboldt.hale.common.instance.extension.metadata.MetadataActionExtension)1 MetadataActionFactory (eu.esdihumboldt.hale.common.instance.extension.metadata.MetadataActionFactory)1 Pair (eu.esdihumboldt.util.Pair)1 URL (java.net.URL)1 List (java.util.List)1 ViewerCell (org.eclipse.jface.viewers.ViewerCell)1 TreeEditor (org.eclipse.swt.custom.TreeEditor)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1 MouseMoveListener (org.eclipse.swt.events.MouseMoveListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Image (org.eclipse.swt.graphics.Image)1 Point (org.eclipse.swt.graphics.Point)1 Control (org.eclipse.swt.widgets.Control)1 ToolBar (org.eclipse.swt.widgets.ToolBar)1 ToolItem (org.eclipse.swt.widgets.ToolItem)1 TreeItem (org.eclipse.swt.widgets.TreeItem)1