Hi Craig,
This might be possible if you are implementing a WorkspaceAccess plugin type.
You can get access to the selected WSEditor and if the current page is a WSTextEditorPage, use this method:
- Code: Select all
ro.sync.exml.workspace.api.editor.page.text.WSTextEditorPage.getTextComponent()
to gain access to the used internal swing JTextArea.
A swing JComponent has an action map inside it and one approach would be to take from the action map the action corresponding to the "paste" key and wrap it in your own action.
For example if you wrap the default swing "paste" action in your own action, you could first look in the clipboard and if it contains simple text which has a certain attribute there, remove the attribute from the string and set it back in the clipboard, then run the default action.
Something like:
- Code: Select all
if(editorAccess != null && editorAccess.getCurrentPage() instanceof WSTextEditorPage) {
WSTextEditorPage tp = (WSTextEditorPage) editorAccess.getCurrentPage();
JTextArea textComponent = (JTextArea) tp.getTextComponent();
final Action defaultPasteAction = textComponent.getActionMap().get("paste");
textComponent.getActionMap().put("paste", new AbstractAction(
(String)defaultPasteAction.getValue(AbstractAction.NAME)) {
@Override
public void actionPerformed(ActionEvent e) {
Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if(contents != null) {
if(contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String stringContents;
try {
stringContents = (String) contents.getTransferData(DataFlavor.stringFlavor);
if(stringContents.contains("to_be_removed")) {
//Pre-process it and set back to clipboard
stringContents = "MODIFIED";
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(stringContents), null);
}
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//Perform default behavior
defaultPasteAction.actionPerformed(e);
}
});
}
The drag and drop is trickier.
You can set a document filter on the document from the text page which can decide to sometimes modify the inserted string like:
- Code: Select all
if(editorAccess != null && editorAccess.getCurrentPage() instanceof WSTextEditorPage) {
WSTextEditorPage tp = (WSTextEditorPage) editorAccess.getCurrentPage();
((AbstractDocument)tp.getDocument()).setDocumentFilter(new DocumentFilter() {
/**
* @see javax.swing.text.DocumentFilter#replace(javax.swing.text.DocumentFilter.FilterBypass, int, int, java.lang.String, javax.swing.text.AttributeSet)
*/
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
if(text != null && text.contains("to_be_removed")) {
//You can either decide to remote the offending data here no matter of the
//source from where it came from.
//or if you want to remove it only for dropped text you can use this hack
boolean isDnD = false;
StackTraceElement[] stackTrace = new Exception().getStackTrace();
for (int i = 0; i < stackTrace.length; i++) {
if("java.awt.dnd.DropTarget".equals(stackTrace[i].getClassName())) {
isDnD = true;
break;
}
}
if(isDnD) {
text = "MODIFIED";
}
}
super.replace(fb, offset, length, text, attrs);
}
});
}
Of course this second approach could also be used for the first use case.
Regards,
Radu