Browse Source

More refactoring

tags/0.6.3m1rc1
Gregory Holmes 15 years ago
parent
commit
285b5a653c

+ 1
- 1
src/com/dmdirc/ui/swing/components/TextFrame.java View File

@@ -253,7 +253,7 @@ public abstract class TextFrame extends JInternalFrame implements Window,
253 253
                             null, getContainer(), myLine);
254 254
                 }
255 255
                 
256
-                textPane.addText(lines);
256
+                textPane.getDocument().addText(lines);
257 257
 
258 258
                 if (frameBufferSize > 0) {
259 259
                     textPane.trim(frameBufferSize);

+ 92
- 19
src/com/dmdirc/ui/swing/textpane/IRCDocument.java View File

@@ -22,7 +22,11 @@
22 22
 
23 23
 package com.dmdirc.ui.swing.textpane;
24 24
 
25
+import com.dmdirc.util.RollingList;
26
+
25 27
 import java.io.Serializable;
28
+import java.text.AttributedCharacterIterator;
29
+import java.text.AttributedString;
26 30
 import java.util.ArrayList;
27 31
 import java.util.List;
28 32
 
@@ -32,26 +36,31 @@ import javax.swing.event.EventListenerList;
32 36
  * Data contained in a TextPane.
33 37
  */
34 38
 public final class IRCDocument implements Serializable {
35
-    
39
+
36 40
     /**
37 41
      * A version number for this class. It should be changed whenever the class
38 42
      * structure is changed (or anything else that would prevent serialized
39 43
      * objects being unserialized with the new class).
40 44
      */
41 45
     private static final long serialVersionUID = 4;
42
-    
43 46
     /** List of lines of text. */
44 47
     private final List<Line> lines;
45
-    
46 48
     /** Listener list. */
47 49
     private final EventListenerList listeners;
48
-    
50
+    /** Cached lines. */
51
+    private RollingList<Line> cachedLines;
52
+    /** Cached attributed strings. */
53
+    private RollingList<AttributedString> cachedStrings;
54
+
49 55
     /** Creates a new instance of IRCDocument. */
50 56
     public IRCDocument() {
51 57
         lines = new ArrayList<Line>();
52 58
         listeners = new EventListenerList();
59
+
60
+        cachedLines = new RollingList<Line>(50);
61
+        cachedStrings = new RollingList<AttributedString>(50);
53 62
     }
54
-    
63
+
55 64
     /**
56 65
      * Returns the number of lines in this document.
57 66
      *
@@ -60,7 +69,7 @@ public final class IRCDocument implements Serializable {
60 69
     public int getNumLines() {
61 70
         return lines.size();
62 71
     }
63
-    
72
+
64 73
     /**
65 74
      * Returns the Line at the specified number.
66 75
      *
@@ -70,8 +79,8 @@ public final class IRCDocument implements Serializable {
70 79
      */
71 80
     public Line getLine(final int lineNumber) {
72 81
         return lines.get(lineNumber);
73
-    }  
74
-    
82
+    }
83
+
75 84
     /**
76 85
      * Adds the stylised string to the canvas.
77 86
      * 
@@ -83,7 +92,7 @@ public final class IRCDocument implements Serializable {
83 92
             fireLineAdded(lines.indexOf(text));
84 93
         }
85 94
     }
86
-    
95
+
87 96
     /**
88 97
      * Adds the stylised string to the canvas.
89 98
      * 
@@ -98,7 +107,7 @@ public final class IRCDocument implements Serializable {
98 107
             fireLinesAdded(start, text.size());
99 108
         }
100 109
     }
101
-    
110
+
102 111
     /**
103 112
      * Trims the document to the specified number of lines.
104 113
      *
@@ -112,7 +121,7 @@ public final class IRCDocument implements Serializable {
112 121
             fireTrimmed();
113 122
         }
114 123
     }
115
-    
124
+
116 125
     /** Clears all lines from the document. */
117 126
     protected void clear() {
118 127
         synchronized (lines) {
@@ -120,7 +129,7 @@ public final class IRCDocument implements Serializable {
120 129
             fireCleared();
121 130
         }
122 131
     }
123
-    
132
+
124 133
     /**
125 134
      * Adds a IRCDocumentListener to the listener list.
126 135
      *
@@ -134,7 +143,7 @@ public final class IRCDocument implements Serializable {
134 143
             listeners.add(IRCDocumentListener.class, listener);
135 144
         }
136 145
     }
137
-    
146
+
138 147
     /**
139 148
      * Removes a IRCDocumentListener from the listener list.
140 149
      *
@@ -143,7 +152,7 @@ public final class IRCDocument implements Serializable {
143 152
     public void removeIRCDocumentListener(final IRCDocumentListener listener) {
144 153
         listeners.remove(IRCDocumentListener.class, listener);
145 154
     }
146
-    
155
+
147 156
     /**
148 157
      * Fires the line added method on all listeners.
149 158
      *
@@ -153,11 +162,12 @@ public final class IRCDocument implements Serializable {
153 162
         final Object[] listenerList = listeners.getListenerList();
154 163
         for (int i = 0; i < listenerList.length; i += 2) {
155 164
             if (listenerList[i] == IRCDocumentListener.class) {
156
-                ((IRCDocumentListener) listenerList[i + 1]).lineAdded(index, lines.size());
165
+                ((IRCDocumentListener) listenerList[i + 1]).lineAdded(index,
166
+                                                                      lines.size());
157 167
             }
158 168
         }
159 169
     }
160
-    
170
+
161 171
     /**
162 172
      * Fires the lines added method on all listeners.
163 173
      *
@@ -168,11 +178,14 @@ public final class IRCDocument implements Serializable {
168 178
         final Object[] listenerList = listeners.getListenerList();
169 179
         for (int i = 0; i < listenerList.length; i += 2) {
170 180
             if (listenerList[i] == IRCDocumentListener.class) {
171
-                ((IRCDocumentListener) listenerList[i + 1]).linesAdded(index, size, lines.size());
181
+                ((IRCDocumentListener) listenerList[i + 1]).linesAdded(index,
182
+                                                                       size,
183
+                                                                       lines.
184
+                        size());
172 185
             }
173 186
         }
174 187
     }
175
-    
188
+
176 189
     /**
177 190
      * Fires the trimmed method on all listeners.
178 191
      */
@@ -184,7 +197,7 @@ public final class IRCDocument implements Serializable {
184 197
             }
185 198
         }
186 199
     }
