View Javadoc
1   /*
2    * License : The MIT License
3    * Copyright(c) 2019 Olyutorskii
4    */
5   
6   package io.github.olyutorskii.quetexj;
7   
8   import javax.swing.Action;
9   import javax.swing.BoundedRangeModel;
10  import javax.swing.DefaultBoundedRangeModel;
11  import javax.swing.JTextArea;
12  import javax.swing.JToggleButton.ToggleButtonModel;
13  import javax.swing.text.DefaultCaret;
14  import javax.swing.text.Document;
15  import javax.swing.text.PlainDocument;
16  
17  /**
18   * Facade of MVC complexes.
19   */
20  public class MvcFacade {
21  
22      private final Document document;
23      private final BoundedRangeModel vertRangeModel;
24      private final ToggleButtonModel trackSwitchButtonModel;
25  
26      private final JTextArea textArea;
27  
28      private final HeightKeeper heightKeeper;
29      private final MaxTracker maxTracker;
30  
31      private final Action clearAction;
32  
33  
34      /**
35       * Constructor.
36       *
37       * <p>PlainDocument, DefaultBoundedRangeModel, and ToggleButtonModel
38       * instances are used as default model.
39       */
40      public MvcFacade() {
41          this(
42                  new PlainDocument(),
43                  new DefaultBoundedRangeModel(),
44                  new ToggleButtonModel());
45          return;
46      }
47  
48      /**
49       * Constructor.
50       *
51       * @param document text document model
52       * @param vertRangeModel vertical scrollbar model
53       * @param trackSwitchButtonModel tracking on-off switch button model
54       */
55      public MvcFacade(
56              Document document,
57              BoundedRangeModel vertRangeModel,
58              ToggleButtonModel trackSwitchButtonModel) {
59          super();
60  
61          this.document = document;
62          this.vertRangeModel = vertRangeModel;
63          this.trackSwitchButtonModel = trackSwitchButtonModel;
64  
65          this.textArea = buildTextArea(this.document);
66  
67          this.heightKeeper =
68                  new HeightKeeper(this.textArea, this.vertRangeModel);
69          this.maxTracker =
70                  new MaxTracker(
71                          this.vertRangeModel, this.trackSwitchButtonModel);
72  
73          this.clearAction = new ClearDocumentAction(this.document);
74  
75          return;
76      }
77  
78  
79      /**
80       * Build text area.
81       *
82       * @param doc document model
83       * @return text area
84       */
85      private static JTextArea buildTextArea(Document doc) {
86          JTextArea textComp = new JTextArea();
87  
88          textComp.setEditable(false);
89          textComp.setLineWrap(true);
90  
91          DefaultCaret caret = new DefaultCaret();
92          caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
93          textComp.setCaret(caret);
94  
95          textComp.setDocument(doc);
96  
97          return textComp;
98      }
99  
100 
101     /**
102      * Return document model.
103      *
104      * @return document model
105      */
106     public Document getDocument() {
107         return this.document;
108     }
109 
110     /**
111      * Return text area view.
112      *
113      * @return text area view
114      */
115     public JTextArea getTextArea() {
116         return this.textArea;
117     }
118 
119     /**
120      * Return vertical BoundedRangeModel.
121      *
122      * @return vertical BoundedRangeModel
123      */
124     public BoundedRangeModel getVerticalBoundedRangeModel() {
125         return this.vertRangeModel;
126     }
127 
128     /**
129      * Return HeightKeeper instance.
130      *
131      * @return HeightKeeper instance
132      */
133     public HeightKeeper getHeightKeeper() {
134         return this.heightKeeper;
135     }
136 
137     /**
138      * Return MaxTracker instance.
139      *
140      * @return MaxTracker instance
141      */
142     public MaxTracker getMaxTracker() {
143         return this.maxTracker;
144     }
145 
146     /**
147      * Return tracking on-off switch ButtonModel.
148      *
149      * @return tracking on-off switch ButtonModel
150      */
151     public ToggleButtonModel getTrackSwitchButtonModel() {
152         return this.trackSwitchButtonModel;
153     }
154 
155     /**
156      * Return clear document Action.
157      *
158      * @return clear document Action
159      */
160     public Action getClearAction() {
161         return this.clearAction;
162     }
163 
164 }