Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Handling Cut/Copy/Paste
Handling Cut/Copy/Paste [message #532033] Thu, 06 May 2010 16:21 Go to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 923
Registered: July 2009
Senior Member
I have a dialog that I wrote that has a tree control on it. I want to enable copy on that tree's selection. I know that I can just hard-code Ctrl+C handling, but I want it to be integrated with eclipse command framework user-configurable key bindings.

I first attempted to register a handler for the copy command when the dialog opens. As soon as I did that I got a handler conflict warning. Looking at what my handler conflicted with, it appears that there is a default handler registered for all dialogs and windows that handles cut/copy/paste.

Ok... So I went looking at what that handler does. Turns out it uses reflection to call active widget's methods that are called "cut", "copy" and "paste".

Ok... My active widget is a Tree. Let me try subclassing it and adding a "copy" method. Nope... Tree cannot be subclasses. That blows up at runtime.

Now what? I am out of ideas for things to try. Web searches haven't turned up anything useful.

- Konstantin
Re: Handling Cut/Copy/Paste [message #532191 is a reply to message #532033] Fri, 07 May 2010 09:09 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

This is a multi-part message in MIME format.
--------------010001090701050609010601
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

For the workbench, workbench window, or parts (editor or views) the
system will calculate a priority for active handlers "for free". But
dialogs aren't part of that hierarchy, and so they have to use the
IFocusService to tell the system when to use their active handlers.

I couldn't find an example, but I've attached a dialog with a tree and a
copy handler that seems to work (also launched from a command :-) when
the tree has focus.

Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


--------------010001090701050609010601
Content-Type: text/x-java;
name="DialogHandler.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="DialogHandler.java"

package z.ex.view.views;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionInfo;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.IHandlerActivation;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.swt.IFocusService;

public class DialogHandler extends AbstractHandler {
// this is always enabled for the example
static class CopyHandler extends AbstractHandler {
private StructuredViewer viewer;

public CopyHandler(StructuredViewer v) {
viewer = v;
}

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Clipboard cb = new Clipboard(viewer.getControl().getDisplay());
TextTransfer t = TextTransfer.getInstance();
String text = getText();
System.out.println("execute: " + text);
cb.setContents(new Object[] { text }, new Transfer[] { t });
cb.dispose();
return null;
}

private String getText() {
ISelection sel = viewer.getSelection();
if (sel instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection) sel;
Object obj = ssel.getFirstElement();
if (obj instanceof Entry) {
return ((Entry) obj).desc
+ " ("
+ SimpleDateFormat.getInstance().format(
((Entry) obj).date) + ")";
}
}
return "";
}
}

static class ControlExpression extends Expression {
private Control control;

public ControlExpression(Control c) {
control = c;
}

@Override
public void collectExpressionInfo(ExpressionInfo info) {
// when these variables change, they'll be re-evaluated
info.addVariableNameAccess(ISources.ACTIVE_FOCUS_CONTROL_NAM E);
}

@Override
public EvaluationResult evaluate(IEvaluationContext context)
throws CoreException {
return EvaluationResult
.valueOf(context
.getVariable(ISources.ACTIVE_FOCUS_CONTROL_NAME) == control);
}
}

static class MyDialog extends Dialog {
private TreeViewer items;
ArrayList<Entry> itemModel = new ArrayList<Entry>();
private IFocusService focusService;
private IHandlerService handlerService;

public MyDialog(Shell parent, IFocusService fs, IHandlerService hs) {
super(parent);
// for dialogs, these should come from the IWorkbench
focusService = fs;
handlerService = hs;
}

protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("MyDialog");
}

@Override
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
// fake model
for (int i = 1; i < 5; i++) {
createEntry(i);
}

//
// setting up a tree with columns
//
items = new TreeViewer(composite, SWT.SINGLE | SWT.H_SCROLL
| SWT.V_SCROLL);
items.getControl().setLayoutData(
new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TreeViewerColumn dateColumn = new TreeViewerColumn(items, SWT.LEFT);
TreeColumn dcol = dateColumn.getColumn();
dcol.setText("Date");
dcol.setWidth(200);
TreeViewerColumn descColumn = new TreeViewerColumn(items, SWT.LEFT);
TreeColumn descCol = descColumn.getColumn();
descCol.setText("Description");
descCol.setWidth(300);
items.setContentProvider(new ViewContentProvider());
items.setLabelProvider(new EntryLabelProvider());
items.setInput(itemModel);

// add the control to the focus service, then it can be used in
// expressions
focusService.addFocusTracker(items.getControl(),
"my.dialog.tree.unique.id");

// activate the handler for that control
final IHandlerActivation activation = handlerService
.activateHandler(IWorkbenchCommandConstants.EDIT_COPY,
new CopyHandler(items),
new ControlExpression(items.getControl()));

// clean up by deactivating the handler. The focus tracker will
// be removed on dispose automatically
items.getControl().addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
handlerService.deactivateHandler(activation);
}
});

return composite;
}

private void createEntry(int index) {
Entry e = new Entry(new Date(), "item " + index);
itemModel.add(e);
}
}

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
IFocusService focusService = (IFocusService) window.getWorkbench()
.getService(IFocusService.class);
IHandlerService handlerService = (IHandlerService) window
.getWorkbench().getService(IHandlerService.class);

final Shell shell = window.getShell();
Dialog dialog = new MyDialog(shell, focusService, handlerService);
dialog.open();
return null;
}

//
// support classes
//
static class Entry {
final public Date date;
final public String desc;

public Entry(Date d, String s) {
date = d;
desc = s;
}
}

static class ViewContentProvider implements IStructuredContentProvider,
ITreeContentProvider {

public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}

public void dispose() {
}

public Object[] getElements(Object inputElement) {
if (inputElement instanceof ArrayList<?>) {
return ((ArrayList<?>) inputElement).toArray();
}
return null;
}

public boolean hasChildren(Object element) {
if (element instanceof ArrayList<?>) {
return true;
}
return false;
}

public Object getParent(Object element) {
// TODO Auto-generated method stub
return null;
}

public Object[] getChildren(Object parentElement) {
if (parentElement instanceof ArrayList<?>) {
return ((ArrayList<?>) parentElement).toArray();
}
return new Object[0];
}
}

private static class EntryLabelProvider extends LabelProvider implements
ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Ob ject)
*/
@Override
public String getText(Object element) {
return getColumnText(element, 0);
}

public String getColumnText(Object element, int columnIndex) {
switch (columnIndex) {
case 0:
return SimpleDateFormat.getInstance().format(
((Entry) element).date);
case 1:
return ((Entry) element).desc;
}
return null;
}
}
}

--------------010001090701050609010601--


Previous Topic:Get co
Next Topic:My plugin TextEditor is never created
Goto Forum:
  


Current Time: Fri Apr 18 14:18:01 EDT 2014

Powered by FUDForum. Page generated in 0.01661 seconds