Browse Source

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 years ago
parent
commit
e9b66c9cf7

+ 64
- 24
src/com/dmdirc/addons/ui_swing/textpane/IRCDocument.java View File

19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
  * SOFTWARE.
20
  * SOFTWARE.
21
  */
21
  */
22
+
22
 package com.dmdirc.addons.ui_swing.textpane;
23
 package com.dmdirc.addons.ui_swing.textpane;
23
 
24
 
24
 import com.dmdirc.config.ConfigManager;
25
 import com.dmdirc.config.ConfigManager;
25
 import com.dmdirc.interfaces.ConfigChangeListener;
26
 import com.dmdirc.interfaces.ConfigChangeListener;
26
 import com.dmdirc.util.RollingList;
27
 import com.dmdirc.util.RollingList;
28
+import java.awt.Font;
27
 
29
 
28
 import java.io.Serializable;
30
 import java.io.Serializable;
29
 import java.text.AttributedCharacterIterator;
31
 import java.text.AttributedCharacterIterator;
30
 import java.text.AttributedString;
32
 import java.text.AttributedString;
31
 import java.util.ArrayList;
33
 import java.util.ArrayList;
32
 import java.util.List;
34
 import java.util.List;
35
+import javax.swing.UIManager;
33
 
36
 
34
 import javax.swing.event.EventListenerList;
37
 import javax.swing.event.EventListenerList;
35
 
38
 
54
     private RollingList<AttributedString> cachedStrings;
57
     private RollingList<AttributedString> cachedStrings;
55
     /** Configuration manager. */
58
     /** Configuration manager. */
56
     private ConfigManager config;
59
     private ConfigManager config;
60
+    /** Font size. */
61
+    private int fontSize;
62
+    /** Font name. */
63
+    private String fontName;
57
 
64
 
58
     /** 
65
     /** 
59
      * Creates a new instance of IRCDocument.
66
      * Creates a new instance of IRCDocument.
67
 
74
 
68
         cachedLines = new RollingList<Line>(50);
75
         cachedLines = new RollingList<Line>(50);
69
         cachedStrings = new RollingList<AttributedString>(50);
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
      * @return Number of lines
87
      * @return Number of lines
76
      */
88
      */
77
     public int getNumLines() {
89
     public int getNumLines() {
78
-        return lines.size();
90
+        synchronized (lines) {
91
+            return lines.size();
92
+        }
79
     }
93
     }
80
 
94
 
81
     /**
95
     /**
86
      * @return Line at the specified number or null
100
      * @return Line at the specified number or null
87
      */
101
      */
88
     Line getLine(final int lineNumber) {
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
      */
112
      */
97
     public void addText(final String[] text) {
113
     public void addText(final String[] text) {
98
         synchronized (lines) {
114
         synchronized (lines) {
99
-            lines.add(new Line(text, config));
115
+            lines.add(new Line(text, fontSize, fontName));
100
             fireLineAdded(lines.indexOf(text));
116
             fireLineAdded(lines.indexOf(text));
101
         }
117
         }
102
     }
118
     }
109
      */
125
      */
110
     public void addText(final String[] text, final int lineHeight) {
126
     public void addText(final String[] text, final int lineHeight) {
111
         synchronized (lines) {
127
         synchronized (lines) {
112
-            lines.add(new Line(text, config, lineHeight));
128
+            lines.add(new Line(text, lineHeight, fontName));
113
             fireLineAdded(lines.indexOf(text));
129
             fireLineAdded(lines.indexOf(text));
114
         }
130
         }
115
     }
131
     }
123
         synchronized (lines) {
139
         synchronized (lines) {
124
             final int start = lines.size();
140
             final int start = lines.size();
125
             for (String[] string : text) {
141
             for (String[] string : text) {
126
-                lines.add(new Line(string, config));
142
+                lines.add(new Line(string, fontSize, fontName));
127
             }
143
             }
128
             fireLinesAdded(start, text.size());
144
             fireLinesAdded(start, text.size());
129
         }
145
         }
135
      * @param text stylised string to add to the text
151
      * @param text stylised string to add to the text
136
      * @param lineHeights line heights for the new lines
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
         synchronized (lines) {
156
         synchronized (lines) {
140
             final int start = lines.size();
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
                 final String[] string = text.get(i);
159
                 final String[] string = text.get(i);
143
                 final int lineHeight = lineHeights.get(i);
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
             fireLinesAdded(start, text.size());
163
             fireLinesAdded(start, text.size());
147
         }
164
         }
247
             }
