java - 那个 "knows"插件当前打开的文件是什么?

标签 java eclipse eclipse-plugin lotus-notes

我想构建一个插件,该插件会在 Notes 客户端 (8.5.2++) 加载时加载,每当打开文档时都会调用该插件并获取该文档的 (Notes) URL。我需要哪些扩展点和 API?

澄清:
我确实知道如何访问当前文档(NotesUIWorkspace.currentDocument)。我不知道的是如何(以及何时)注册监听器以获得通知。
特殊挑战:文档可以在框架集中(多个)打开,并且文档可以作为复合页面的一部分打开。框架集并不是一个大问题,而是复合 Material 。如果这需要监听任何页面打开并检查它 - 我同意

最佳答案

我们通过获取任何类型的“帖子选择”的所有请求解决了这个问题。以下代码片段来自侧边栏插件,并在 createViewPart() 中调用:

m_Observer = new NotesSelectionObserverImpl();
NotesSelectionObservable cObservable = new NotesSelectionObservable();
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().addPostSelectionListener(cObservable);
cObservable.addObserver(this.m_Observer);

魔法的第一部分是“PlattformUI ..... .addPostSelecitonListener();”此时我们注册我们的监听器,这也是基于观察者模式的。

class NotesSelectionObserverImpl implements NotesSelectionObserver {

            @Override
            public void onSelectionChange(NotesSelectionContext cContext)
                throws NotesException {
            Database ndbCurrent = cContext.getDatabase();
            Document docCurrent = cContext.getDocument();

            if (ndbCurrent != null && docCurrent != null) {
                String strEMail = "";
                if (docCurrent.getItemValueString("Form").equals("Memo")
                        || docCurrent.getItemValueString("Form")
                                .equals("Reply")) {
                    strEMail = docCurrent.getItemValueString("From");
                    strEMail = parseEMail(strEMail);

                    System.out.println("EMAIL: " + strEMail);
                    ContextCommand ccCurrent = new ContextCommand(strEMail,
                            docCurrent.getItemValueString("Subject"));
                    m_State.doFeedAction(false, m_Feeds.get(Activator.FEED_CONTEXT_ID), ccCurrent);
                }

            }
        }

        @Override
        public void onUpdateAfterSelectionChange() {
            // TODO Auto-generated method stub

        }

    }

NotesSelectionObserver 是一个具有以下定义的接口(interface):

import lotus.domino.NotesException;

public interface NotesSelectionObserver
{
    void onSelectionChange(NotesSelectionContext cContext) throws NotesException;

    void onUpdateAfterSelectionChange();
}

NotesSelecitonContext 是另一个接口(interface),它提供有关选择的所有信息。定义如下:

import lotus.domino.Database;
import lotus.domino.Document;

public interface NotesSelectionContext
{
    public Database getDatabase();

    public Document getDocument();

    public String getField();
}

现在是最后一部分,线索就是...... NotesSelectionObservable:

import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.Vector;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.Session;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.INullSelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.progress.UIJob;

import com.ibm.csi.types.DocumentSummary;
import com.ibm.notes.java.api.util.NotesSessionJob;
import com.ibm.notes.java.ui.documents.NotesUIField;
import com.ibm.workplace.noteswc.selection.NotesApplicationInfo;
import com.ibm.workplace.noteswc.selection.NotesFieldSelection;
import com.ibm.workplace.noteswc.selection.NotesTextSelection;

public class NotesSelectionObservable implements INullSelectionListener {
    public void addObserver(NotesSelectionObserver cObserver) {
        if (!this.m_cObserverList.contains(cObserver))
            this.m_cObserverList.add(cObserver);
    }

    public void removeObserver(NotesSelectionObserver cObserver) {
        this.m_cObserverList.remove(cObserver);
    }

