Przeglądaj źródła

Fixes issue 3449: Changing textpane font size sometimes doesn't update a few lines

Change-Id: Ifc1d9475c80116d498e6f5df02fa7cd6a302f7d0
Reviewed-on: http://gerrit.dmdirc.com/369
Tested-by: Gregory Holmes <greboid@dmdirc.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Gregory Holmes 14 lat temu
rodzic
commit
e9b66c9cf7

+ 64
- 24
src/com/dmdirc/addons/ui_swing/textpane/IRCDocument.java Wyświetl plik

@@ -19,17 +19,20 @@
19 19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 20
  * SOFTWARE.
21 21
  */
22
+
22 23
 package com.dmdirc.addons.ui_swing.textpane;
23 24
 
24 25
 import com.dmdirc.config.ConfigManager;
25 26
 import com.dmdirc.interfaces.ConfigChangeListener;
26 27
 import com.dmdirc.util.RollingList;
28
+import java.awt.Font;
27 29
 
28 30
 import java.io.Serializable;
29 31
 import java.text.AttributedCharacterIterator;
30 32
 import java.text.AttributedString;
31 33
 import java.util.ArrayList;
32 34
 import java.util.List;
35
+import javax.swing.UIManager;
33 36
 
34 37
 import javax.swing.event.EventListenerList;
35 38
 
@@ -54,6 +57,10 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
54 57
     private RollingList<AttributedString> cachedStrings;
55 58
     /** Configuration manager. */
56 59
     private ConfigManager config;
60
+    /** Font size. */
61
+    private int fontSize;
62
+    /** Font name. */
63
+    private String fontName;
57 64
 
58 65
     /** 
59 66
      * Creates a new instance of IRCDocument.
@@ -67,6 +74,11 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
67 74
 
68 75
         cachedLines = new RollingList<Line>(50);
69 76
         cachedStrings = new RollingList<AttributedString>(50);
77
+
78
+        setCachedSettings();
79
+
80
+        config.addChangeListener("ui", "textPaneFontSize", this);
81
+        config.addChangeListener("ui", "textPaneFontName", this);
70 82
     }
71 83
 
72 84
     /**
@@ -75,7 +87,9 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
75 87
      * @return Number of lines
76 88
      */
77 89
     public int getNumLines() {
78
-        return lines.size();
90
+        synchronized (lines) {
91
+            return lines.size();
92
+        }
79 93
     }
80 94
 
81 95
     /**
@@ -86,7 +100,9 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
86 100
      * @return Line at the specified number or null
87 101
      */
88 102
     Line getLine(final int lineNumber) {
89
-        return lines.get(lineNumber);
103
+        synchronized (lines) {
104
+            return lines.get(lineNumber);
105
+        }
90 106
     }
91 107
 
92 108
     /**
@@ -96,7 +112,7 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
96 112
      */
97 113
     public void addText(final String[] text) {
98 114
         synchronized (lines) {
99
-            lines.add(new Line(text, config));
115
+            lines.add(new Line(text, fontSize, fontName));
100 116
             fireLineAdded(lines.indexOf(text));
101 117
         }
102 118
     }
@@ -109,7 +125,7 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
109 125
      */
110 126
     public void addText(final String[] text, final int lineHeight) {
111 127
         synchronized (lines) {
112
-            lines.add(new Line(text, config, lineHeight));
128
+            lines.add(new Line(text, lineHeight, fontName));
113 129
             fireLineAdded(lines.indexOf(text));
114 130
         }
115 131
     }
@@ -123,7 +139,7 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
123 139
         synchronized (lines) {
124 140
             final int start = lines.size();
125 141
             for (String[] string : text) {
126
-                lines.add(new Line(string, config));
142
+                lines.add(new Line(string, fontSize, fontName));
127 143
             }
128 144
             fireLinesAdded(start, text.size());
129 145
         }
@@ -135,13 +151,14 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
135 151
      * @param text stylised string to add to the text
136 152
      * @param lineHeights line heights for the new lines
137 153
      */
