Browse Source

Don't add tool tips for null components.

Fixes issue 4324

Change-Id: I1c519eba8df88f724e53c6a73121f4fcd7318c72
Reviewed-on: http://gerrit.dmdirc.com/1436
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.4
Greg Holmes 14 years ago
parent
commit
abf15cf81a
1 changed files with 42 additions and 37 deletions
  1. 42
    37
      src/com/dmdirc/addons/ui_swing/components/ToolTipPanel.java

+ 42
- 37
src/com/dmdirc/addons/ui_swing/components/ToolTipPanel.java View File

@@ -1,20 +1,17 @@
1
-
2
-package com.dmdirc.addons.ui_swing.components;
3
-
4 1
 /*
5
- * 
2
+ *
6 3
  * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
7
- * 
4
+ *
8 5
  * Permission is hereby granted, free of charge, to any person obtaining a copy
9 6
  * of this software and associated documentation files (the "Software"), to deal
10 7
  * in the Software without restriction, including without limitation the rights
11 8
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 9
  * copies of the Software, and to permit persons to whom the Software is
13 10
  * furnished to do so, subject to the following conditions:
14
- * 
11
+ *
15 12
  * The above copyright notice and this permission notice shall be included in
16 13
  * all copies or substantial portions of the Software.
17
- * 
14
+ *
18 15
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 16
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 17
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -23,6 +20,9 @@ package com.dmdirc.addons.ui_swing.components;
23 20
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 21
  * SOFTWARE.
25 22
  */
23
+
24
+package com.dmdirc.addons.ui_swing.components;
25
+
26 26
 import com.dmdirc.addons.ui_swing.UIUtilities;
27 27
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
28 28
 import com.dmdirc.ui.IconManager;
@@ -47,7 +47,7 @@ import org.jdesktop.jxlayer.plaf.AbstractLayerUI;
47 47
 import org.jdesktop.jxlayer.plaf.LayerUI;
48 48
 
49 49
 /**
50
- * Panel to display toolstips of a component.
50
+ * Panel to display tool tips of a component.
51 51
  */
