Versionen im Vergleich

Schlüssel

  • Diese Zeile wurde hinzugefügt.
  • Diese Zeile wurde entfernt.
  • Formatierung wurde geändert.

...

Codeblock
/**
 * This command requires jadice 5.1.0.4 or newer.
 **/
public class SaveAnnotationCommand extends AbstractDocumentCommand {

  /**
   * Observes the edit mode of annotations.
   * <p>
   * For consistency reasons and in order to finish the editing process before the annotation saving
   * process can be started, this watcher class observes the edit mode of annotations. If an
   * annotation is currently edited, the watcher disables the start of an annotation saving process.
   * When the editing of the annotation is finished, the watcher enables this commend again.
   */
  private class AnnoEditStateWatcher extends DocumentAdapter {
    @Override
    public void pageSegmentModified(PageSegmentEvent evt) {
      if (evt.getLayer() == DocumentLayer.ANNOTATIONS && evt instanceof AnnotationPageSegmentEvent) {
        final AnnotationPageSegmentEvent apse = (AnnotationPageSegmentEvent) evt;
        switch (apse.getEventType()){
          case ACTION_EDIT_ON :
            annnotationsSavable = false;
            propagateContextChange();
            break;

          case ACTION_EDIT_OFF :
            annnotationsSavable = true;
            propagateContextChange();
            break;

          default :
            break;
        }
      }
    }

  }

  private AnnoEditStateWatcher watcher = new AnnoEditStateWatcher();
  private boolean annnotationsSavable = true;


  @Override
  protected void execute() {
    // start anno saving task here...
    System.err.println("Do save annotations...");
    boolean saveAnnotationSuccessful = ...;
    if(saveAnnotationSuccessful) {
       // update command state     
       propagateContextChange();
    }
  }

  @Override
  protected boolean canExecute() {
    PageView pv = getPageView();
    if (pv == null) {
      // if there is no page view, we clean up any listener registration of possibly referenced
      // document and remove the document reference as well
      unregisterListener(document);
      document = null;
      annnotationsSavable = false;
      return false;
    }

    Document doc = pv.getDocument();
    if (doc != document) {
      // if a document change happens, we have to unregister the listener of the old referenced
      // document, ...
      unregisterListener(document);
      // ...register it to the new document reference and ...
      registerListener(doc);
      document = doc;
      // ...update the "annotationsSavable" state
      annnotationsSavable = (document == null ? false : Annotations.isModified(document));
    }

    // finally return the enabled state of this command
    return document != null && annnotationsSavable && Annotations.isModified(document);
  }

  private void unregisterListener(Document doc) {
    if (doc != null)
      doc.removeDocumentListener(watcher);
  }

  private void registerListener(Document doc) {
    if (doc != null)
      doc.addDocumentListener(watcher);
  }

}