Преглед изворни кода

Allow styliser formatting in treeview.

Fixes issue CLIENT-24

Depends-On: Ia561f2b92cc4f007f7fefb24a28231fff927bc36
Change-Id: I9ca03e2fdc16f8a4c5b3c34a8304fa46555dc061
Reviewed-on: http://gerrit.dmdirc.com/1695
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.6.5
Greg Holmes пре 13 година
родитељ
комит
058a15b4fd

+ 1
- 1
src/com/dmdirc/addons/ui_swing/components/ImageButton.java Прегледај датотеку

@@ -81,7 +81,7 @@ public class ImageButton extends JButton {
81 81
         setContentAreaFilled(false);
82 82
         setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
83 83
         setMargin(new Insets(0, 0, 0, 0));
84
-        setPreferredSize(new Dimension(16, 0));
84
+        setPreferredSize(new Dimension(16, 16));
85 85
         setActionCommand(actionCommand);
86 86
     }
87 87
 

+ 117
- 7
src/com/dmdirc/addons/ui_swing/framemanager/tree/NodeLabel.java Прегледај датотеку

@@ -23,22 +23,32 @@
23 23
 package com.dmdirc.addons.ui_swing.framemanager.tree;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26
+import com.dmdirc.addons.ui_swing.components.ImageButton;
26 27
 import com.dmdirc.interfaces.FrameInfoListener;
27 28
 import com.dmdirc.interfaces.NotificationListener;
28 29
 import com.dmdirc.interfaces.SelectionListener;
29 30
 import com.dmdirc.ui.IconManager;
31
+import com.dmdirc.ui.messages.Styliser;
30 32
 
31 33
 import java.awt.Color;
32 34
 import java.awt.Dimension;
35
+import java.awt.Font;
36
+
33 37
 import javax.swing.BorderFactory;
34
-import javax.swing.JLabel;
38
+import javax.swing.JPanel;
39
+import javax.swing.JTextPane;
40
+import javax.swing.SwingUtilities;
41
+import javax.swing.UIManager;
42
+import javax.swing.text.DefaultStyledDocument;
43
+import javax.swing.text.StyledDocument;
35 44
 
36 45
 import net.miginfocom.layout.PlatformDefaults;
46
+import net.miginfocom.swing.MigLayout;
37 47
 
38 48
 /**
39 49
  * Node label.
40 50
  */
