Handling drag and drop of files inside Eclipse Package Explorer - eclipse

I'm trying to create an Eclipse plugin to support a proprietary project file format. My goal is to be able to drag and drop a file in the Project Explorer (any type of file) onto a file of the type I support, and have the name of the file being dragged appended to the end of the proprietary file.
Right now, I have a custom editor that can parse out some data from an existing file in a manageable way. This means that I have an editor associated with the file type, such that my special icon shows up next to it. I don't know if that's relevant.
I'm attempting to use the extension point "org.eclipse.ui.dropActions" but I'm not sure how to register my DropActionDelegate (implements org.eclipse.ui.part.IDropActionDelegate) such that it will be called when a file is dropped onto one of my type within the Project Explorer.
Anybody have any ideas? Am I even on the right track with the DropActionDelegate?

You are on the right track implementing an IDropActionDelegate:
class DropActionDelegate implements IDropActionDelegate {
#Override
public boolean run(Object source, Object target) {
String transferredData (String) target; // whatever type is needed
return true; // if drop successful
}
}
The purpose of the extension point org.eclipse.ui.dropActions is to provide drop behaviour to views which you don't have defined yourself (like the Project Explorer).
You register the drop action extension like this:
<extension point="org.eclipse.ui.dropActions">
<action
id="my_drop_action"
class="com.xyz.DropActionDelegate">
</action>
</extension>
Don't forget to attach an adequate listener to your editor in your plugin code:
class DragListener implements DragSourceListener {
#Override
public void dragStart(DragSourceEvent event) {
}
#Override
public void dragSetData(DragSourceEvent event) {
PluginTransferData p;
p = new PluginTransferData(
"my_drop_action", // must be id of registered drop action
"some_data" // may be of arbitrary type
);
event.data = p;
}
#Override
public void dragFinished(DragSourceEvent event) {
}
}

Related

E4: drag an object from a TableViewer to Windows Explorer (or OS specific file system)

In my Eclipse RCP application I display some business data in a TableViewer.
I want the user to be able to drag a row from the table viewer and drop it on the windows desktop/explorer. Windows should then create a file with the data from the selected row that I could provide in the dragSetData(..) method of the DragSourceAdapter class.
How to implement this? It seems that using FileTransfer as the dragSourceSupport on the table viewer is the way to go as it trigger a call to the dragSetData() method. But what object should I create and assign to "event.data" in this method?
A working example would be appreciated.
I've implemented the reverse without problem, i.e. drag a file from windows explorer onto the TableViewer and add a row in the table. There are plenty on sample for this on the net but can't find a sample of the opposite, drag from eclipse to the OS
[edit + new requirement]
So I understand that I have to create a temporary file somewhere and set the name of that temp file in event.data in dragSetData()
Q: is there a simpler way to do that, eg set somewhere (iun data) the content of the file directly without the temp file?
There is another requirement. When the drop operation is about to occur, I want to show a popup to the user that will have to choose what "business data" from the "row" he wants to export and the name of the file that will be created. I tried the following (only asking for the filename for now) but it does not work as expected as the popup shows up as soon as the cursor reach the first pixel outside my app. I would like to show the popup just "before" the drop operation occurs.
Q: is there a way to have this popup show just before the drop operation occurs, ie when the user "release" the mouse button?
#Override
public void dragSetData(final DragSourceEvent event){
if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
// Will be a more complex dialog with multiple fields..
InputDialog inputDialog = new InputDialog(shell, "Please enter a file name", "File Name:", "", null);
if (inputDialog.open() != Window.OK) {
event.doit = false;
return;
}
event.data = new String[] { inputDialog.getValue() };
}
}
The event.data for FileTransfer is an array of file path strings.
You DragSourceAdapter class might look something like:
public class MyDragSourceAdapter extends DragSourceAdapter
{
private final StructuredViewer viewer;
public MyDragSourceAdapter(final StructuredViewer viewer)
{
super();
this.viewer = viewer;
}
#Override
public void dragStart(final DragSourceEvent event)
{
IStructuredSelection selection = viewer.getStructuredSelection();
if (selection == null)
return;
// TODO check if the selection contains any files
// TODO set event.doit = false if not
}
#Override
public void dragSetData(final DragSourceEvent event)
{
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
IStructuredSelection selection = viewer.getStructuredSelection();
List<String> files = new ArrayList<>(selection.size());
// TODO add files in the selection to 'files'
event.data = files.toArray(new String [files.size()]);
}
}
and you install it on your viewer with:
MyDragSourceAdapter adapter = new MyDragSourceAdapter(viewer);
viewer.addDragSupport(DND.DROP_COPY, new Transfer [] {FileTransfer.getInstance()}, adapter);

