Search in sources :

Example 1 with MetadataActionProvider

use of eu.esdihumboldt.hale.ui.views.data.internal.MetadataActionProvider in project hale by halestudio.

the class InstanceExplorer method createControls.

/**
 * @see InstanceViewer#createControls(Composite, SchemaSpaceID)
 */
@Override
public void createControls(Composite parent, SchemaSpaceID schemaSpace) {
    main = new Composite(parent, SWT.NONE);
    main.setLayout(GridLayoutFactory.fillDefaults().numColumns(1).create());
    // selector composite
    selectorComposite = new Composite(main, SWT.NONE);
    selectorComposite.setLayoutData(GridDataFactory.swtDefaults().create());
    selectorComposite.setLayout(GridLayoutFactory.fillDefaults().create());
    // viewer composite
    Composite viewerComposite = new Composite(main, SWT.NONE);
    viewerComposite.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    TreeColumnLayout layout = new TreeColumnLayout();
    viewerComposite.setLayout(layout);
    viewer = new TreeViewer(viewerComposite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
    viewer.getTree().setLinesVisible(true);
    // set content provider
    viewer.setContentProvider(new InstanceContentProvider());
    viewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS);
    // TODO set label provider?
    // add definition columns
    TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.LEFT);
    column.getColumn().setText("Definition");
    column.setLabelProvider(new TreeColumnViewerLabelProvider(new PairLabelProvider(true, new DefinitionMetaLabelProvider(viewer, false, true))));
    layout.setColumnData(column.getColumn(), new ColumnWeightData(1));
    // add value column
    column = new TreeViewerColumn(viewer, SWT.LEFT);
    column.getColumn().setText("Value");
    final InstanceValueLabelProvider instanceLabelProvider = new InstanceValueLabelProvider();
    column.setLabelProvider(instanceLabelProvider);
    // new PairLabelProvider(false, new LabelProvider())));
    ColumnViewerToolTipSupport.enableFor(viewer);
    layout.setColumnData(column.getColumn(), new ColumnWeightData(1));
    MetadataActionProvider maep = new MetadataExploreActionProvider(viewer);
    maep.setup();
    // Add an editor for copying instance values
    editor = new TreeEditor(viewer.getTree());
    editor.horizontalAlignment = SWT.RIGHT;
    viewer.getTree().addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent e) {
            final ViewerCell cell = viewer.getCell(new Point(e.x, e.y));
            // Selected cell changed?
            if (cell == null || editor.getItem() != cell.getItem() || editor.getColumn() != cell.getColumnIndex()) {
                // Clean up any previous editor control
                Control oldEditor = editor.getEditor();
                if (oldEditor != null) {
                    oldEditor.dispose();
                    editor.setEditor(null, null, 0);
                }
            }
            // No valid selected cell
            if (cell == null || cell.getColumnIndex() == 0) {
                return;
            }
            // cell didn't change
            if ((editor.getItem() == cell.getItem() && editor.getColumn() == cell.getColumnIndex())) {
                return;
            }
            // determine instance value
            Object value = ((Pair<?, ?>) cell.getViewerRow().getTreePath().getLastSegment()).getSecond();
            if (value instanceof Instance) {
                value = ((Instance) value).getValue();
            }
            // copy button
            if (value != null) {
                final String textValue = value.toString();
                if (!textValue.isEmpty()) {
                    // empty string invalid for
                    // clipboard
                    Button button = new Button(viewer.getTree(), SWT.PUSH | SWT.FLAT);
                    button.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_COPY));
                    button.setToolTipText("Copy string value");
                    button.addSelectionListener(new SelectionAdapter() {

                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            // copy content to clipboard
                            Clipboard clipBoard = new Clipboard(Display.getCurrent());
                            clipBoard.setContents(new Object[] { textValue }, new Transfer[] { TextTransfer.getInstance() });
                            clipBoard.dispose();
                        }
                    });
                    // calculate editor size
                    Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
                    editor.minimumHeight = size.y;
                    editor.minimumWidth = size.x;
                    editor.setEditor(button, (TreeItem) cell.getItem(), cell.getColumnIndex());
                }
            }
        }
    });
    update();
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) TreeColumnLayout(org.eclipse.jface.layout.TreeColumnLayout) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TreeItem(org.eclipse.swt.widgets.TreeItem) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Control(org.eclipse.swt.widgets.Control) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) PairLabelProvider(eu.esdihumboldt.hale.ui.util.viewer.PairLabelProvider) TreeColumnViewerLabelProvider(org.eclipse.jface.viewers.TreeColumnViewerLabelProvider) MouseEvent(org.eclipse.swt.events.MouseEvent) Composite(org.eclipse.swt.widgets.Composite) TreeEditor(org.eclipse.swt.custom.TreeEditor) MetadataActionProvider(eu.esdihumboldt.hale.ui.views.data.internal.MetadataActionProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Point(org.eclipse.swt.graphics.Point) ViewerCell(org.eclipse.jface.viewers.ViewerCell) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) MouseMoveListener(org.eclipse.swt.events.MouseMoveListener) Clipboard(org.eclipse.swt.dnd.Clipboard)

Aggregations

Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 PairLabelProvider (eu.esdihumboldt.hale.ui.util.viewer.PairLabelProvider)1 MetadataActionProvider (eu.esdihumboldt.hale.ui.views.data.internal.MetadataActionProvider)1 TreeColumnLayout (org.eclipse.jface.layout.TreeColumnLayout)1 ColumnWeightData (org.eclipse.jface.viewers.ColumnWeightData)1 TreeColumnViewerLabelProvider (org.eclipse.jface.viewers.TreeColumnViewerLabelProvider)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1 TreeViewerColumn (org.eclipse.jface.viewers.TreeViewerColumn)1 ViewerCell (org.eclipse.jface.viewers.ViewerCell)1 TreeEditor (org.eclipse.swt.custom.TreeEditor)1 Clipboard (org.eclipse.swt.dnd.Clipboard)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 Point (org.eclipse.swt.graphics.Point)1 Button (org.eclipse.swt.widgets.Button)1 Composite (org.eclipse.swt.widgets.Composite)1 Control (org.eclipse.swt.widgets.Control)1 TreeItem (org.eclipse.swt.widgets.TreeItem)1