    public void selectionChanged(IWorkbenchPart cPart, ISelection cSelection) {
        if (cSelection == null || cSelection.isEmpty())
            this.clearSelection();
        else if (cSelection instanceof StructuredSelection) {
            Object cObj = ((StructuredSelection) cSelection).getFirstElement();
            if (cObj instanceof DocumentSummary) {
                DocumentSummary cSummary = (DocumentSummary) cObj;
                this.setURL(cSummary.getUrl());
                if (cSummary.getDocumentKey() != null)
                     this.setURL(cSummary.getDocumentKey().getUniqueId());
            } else if (cObj instanceof NotesApplicationInfo) {
                NotesApplicationInfo cInfo = (NotesApplicationInfo) cObj;
                this.setURL(cInfo.getUrl());
            }
            if (cObj instanceof NotesFieldSelection) {
                NotesUIField f = ((NotesFieldSelection)cObj).getCurrentField();
                this.setField(f.getName());
            }
            if (cObj instanceof NotesTextSelection) {
                // String strOut = ((NotesTextSelection) cObj).getText();
                // System.out.println("Selektierter Text: " + strOut);
            }
            if (false) {
                Class<? extends Object> c;
                String cl = null;
                for (c = cObj.getClass(); c != null; c = c.getSuperclass()) {
                    if (c.equals(Object.class))
                        break;
                    cl = cl != null ? cl + " : " + c.getName() : c.getName();
                }
                System.out.println("Type of selected object: " + cl);
            }
        }
        if (this.m_bModified) {
            this.startJob();
            this.m_bModified = false;
        }
    }

    private void clearSelection() {
        this.m_strDatabaseRepID = null;
        this.m_strDatabaseServer = null;
        this.m_strDocumentUNID = null;
        this.m_strDesginElement = null;
        this.m_strField = null;
        this.m_bModified = true;
    }

    private void setDatabase(String strRepID, String strServer) {
        if (strRepID.equals(this.m_strDatabaseRepID) == false
                || (strServer != null && this.m_strDatabaseServer == null)
                || (this.m_strDatabaseServer != null && this.m_strDatabaseServer.equals(strServer) == false)) {
            this.m_strDatabaseRepID = strRepID;
            this.m_strDatabaseServer = strServer;
            this.m_strDesginElement = null;
            this.m_strDocumentUNID = null;
            this.m_strField = null;
            this.m_bModified = true;
        }
    }

    private void setDesignElement(String strUNID) {
        if (!strUNID.equals(this.m_strDesginElement)) {
            this.m_strDesginElement = strUNID;
            this.m_strDocumentUNID = null;
            this.m_strField = null;
            this.m_bModified = true;
        }
    }

    private void setDocument(String strDocumentUNID) {
        if (!strDocumentUNID.equals(this.m_strDocumentUNID)) {
            this.m_strDocumentUNID = strDocumentUNID;
            this.m_strField = null;
            this.m_bModified = true;
        }
    }

    private void setField(String strField) {
        if (!strField.equals(this.m_strField)) {
            this.m_strField = strField;
            this.m_bModified = true;
        }
    }

    private void setURL(String strURL) {
        if (strURL == null || strURL.isEmpty())
            return;
        URL cURL;
        try {
            cURL = new URL(strURL);
        } catch (MalformedURLException e) {
            return;
        }
        if (!cURL.getProtocol().equalsIgnoreCase("notes"))
            return;
        StringTokenizer cToken = new StringTokenizer(cURL.getPath()
                .substring(1), "/");
        if (cToken.hasMoreElements())
            this.setDatabase(cToken.nextToken(),
                    cURL.getHost().isEmpty() ? null : cURL.getHost());
        else
            return;
        if (cToken.hasMoreElements())
            this.setDesignElement(cToken.nextToken());
        else
            return;
        if (cToken.hasMoreElements())
            this.setDocument(cToken.nextToken());
    }

    private void startJob() {
        if (this.m_strDatabaseRepID != null && this.m_cObserverList.size() > 0) {
            Job cJob = new TheJob(this);
            cJob.schedule();
            try {
                cJob.join();
                for (NotesSelectionObserver o : this.m_cObserverList)
                    o.onUpdateAfterSelectionChange();
                this.m_bModified = false;
            } catch (InterruptedException e) {
                return;
            }
            cJob = new TheUIJob(this);
            cJob.schedule();
        }
    }

    private class NotesSelectionContextImp implements NotesSelectionContext {
        public Database getDatabase() {
            return cDatabase;
        }

