Page 1 of 1

AuthorDnDListener Sample

Posted: Tue Mar 21, 2017 7:06 pm
by bpopp
Hi, I'm trying to implement a drag and drop listener in the authoring environment. Essentially what I am looking for is the ability to allow files to be dragged from a project or folder, and then have an element added to the target document which references the dropped file via an href attribute (similar to the way images are handled with the DITA framework).

I found this page which I believe describes the functionality, but I haven't had any luck finding any in-depth information or examples in the referenced SDK documentation.

I have a framework extension created that extends AuthorDnDListener and have been able to debug it. I catch an authorDragEnter event, but whether I return true or false, I don't seem to catch any other events (presuming I'm looking for authorDragExit).

Any information you can point me to would be very much appreciated.

Brian

Re: AuthorDnDListener Sample

Posted: Tue Mar 21, 2017 10:27 pm
by bpopp
I think I found my answer. The DITA framework apparently does not use the drag and drop handler, but instead uses an ExternalObjectInsertionHandler called DITAMapExternalObjectInsertionHandler. Still wouldn't mind seeing a sample DnD handler, but I think this alternative method will work for what I was trying to do. For anyone else that happens across this, here's what my modified version looks like:

Code: Select all


@API(type=APIType.INTERNAL, src=SourceType.PUBLIC)
public class ExternalObjectInsertionHandler extends AuthorExternalObjectInsertionHandler{

@Override
public void insertURLs(AuthorAccess authorAccess, List<URL> urls, int source) throws AuthorOperationException
{
if(! urls.isEmpty())
{
URL base = getBaseURLAtCaretPosition(authorAccess);
for (int i = 0; i < urls.size(); i++) {
URL url = urls.get(i);
// Obtain the relative url from the given url.
String relativeLocation = authorAccess.getUtilAccess().makeRelative(base, url);
//Probably add a topicref to it.
int cp = authorAccess.getEditorAccess().getCaretOffset();
String extension = authorAccess.getUtilAccess().getExtension(url);
String filename = relativeLocation.substring(0, relativeLocation.lastIndexOf('.'));

SchemaAwareHandlerResult result;

if ( "ngpsmap".equals(extension) )
{
result = authorAccess.getDocumentController().insertXMLFragmentSchemaAware(
"<MAPREF UniqueID=\"" + filename + "\" "
+ "HREF=\"" + relativeLocation + "\" "
+ "CHG=\"U\" "
+ "/>",
cp,
true);

}
else
{
result = authorAccess.getDocumentController().insertXMLFragmentSchemaAware(
"<ITEMREF UniqueID=\"" + filename + "\" "
+ "HREF=\"" + relativeLocation + "\" "
+ "CHG=\"U\" "
+ "/>",
cp,
true);

}
if(result != null && i < urls.size() - 1)
{
//Move after the inserted element to insert the next one.
Integer off = (Integer) result.getResult(SchemaAwareHandlerResultInsertConstants.RESULT_ID_HANDLE_INSERT_FRAGMENT_OFFSET);
if(off != null)
{
authorAccess.getEditorAccess().setCaretPosition(off.intValue() + 2);
}
}
}
}
}
}

Re: AuthorDnDListener Sample

Posted: Wed Mar 22, 2017 9:22 am
by Radu
Hi,

Indeed the handling of dropped files and URLs is made through this special extension you found.
We'll try to update our documentation to add more details about this.
This same extension is also useful for smart paste (interpreting HTML clipboard content pasted from MS Office or from a web browser and converting it to your target vocabulary):

http://blog.oxygenxml.com/2015/10/how-s ... xygen.html

Regards,
Radu