187
-    
200
+
188 201
     /**
189 202
      * fires the cleared method on all listeners.
190 203
      */
@@ -196,5 +209,65 @@ public final class IRCDocument implements Serializable {
196 209
             }
197 210
         }
198 211
     }
212
+
213
+    /**
214
+     * Returns an attributed string for a particular line, utilising the
215
+     * document cache where possible.
216
+     *
217
+     * @param line Line to be styled
218
+     *
219
+     * @return Styled line
220
+     */
221
+    public AttributedString getStyledString(final Line line) {
222
+        AttributedString styledLine = null;
223
+        if (cachedLines.contains(line)) {
224
+            final int index = cachedLines.getList().indexOf(line);
225
+            styledLine = cachedStrings.get(index);
226
+        }
227
+
228
+        if (styledLine == null) {
229
+            styledLine = line.getStyled();
230
+            cachedLines.add(line);
231
+            cachedStrings.add(styledLine);
232
+        }
233
+
234
+        return styledLine;
235
+    }
236
+
237
+    /**
238
+     * Returns an attributed string for a particular line, utilising the
239
+     * document cache where possible.
240
+     *
241
+     * @param line Line number to be styled
242
+     *
243
+     * @return Styled line
244
+     */
245
+    public AttributedString getStyledString(final int line) {
246
+        return getStyledString(getLine(line));
247
+    }
248
+
249
+    /**
250
+     * Returns an attributed character iterator for a particular line,
251
+     * utilising the document cache where possible.
252
+     *
253
+     * @param line Line to be styled
254
+     *
255
+     * @return Styled line iterator
256
+     */
257
+    public AttributedCharacterIterator getStyledIterator(final Line line) {
258
+        return getStyledString(line).getIterator();
259
+    }
260
+
261
+    /**
262
+     * Returns an attributed character iterator for a particular line,
263
+     * utilising the document cache where possible.
264
+     *
265
+     * @param line Line number to be styled
266
+     *
267
+     * @return Styled line iterator
268
+     */
269
+    public AttributedCharacterIterator getStyledIterator(final int line) {
270
+        return getStyledString(line).getIterator();
271
+    }
199 272
 }