41
-public class NodeLabel extends JLabel implements SelectionListener,
51
+public class NodeLabel extends JPanel implements SelectionListener,
42 52
         NotificationListener, FrameInfoListener {
43 53
 
44 54
     /**
@@ -55,6 +65,13 @@ public class NodeLabel extends JLabel implements SelectionListener,
55 65
     private Color notificationColour;
56 66
     /** Are we the selected window? */
57 67
     private boolean selected;
68
+    /** Node icon. */
69
+    private final ImageButton icon = new ImageButton("", IconManager
70
+            .getIconManager().getIcon("icon"));
71
+    /** Text label. */
72
+    private final JTextPane text = new JTextPane(new DefaultStyledDocument());
73
+    /** Current styled text. */
74
+    private String currentText = "";
58 75
 
59 76
     /**
60 77
      * Instantiates a new node label.
@@ -77,11 +94,17 @@ public class NodeLabel extends JLabel implements SelectionListener,
77 94
             return;
78 95
         }
79 96
 
80
-        setText(window.toString());
97
+        icon.setIcon(IconManager.getIconManager().getIcon(window.getIcon()));
98
+        text.setText(window.toString());
99
+        text.setBorder(null);
100
+
101
+        setLayout(new MigLayout("ins 0"));
102
+        add(icon, "left");
103
+        add(text, "left, grow, pushx");
81 104
 
82 105
 
83
-        setToolTipText(null);
84
-        setIcon(IconManager.getIconManager().getIcon(window.getIcon()));
106
+        icon.setToolTipText(null);
107
+        text.setToolTipText(null);
85 108
         setBorder(BorderFactory.createEmptyBorder(1, 0, 2, 0));
86 109
 
87 110
         setPreferredSize(new Dimension(100000, getFont().getSize()
@@ -122,7 +145,7 @@ public class NodeLabel extends JLabel implements SelectionListener,
122 145
     @Override
123 146
     public void iconChanged(final FrameContainer<?> window, final String icon) {
124 147
         if (equals(window)) {
125
-            setIcon(IconManager.getIconManager().getIcon(icon));
148
+            this.icon.setIcon(IconManager.getIconManager().getIcon(icon));
126 149
         }
127 150
     }
128 151
 
@@ -130,7 +153,7 @@ public class NodeLabel extends JLabel implements SelectionListener,
130 153
     @Override
131 154
     public void nameChanged(final FrameContainer<?> window, final String name) {
132 155
         if (equals(window)) {
133
-            setText(name);
156
+            text.setText(name);
134 157
         }
135 158
     }
136 159
 
@@ -197,4 +220,91 @@ public class NodeLabel extends JLabel implements SelectionListener,
197 220
         return window.hashCode();
198 221
     }
199 222
 
223
+    /** {@inheritDoc} */
224
+    @Override
225
+    public Font getFont() {
226
+        return UIManager.getFont("TextPane.font");
227
+    }
228
+
229
+    /**
230
+     * Sets the foreground colour for this label.
231
+     *
232
+     * @param colour New foreground colour
233
+     */
234
+    @Override
235
+    public void setForeground(final Color colour) {
236
+        if (text != null) {
237
+            text.setForeground(colour);
238
+        }
239
+    }
240
+
241
+    /**
242
+     * Sets the background colour for this label.
243
+     *
244
+     * @param colour New background colour
245
+     */
246
+    @Override
247
+    public void setBackground(final Color colour) {
248
+        if (text != null) {
249
+            text.setBackground(colour);
250
+        }
251
+    }
252
+
253
+    /**
254
+     * Sets the font for this label.
255
+     *
256
+     * @param font New font
257
+     */
258
+    @Override
259
+    public void setFont(final Font font) {
260
+        if (text != null) {
261
+            text.setFont(font);
262
+        }
263
+    }
264
+
265
+    /**
266
+     * Sets the opacity of this label.
267
+     *
268
+     * @param opacity Desired opacity
269
+     */
270
+    @Override
271
+    public void setOpaque(final boolean opacity) {
272
+        if (text != null) {
273
+            text.setOpaque(opacity);
274
+        }
275
+    }
276
+
277
+    /**
278
+     * Sets the text and style in this label.
279
+     *
280
+     * @param styliser Styliser to use to style text
281
+     * @param styledText Styled text string to use
282
+     */
283
+    public void setStyledText(final Styliser styliser,
284
+            final String styledText) {
285
+        if (currentText.equals(styledText)) {
286
+            return;
287
+        }
288
+        text.setText("");
289
+        currentText = styledText;
290
+        styliser.addStyledString((StyledDocument) text.getDocument(),
291
+                new String[] {styledText, });
292
+    }
293
+
294
+    /**
295
+     * Sets the styles for the text in this label.
296
+     *
297
+     * @param styliser Styliser to use
298
+     * @param newText Style to set
299
+     */
300
+    public void setTextStyle(final Styliser styliser, final String newText) {
301
+        if (currentText.equals(newText + window.toString())) {
302
+            return;
303
+        }
304
+        text.setText("");
305
+        currentText = newText + window.toString();
306
+        styliser.addStyledString((StyledDocument) text.getDocument(),
307
+                new String[] {newText, window.toString(), });
308
+    }
309
+
200 310
 }

+ 2
- 3
src/com/dmdirc/addons/ui_swing/framemanager/tree/TreeFrameManager.java Прегледај датотеку

@@ -220,9 +220,8 @@ public final class TreeFrameManager implements FrameManager,
220 220
             /** {@inheritDoc} */
221 221
             @Override
