문제

Say I have a Handler that is logging data to some object via a listener.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IHandlerService service;
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    try {
        RecordingDocument d = new RecordingDocument("TestProject", "Tester", true);
        d.record();
        MessageDialog.openInformation(
                window.getShell(),
                "JavaTV",
                "You are now recording.");
    } catch (CoreException e) {
        e.printStackTrace();
    }
    return null;
}

This object is created when one of the menu items is selected and begins logging to a data structure inside the object.

How do I retrieve this document from a different handler? I need this if someone uses the menu to stop the recording.

도움이 되었습니까?

해결책

It seems like a general Java question on how to make some object accessible by other objects. Obviously, you need to store it in a place, where it can be accessed by others (provide a getter, put to the registry, store to the DB, serialize to the hard drive, etc). There are many design patterns to solve your problem, so no ideal answer can be provided.

You can't, probably use getters, because as you mentioned Handler is created at the time menu is executed. I think that handler is not recreated each time, but only on first access, so you could make an instance variable in the handler, but this doesn't seem right.

Storing to a DB and serialization is probably way too overkill at this stage for you, so I would suggest you to put object to the registry, using Registry pattern (think of it as a global cache). So, here is what you could do:

public class DocumentsRegistry {
    private Map<String, RecordingDocument> registry = new HashMap<String, RecordingDocument>();
    private static DocumentRegistry instace = new DocumentRegistry();

    public static DocumentRegistry getInstance() {
        return instance;
    }

    public void registerDocument(String key, RecordingDocument document) {
        registry.put(key, document);
    }

    public RecordingDocument getDocument(String key) {
        return registry.get(key);
    }
} 

// your handler

public static final String DOCUMENT_KEY = "DOCUMENT";

public Object execute(ExecutionEvent event) throws ExecutionException {
    IHandlerService service;
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    try {
        RecordingDocument d = new RecordingDocument("TestProject", "Tester", true);
        d.record();
        MessageDialog.openInformation(
                window.getShell(),
                "JavaTV",
                "You are now recording.");
        DocumentsRegistry.getInstance().registerDocument(DOCUMENT_KEY, d);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    return null;
}

// another handler
 public Object execute(ExecutionEvent event) throws ExecutionException {
    RecordingDocument d = DocumentsRegistry.getInstance().getDocument(DOCUMENT_KEY);
    // do something with it
    return null;
}

If you want to support concurrent recordings, so that many documents can be opened at the same time, you would need a solution for generating keys per document.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top