How to retain the state of a treeviewer in Java RCP application?

I have a simple RCP application having couple of wizards out of which one is having a tree viewer. I want to retain the state of the selected item in the tree viewer next time I open that particular view. As of now I have implemented using static variables and its working fine.I want to know how it can be done in a better way?
//Sample Code
private static RepositoryLocationItem lastRepoItemSelected;
Composite parent=new Composite(SWT.NONE)
treeViewer = new TreeViewer(parent);
treeViewer.setContentProvider(new MovingBoxContentProvider());
treeViewer.setLabelProvider(new MovingBoxLabelProvider());
treeViewer.setInput(getInitalInput());
treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
/* Setting the value of lastRepoItemSelected */
});
if(lastRepoItemSelected !=null)
{
treeViewer.setSelection(new StructuredSelection(lastRepoItemSelected),true);
}
Assuming this is a 3.x style RCP (your view extends ViewPart) you can use the saveState method to save your view state:
#Override
public void saveState(final IMemento memento)
{
// TODO set values in the 'memento'
}
You can then use the init method to restore values from the memento when the view is shown again:
#Override
public void init(final IViewSite site, final IMemento memento)
throws PartInitException
{
super.init(site, memento);
// TODO restore from 'memento'
}
Note: Mementos are persisted across restarts of your RCP so you need to store values in them which are valid in a new instance of the RCP.
Also look at the Eclipse wiki entry for more information.
For a WizardPage you can use the IDialogSettings. You must set this up in your Wizard using something like:
IDialogSettings pluginSettings = Activator.getDefault().getDialogSettings();
IDialogSettings wizardSettings = pluginSettings.getSection("id of your wizard");
if (wizardSettings == null) {
wizardSettings= new DialogSettings("id of your wizard");
pluginSettings.addSection(wizardSettings);
}
setDialogSettings(wizardSettings);
where Activator is your plugin activator class and "name of your wizard" is a id for your wizard (which can be anything as long as it is unique in your plugin).
In your wizard page you can then get the settings with:
IDialogSettings settings = getDialogSettings();
IDialogSettings has lots of methods for saving and restore various sorts of values, such as:
settings.put("key", "string value");
String value = settings.get("key");

How to show standard editor statusbar items for MultiPageEditor

When I open a file in a TextEditor the status bar shows information like positions, whether the file is writable or not ...
Now I have created a MultiPageEditor that contains a class derived from org.eclipse.ui.editors.text.TextEditor. If I edit a file with this editor the status bar remains empty.
Is there an easy way make the standard status bar items visible if the TextEditor is embedded in a MultiPageEditor?
Im using Eclipse Kepler.
In your MultiPageEditorActionBarContributor class you need to do:
private StatusLineContributionItem _showLine;
...
_showLine = new StatusLineContributionItem(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION, true, 14);
#Override
public void setActivePage(IEditorPart part)
{
if (part instanceof ITextEditorExtension)
{
ITextEditorExtension extension = (ITextEditorExtension)part;
extension.setStatusField(_showLine, ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION);
}
}
#Override
public void contributeToStatusLine(IStatusLineManager statusLineManager)
{
statusLineManager.add(_showLine);
}

adding annotations to file outside workspace

