View Javadoc

1   package org.catacomb.druid.swing;
2   
3   import java.awt.Color;
4   import java.awt.Dimension;
5   import java.awt.Font;
6   import java.awt.event.MouseEvent;
7   import java.awt.event.MouseListener;
8   import java.util.HashSet;
9   
10  import javax.swing.DefaultListSelectionModel;
11  import javax.swing.JList;
12  import javax.swing.event.ListSelectionEvent;
13  import javax.swing.event.ListSelectionListener;
14  
15  import org.catacomb.druid.event.LabelActor;
16  import org.catacomb.interlish.interact.ClickListener;
17  import org.catacomb.interlish.interact.DComponent;
18  import org.catacomb.util.MouseUtil;
19  
20  
21  public class DList extends JList implements DComponent, ListSelectionListener, MouseListener {
22  
23      static final long serialVersionUID = 1001;
24      static Font plainfont;
25      static Font boldfont;
26  
27      public LabelActor labelActor;
28      Object bufSel;
29  
30      boolean dropEvents;
31      ClickListener clickListener;
32      String[] tooltips;
33      int nline = 3;
34      boolean multiple = false;
35  
36      HashSet<Object> siHS = new HashSet<Object>();
37  
38  
39      public DList() {
40          super();
41          dropEvents = false;
42          setPlainFont();
43          addListSelectionListener(this);
44      }
45  
46  
47      public void setTooltip(String s) {
48          setToolTipText(s);
49      }
50  
51      public void setBg(Color c) {
52          setBackground(c);
53      }
54  
55  
56      public void clearBufferedSelection() {
57          bufSel = null;
58      }
59  
60  
61      public void setMultiple() {
62          multiple = true;
63          super.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
64      }
65  
66  
67  
68      public void setTooltips(String[] sa) {
69          tooltips = sa;
70      }
71  
72  
73      public void setPlainFont() {
74          if (plainfont == null) {
75              plainfont = new Font("sansserif", Font.PLAIN, 12);
76          }
77  
78          setFont(plainfont);
79      }
80  
81  
82      public String getToolTipText(MouseEvent me) {
83          int idx = locationToIndex(me.getPoint());
84          String ret = null;
85          if (idx >= 0 && tooltips != null && tooltips.length > idx) {
86              ret = tooltips[idx];
87          }
88          if (ret == null) {
89              ret = "";
90          }
91          return ret;
92      }
93  
94  
95  
96      public void setLabelActor(LabelActor la) {
97          labelActor = la;
98      }
99  
100 
101     public void valueChanged(ListSelectionEvent lse) {
102         if (dropEvents) {
103 
104         } else if (lse != null && lse.getValueIsAdjusting()) {
105 
106         } else {
107             if (multiple) {
108                 Object onew = null;
109                 Object ofirst = null;
110                 for (Object obj : getSelectedValues()) {
111                     if (siHS.contains(obj)) {
112                         if (ofirst == null) {
113                             ofirst = obj;
114                         }
115                     } else {
116                         if (onew == null) {
117                             onew = obj;
118                         }
119                     }
120                 }
121                 siHS.clear();
122                 for (Object obj : getSelectedValues()) {
123                     siHS.add(obj);
124                 }
125                 if (onew != null) {
126                     bufSel = onew;
127                     labelActor.labelAction("selected", true);
128                 } else if (ofirst != null) {
129                     bufSel = ofirst;
130                     labelActor.labelAction("selected", true);
131                 } else {
132                     bufSel = null;
133                     labelActor.labelAction("selected", false);
134                 }
135 
136 
137             } else {
138                 Object obj = getSelectedValue();
139                 //   E.info("sel " + obj + " " + bufSel + " " + (obj == bufSel));
140                 if (obj != null && obj != bufSel && labelActor != null) {
141                     bufSel = obj;
142                     labelActor.labelAction("selected", true);
143 
144                 }
145             }
146         }
147 
148     }
149 
150 
151     public void selectAt(int i) {
152         dropEvents = true;
153         if (multiple) {
154             addSelectionInterval(i, i);
155 
156         } else {
157             if (i >= 0) {
158                 setSelectedIndex(i);
159             } else {
160                 bufSel = null;
161                 clearSelection();
162             }
163         }
164         dropEvents = false;
165     }
166 
167 
168     public void setClickListener(ClickListener cl) {
169         clickListener = cl;
170         addMouseListener(this);
171     }
172 
173 
174     public void mouseClicked(MouseEvent e) {
175         if (clickListener != null) {
176             clickListener.pointClicked(e.getX(), e.getY(), MouseUtil.getButton(e));
177         }
178 
179     }
180 
181 
182     public void mousePressed(MouseEvent e) {
183 
184     }
185 
186 
187     public void mouseReleased(MouseEvent e) {
188     }
189 
190 
191     public void mouseEntered(MouseEvent e) {
192     }
193 
194 
195     public void mouseExited(MouseEvent e) {
196     }
197 
198 
199     public void addLine() {
200         nline += 1;
201         setPreferredSize(new Dimension(100, 20 * nline));
202     }
203 
204 
205     public void removeLine() {
206         if (nline > 3) {
207             nline -= 1;
208         }
209         setPreferredSize(new Dimension(100, 20 * nline));
210     }
211 
212 
213     public void ensureHasSelection() {
214         if (isSelectionEmpty() && getModel().getSize() > 0) {
215             selectAt(0);
216             valueChanged(null);
217         }
218     }
219 
220     public int listSize() {
221         return getModel().getSize();
222     }
223 
224 
225     public void setToggleAction() {
226         setSelectionModel(new ToggleSelectionModel());
227     }
228 
229 
230     /*
231      *
232      * public void setBoldFont() { if (boldfont == null) { boldfont = new Font
233      * ("sansserif", Font.BOLD, 12); } setFont(boldfont); }
234      */
235 }