138
-    public void addText(final List<String[]> text, final List<Integer> lineHeights) {
154
+    public void addText(final List<String[]> text,
155
+            final List<Integer> lineHeights) {
139 156
         synchronized (lines) {
140 157
             final int start = lines.size();
141
-            for (int i = 0; i < text.size() ; i++) {
158
+            for (int i = 0; i < text.size(); i++) {
142 159
                 final String[] string = text.get(i);
143 160
                 final int lineHeight = lineHeights.get(i);
144
-                lines.add(new Line(string, config, lineHeight));
161
+                lines.add(new Line(string, lineHeight, fontName));
145 162
             }
146 163
             fireLinesAdded(start, text.size());
147 164
         }
@@ -247,7 +264,7 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
247 264
             }
248 265
         }
249 266
     }
250
-    
267
+
251 268
     /**
252 269
      * fires the need repaint method on all listeners.
253 270
      */
@@ -269,19 +286,21 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
269 286
      * @return Styled line
270 287
      */
271 288
     AttributedCharacterIterator getStyledLine(final Line line) {
272
-        AttributedString styledLine = null;
273
-        if (cachedLines.contains(line)) {
274
-            final int index = cachedLines.getList().indexOf(line);
275
-            styledLine = cachedStrings.get(index);
276
-        }
289
+        synchronized (lines) {
290
+            AttributedString styledLine = null;
291
+            if (cachedLines.contains(line)) {
292
+                final int index = cachedLines.getList().indexOf(line);
293
+                styledLine = cachedStrings.get(index);
294
+            }
277 295
 
278
-        if (styledLine == null) {
279
-            styledLine = line.getStyled();
280
-            cachedLines.add(line);
281
-            cachedStrings.add(styledLine);
282
-        }
296
+            if (styledLine == null) {
297
+                styledLine = line.getStyled();
298
+                cachedLines.add(line);
299
+                cachedStrings.add(styledLine);
300
+            }
283 301
 
284
-        return styledLine.getIterator();
302
+            return styledLine.getIterator();
303
+        }
285 304
     }
286 305
 
287 306
     /**
@@ -295,7 +314,7 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
295 314
     public AttributedCharacterIterator getStyledLine(final int line) {
296 315
         return getStyledLine(getLine(line));
297 316
     }
298
-    
317
+
299 318
     /**
300 319
      * Returns the line height of the specified line
301 320
      * 
@@ -304,9 +323,9 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
304 323
      * @return Line height
305 324
      */
306 325
     int getLineHeight(final Line line) {
307
-        return line.getHeight();
326
+        return line.getFontSize();
308 327
     }
309
-    
328
+
310 329
     /**
311 330
      * Returns the line height of the specified line
312 331
      * 
@@ -315,14 +334,35 @@ public final class IRCDocument implements Serializable, ConfigChangeListener {
315 334
      * @return Line height
316 335
      */
317 336
     public int getLineHeight(final int line) {
318
-        return  getLineHeight(getLine(line));
337
+        return getLineHeight(getLine(line));
338
+    }
339
+
340
+    private void setCachedSettings() {
341
+        final Font defaultFont = UIManager.getFont("TextPane.font");
342
+        if (config.hasOptionString("ui", "textPaneFontName")) {
343
+            fontName = config.getOption("ui", "textPaneFontName");
344
+        } else {
345
+            fontName = defaultFont.getName();
346
+        }
347
+        if (config.hasOptionString("ui", "textPaneFontSize")) {
348
+            fontSize = config.getOptionInt("ui", "textPaneFontSize");
349
+        } else {
350
+            fontSize = defaultFont.getSize();
351
+        }
319 352
     }
320 353
 
321 354
     /** {@inheritDoc} */
322 355
     @Override
323 356
     public void configChanged(final String domain, final String key) {
357
+        setCachedSettings();
324 358
         cachedLines.clear();
325 359
         cachedStrings.clear();
360
+        synchronized (lines) {
361
+            for (Line line : lines) {
362
+                line.setFontName(fontName);
363
+                line.setFontSize(fontSize);
364
+            }
365
+        }
326 366
         fireRepaintNeeded();
327 367
     }
328 368
 }

+ 30
- 53
src/com/dmdirc/addons/ui_swing/textpane/Line.java Wyświetl plik

@@ -22,56 +22,35 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.textpane;
24 24
 
25
-import com.dmdirc.config.ConfigManager;
26
-import com.dmdirc.interfaces.ConfigChangeListener;
27 25
 import com.dmdirc.ui.core.util.ExtendedAttributedString;
28 26
 import com.dmdirc.ui.core.util.Utils;
29 27
 import com.dmdirc.ui.messages.Styliser;