        public Document getDocument() {
            return cDocument;
        }

        public String getField() {
            return strField;
        }

        public Database cDatabase;
        public Document cDocument;
        public String strField;
    }

    private class TheJob extends NotesSessionJob {
        public TheJob(NotesSelectionObservable cObservable) {
            super(cObservable.getClass().getName() + ".selectionChanged()");
            this.m_strRepID = cObservable.m_strDatabaseRepID;
            this.m_strServer = cObservable.m_strDatabaseServer != null ? cObservable.m_strDatabaseServer
                    : "";
            if (this.m_strServer.contains("%2F"))
                this.m_strServer = this.m_strServer.replace("%2F", "/");
            this.m_strDocumentUNID = cObservable.m_strDocumentUNID;
            this.m_cContext.strField = cObservable.m_strField;
            this.m_cObservable = cObservable;
        }

        protected IStatus runInNotesThread(Session cSession,
                IProgressMonitor cProgress) throws NotesException {
            this.m_cContext.cDatabase = cSession.getDbDirectory(this.m_strServer).openDatabaseByReplicaID(this.m_strRepID);
            if (!this.m_cContext.cDatabase.isOpen()) {
                this.m_cContext.cDatabase.open();

            }
            this.m_cContext.cDocument = this.m_strDocumentUNID != null ? this.m_cContext.cDatabase.getDocumentByUNID(this.m_strDocumentUNID)
                    : null;
            for (NotesSelectionObserver o : this.m_cObservable.m_cObserverList)
                o.onSelectionChange(this.m_cContext);
            return Status.OK_STATUS;
        }

        private NotesSelectionContextImp m_cContext = new NotesSelectionContextImp();
        private NotesSelectionObservable m_cObservable;
        private String m_strRepID;
        private String m_strServer;
        private String m_strDocumentUNID;
    }

    private class TheUIJob extends UIJob {
        public TheUIJob(NotesSelectionObservable cObservable) {
            super(cObservable.getClass().getName() + ".selectionChanged()");
            this.m_cObservable = cObservable;
        }

        public IStatus runInUIThread(IProgressMonitor arg0) {
            for (NotesSelectionObserver o : this.m_cObservable.m_cObserverList)
                o.onUpdateAfterSelectionChange();
            this.m_cObservable.m_bModified = false;
            return Status.OK_STATUS;
        }

        private NotesSelectionObservable m_cObservable;
    }

    private String m_strDatabaseRepID = null;
    private String m_strDatabaseServer = null;
    private String m_strDesginElement = null;
    private String m_strDocumentUNID = null;
    private String m_strField = null;
    private boolean m_bModified = false;
    private Vector<NotesSelectionObserver> m_cObserverList = new Vector<NotesSelectionObserver>();
}

您还询问了插件列表:

 org.eclipse.ui,
 org.eclipse.core.runtime,
 com.ibm.notes.java.api;bundle-version="1.5.1",
 com.ibm.notes.java.ui;bundle-version="8.5.1",
 com.ibm.csi;bundle-version="1.5.1",
 com.ibm.notes.client;bundle-version="8.5.1"

我们也会将此代码作为插件引入 openNTF 社区。我认为任何想要通过侧边栏扩展 NotesClient 的人都需要了解用户所在的上下文并希望对此上下文做出响应。

关于java - 那个 "knows"插件当前打开的文件是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12265215/

相关文章:

java - Eclipse:在重新运行之前终止当前进程

包 com.twilio.sdk 的 java.lang.ClassNotFoundException

java - 如何将HashMap显示为DataTable

java - 我如何根据输入到数字字段中的数字在切换按钮上设置延迟?

eclipse - Tomcat with Eclipse : Made a copy of webapp and renamed, 上下文路径没有改变

javascript - Eclipse 插件开发 - JavaDoc,如 JavaScript 的悬停提示 (Eclipse JSDT)

Eclipse-plugin 如何获取当前文本编辑器光标位置

eclipse - 是时候在 Eclipse 中开发一个选项来修改 Java 文件源了

java - Java中如何实现多个类的代码?

java - 解析删除对象