200 273
 

+ 0
- 12
src/com/dmdirc/ui/swing/textpane/TextPane.java View File

@@ -77,8 +77,6 @@ public final class TextPane extends JComponent implements AdjustmentListener,
77 77
 
78 78
         document = new IRCDocument();
79 79
 
80
-        setMinimumSize(new Dimension(0, 0));
81
-
82 80
         setLayout(new MigLayout("fill"));
83 81
 
84 82
         canvas = new TextPaneCanvas(this, document);
@@ -89,8 +87,6 @@ public final class TextPane extends JComponent implements AdjustmentListener,
89 87
         scrollBar = new JScrollBar(JScrollBar.VERTICAL);
90 88
         add(scrollBar, "dock east");
91 89
 
92
-        setAutoscrolls(true);
93
-
94 90
         scrollBar.setMaximum(document.getNumLines());
95 91
         scrollBar.setBlockIncrement(10);
96 92
         scrollBar.setUnitIncrement(1);
@@ -128,14 +124,6 @@ public final class TextPane extends JComponent implements AdjustmentListener,
128 124
         setUI(new TextPaneUI());
129 125
     }
130 126
 
131
-    /**
132
-     * Adds styled text to the textpane.
133
-     * @param text styled text to add
134
-     */
135
-    public void addText(final List<Line> text) {
136
-        document.addText(text);
137
-    }
138
-
139 127
     /**
140 128
      * Sets the new position for the scrollbar and the associated position
141 129
      * to render the text from.

+ 18
- 43
src/com/dmdirc/ui/swing/textpane/TextPaneCanvas.java View File

@@ -23,9 +23,7 @@
23 23
 package com.dmdirc.ui.swing.textpane;
24 24
 
25 25
 import com.dmdirc.ui.messages.IRCTextAttribute;
26
-import com.dmdirc.ui.swing.textpane.ClickType;
27
-
28
-import com.dmdirc.util.RollingList;
26
+    
29 27
 import java.awt.Cursor;
30 28
 import java.awt.Graphics;
31 29
 import java.awt.Graphics2D;
@@ -87,10 +85,6 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
87 85
     private int lastVisibleLine;
88 86
     /** Line wrapping cache. */
89 87
     private final Map<Integer, Integer> lineWrap;
90
-    /** Cached lines. */
91
-    private RollingList<Line> cachedLines;
92
-    /** Cached attributed strings. */
93
-    private RollingList<AttributedString> cachedStrings;
94 88
 
