Quellcode durchsuchen

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 vor 14 Jahren
Ursprung
Commit
abf15cf81a
1 geänderte Dateien mit 42 neuen und 37 gelöschten Zeilen
  1. 42
    37
      src/com/dmdirc/addons/ui_swing/components/ToolTipPanel.java

+ 42
- 37
src/com/dmdirc/addons/ui_swing/components/ToolTipPanel.java Datei anzeigen

1
-
2
-package com.dmdirc.addons.ui_swing.components;
3
-
4
 /*
1
 /*
5
- * 
2
+ *
6
  * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
  * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
7
- * 
4
+ *
8
  * Permission is hereby granted, free of charge, to any person obtaining a copy
5
  * Permission is hereby granted, free of charge, to any person obtaining a copy
9
  * of this software and associated documentation files (the "Software"), to deal
6
  * of this software and associated documentation files (the "Software"), to deal
10
  * in the Software without restriction, including without limitation the rights
7
  * in the Software without restriction, including without limitation the rights
11
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
  * copies of the Software, and to permit persons to whom the Software is
9
  * copies of the Software, and to permit persons to whom the Software is
13
  * furnished to do so, subject to the following conditions:
10
  * furnished to do so, subject to the following conditions:
14
- * 
11
+ *
15
  * The above copyright notice and this permission notice shall be included in
12
  * The above copyright notice and this permission notice shall be included in
16
  * all copies or substantial portions of the Software.
13
  * all copies or substantial portions of the Software.
17
- * 
14
+ *
18
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
  * SOFTWARE.
21
  * SOFTWARE.
25
  */
22
  */
23
+
24
+package com.dmdirc.addons.ui_swing.components;
25
+
26
 import com.dmdirc.addons.ui_swing.UIUtilities;
26
 import com.dmdirc.addons.ui_swing.UIUtilities;
27
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
27
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
28
 import com.dmdirc.ui.IconManager;
28
 import com.dmdirc.ui.IconManager;
47
 import org.jdesktop.jxlayer.plaf.LayerUI;
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
 public class ToolTipPanel extends JPanel implements MouseListener {
52
 public class ToolTipPanel extends JPanel implements MouseListener {
53
 
53
 
57
      * serialized objects being unserialized with the new class).
57
      * serialized objects being unserialized with the new class).
58
      */
58
      */
59
     private static final long serialVersionUID = -8929794537312606692L;
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
     /** Error icon. */
64
     /** Error icon. */
65
-    private JLabel icon;
65
+    private final JLabel icon;
66
     /** Whether or not this is a warning. */
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
     private final Map<JComponent, String> tooltips;
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
         super(new MigLayout("hidemode 3"));
77
         super(new MigLayout("hidemode 3"));
78
 
78
 
79
-        this.defaultHelp = defaultHelp;
79
+        defaultHelp = helpText;
80
         this.tooltips = new HashMap<JComponent, String>();
80
         this.tooltips = new HashMap<JComponent, String>();
81
         this.icon = new JLabel(IconManager.getIconManager().getIcon("warning"));
81
         this.icon = new JLabel(IconManager.getIconManager().getIcon("warning"));
82
 
82
 
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
         StyleConstants.setForeground(sas, Color.BLACK);
100
         StyleConstants.setForeground(sas, Color.BLACK);
101
         StyleConstants.setBackground(sas, Color.WHITE);
101
         StyleConstants.setBackground(sas, Color.WHITE);
102
-        if (warning == null || warning.isEmpty()) {
102
+        if (warningText == null || warningText.isEmpty()) {
103
             tooltip.setText(defaultHelp);
103
             tooltip.setText(defaultHelp);
104
             icon.setVisible(false);
104
             icon.setVisible(false);
105
             StyleConstants.setItalic(sas, true);
105
             StyleConstants.setItalic(sas, true);
106
         } else {
106
         } else {
107
             icon.setVisible(true);
107
             icon.setVisible(true);
108
-            tooltip.setText(warning);
108
+            tooltip.setText(warningText);
109
         }
109
         }
110
         tooltip.getDocument().setParagraphAttributes(0, tooltip.getDocument().
110
         tooltip.getDocument().setParagraphAttributes(0, tooltip.getDocument().
111
                 getLength(), sas, true);
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
      * @param text The text to be displayed
117
      * @param text The text to be displayed
118
      */
118
      */
120
         if (tooltip == null) {
120
         if (tooltip == null) {
121
             return;
121
             return;
122
         }
122
         }
123
-        
123
+
124
         tooltip.setText(text);
124
         tooltip.setText(text);
125
         if (tooltip.getDocument() == null || text == null) {
125
         if (tooltip.getDocument() == null || text == null) {
126
             return;
126
             return;
127
         }
127
         }
128
 
128
 
129
         icon.setVisible(false);
129
         icon.setVisible(false);
130
-        SimpleAttributeSet sas = new SimpleAttributeSet();
130
+        final SimpleAttributeSet sas = new SimpleAttributeSet();
131
         StyleConstants.setItalic(sas, false);
131
         StyleConstants.setItalic(sas, false);
132
         StyleConstants.setForeground(sas, Color.BLACK);
132
         StyleConstants.setForeground(sas, Color.BLACK);
133
         StyleConstants.setBackground(sas, Color.WHITE);
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
      * @param warning Warning string, null or empty to reset.
141
      * @param warning Warning string, null or empty to reset.
141
      * @since 0.6.3
142
      * @since 0.6.3
142
      */
143
      */
143
     public void setWarning(final String warning) {
144
     public void setWarning(final String warning) {
144
-        this.warning = warning;
145
+        warningText = warning;
145
         reset();
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
      * @param component Component to register
152
      * @param component Component to register
152
      */
153
      */
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
      * @param component Component to register
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
     @SuppressWarnings("unchecked")
165
     @SuppressWarnings("unchecked")
165
     public void registerTooltipHandler(final JComponent component,
166
     public void registerTooltipHandler(final JComponent component,
166
             final String tooltipText) {
167
             final String tooltipText) {
168
+        if (component == null) {
169
+            return;
170
+        }
167
         tooltips.put(component, tooltipText);
171
         tooltips.put(component, tooltipText);
168
         if (component instanceof JXLayer<?>) {
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
                 private static final long serialVersionUID =
176
                 private static final long serialVersionUID =
172
                         -8698248993206174390L;
177
                         -8698248993206174390L;
173
 
178
 
174
                 /** {@inheritDoc} */
179
                 /** {@inheritDoc} */
175
                 @Override
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
                     if (e.getID() == MouseEvent.MOUSE_ENTERED) {
183
                     if (e.getID() == MouseEvent.MOUSE_ENTERED) {
179
                         setText(tooltips.get(comp));
184
                         setText(tooltips.get(comp));
180
                     } else if (e.getID() == MouseEvent.MOUSE_EXITED && comp.
185
                     } else if (e.getID() == MouseEvent.MOUSE_EXITED && comp.
235
     @Override
240
     @Override
236
     public void mouseEntered(final MouseEvent e) {
241
     public void mouseEntered(final MouseEvent e) {
237
         if (e.getSource() instanceof JComponent) {
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
 

Laden…
Abbrechen
Speichern