View Javadoc

1   package com.melloware.jukes.gui.view.component;
2   
3   import java.awt.Component;
4   
5   import javax.swing.Icon;
6   import javax.swing.JLabel;
7   import javax.swing.JList;
8   import javax.swing.ListCellRenderer;
9   
10  import com.jgoodies.validation.Severity;
11  import com.jgoodies.validation.view.ValidationResultViewFactory;
12  import com.melloware.jukes.util.JukesValidationMessage;
13  
14  /**
15   * Custom ListCellRenderer used to display Icons in JList messages from a
16   * JukesValidationMessage.
17   * <p>
18   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
19   * @author Emil A. Lefkof III <info@melloware.com>
20   * @version 4.0
21   */
22  public final class MessageCellRenderer
23      extends JLabel
24      implements ListCellRenderer {
25  
26      /**
27       * Default Constructor
28       */
29      public MessageCellRenderer() {
30          super();
31      }
32  
33      /**
34       * Constructor that take the display string.
35       * <p>
36       * @param aText the String to display
37       */
38      public MessageCellRenderer(final String aText) {
39          super(aText);
40      }
41  
42      /**
43       * Constructor that take the display string.
44       * <p>
45       * @param aText the String to display
46       * @param aIcon the Icon to display with this label
47       */
48      public MessageCellRenderer(final String aText, final Icon aIcon) {
49          super(aText);
50          this.setIcon(aIcon);
51      }
52  
53      /**
54       * Returns the warn icon for warns, the error icon for errors
55       * and <code>null</code> otherwise.
56       *
57       * @param severity   the severity used to lookup the icon
58       * @return the warn icon for warns, error icon for errors,
59       *     <code>null</code> otherwise
60       */
61      public static Icon getIcon(final Severity severity) {
62          if (severity == Severity.ERROR) {
63              return ValidationResultViewFactory.getErrorIcon();
64          } else if (severity == Severity.WARNING) {
65              return ValidationResultViewFactory.getWarningIcon();
66          } else if (severity == Severity.OK) {
67              return ValidationResultViewFactory.getCheckIcon();
68          } else {
69              return null;
70          }
71      }
72  
73      /* (non-Javadoc)
74       * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean,
75       * boolean)
76       */
77      public Component getListCellRendererComponent(final JList list,
78                                                    final Object value,
79                                                    final int index,
80                                                    final boolean isSelected,
81                                                    final boolean cellHasFocus) {
82  
83          final JukesValidationMessage message = (JukesValidationMessage)value;
84          this.setIcon(MessageCellRenderer.getIcon(message.severity()));
85          this.setText(message.formattedText());
86          this.setToolTipText(message.getToolTip());
87          if (isSelected) {
88              setBackground(list.getSelectionBackground());
89              setForeground(list.getSelectionForeground());
90          } else {
91              setBackground(list.getBackground());
92              setForeground(list.getForeground());
93          }
94          setEnabled(list.isEnabled());
95          setFont(list.getFont());
96          setOpaque(true);
97          return this;
98      }
99  
100 }