30
-import java.awt.Font;
31 28
 
32 29
 import java.text.AttributedString;
33 30
 import java.util.Arrays;
34 31
 
35
-import javax.swing.UIManager;
36 32
 
37 33
 /**
38 34
  * Represents a line of text in IRC.
39 35
  */
40
-class Line implements ConfigChangeListener {
36
+class Line {
41 37
 
42 38
     private final String[] lineParts;
43
-    private final ConfigManager config;
44
-    private int lineHeight;
39
+    private int fontSize;
45 40
     private String fontName;
46 41
 
47
-    /**
48
-     * Creates a new line.
49
-     *
50
-     * @param lineParts Parts of the line
51
-     * @param config Configuration manager for this line
52
-     */
53
-    public Line(final String[] lineParts, final ConfigManager config) {
54
-        this.lineParts = lineParts;
55
-        this.config = config;
56
-        setCachedSettings();
57
-        config.addChangeListener("ui", "textPaneFontSize", this);
58
-        config.addChangeListener("ui", "textPaneFontName", this);
59
-    }
60
-
61 42
     /**
62 43
      * Creates a new line with a specified height.
63 44
      *
64 45
      * @param lineParts Parts of the line
65
-     * @param config Configuration manager for this line
66
-     * @param lineHeight The height for this line
46
+     * @param fontSize The height for this line
47
+     * @param fontName
67 48
      */
68
-    public Line(final String[] lineParts, final ConfigManager config,
69
-            final int lineHeight) {
49
+    public Line(final String[] lineParts, final int fontSize,
50
+            final String fontName) {
70 51
         this.lineParts = lineParts;
71
-        this.config = config;
72
-        setCachedSettings();
73
-        config.addChangeListener("ui", "textPaneFontSize", this);
74
-        config.addChangeListener("ui", "textPaneFontName", this);
52
+        this.fontName = fontName;
53
+        this.fontSize = fontSize;
75 54
     }
76 55
 
77 56
     /**
@@ -101,8 +80,26 @@ class Line implements ConfigChangeListener {
101 80
      * 
102 81
      * @return Line height
103 82
      */
104
-    public int getHeight() {
105
-        return lineHeight;
83
+    public int getFontSize() {
84
+        return fontSize;
85
+    }
86
+
87
+    /**
88
+     * Sets the default font size for this line.
89
+     *
90
+     * @param fontSize New default font size
91
+     */
92
+    public void setFontSize(final int fontSize) {
93
+        this.fontSize = fontSize;
94
+    }
95
+
96
+    /**
97
+     * Sets the default font name for this line.
98
+     *
99
+     * @param fontName New default font name
100
+     */
101
+    public void setFontName(final String fontName) {
102
+        this.fontName = fontName;
106 103
     }
107 104
 
108 105
     /**
@@ -140,8 +137,8 @@ class Line implements ConfigChangeListener {
140 137
      */
141 138
     public AttributedString getStyled() {
142 139
         final ExtendedAttributedString string = Utils.getAttributedString(lineParts,
143
-                fontName, lineHeight);
144
-        lineHeight = string.getMaxLineHeight();
140
+                fontName, fontSize);
141
+        fontSize = string.getMaxLineHeight();
145 142
         return string.getAttributedString();
146 143
     }
147 144
 
@@ -159,24 +156,4 @@ class Line implements ConfigChangeListener {
159 156
     public int hashCode() {
160 157
         return getLineParts().hashCode();
161 158
     }
162
-
163
-    /** {@inheritDoc} */
164
-    @Override
165
-    public void configChanged(final String domain, final String key) {
166
-        setCachedSettings();
167
-    }
168
-
169
-    private void setCachedSettings() {
170
-        final Font defaultFont = UIManager.getFont("TextPane.font");
171
-        if (config.hasOptionString("ui", "textPaneFontName")) {
172
-            fontName = config.getOption("ui", "textPaneFontName");
173
-        } else {
174
-            fontName = defaultFont.getName();
175
-        }
176
-        if (config.hasOptionString("ui", "textPaneFontSize")) {
177
-            lineHeight = config.getOptionInt("ui", "textPaneFontSize");
178
-        } else {
179
-            lineHeight = defaultFont.getSize();
180
-        }
181
-    }
182 159
 }

Ładowanie…
Anuluj
Zapisz