264
             }
248
         }
265
         }
249
     }
266
     }
250
-    
267
+
251
     /**
268
     /**
252
      * fires the need repaint method on all listeners.
269
      * fires the need repaint method on all listeners.
253
      */
270
      */
269
      * @return Styled line
286
      * @return Styled line
270
      */
287
      */
271
     AttributedCharacterIterator getStyledLine(final Line line) {
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
     public AttributedCharacterIterator getStyledLine(final int line) {
314
     public AttributedCharacterIterator getStyledLine(final int line) {
296
         return getStyledLine(getLine(line));
315
         return getStyledLine(getLine(line));
297
     }
316
     }
298
-    
317
+
299
     /**
318
     /**
300
      * Returns the line height of the specified line
319
      * Returns the line height of the specified line
301
      * 
320
      * 
304
      * @return Line height
323
      * @return Line height
305
      */
324
      */
306
     int getLineHeight(final Line line) {
325
     int getLineHeight(final Line line) {
307
-        return line.getHeight();
326
+        return line.getFontSize();
308
     }
327
     }
309
-    
328
+
310
     /**
329
     /**
311
      * Returns the line height of the specified line
330
      * Returns the line height of the specified line
312
      * 
331
      * 
315
      * @return Line height
334
      * @return Line height
316
      */
335
      */
317
     public int getLineHeight(final int line) {
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
     /** {@inheritDoc} */
354
     /** {@inheritDoc} */
322
     @Override
355
     @Override
323
     public void configChanged(final String domain, final String key) {
356
     public void configChanged(final String domain, final String key) {
357
+        setCachedSettings();
324
         cachedLines.clear();
358
         cachedLines.clear();
325
         cachedStrings.clear();
359
         cachedStrings.clear();
360
+        synchronized (lines) {
361
+            for (Line line : lines) {
362
+                line.setFontName(fontName);
363
+                line.setFontSize(fontSize);
364
+            }
365
+        }
326
         fireRepaintNeeded();
366
         fireRepaintNeeded();
327
     }
367
     }
328
 }
368
 }

+ 30
- 53
src/com/dmdirc/addons/ui_swing/textpane/Line.java View File

22
 
22
 
23
 package com.dmdirc.addons.ui_swing.textpane;
23
 package com.dmdirc.addons.ui_swing.textpane;
24
 
24
 
25
-import com.dmdirc.config.ConfigManager;
26
-import com.dmdirc.interfaces.ConfigChangeListener;
27
 import com.dmdirc.ui.core.util.ExtendedAttributedString;
25
 import com.dmdirc.ui.core.util.ExtendedAttributedString;
28
 import com.dmdirc.ui.core.util.Utils;
26
 import com.dmdirc.ui.core.util.Utils;
29
 import com.dmdirc.ui.messages.Styliser;
27
 import com.dmdirc.ui.messages.Styliser;
30
-import java.awt.Font;
31
 
28
 
32
 import java.text.AttributedString;
29
 import java.text.AttributedString;
33
 import java.util.Arrays;
30
 import java.util.Arrays;
34
 
31
 
35
-import javax.swing.UIManager;
36
 
32
 
37
 /**
33
 /**
38
  * Represents a line of text in IRC.
34
  * Represents a line of text in IRC.
39
  */
35
  */
40
-class Line implements ConfigChangeListener {
36
+class Line {
41
 
37
 
42
     private final String[] lineParts;
38
     private final String[] lineParts;
43
-    private final ConfigManager config;
44
-    private int lineHeight;
39
+    private int fontSize;
45
     private String fontName;
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
      * Creates a new line with a specified height.
43
      * Creates a new line with a specified height.
63
      *
44
      *
64
      * @param lineParts Parts of the line
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
         this.lineParts = lineParts;
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
      * 
80
      * 
102
      * @return Line height
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
      */
137
      */
141
     public AttributedString getStyled() {
138
     public AttributedString getStyled() {
142
         final ExtendedAttributedString string = Utils.getAttributedString(lineParts,
139
         final ExtendedAttributedString string = Utils.getAttributedString(lineParts,
143
-                fontName, lineHeight);
144
-        lineHeight = string.getMaxLineHeight();
140
+                fontName, fontSize);
141
+        fontSize = string.getMaxLineHeight();
145
         return string.getAttributedString();
142
         return string.getAttributedString();
146
     }
143
     }
147
 
144
 
159
     public int hashCode() {
156
     public int hashCode() {
160
         return getLineParts().hashCode();
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
 }

Loading…
Cancel
Save