Kaynağa Gözat

Issue 1737

git-svn-id: http://svn.dmdirc.com/trunk@4638 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6.3m1rc1
Chris Smith 16 yıl önce
ebeveyn
işleme
b52ae2dd31

+ 116
- 7
src/com/dmdirc/ui/swing/dialogs/prefs/SwingPreferencesDialog.java Dosyayı Görüntüle

@@ -34,11 +34,17 @@ import com.dmdirc.ui.swing.components.OptionalColourChooser;
34 34
 import com.dmdirc.ui.swing.components.StandardDialog;
35 35
 import com.dmdirc.ui.swing.components.TextLabel;
36 36
 import com.dmdirc.ui.swing.components.TreeScroller;
37
-
38 37
 import com.dmdirc.ui.swing.components.durationeditor.DurationDisplay;
38
+
39 39
 import java.awt.CardLayout;
40
+import java.awt.Color;
41
+import java.awt.Component;
42
+import java.awt.Container;
43
+import java.awt.Font;
40 44
 import java.awt.event.ActionEvent;
41 45
 import java.awt.event.ActionListener;
46
+import java.awt.event.MouseEvent;
47
+import java.awt.event.MouseListener;
42 48
 import java.util.ArrayList;
43 49
 import java.util.Collection;
44 50
 import java.util.HashMap;
@@ -56,6 +62,7 @@ import javax.swing.JTree;
56 62
 import javax.swing.WindowConstants;
57 63
 import javax.swing.event.TreeSelectionEvent;
58 64
 import javax.swing.event.TreeSelectionListener;
65
+import javax.swing.plaf.metal.MetalTreeUI;
59 66
 import javax.swing.text.Position;
60 67
 import javax.swing.tree.DefaultMutableTreeNode;
61 68
 import javax.swing.tree.DefaultTreeModel;
@@ -70,7 +77,7 @@ import net.miginfocom.swing.MigLayout;
70 77
  * Allows the user to modify global client preferences.
71 78
  */
72 79
 public final class SwingPreferencesDialog extends StandardDialog implements
73
-        ActionListener, TreeSelectionListener {
80
+        ActionListener, TreeSelectionListener, MouseListener {
74 81
 
75 82
     /**
76 83
      * A version number for this class. It should be changed whenever the
@@ -96,6 +103,12 @@ public final class SwingPreferencesDialog extends StandardDialog implements
96 103
     private CardLayout cardLayout;
97 104
     /** Main panel. */
98 105
     private JPanel mainPanel;
106
+    /** Title label. */
107
+    private JLabel title;
108
+    /** Tooltip display area. */
109
+    private TextLabel tooltip;
110
+    /** Cached tooltips. */
111
+    private final Map<Component, String> tooltips = new HashMap<Component, String>();
99 112
     /** root node. */
100 113
     private DefaultMutableTreeNode rootNode;
101 114
     /** Previously selected category. */
@@ -130,6 +143,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
130 143
         new TreeScroller(tabList);
131 144
 
132 145
         addCategories(manager.getCategories());
146
+        addMouseListeners(mainPanel.getComponents());
133 147
     }
134 148
     
135 149
     /** Returns the instance of SwingPreferencesDialog. */
@@ -161,13 +175,32 @@ public final class SwingPreferencesDialog extends StandardDialog implements
161 175
         cardLayout = new CardLayout();
162 176
         mainPanel = new JPanel(cardLayout);
163 177
 
178
+        final JPanel titlePanel = new JPanel(new MigLayout());
179
+
180
+        title = new JLabel("Preferences");
181
+        title.setFont(title.getFont().deriveFont((float) 18));
182
+
183
+        titlePanel.add(title, "growx, growy");
184
+        titlePanel.setOpaque(true);
185
+        titlePanel.setBackground(Color.WHITE);
186
+        titlePanel.setBorder(BorderFactory.createEtchedBorder());
187
+
188
+        tooltip = new TextLabel("");
189
+        resetTooltip();
190
+
191
+        final JPanel tooltipPanel = new JPanel(new MigLayout());
192
+        tooltipPanel.add(tooltip, "growx, growy");
193
+        tooltipPanel.setOpaque(true);
194
+        tooltipPanel.setBackground(Color.WHITE);
195
+        tooltipPanel.setBorder(BorderFactory.createEtchedBorder());
196
+
164 197
         rootNode = new DefaultMutableTreeNode("root");
165 198
 
166 199
         tabList = new JTree(new DefaultTreeModel(rootNode));
167 200
         tabList.getSelectionModel().setSelectionMode(
168 201
                 TreeSelectionModel.SINGLE_TREE_SELECTION);
169 202
         tabList.putClientProperty("JTree.lineStyle", "Angled");
170
-        tabList.setUI(new javax.swing.plaf.metal.MetalTreeUI());
203
+        tabList.setUI(new MetalTreeUI());
171 204
         tabList.setRootVisible(false);
172 205
         tabList.setShowsRootHandles(false);
173 206
         tabList.setCellRenderer(new PreferencesTreeCellRenderer());
@@ -187,12 +220,53 @@ public final class SwingPreferencesDialog extends StandardDialog implements
187 220
         getCancelButton().addActionListener(this);
188 221
         
189 222
         setLayout(new MigLayout("fillx, wmin 650, wmax 650, hmax 600"));
190
-        add(tabList, "width 150, growy, spany");
191
-        add(mainPanel, "wrap, wmin 480, wmax 480, grow, hmax 550");
192
-        add(getLeftButton(), "split, right");
223
+        add(tabList, "width 150, growy, spany 3");
224
+        add(titlePanel, "wrap, wmin 480, wmax 480");
225
+        add(mainPanel, "wrap, wmin 480, wmax 480, grow, hmax 450");
226
+        add(tooltipPanel, "wrap, wmin 480, wmax 480, hmin 65, hmax 65");
227
+        add(getLeftButton(), "span, split, right");
193 228
         add(getRightButton(), "right");
194 229
     }