52 52
 public class ToolTipPanel extends JPanel implements MouseListener {
53 53
 
@@ -57,26 +57,26 @@ public class ToolTipPanel extends JPanel implements MouseListener {
57 57
      * serialized objects being unserialized with the new class).
58 58
      */
59 59
     private static final long serialVersionUID = -8929794537312606692L;
60
-    /** Default tooltip. */
61
-    private String defaultHelp;
62
-    /** Tooltip display. */
63
-    private TextLabel tooltip;
60
+    /** Default tool tip. */
61
+    private final String defaultHelp;
62
+    /** Tool tip display. */
63
+    private final TextLabel tooltip;
64 64
     /** Error icon. */
65
-    private JLabel icon;
65
+    private final JLabel icon;
66 66
     /** Whether or not this is a warning. */
67
-    private String warning = null;
68
-    /** Map of registered components to their tooltips. */
67
+    private String warningText = null;
68
+    /** Map of registered components to their tool tips. */
69 69
     private final Map<JComponent, String> tooltips;
70 70
 
71 71
     /**
72
-     * Instantiates a new tooltip panel.
72
+     * Instantiates a new tool tip panel.
73 73
      *
74
-     * @param defaultHelp Default help message when idle
74
+     * @param helpText Default help message when idle
75 75
      */
76
-    public ToolTipPanel(final String defaultHelp) {
76
+    public ToolTipPanel(final String helpText) {
77 77
         super(new MigLayout("hidemode 3"));
78 78
 
79
-        this.defaultHelp = defaultHelp;
79
+        defaultHelp = helpText;
80 80
         this.tooltips = new HashMap<JComponent, String>();
81 81
         this.icon = new JLabel(IconManager.getIconManager().getIcon("warning"));
82 82
 
@@ -92,27 +92,27 @@ public class ToolTipPanel extends JPanel implements MouseListener {
92 92
     }
93 93
 
94 94
     /**
95
-     * Resets the content of the tooltip.
95
+     * Resets the content of the tool tip.
96 96
      */
97
-    protected void reset() {
98
-        SimpleAttributeSet sas = new SimpleAttributeSet();
97
+    protected final void reset() {
98
+        final SimpleAttributeSet sas = new SimpleAttributeSet();
99 99
 
100 100
         StyleConstants.setForeground(sas, Color.BLACK);
101 101
         StyleConstants.setBackground(sas, Color.WHITE);
102
-        if (warning == null || warning.isEmpty()) {
102
+        if (warningText == null || warningText.isEmpty()) {
103 103
             tooltip.setText(defaultHelp);
104 104
             icon.setVisible(false);
105 105
             StyleConstants.setItalic(sas, true);
106 106
         } else {
107 107
             icon.setVisible(true);
108
-            tooltip.setText(warning);
108
+            tooltip.setText(warningText);
109 109
         }
110 110
         tooltip.getDocument().setParagraphAttributes(0, tooltip.getDocument().
111 111
                 getLength(), sas, true);
112 112
     }
113 113
 
114 114
     /**
115
-     * Sets the content of the tooltip area to the specified text.
115
+     * Sets the content of the tool tip area to the specified text.
116 116
      *
117 117
      * @param text The text to be displayed
118 118
      */
@@ -120,33 +120,34 @@ public class ToolTipPanel extends JPanel implements MouseListener {
120 120
         if (tooltip == null) {
121 121
             return;
122 122
         }
123
-        
123
+
124 124
         tooltip.setText(text);
125 125
         if (tooltip.getDocument() == null || text == null) {
126 126
             return;
127 127
         }
128 128
 
129 129
         icon.setVisible(false);
130
-        SimpleAttributeSet sas = new SimpleAttributeSet();
130
+        final SimpleAttributeSet sas = new SimpleAttributeSet();
131 131
         StyleConstants.setItalic(sas, false);
132 132
         StyleConstants.setForeground(sas, Color.BLACK);
133 133
         StyleConstants.setBackground(sas, Color.WHITE);
134
-        tooltip.getDocument().setParagraphAttributes(0, text.length(), sas, true);
134
+        tooltip.getDocument().setParagraphAttributes(0, text.length(), sas,
135
+                true);
135 136
     }
136 137
 
137 138
     /**
138
-     * Sets whether or not this tooltip should be rendered as a warning.
139
+     * Sets whether or not this tool tip should be rendered as a warning.
139 140
      *
140 141
      * @param warning Warning string, null or empty to reset.
141 142
      * @since 0.6.3
142 143
      */
143 144
     public void setWarning(final String warning) {
144
-        this.warning = warning;
145
+        warningText = warning;
145 146
         reset();
146 147
     }
147 148
 
148 149
     /**
149
-     * Registers a component with this tooltip handler.
150
+     * Registers a component with this tool tip handler.
150 151
      *
151 152
      * @param component Component to register
152 153
      */
@@ -156,25 +157,29 @@ public class ToolTipPanel extends JPanel implements MouseListener {
156 157
     }
157 158
 
158 159
     /**
159
-     * Registers a component with this tooltip handler.
160
+     * Registers a component with this tool tip handler.
160 161
      *
161 162
      * @param component Component to register
162
-     * @param tooltipText Tooltip text for the component
163
+     * @param tooltipText Tool tip text for the component
163 164
      */
164 165
     @SuppressWarnings("unchecked")
165 166
     public void registerTooltipHandler(final JComponent component,
166 167
             final String tooltipText) {
168
+        if (component == null) {
169
+            return;
170
+        }
167 171
         tooltips.put(component, tooltipText);
168 172
         if (component instanceof JXLayer<?>) {
169
-            final LayerUI<JComponent> layerUI = new AbstractLayerUI<JComponent>() {
173
+            final LayerUI<JComponent> layerUI =
174
+                    new AbstractLayerUI<JComponent>() {
170 175
 
171 176
                 private static final long serialVersionUID =
172 177
                         -8698248993206174390L;
173 178
 
174 179
                 /** {@inheritDoc} */
175 180
                 @Override
176
-                protected void processMouseEvent(MouseEvent e,
177
-                        JXLayer<? extends JComponent> comp) {
181
+                protected void processMouseEvent(final MouseEvent e,
182
+                        final JXLayer<? extends JComponent> comp) {
178 183
                     if (e.getID() == MouseEvent.MOUSE_ENTERED) {
179 184
                         setText(tooltips.get(comp));
180 185
                     } else if (e.getID() == MouseEvent.MOUSE_EXITED && comp.
@@ -235,7 +240,7 @@ public class ToolTipPanel extends JPanel implements MouseListener {
235 240
     @Override
236 241
     public void mouseEntered(final MouseEvent e) {
237 242
         if (e.getSource() instanceof JComponent) {
238
-            setText(tooltips.get(e.getSource()));
243
+            setText(tooltips.get((JComponent) e.getSource()));
239 244
         }
240 245
     }
241 246
 

Loading…
Cancel
Save