View Javadoc

1   package org.catacomb.druid.swing;
2   
3   import org.catacomb.druid.event.LabelActor;
4   import org.catacomb.druid.event.TextActor;
5   import org.catacomb.report.E;
6   import org.catacomb.util.StringUtil;
7   
8   import java.awt.BorderLayout;
9   import java.awt.Color;
10  import java.awt.Font;
11  
12  import javax.swing.BorderFactory;
13  import javax.swing.JTextArea;
14  import javax.swing.event.DocumentEvent;
15  import javax.swing.event.DocumentListener;
16  
17  import java.awt.event.FocusListener;
18  
19  import java.awt.event.FocusEvent;
20  
21  
22  public class DTextArea extends DPanel implements DocumentListener, FocusListener {
23  
24      static final long serialVersionUID = 1001;
25  
26      public final static int BARE = 1;
27      public final static int SCROLLABLE = 2;
28  
29      JTextArea jta;
30      Font keepFont;
31  
32      TextActor textActor;
33  
34      DScrollPane jsp;
35  
36      LabelActor lableActor;
37  
38      boolean ignoreChanges = false;
39  
40      boolean changedWhileFocused;
41  
42  
43      public DTextArea(String content) {
44          this(20, 5, BARE);
45          setText(content);
46      }
47  
48  
49      public DTextArea(int ia, int ib) {
50          this(ia, ib, BARE);
51      }
52  
53  
54      public DTextArea(int ia, int ib, String content) {
55          this(ia, ib, BARE);
56          setText(content);
57      }
58  
59  
60      public DTextArea(int width, int height, int type) {
61  
62          int nrow = height;
63          int ncol = width;
64  
65          if (nrow < 2) {
66              nrow = 2;
67          }
68  
69          if (ncol < 10) {
70              ncol = 10;
71          }
72  
73  
74          setLayout(new BorderLayout());
75          jta = new JTextArea(nrow, ncol);
76          jta.setLineWrap(true);
77  
78  
79          if (keepFont != null) {
80              jta.setFont(keepFont);
81          }
82  
83          if (type == SCROLLABLE) {
84              jsp = new DScrollPane(jta);
85              jsp.setNoBorder();
86  
87              add("Center", jsp);
88  
89          } else {
90              add("Center", jta);
91          }
92          jta.getDocument().addDocumentListener(this);
93          jta.addFocusListener(this);
94      }
95  
96  
97      public void setLineBorder(int icol) {
98          setBorder(BorderFactory.createLineBorder(new Color(icol)));
99      }
100 
101 
102     public void addLine() {
103         int nr = jta.getRows();
104         jta.setRows(nr + 1);
105     }
106 
107 
108     public void removeLine() {
109         int nr = jta.getRows();
110         if (nr > 1) {
111             jta.setRows(nr - 1);
112         }
113     }
114 
115 
116 
117     public void setBg(Color c) {
118         jta.setBackground(c);
119         jsp.setBackground(c);
120         setBackground(c);
121     }
122 
123 
124     public void setLabelActor(LabelActor lact) {
125         System.out.println("NB label actor in text are is unused");
126         (new Exception()).printStackTrace();
127     }
128 
129 
130 
131     public void setEnabled(boolean b) {
132         jta.setEnabled(b);
133         jta.setEditable(b);
134     }
135 
136 
137     public void setFont(Font f) {
138         if (jta != null) {
139             jta.setFont(f);
140 
141         } else {
142             keepFont = f;
143         }
144     }
145 
146     /*
147        public void setScrollSize(int w, int h) {
148           if (jsp != null)
149              jsp.setScrollSize(w, h);
150        }
151     */
152 
153     public void setBackgroundColor(Color c) {
154         jta.setBackground(c);
155         setBackground(c);
156     }
157 
158 
159     public void setTextActor(TextActor ed) {
160         textActor = ed;
161     }
162 
163 
164     public void setColumns(int nc) {
165         jta.setColumns(nc);
166     }
167 
168 
169     public void setRows(int nc) {
170         jta.setRows(nc);
171     }
172 
173 
174     /*
175      * public Dimension getPreferredSize() { S.p("cta - jta pref size " +
176      * jta.getPreferredSize()); return new Dimension(500, 180); }
177      */
178 
179     public void changedUpdate(DocumentEvent d) {
180         flagChange();
181     }
182 
183 
184     public void insertUpdate(DocumentEvent d) {
185         flagChange();
186     }
187 
188 
189     public void removeUpdate(DocumentEvent d) {
190         flagChange();
191     }
192 
193 
194 
195     private void flagChange() {
196         if (ignoreChanges) {
197 
198         } else {
199             changedWhileFocused = true;
200             if (textActor != null) {
201                 textActor.textChanged(null);
202             }
203         }
204     }
205 
206 
207     public void focusGained(FocusEvent fev) {
208         changedWhileFocused = false;
209     }
210 
211 
212     public void focusLost(FocusEvent fev) {
213         if (changedWhileFocused) {
214             if (textActor != null) {
215                 textActor.textEdited(getText());
216             }
217         }
218 
219     }
220 
221 
222     public String getText() {
223         return jta.getText();
224     }
225 
226 
227     public void setText(String s) {
228         ignoreChanges = true;
229         jta.setText(s);
230         ignoreChanges = false;
231     }
232 
233 
234     public void setEditable(boolean b) {
235         jta.setEditable(b);
236     }
237 
238 
239     public void highlightLine(int highlight) {
240         try {
241             int c0 = jta.getLineStartOffset(highlight);
242             int c1 = jta.getLineEndOffset(highlight);
243             jta.setSelectionColor(new java.awt.Color(255, 102, 51));
244             jta.setSelectionStart(c0);
245             jta.setSelectionEnd(c1);
246             jta.requestFocusInWindow();
247         } catch (Exception ex) {
248             E.warning("Exception highlighting text? " + ex + " want line " + highlight +
249                       " from text " + jta.getText());
250         }
251 
252     }
253 
254     public void clearHighlight() {
255 
256     }
257 
258 
259     public void setAntialiased() {
260         E.missing("setting aa in DTextArea");
261         // jta.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, Boolean.TRUE);
262     }
263 
264 
265 
266 
267 
268 
269     public void setPadding(int padding) {
270         // TODO Auto-generated method stub
271 
272     }
273 
274 
275     public void setFontSize(int fs) {
276         setFont(new Font(null, Font.PLAIN, fs));
277     }
278 
279 
280     public void resizeUpToText() {
281         String stxt = getText().trim();
282         int nlines = StringUtil.countLines(stxt);
283         if (nlines >= jta.getRows()) {
284             jta.setRows(nlines+1);
285             revalidate();
286         }
287     }
288 
289 
290 
291 
292 }