I need to show annotations on files outside of Workspace.
I am able to show annotations on files present in Worspace
When i try to do the same for files outside the Workspace I need to create resource and ifile object. How do i achieve the same?
How do i read contents of file outside workspace since i am unable to create a ifile object.
Here is what i am doing right now:
IEditorPart editor =(IEditorPart) wins[i].getPartService().getActivePart()
IEditorInput input = editor.getEditorInput();
IPath path = ((FileEditorInput)input).getPath();
IFile file= workspace.getRoot().getFileForLocation(path);
You cannot annotation files outside of workspace. An IFile is only defined for workspace files.
Just in case you don't know, files and projects don't have to the under the workspace's folder on the file system to be in the workspace.
Maybe you can surreptitiously add a hidden project to the workspace and link the file as a resource in it. I don't think that an external file converted to an internal file loses any behaviors. But, it could gain more than you want. One that I can think of is that source control plugins might then detect it.
Or, you could point out to the user that there are advantages to adding the file to the workspace and let them make the choice. You might be able to show a dialog asking which new or existing project/folder to add it to. Of course, if they decline then you should remember and not ask again about that external file.
UPDATE:
FileStoreEditorInput represents a file that is not part of the current workspace. To listen for external files being opened, subscribe with an IPartListener2 on each window.
public class Activator extends AbstractUIPlugin implements IStartup {
#Override
public void earlyStartup() {
final PartListener partListener = new PartListener();
for (final IWorkbenchWindow window : getWorkbench().getWorkbenchWindows()) {
window.getPartService().addPartListener(partListener);
}
getWorkbench().addWindowListener(new IWindowListener() {
#Override
public void windowOpened(IWorkbenchWindow window) {
window.getPartService().addPartListener(partListener);
}
});
}
private class PartListener implements IPartListener2 {
#Override
public void partOpened(final IWorkbenchPartReference partRef) {
if (partRef.getPart(false) instanceof EditorPart) {
final EditorPart editor = (EditorPart) partRef.getPart(false);
if (editor.getEditorInput() instanceof FileStoreEditorInput) {
final FileStoreEditorInput input = (FileStoreEditorInput) editor.getEditorInput();
System.out.println(input.getURI());
}
}
}
}

Making Ctrl-C copy from whichever SourceViewer has focus in Eclipse plug-in

I successfully extended the PyDev editor in Eclipse with a side-by-side display, but I can't copy the contents of the extra SourceViewer that I added. I can select some text in the display, but when I press Ctrl+C, it always copies the main PyDev editor's selected text.
I found an article on key bindings in Eclipse editors, but the code there seems incomplete and a bit out-of-date. How can I configure the copy command to copy from whichever SourceViewer has focus?
The reason I want to do this is that I've written a tool for live coding in Python, and it would be much easier for users to submit bug reports if they could just copy the display and paste it into the bug description.
David Green's article was a good start, but it took a bit of digging to make it all work. I published a full example project on GitHub, and I'll post a couple of snippets here.
The TextViewerSupport class wires up a new action handler for each command you want to delegate to the extra text viewer. If you have multiple text viewers, just instantiate a TextViewerSupport object for each of them. It wires up everything in its constructor.
public TextViewerSupport(TextViewer textViewer) {
this.textViewer = textViewer;
StyledText textWidget = textViewer.getTextWidget();
textWidget.addFocusListener(this);
textWidget.addDisposeListener(this);
IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
handlerService = (IHandlerService) window
.getService(IHandlerService.class);
if (textViewer.getTextWidget().isFocusControl()) {
activateContext();
}
}
The activateContext() method has a list of all the commands you want to delegate, and registers a new handler for each one. This was one of the changes from David's article; his ITextEditorActionDefinitionIds has been deprecated and replaced with IWorkbenchCommandConstants.
protected void activateContext() {
if (handlerActivations.isEmpty()) {
activateHandler(ITextOperationTarget.COPY,
IWorkbenchCommandConstants.EDIT_COPY);
}
}
// Add a single handler.
protected void activateHandler(int operation, String actionDefinitionId) {
StyledText textWidget = textViewer.getTextWidget();
IHandler actionHandler = createActionHandler(operation,
actionDefinitionId);
IHandlerActivation handlerActivation = handlerService.activateHandler(
actionDefinitionId, actionHandler,
new ActiveFocusControlExpression(textWidget));
handlerActivations.add(handlerActivation);
}
// Create a handler that delegates to the text viewer.
private IHandler createActionHandler(final int operation,
String actionDefinitionId) {
Action action = new Action() {
#Override
public void run() {
if (textViewer.canDoOperation(operation)) {
textViewer.doOperation(operation);
}
}
};
action.setActionDefinitionId(actionDefinitionId);
return new ActionHandler(action);
}
The ActiveFocusControlExpression gives the new handler a high enough priority that it will replace the standard handler, and it's almost identical to David's version. However, to get it to compile, I had to add extra dependencies to my plug-in manifest: I imported packages org.eclipse.core.expressions and org.eclipse.ui.texteditor.