如何使用ole监听器获取文字DocumentBeforeSaveEvent?

时间:2014-11-12 08:57:56

标签: eclipse swt eclipse-rcp rcp ole

我试图在我的eclipse RCP应用程序嵌入单词。如果在单词应用程序中按下 CTRL + S ,则在保存打开的编辑器时遇到问题。

我尝试了以下剪辑,但即使在此(从eclipse分离)剪断我没有得到DocumentBeforeSaveEvent事件:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleEvent;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class WordMain {

    final static int DocumentBeforeSaveEvent = 0x00000008;

    public static void main(String[] args) {
        Display display = new Display();

        Shell shell = new Shell(display);
        OleFrame frame = new OleFrame(shell, SWT.NONE);
        shell.setLayout(new GridLayout());
        frame.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));

        OleControlSite site = new OleControlSite(frame, SWT.NONE, "Word.Document");
        OleAutomation doc = new OleAutomation(site);
        int[] dispInfo = doc.getIDsOfNames(new String[] { "Application" });
        Variant variant = doc.getProperty(dispInfo[0]);
        OleAutomation app = variant.getAutomation();
        variant.dispose();

        OleListener listener = new OleListener() {

            @Override
            public void handleEvent(OleEvent event) {
                System.out.println("DocumentBeforeSave Event, no. params:" + event.arguments.length);
            }
        };

        site.addEventListener(app, DocumentBeforeSaveEvent, listener);

        site.doVerb(OLE.OLEIVERB_OPEN);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        app.dispose();

    }
}

摘录源:http://www.eclipse.org/forums/index.php/t/139593/

也许DocumentBeforeSaveEvent是错误的eventId我需要某种关键的监听器,听CTRL + S?

以下是所有eventIds的列表,DocumentBeforeSave似乎是我唯一可以使用的一个:

0x2退出
0x3 DocumentChange
0x4 DocumentOpen
0x6 DocumentBeforeClose
0x7 DocumentBeforePrint
0x8 DocumentBeforeSave
0x9 NewDocument
0xa WindowActivate
0xb WindowDeactivate
0xc WindowSelectionChange
0xd WindowBeforeRightClick
0xe WindowBeforeDoubleClick
0xf EPostagePropertyDialog
0x10 EPostageInsert
0x11 MailMergeAfterMerge
0x12 MailMergeAfterRecordMerge
0x13 MailMergeBeforeMerge
0x14 MailMergeBeforeRecordMerge
0x15 MailMergeDataSourceLoad
0x16 MailMergeDataSourceValidate
0x17 MailMergeWizardSendToCustom
0x18 MailMergeWizardStateChange
0x19 WindowSize
0x1a XMLSelectionChange
0x1b XMLValidationError
0x1c DocumentSync
0x1d EPostageInsertEx
0x1e MailMergeDataSourceValidate2
0x1f ProtectedViewWindowOpen
0x20 ProtectedViewWindowBeforeEdit
0x21 ProtectedViewWindowBeforeClose
0x22 ProtectedViewWindowSize
0x23 ProtectedViewWindowActivate
0x24 ProtectedViewWindowDeactivate

我注意到,嵌入的词有它自己"保存"按钮(左上角)总是被取消激活,即使修改了这个词,也应该是"脏"。因此,激活保存按钮(或保存功能)也会触发DocumentBeforeSaveEvent事件。

使用MS Word 2010,Java 7.

1 个答案:

答案 0 :(得分:0)

这似乎是一个日食错误: https://bugs.eclipse.org/bugs/show_bug.cgi?id=64488

看起来无效