View Javadoc
1   /*
2    * License : The MIT License
3    * Copyright(c) 2019 Olyutorskii
4    */
5   
6   package io.github.olyutorskii.quetexj;
7   
8   import java.awt.event.ActionEvent;
9   import java.util.Objects;
10  import javax.swing.AbstractAction;
11  import javax.swing.text.BadLocationException;
12  import javax.swing.text.Document;
13  
14  /**
15   * Clear document action.
16   */
17  @SuppressWarnings("serial")
18  class ClearDocumentAction extends AbstractAction {
19  
20      private final Document document;
21  
22  
23      /**
24       * Constructor.
25       *
26       * @param document target document
27       */
28      ClearDocumentAction(Document document) {
29          super();
30  
31          Objects.requireNonNull(document);
32          this.document = document;
33  
34          return;
35      }
36  
37      /**
38       * Receive clear document event.
39       */
40      private void eventClearDocument() {
41          int len = this.document.getLength();
42          try {
43              this.document.remove(0, len);
44          } catch (BadLocationException e) {
45              assert false;
46          }
47  
48          return;
49      }
50  
51      /**
52       * {@inheritDoc}
53       *
54       * @param ev {@inheritDoc}
55       */
56      @Override
57      public void actionPerformed(ActionEvent ev) {
58          // assert EventQueue.isDispatchThread();
59          eventClearDocument();
60          return;
61      }
62  
63  }