95 89
     /**
96 90
      * Creates a new text pane canvas.
@@ -112,9 +106,6 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
112 106
         addMouseListener(this);
113 107
         addMouseMotionListener(this);
114 108
         addComponentListener(this);
115
-
116
-        cachedLines = new RollingList<Line>(50);
117
-        cachedStrings = new RollingList<AttributedString>(50);
118 109
     }
119 110
 
120 111
     /**
@@ -201,8 +192,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
201 192
         // Iterate through the lines
202 193
         for (int i = startLine; i >= 0; i--) {
203 194
             float drawPosX;
204
-            final AttributedCharacterIterator iterator = getStyledLine(document.
205
-                    getLine(i)).getIterator();
195
+            final AttributedCharacterIterator iterator = document.
196
+                    getStyledIterator(i);
206 197
             paragraphStart = iterator.getBeginIndex();
207 198
             paragraphEnd = iterator.getEndIndex();
208 199
             lineMeasurer = new LineBreakMeasurer(iterator,
@@ -278,22 +269,6 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
278 269
         checkForLink();
279 270
     }
280 271
 
281
-    private AttributedString getStyledLine(final Line line) {
282
-        AttributedString styledLine = null;
283
-        if (cachedLines.contains(line)) {
284
-            final int index = cachedLines.getList().indexOf(line);
285
-            styledLine = cachedStrings.get(index);
286
-        }
287
-
288
-        if (styledLine == null) {
289
-            styledLine = line.getStyled();
290
-            cachedLines.add(line);
291
-            cachedStrings.add(styledLine);
292
-        }
293
-
294
-        return styledLine;
295
-    }
296
-
297 272
     /**
298 273
      * Returns the number of timesa line will wrap.
299 274
      *
@@ -375,8 +350,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
375 350
                 }
376 351
 
377 352
                 final int trans = (int) (lineHeight / 2f + drawPosY);
378
-                final AttributedCharacterIterator iterator = getStyledLine(
379
-                        document.getLine(line)).getIterator();
353
+                final AttributedCharacterIterator iterator = document.
354
+                        getStyledIterator(line);
380 355
                 final AttributedString as = new AttributedString(iterator,
381 356
                                                                  firstChar,
382 357
                                                                  lastChar);
@@ -482,8 +457,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
482 457
      */
483 458
     public ClickType getClickType(final LineInfo lineInfo) {
484 459
         if (lineInfo.getLine() != -1) {
485
-            final AttributedCharacterIterator iterator = getStyledLine(
486
-                    document.getLine(lineInfo.getLine())).getIterator();
460
+            final AttributedCharacterIterator iterator = document.
461
+                    getStyledIterator(lineInfo.getLine());
487 462
             iterator.setIndex(lineInfo.getIndex());
488 463
             Object linkattr =
489 464
                    iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
@@ -511,8 +486,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
511 486
      */
512 487
     public Object getAttributeValueAtPoint(LineInfo lineInfo) {
513 488
         if (lineInfo.getLine() != -1) {
514
-            final AttributedCharacterIterator iterator = getStyledLine(
515
-                    document.getLine(lineInfo.getLine())).getIterator();
489
+            final AttributedCharacterIterator iterator = document.
490
+                    getStyledIterator(lineInfo.getLine());
516 491
             iterator.setIndex(lineInfo.getIndex());
517 492
             Object linkattr =
518 493
                    iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
@@ -671,17 +646,17 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
671 646
 
672 647
     /** Checks for a link under the cursor and sets appropriately. */
673 648
     private void checkForLink() {
674
-        final LineInfo info = getClickPosition(getMousePosition());
675
-
676
-        if (info.getLine() != -1 && document.getLine(info.getLine()) != null) {
677
-            final AttributedCharacterIterator iterator = getStyledLine(
678
-                    document.getLine(info.getLine())).getIterator();
679
-            if (info.getIndex() < iterator.getBeginIndex() || info.getIndex() >
680
-                                                              iterator.
681
-                    getEndIndex()) {
649
+        final LineInfo lineInfo = getClickPosition(getMousePosition());
650
+
651
+        if (lineInfo.getLine() != -1 && document.getLine(lineInfo.getLine()) !=
652
+                                        null) {
653
+            final AttributedCharacterIterator iterator = document.
654
+                    getStyledIterator(lineInfo.getLine());
655
+            if (lineInfo.getIndex() < iterator.getBeginIndex() ||
656
+                    lineInfo.getIndex() > iterator.getEndIndex()) {
682 657
                 return;
683 658
             }
684
-            iterator.setIndex(info.getIndex());
659
+            iterator.setIndex(lineInfo.getIndex());
685 660
             Object linkattr =
686 661
                    iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
687 662
             if (linkattr instanceof String) {

Loading…
Cancel
Save