View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2020.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss about it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.list;
12  
13  import com.jsql.view.swing.util.UiUtil;
14  
15  import javax.swing.*;
16  import javax.swing.border.AbstractBorder;
17  import java.awt.*;
18  
19  /**
20   * Item renderer for JList.
21   */
22  public class RendererComplexCell implements ListCellRenderer<ItemList> {
23      
24      /**
25       * List component renderer.
26       */
27      private static final DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
28  
29      @Override
30      public Component getListCellRendererComponent(
31          JList<? extends ItemList> list,
32          ItemList value,
33          int index,
34          boolean isSelected,
35          boolean isFocused
36      ) {
37          
38          JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, isFocused);
39  
40          renderer.setFont(UiUtil.FONT_NON_MONO);
41  
42          // setBackground
43          if (isSelected) {
44              if (list.isFocusOwner()) {
45                  renderer.setBackground(UiUtil.COLOR_FOCUS_GAINED);
46              } else {
47                  renderer.setBackground(UiUtil.COLOR_FOCUS_LOST);
48              }
49          } else {
50              renderer.setBackground(Color.WHITE);
51          }
52          
53          // setForeground
54          if (value.getIsVulnerable()) {
55              renderer.setForeground(UiUtil.COLOR_GREEN);
56          } else if (value.getIsDatabaseConfirmed()) {
57              renderer.setForeground(Color.BLUE);
58          } else {
59              renderer.setForeground(Color.BLACK);  // Hardcode black for Mac default is white
60          }
61  
62          // setBorder
63          if (isSelected) {
64              if (list.isFocusOwner()) {
65                  renderer.setBorder(UiUtil.BORDER_FOCUS_GAINED);
66              } else {
67                  renderer.setBorder(UiUtil.BORDER_FOCUS_LOST);
68              }
69          } else if (isFocused) {
70              renderer.setBorder(BorderFactory.createCompoundBorder(new BorderList(), BorderFactory.createEmptyBorder(0, 1, 0, 0)));
71          } else {
72              renderer.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
73          }
74  
75          return renderer;
76      }
77      
78          private static class BorderList extends AbstractBorder {
79          
80          @Override
81          public void paintBorder(Component comp, Graphics g, int x, int y, int w, int h) {
82              
83              Graphics2D g2D = (Graphics2D) g;
84              g2D.setColor(Color.GRAY);
85              g2D.setStroke(
86                  new BasicStroke(
87                      1,
88                      BasicStroke.CAP_BUTT,
89                      BasicStroke.JOIN_BEVEL,
90                      0,
91                      new float[]{1},
92                      0
93                  )
94              );
95              g2D.drawRect(x, y, w - 1, h - 1);
96          }
97      }
98  }