Add New Paste command in author editor page

Post here questions and problems related to oXygen frameworks/document types.
changke
Posts: 15
Joined: Tue Aug 15, 2023 10:58 pm

Add New Paste command in author editor page

Post by changke »

We would like to add new paste commands in editor author page to paste a topic copied from an external application.

If the document in the editor is topic, the commands will paste the topic from external application as xref or link. at the position of the cursor.
paste from AW ->
paste as xref
paste as link
What APIs should I use to get the content from clipboard and insert xref or link elements?

Thanks,

Kehua
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Add New Paste command in author editor page

Post by Radu »

Hi Kehua,
It's good to give more context as we have multiple products, I think you are customizing the Oxygen Eclipse plugin, correct?
In the Oxygen Eclipse plugin's "plugin.xml" file there is an extension point defined named actionBarContributorCustomizer which you can implement with a custom Java class in your own plugin something like this for example:

Code: Select all

package com.oxygenxml.editor.editors;

import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.Display;

import ro.sync.ecss.extensions.api.AuthorAccess;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.exml.workspace.api.editor.page.author.WSAuthorEditorPage;

/**
 * @author raducoravu
 */
public class CustomActionBarContributorCustomizer extends ActionBarContributorCustomizer {

  /**
   * @see com.oxygenxml.editor.editors.ActionBarContributorCustomizer#customizeActionsContributedToDocumentMenu(java.util.List)
   */
  @Override
  public List<IAction> customizeActionsContributedToDocumentMenu(List<IAction> actions) {
    return actions;
  }

  /**
   * @see com.oxygenxml.editor.editors.ActionBarContributorCustomizer#customizeActionsContributedToDocumentToolbar(java.util.List)
   */
  @Override
  public List<IAction> customizeActionsContributedToDocumentToolbar(List<IAction> actions) {
    return actions;
  }

  /**
   * @see com.oxygenxml.editor.editors.ActionBarContributorCustomizer#customizeAuthorPageInternalCoolbar(org.eclipse.swt.widgets.CoolBar, ro.sync.exml.workspace.api.editor.page.author.WSAuthorEditorPage)
   */
  @Override
  public void customizeAuthorPageInternalCoolbar(CoolBar coolbar,
      WSAuthorEditorPage authorEditorPage) {
  }
  
  /**
   * @see com.oxygenxml.editor.editors.ActionBarContributorCustomizer#customizeAuthorPopUpMenu(org.eclipse.jface.action.IMenuManager, ro.sync.ecss.extensions.api.AuthorAccess)
   */
  @Override
  public void customizeAuthorPopUpMenu(IMenuManager popUp, AuthorAccess authorAccess) {
    popUp.add(new Action("Paste link") {
      /**
       * @see org.eclipse.jface.action.IAction#run()
       */
      @Override
      public void run() {
        Clipboard clipboard = new Clipboard(Display.getDefault());
        String contents = (String) clipboard.getContents(TextTransfer.getInstance());
        try {
          authorAccess.getDocumentController().insertXMLFragment("<link href=\"" + contents + "\"/>", authorAccess.getEditorAccess().getCaretOffset());
        } catch (AuthorOperationException e) {
          e.printStackTrace();
        }
        clipboard.dispose();
      }
    });
  }
}
So you need to use the mechanisms in Eclipse to read the clipboard (maybe you have there a different flavor than plain text) and then use our AuthorDocumentController APis to make changes inside the current XML document.
https://www.oxygenxml.com/InstData/Edit ... oller.html
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
changke
Posts: 15
Joined: Tue Aug 15, 2023 10:58 pm

Re: Add New Paste command in author editor page

Post by changke »

Hi Radu,

We are customizing Oxygen XML editor standalone desktop application, not oxygen eclipse plugin.

Thanks,

Kehua
changke
Posts: 15
Joined: Tue Aug 15, 2023 10:58 pm

Re: Add New Paste command in author editor page

Post by changke »

and we have implemented a Workspace Access plugin. Would the approach you proposed work for it as well?

Thanks,

Kehua
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Add New Paste command in author editor page

Post by Radu »

Hi Kehua,
For Oxygen desktop there is a similar API to add a menu and toolbars customizer:
https://www.oxygenxml.com/InstData/Edit ... ustomizer)
Then you override the method:
https://www.oxygenxml.com/InstData/Edit ... horAccess)
and it will get called whenever Oxygen wants to show the popup menu in the Author visual editng mode allowing you to add a Java swing action to the popup menu then use the regular Java Swing ways of accessing the clipboard.
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
changke
Posts: 15
Joined: Tue Aug 15, 2023 10:58 pm

Re: Add New Paste command in author editor page

Post by changke »

Radu,

Thank you.
While I am adding the menu to popup menu, I would prefer it to be added at a specific location, for example, right after paste special menu.
what would be the right way to decide the position of paste special menu. I assume menu item name or label is localized, what is the key or property to be used to identify the menu item for paste special?

Thanks,

Kehua
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Add New Paste command in author editor page

Post by Radu »

Hi Kehua,
Good question, we have an API method which for an action of a component which has some text "pluginWorkspaceAccess.getActionsProvider().getActionID(comp)" should return the translation "key" instead of the translated text.
You should try running some code like this to list all IDs from the popup menu, I think the menu you are interested in has the ID "Author/Paste_special".

Code: Select all

    public void applicationStarted(StandalonePluginWorkspace pluginWorkspaceAccess) {
      pluginWorkspaceAccess.addMenusAndToolbarsContributorCustomizer(new MenusAndToolbarsContributorCustomizer() {
        @Override
        public void customizeAuthorPopUpMenu(javax.swing.JPopupMenu popUp, ro.sync.ecss.extensions.api.AuthorAccess authorAccess) {
          iterate(popUp);
        }

        private void iterate(JComponent comp) {
          System.err.println(pluginWorkspaceAccess.getActionsProvider().getActionID(comp));
          int cc = comp.getComponentCount();
          for (int i = 0; i < cc; i++) {
            if(comp.getComponent(i) instanceof JComponent) {
              iterate((JComponent) comp.getComponent(i));
            }
          }
        }
      });
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
changke
Posts: 15
Joined: Tue Aug 15, 2023 10:58 pm

Re: Add New Paste command in author editor page

Post by changke »

Hi Radu,

Thank you very much. I will give it a try.

Kehua
Post Reply