195 230
 
231
+    /**
232
+     * Resets the content of the tooltip.
233
+     */
234
+    protected void resetTooltip() {
235
+        tooltip.setFont(tooltip.getFont().deriveFont(Font.ITALIC));
236
+        tooltip.setText("Hover over a setting to see a description, if available.");
237
+    }
238
+
239
+    /**
240
+     * Sets the content of the tooltip area to the specified text.
241
+     *
242
+     * @param text The text to be displayed
243
+     */
244
+    protected void setTooltip(final String text) {
245
+        tooltip.setFont(tooltip.getFont().deriveFont(Font.PLAIN));
246
+        tooltip.setText(text);
247
+    }
248
+
249
+    /**
250
+     * Iterates and recurses over the specified components, adding mouse
251
+     * listeners to any {@link JComponent} with a tooltip set.
252
+     *
253
+     * @param components The components to iterate over
254
+     */
255
+    protected void addMouseListeners(final Component[] components) {
256
+        for (Component component : components) {
257
+            if (component instanceof JComponent && ((JComponent) component).getToolTipText() != null) {
258
+                tooltips.put(component, ((JComponent) component).getToolTipText());
259
+                ((JComponent) component).setToolTipText(null);
260
+
261
+                component.addMouseListener(this);
262
+            }
263
+
264
+            if (component instanceof Container) {
265
+                addMouseListeners(((Container) component).getComponents());
266
+            }
267
+        }
268
+    }
269
+
196 270
     /**
197 271
      * Initialises and adds a component to a panel.
198 272
      *
@@ -203,7 +277,10 @@ public final class SwingPreferencesDialog extends StandardDialog implements
203 277
             final PreferencesSetting setting) {
204 278
 
205 279
         final JLabel label = getLabel(setting);
280
+        
206 281
         JComponent option = PrefsComponentFactory.getComponent(setting);
282
+        option.setToolTipText(setting.getHelptext());
283
+
207 284
         if (option instanceof DurationDisplay) {
208 285
             ((DurationDisplay) option).setWindow(this);
209 286
         } else if (option instanceof ColourChooser) {
@@ -213,7 +290,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
213 290
         }
214 291
         components.put(setting, option);
215 292
 
216
-        categories.get(category).add(label, "align label");
293
+        categories.get(category).add(label, "align label, growx");
217 294
 
218 295
         label.setLabelFor(option);
219 296
         categories.get(category).add(option, "growx, w 70%");
@@ -280,6 +357,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
280 357
 
281 358
         final JScrollPane scrollPane = new JScrollPane(panel);
282 359
         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
360
+
283 361
         mainPanel.add(scrollPane, path);
284 362
         ((DefaultTreeModel) tabList.getModel()).insertNodeInto(newNode,
285 363
                 parentNode, parentNode.getChildCount());
@@ -389,6 +467,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
389 467
 
390 468
         selected = paths.get(path);
391 469
         selected.fireCategorySelected();
470
+        title.setText(selected.getTitle());
392 471
     }
393 472
 
394 473
     /** {@inheritDoc} */
@@ -451,4 +530,34 @@ public final class SwingPreferencesDialog extends StandardDialog implements
451 530
             me = null;
452 531
         }
453 532
     }
533
+
534
+    /** {@inheritDoc} */
535
+    @Override
536
+    public void mouseClicked(final MouseEvent e) {
537
+        // Not used
538
+    }
539
+
540
+    /** {@inheritDoc} */
541
+    @Override
542
+    public void mousePressed(final MouseEvent e) {
543
+        // Not used
544
+    }
545
+
546
+    /** {@inheritDoc} */
547
+    @Override
548
+    public void mouseReleased(final MouseEvent e) {
549
+        // Not used
550
+    }
551
+
552
+    /** {@inheritDoc} */
553
+    @Override
554
+    public void mouseEntered(final MouseEvent e) {
555
+        setTooltip(tooltips.get(e.getComponent()));
556
+    }
557
+
558
+    /** {@inheritDoc} */
559
+    @Override
560
+    public void mouseExited(final MouseEvent e) {
561
+        resetTooltip();
562
+    }
454 563
 }

Loading…
İptal
Kaydet