222 222
             public void run() {
223
-                final TreeViewNode node =
224
-                        new TreeViewNode(new NodeLabel(window),
225
-                        window);
223
+                final NodeLabel label = new NodeLabel(window);
224
+                final TreeViewNode node = new TreeViewNode(label, window);
226 225
                 synchronized (nodes) {
227 226
                     nodes.put(window, node);
228 227
                 }

+ 2
- 2
src/com/dmdirc/addons/ui_swing/framemanager/tree/TreeViewNode.java Прегледај датотеку

@@ -38,9 +38,9 @@ public class TreeViewNode extends DefaultMutableTreeNode {
38 38
      */
39 39
     private static final long serialVersionUID = 5;
40 40
     /** Node's label. */
41
-    private NodeLabel label;
41
+    private final NodeLabel label;
42 42
     /** Node's frame container. */
43
-    private FrameContainer<?> window;
43
+    private final FrameContainer<?> window;
44 44
 
45 45
     /**
46 46
      * Instantiates a new tree view node.

+ 26
- 17
src/com/dmdirc/addons/ui_swing/framemanager/tree/TreeViewTreeCellRenderer.java Прегледај датотеку

@@ -25,10 +25,11 @@ package com.dmdirc.addons.ui_swing.framemanager.tree;
25 25
 import com.dmdirc.config.ConfigManager;
26 26
 import com.dmdirc.config.IdentityManager;
27 27
 import com.dmdirc.interfaces.ConfigChangeListener;
28
+import com.dmdirc.ui.messages.ColourManager;
29
+import com.dmdirc.ui.messages.Styliser;
28 30
 
29 31
 import java.awt.Color;
30 32
 import java.awt.Component;
31
-import java.awt.Font;
32 33
 
33 34
 import javax.swing.JLabel;
34 35
 import javax.swing.JTree;
@@ -50,6 +51,8 @@ public class TreeViewTreeCellRenderer implements TreeCellRenderer,
50 51
     private final TreeFrameManager manager;
51 52
     /** Config manager. */
52 53
     private final ConfigManager config;
54
+    /** Styliser to use. */
55
+    private final Styliser styliser;
53 56
     /** Rollover colours. */
54 57
     private Color rolloverColour;
55 58
     /** Active bold. */
@@ -68,6 +71,7 @@ public class TreeViewTreeCellRenderer implements TreeCellRenderer,
68 71
         this.manager = manager;
69 72
 
70 73
         config = IdentityManager.getGlobalConfig();
74
+        styliser = new Styliser(null, config);
71 75
 
72 76
         setColours();
73 77
 
@@ -100,35 +104,40 @@ public class TreeViewTreeCellRenderer implements TreeCellRenderer,
100 104
         if (label == null) {
101 105
             return new JLabel("Label == null");
102 106
         }
103
-
104
-        label.setOpaque(false);
105
-        label.setBackground(tree.getBackground());
106
-        label.setForeground(tree.getForeground());
107
+        boolean bold = false;
108
+        Color background = tree.getBackground();
109
+        Color foreground = tree.getForeground();
107 110
 
108 111
         if (label.isRollover()) {
109
-            label.setOpaque(true);
110
-            label.setBackground(rolloverColour);
112
+            background = rolloverColour;
111 113
         }
112 114
 
113 115
         final Color colour = label.getNotificationColour();
114 116
         if (colour != null) {
115
-            label.setForeground(colour);
117
+            foreground = colour;
116 118
         }
117 119
 
118 120
         if (label.isSelected()) {
119
-            if (activeBold) {
120
-                label.setFont(label.getFont().deriveFont(Font.BOLD));
121
-            } else {
122
-                label.setFont(label.getFont().deriveFont(Font.PLAIN));
123
-            }
121
+            bold = activeBold;
124 122
             if (!tree.getBackground().equals(activeBackground)) {
125
-                label.setOpaque(true);
126
-                label.setBackground(activeBackground);
123
+                background = activeBackground;
127 124
             }
128
-            label.setForeground(activeForeground);
125
+            foreground = activeForeground;
129 126
         } else {
130
-            label.setFont(label.getFont().deriveFont(Font.PLAIN));
127
+            bold = false;
128
+        }
129
+
130
+        final StringBuilder sb = new StringBuilder();
131
+        if (bold) {
132
+            sb.append(Styliser.CODE_BOLD);
131 133
         }
134
+        sb.append(Styliser.CODE_HEXCOLOUR);
135
+        sb.append(ColourManager.getHex(foreground));
136
+        sb.append(',');
137
+        sb.append(ColourManager.getHex(background));
138
+        label.setBackground(background);
139
+        label.setOpaque(true);
140
+        label.setTextStyle(styliser, sb.toString());
132 141
 
133 142
         return label;
134 143
     }

Loading…
Откажи
Сачувај