Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package timeflow.app.ui.filter;
 
import timeflow.util.*;
 
import javax.swing.*;
 
import timeflow.data.db.*;
import timeflow.data.db.filter.*;
import timeflow.model.ModelPanel;
 
import java.awt.*;
import java.awt.event.*;
 
public class FilterCategoryPanel extends FilterDefinitionPanel 
{
   public JList dataList=new JList();
   Field field;
   
   public FilterCategoryPanel(final Field field, final ModelPanel parent)
   {
       this(field.getName(), field, parent);
   }
   
   public FilterCategoryPanel(String title, final Field field, final ModelPanel parent)
   {
       this.field=field;
       setLayout(new BorderLayout());
       setBackground(Color.white);
       setBorder(BorderFactory.createEmptyBorder(0,5,0,5));
 
       add(new FilterTitle(title, field, parent, true), BorderLayout.NORTH);
       
       
       JScrollPane scroller=new JScrollPane(dataList);
       scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
       scroller.setBorder(null);
       add(scroller, BorderLayout.CENTER);
       dataList.setForeground(Color.darkGray);
       dataList.setSelectionForeground(Color.black);
       dataList.setSelectionBackground(new Color(220,235,255));
       dataList.setFont(parent.getModel().getDisplay().small());
       scroller.getVerticalScrollBar().setBackground(Color.white);
       
       
       // ok, the following is ugly code to insert a new mouselistener
       // that lets the user deselect items when they are clicked.
       // i tried a bunch of stuff but this is all that would work--
       // and searching the web yielded only solutions similar to this.
       // also, there's a weird dance with consuming/not consuming events
       // that is designed to allow a certain kind of multi-selection behavior
       // with the mouse, while letting you scroll through items one at a time
       // with the keyboard. this was the product of a long series of
       // conversations with target users.
       MouseListener[] old = dataList.getMouseListeners();
       for (MouseListener m: old)
          dataList.removeMouseListener(m);
       
       dataList.addMouseListener(new MouseAdapter()
       {
          public void mousePressed(MouseEvent e)
          {
             if (e.isControlDown() || e.isMetaDown() || e.isShiftDown())
                 return;
             final int index = dataList.locationToIndex(e.getPoint());
             if (dataList.isSelectedIndex(index))
             {
                SwingUtilities.invokeLater(new Runnable()
                {
                   public void run()
                   {
                      dataList.removeSelectionInterval(index, index);
                     
                   }
                });
                e.consume();
             }
             else
             {
                 SwingUtilities.invokeLater(new Runnable()
                    {
                       public void run()
                       {
                           dataList.addSelectionInterval(index, index);
                         
                       }
                    });                  
                 e.consume();
             }
          }
       });
 
       for (MouseListener m: old)
          dataList.addMouseListener(m);
       
       dataList.setCellRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(JList list,
                   Object value, int index, boolean isSelected,
                   boolean cellHasFocus) {
               Component c=super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (field==parent.getModel().getColorField())
               {
                   String text=value.toString();
                   int n=text.lastIndexOf('-');
                   if (n>1)
                       text=text.substring(0,n-1);
                   c.setForeground(parent.getModel().getDisplay().makeColor(text));
               }
               return c;
           }});
           
   }
 
   public void setData(Bag<String> data)
   {
       dataList.removeAll();
       java.util.List<String> items=data.list();
       String[] s=(String[])items.toArray(new String[0]);
       for (int i=0; i<s.length; i++)
       {
           int num=data.num(s[i]);
           if (s[i]==null || s[i].length()==0)
               s[i]="(missing)";
           s[i]+=" - "+num;
       }
       dataList.setListData(s);
   }
   
   public Dimension getPreferredSize()
   {
       return new Dimension(200,200);
   }
 
   @Override
   public ActFilter defineFilter() {
       Object[] o=dataList.getSelectedValues();    
       if (o==null || o.length==0)
           return null;
       
       int n=o.length;
       String[] s=new String[n];
       for (int i=0; i<n; i++)
       {
           String w=(String)o[i];
           int m=w.lastIndexOf('-');
           s[i]=w.substring(0, m-1);
           if ("(missing)".equals(s[i]))
               s[i]="";
       }
       
       if (s.length==1)
           return new FieldValueFilter(field, s[0]);
       FieldValueSetFilter f=new FieldValueSetFilter(field);
       for (int i=0; i<s.length; i++)
           f.addValue(s[i]);
       return f;
   }
 
   @Override
   public void clearFilter() {
       dataList.clearSelection();
   }
   
}