Browse Source

Some more refactoring

tags/0.6.3m1rc1
Gregory Holmes 15 years ago
parent
commit
d6ff61e4be

+ 6
- 30
src/com/dmdirc/ui/swing/textpane/IRCDocument.java View File

@@ -211,14 +211,14 @@ public final class IRCDocument implements Serializable {
211 211
     }
212 212
 
213 213
     /**
214
-     * Returns an attributed string for a particular line, utilising the
215
-     * document cache where possible.
214
+     * Returns an attributed character iterator for a particular line,
215
+     * utilising the document cache where possible.
216 216
      *
217 217
      * @param line Line to be styled
218 218
      *
219 219
      * @return Styled line
220 220
      */
221
-    public AttributedString getStyledString(final Line line) {
221
+    public AttributedCharacterIterator getStyledLine(final Line line) {
222 222
         AttributedString styledLine = null;
223 223
         if (cachedLines.contains(line)) {
224 224
             final int index = cachedLines.getList().indexOf(line);
@@ -231,7 +231,7 @@ public final class IRCDocument implements Serializable {
231 231
             cachedStrings.add(styledLine);
232 232
         }
233 233
 
234
-        return styledLine;
234
+        return styledLine.getIterator();
235 235
     }
236 236
 
237 237
     /**
@@ -242,32 +242,8 @@ public final class IRCDocument implements Serializable {
242 242
      *
243 243
      * @return Styled line
244 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();
245
+    public AttributedCharacterIterator getStyledLine(final int line) {
246
+        return getStyledLine(getLine(line));
271 247
     }
272 248
 }
273 249
 

+ 1
- 9
src/com/dmdirc/ui/swing/textpane/TextPane.java View File

@@ -24,7 +24,6 @@ package com.dmdirc.ui.swing.textpane;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26 26
 
27
-import java.awt.Dimension;
28 27
 import java.awt.Point;
29 28
 import java.awt.Toolkit;
30 29
 import java.awt.datatransfer.StringSelection;
@@ -35,7 +34,6 @@ import java.awt.event.MouseMotionAdapter;
35 34
 import java.awt.event.MouseMotionListener;
36 35
 import java.awt.event.MouseWheelEvent;
37 36
 import java.awt.event.MouseWheelListener;
38
-import java.util.List;
39 37
 
40 38
 import javax.swing.JComponent;
41 39
 import javax.swing.JScrollBar;
@@ -70,23 +68,16 @@ public final class TextPane extends JComponent implements AdjustmentListener,
70 68
      */
71 69
     public TextPane(final FrameContainer frame) {
72 70
         super();
73
-
74 71
         setUI(new TextPaneUI());
75 72
 
76 73
         this.frame = frame;
77
-
78 74
         document = new IRCDocument();
79 75
 
80 76
         setLayout(new MigLayout("fill"));
81
-
82 77
         canvas = new TextPaneCanvas(this, document);
83
-
84 78
         add(canvas, "dock center");
85
-
86
-
87 79
         scrollBar = new JScrollBar(JScrollBar.VERTICAL);
88 80
         add(scrollBar, "dock east");
89
-
90 81
         scrollBar.setMaximum(document.getNumLines());
91 82
         scrollBar.setBlockIncrement(10);
92 83
         scrollBar.setUnitIncrement(1);
@@ -94,6 +85,7 @@ public final class TextPane extends JComponent implements AdjustmentListener,
94 85
 
95 86
         addMouseWheelListener(this);
96 87
         document.addIRCDocumentListener(this);
88
+        setAutoscrolls(true);
97 89
 
98 90
         MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() {
99 91
 

+ 55
- 82
src/com/dmdirc/ui/swing/textpane/TextPaneCanvas.java View File

@@ -97,6 +97,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
97 97
         textLayouts = new HashMap<TextLayout, LineInfo>();
98 98
         positions = new HashMap<Rectangle, TextLayout>();
99 99
         lineWrap = new HashMap<Integer, Integer>();
100
+        selection = new LinePosition(-1, -1, -1, -1);
100 101
         addMouseListener(this);
101 102
         addMouseMotionListener(this);
102 103
         addComponentListener(this);
@@ -122,10 +123,6 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
122 123
         float drawPosY = formatHeight;
123 124
         int startLine = scrollBarPosition;
124 125
 
125
-        int useStartLine;
126
-        int useStartChar;
127
-        int useEndLine;
128
-        int useEndChar;
129 126
         int paragraphStart;
130 127
         int paragraphEnd;
131 128
         LineBreakMeasurer lineMeasurer;
@@ -136,14 +133,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
136 133
         textLayouts.clear();
137 134
         positions.clear();
138 135
 
139
-        //check theres something to draw
140
-        if (document.getNumLines() == 0) {
141
-            setCursor(Cursor.getDefaultCursor());
142
-            return;
143
-        }
144
-
145
-        //check there is some space to draw in
146
-        if (formatWidth < 1) {
136
+        //check theres something to draw and theres some space to draw in
137
+        if (document.getNumLines() == 0 || formatWidth < 1) {
147 138
             setCursor(Cursor.getDefaultCursor());
148 139
             return;
149 140
         }
@@ -161,34 +152,10 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
161 152
         lastVisibleLine = startLine;
162 153
         firstVisibleLine = startLine;
163 154
 
164
-        if (selection.getStartLine() > selection.getEndLine()) {
165
-            // Swap both
166
-            useStartLine = selection.getEndLine();
167
-            useStartChar = selection.getEndPos();
168
-            useEndLine = selection.getStartLine();
169
-            useEndChar = selection.getStartPos();
170
-        }
171
-        else if (selection.getStartLine() == selection.getEndLine() &&
172
-                 selection.getStartPos() > selection.getEndPos()) {
173
-            // Just swap the chars
174
-            useStartLine = selection.getStartLine();
175
-            useStartChar = selection.getEndPos();
176
-            useEndLine = selection.getEndLine();
177
-            useEndChar = selection.getStartPos();
178
-        }
179
-        else {
180
-            // Swap nothing
181
-            useStartLine = selection.getStartLine();
182
-            useStartChar = selection.getStartPos();
183
-            useEndLine = selection.getEndLine();
184
-            useEndChar = selection.getEndPos();
185
-        }
186
-
187 155
         // Iterate through the lines
188 156
         for (int i = startLine; i >= 0; i--) {
189 157
             float drawPosX;
190
-            final AttributedCharacterIterator iterator = document.
191
-                    getStyledIterator(i);
158
+            final AttributedCharacterIterator iterator = document.getStyledLine(i);
192 159
             paragraphStart = iterator.getBeginIndex();
193 160
             paragraphEnd = iterator.getEndIndex();
194 161
             lineMeasurer = new LineBreakMeasurer(iterator,
@@ -201,8 +168,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
201 168
             if (lineWrap.containsKey(i)) {
202 169
                 //use it
203 170
                 wrappedLine = lineWrap.get(i);
204
-            }
205
-            else {
171
+            } else {
206 172
                 //get it and populate the cache
207 173
                 wrappedLine = getNumWrappedLines(lineMeasurer,
208 174
                                                  paragraphStart, paragraphEnd,
@@ -223,16 +189,14 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
223 189
                 // Calculate the Y offset
224 190
                 if (wrappedLine == 1) {
225 191
                     drawPosY -= lineHeight;
226
-                }
227
-                else if (j != 0) {
192
+                } else if (j != 0) {
228 193
                     drawPosY += lineHeight;
229 194
                 }
230 195
 
231 196
                 // Calculate the initial X position
232 197
                 if (layout.isLeftToRight()) {
233 198
                     drawPosX = 3;
234
-                }
235
-                else {
199
+                } else {
236 200
                     drawPosX = formatWidth - layout.getAdvance();
237 201
                 }
238 202
 
@@ -242,8 +206,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
242 206
                     g.setColor(textPane.getForeground());
243 207
 
244 208
                     layout.draw(g, drawPosX, drawPosY + lineHeight / 2f);
245
-                    doHighlight(i, useStartLine, useEndLine, useStartChar,
246
-                                useEndChar, chars, layout, g, drawPosY, drawPosX);
209
+                    doHighlight(i, chars, layout, g, drawPosY, drawPosX);
247 210
                     firstVisibleLine = i;
248 211
                     textLayouts.put(layout, new LineInfo(i, j));
249 212
                     positions.put(new Rectangle(0, (int) drawPosY,
@@ -295,22 +258,41 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
295 258
      * Redraws the text that has been highlighted.
296 259
      *
297 260
      * @param line Line number
298
-     * @param startLine Selection start line
299
-     * @param endLine Selection end line
300
-     * @param startChar Selection start char
301
-     * @param endChar Selection end char
302 261
      * @param chars Number of characters so far in the line
303 262
      * @param layout Current line textlayout
304 263
      * @param g Graphics surface to draw highlight on
305 264
      * @param drawPosY current y location of the line
306 265
      * @param drawPosX current x location of the line
307 266
      */
308
-    private void doHighlight(final int line, final int startLine,
309
-                             final int endLine, final int startChar,
310
-                             final int endChar,
311
-                             final int chars, final TextLayout layout,
312
-                             final Graphics2D g,
267
+    private void doHighlight(final int line, final int chars,
268
+                             final TextLayout layout, final Graphics2D g,
313 269
                              final float drawPosY, final float drawPosX) {
270
+        int startLine;
271
+        int startChar;
272
+        int endLine;
273
+        int endChar;
274
+
275
+        if (selection.getStartLine() > selection.getEndLine()) {
276
+            // Swap both
277
+            startLine = selection.getEndLine();
278
+            startChar = selection.getEndPos();
279
+            endLine = selection.getStartLine();
280
+            endChar = selection.getStartPos();
281
+        } else if (selection.getStartLine() == selection.getEndLine() &&
282
+                   selection.getStartPos() > selection.getEndPos()) {
283
+            // Just swap the chars
284
+            startLine = selection.getStartLine();
285
+            startChar = selection.getEndPos();
286
+            endLine = selection.getEndLine();
287
+            endChar = selection.getStartPos();
288
+        } else {
289
+            // Swap nothing
290
+            startLine = selection.getStartLine();
291
+            startChar = selection.getStartPos();
292
+            endLine = selection.getEndLine();
293
+            endChar = selection.getEndPos();
294
+        }
295
+
314 296
         //Does this line need highlighting?
315 297
         if (startLine <= line && endLine >= line) {
316 298
             int firstChar;
@@ -319,16 +301,14 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
319 301
             // Determine the first char we care about
320 302
             if (startLine < line || startChar < chars) {
321 303
                 firstChar = chars;
322
-            }
323
-            else {
304
+            } else {
324 305
                 firstChar = startChar;
325 306
             }
326 307
 
327 308
             // ... And the last
328 309
             if (endLine > line || endChar > chars + layout.getCharacterCount()) {
329 310
                 lastChar = chars + layout.getCharacterCount();
330
-            }
331
-            else {
311
+            } else {
332 312
                 lastChar = endChar;
333 313
             }
334 314
 
@@ -346,7 +326,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
346 326
 
347 327
                 final int trans = (int) (lineHeight / 2f + drawPosY);
348 328
                 final AttributedCharacterIterator iterator = document.
349
-                        getStyledIterator(line);
329
+                        getStyledLine(line);
350 330
                 final AttributedString as = new AttributedString(iterator,
351 331
                                                                  firstChar,
352 332
                                                                  lastChar);
@@ -416,8 +396,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
416 396
             if (lineInfo.getIndex() == -1) {
417 397
                 start = -1;
418 398
                 end = -1;
419
-            }
420
-            else {
399
+            } else {
421 400
                 final int[] extent =
422 401
                             getSurroundingWordIndexes(clickedText,
423 402
                                                       lineInfo.getIndex());
@@ -430,8 +409,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
430 409
                 selection.setEndLine(lineInfo.getLine());
431 410
                 selection.setStartPos(start);
432 411
                 selection.setEndPos(end);
433
-            }
434
-            else if (e.getClickCount() == 3) {
412
+            } else if (e.getClickCount() == 3) {
435 413
                 selection.setStartLine(lineInfo.getLine());
436 414
                 selection.setEndLine(lineInfo.getLine());
437 415
                 selection.setStartPos(0);
@@ -452,8 +430,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
452 430
      */
453 431
     public ClickType getClickType(final LineInfo lineInfo) {
454 432
         if (lineInfo.getLine() != -1) {
455
-            final AttributedCharacterIterator iterator = document.
456
-                    getStyledIterator(lineInfo.getLine());
433
+            final AttributedCharacterIterator iterator = document.getStyledLine(
434
+                    lineInfo.getLine());
457 435
             iterator.setIndex(lineInfo.getIndex());
458 436
             Object linkattr =
459 437
                    iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
@@ -481,8 +459,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
481 459
      */
482 460
     public Object getAttributeValueAtPoint(LineInfo lineInfo) {
483 461
         if (lineInfo.getLine() != -1) {
484
-            final AttributedCharacterIterator iterator = document.
485
-                    getStyledIterator(lineInfo.getLine());
462
+            final AttributedCharacterIterator iterator = document.getStyledLine(
463
+                    lineInfo.getLine());
486 464
             iterator.setIndex(lineInfo.getIndex());
487 465
             Object linkattr =
488 466
                    iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
@@ -645,8 +623,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
645 623
 
646 624
         if (lineInfo.getLine() != -1 && document.getLine(lineInfo.getLine()) !=
647 625
                                         null) {
648
-            final AttributedCharacterIterator iterator = document.
649
-                    getStyledIterator(lineInfo.getLine());
626
+            final AttributedCharacterIterator iterator = document.getStyledLine(
627
+                    lineInfo.getLine());
650 628
             if (lineInfo.getIndex() < iterator.getBeginIndex() ||
651 629
                 lineInfo.getIndex() > iterator.getEndIndex()) {
652 630
                 return;
@@ -690,16 +668,14 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
690 668
                 final Point mousePos = e.getPoint();
691 669
                 if (mousePos.getX() < bounds.getX()) {
692 670
                     point.setLocation(bounds.getX() + 3, point.getY());
693
-                }
694
-                else if (mousePos.getX() > (bounds.getX() + bounds.getWidth())) {
671
+                } else if (mousePos.getX() > (bounds.getX() + bounds.getWidth())) {
695 672
                     point.setLocation(bounds.getX() + bounds.getWidth() - 3,
696 673
                                       point.getY());
697 674
                 }
698 675
                 if (mousePos.getY() < bounds.getY()) {
699 676
                     point.setLocation(point.getX(), bounds.getY() + 6);
700
-                }
701
-                else if (mousePos.getY() >
702
-                         (bounds.getY() + bounds.getHeight())) {
677
+                } else if (mousePos.getY() >
678
+                           (bounds.getY() + bounds.getHeight())) {
703 679
                     //Nice text selection behaviour
704 680
                     //point.setLocation(point.getX(), bounds.getY() +
705 681
                     //        bounds.getHeight() - 6);
@@ -776,9 +752,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
776 752
             if (textLayouts.get(entry.getValue()).getLine() == lineNumber) {
777 753
                 if (textLayouts.get(entry.getValue()).getPart() < linePart) {
778 754
                     pos += entry.getValue().getCharacterCount();
779
-                }
780
-                else if (textLayouts.get(entry.getValue()).getPart() ==
781
-                         linePart) {
755
+                } else if (textLayouts.get(entry.getValue()).getPart() ==
756
+                           linePart) {
782 757
                     final TextHitInfo hit = entry.getValue().hitTestChar(x - 6,
783 758
                                                                          y);
784 759
                     pos += hit.getInsertionIndex();
@@ -801,15 +776,13 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
801 776
                                     selection.getEndPos(), selection.
802 777
                     getStartLine(),
803 778
                                     selection.getStartPos());
804
-        }
805
-        else if (selection.getStartLine() == selection.getEndLine() &&
806
-                 selection.getStartPos() > selection.getEndPos()) {
779
+        } else if (selection.getStartLine() == selection.getEndLine() &&
780
+                   selection.getStartPos() > selection.getEndPos()) {
807 781
             // Just swap the chars
808 782
             return new LinePosition(selection.getStartLine(), selection.
809 783
                     getEndPos(), selection.getEndLine(),
810 784
                                     selection.getStartPos());
811
-        }
812
-        else {
785
+        } else {
813 786
             // Swap nothing
814 787
             return new LinePosition(selection.getStartLine(), selection.
815 788
                     getStartPos(), selection.getEndLine(),

+ 26
- 0
src/com/dmdirc/ui/swing/textpane/TextPaneUI.java View File

@@ -22,6 +22,9 @@
22 22
 
23 23
 package com.dmdirc.ui.swing.textpane;
24 24
 
25
+import java.awt.Graphics;
26
+import java.awt.Graphics2D;
27
+
25 28
 import javax.swing.JComponent;
26 29
 import javax.swing.UIManager;
27 30
 import javax.swing.plaf.ComponentUI;
@@ -36,4 +39,27 @@ public class TextPaneUI extends ComponentUI {
36 39
     public void installUI(final JComponent c) {
37 40
         c.setBorder(UIManager.getBorder("TextField.border"));
38 41
     }
42
+
43
+    /** {@inheritDoc} */
44
+    /*@Override
45
+    public void paint(final Graphics g, final JComponent c) {
46
+        if (c.isOpaque()) {
47
+            g.setColor(c.getBackground());
48
+            g.fillRect(0, 0, c.getWidth(), c.getHeight());
49
+        }
50
+        paint(g, c);
51
+    }*/
52
+
53
+    /** {@inheritDoc} */
54
+    /*@Override
55
+    public void update(final Graphics g, final JComponent c) {
56
+        if (c.isOpaque()) {
57
+            g.setColor(c.getBackground());
58
+            g.fillRect(0, 0, c.getWidth(), c.getHeight());
59
+        }
60
+        paint(g, c);
61
+    }*/
62
+
63
+    protected void paintTextPane(final Graphics2D g, final TextPane c) {
64
+    }
39 65
 }

Loading…
Cancel
Save