View Javadoc

1   package org.catacomb.druid.gui.edit;
2   
3   import org.catacomb.druid.event.LabelActor;
4   import org.catacomb.druid.gui.base.DruListCellRenderer;
5   import org.catacomb.druid.gui.base.DruListClickActor;
6   import org.catacomb.druid.swing.DList;
7   import org.catacomb.druid.util.ListDisplay;
8   import org.catacomb.interlish.content.KeyedList;
9   import org.catacomb.interlish.interact.ClickListener;
10  import org.catacomb.interlish.structure.IDd;
11  import org.catacomb.interlish.structure.List;
12  import org.catacomb.interlish.structure.ListWatcher;
13  import org.catacomb.report.E;
14  import org.catacomb.util.ArrayUtil;
15  
16  
17  import java.awt.Color;
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  
21  
22  public class DruListPanel extends DruGCPanel
23      implements ListDisplay, LabelActor, ClickListener, ListWatcher, List {
24  
25      static final long serialVersionUID = 1001;
26  
27      public static final int NORMAL_ORDER = 100;
28      public static final int REVERSE_ORDER = 101;
29      int order = NORMAL_ORDER;
30  
31  
32      int nrow;
33  
34      DList dList;
35  
36      Object[] items;
37      String[] tooltips;
38  
39      ArrayList<DruListClickActor> clickActors;
40  
41      KeyedList<? extends IDd> targetKL;
42  
43      boolean multiple = false;
44  
45  
46      public DruListPanel() {
47          this(10);
48      }
49  
50  
51      public DruListPanel(int nr) {
52          super();
53          nrow = nr;
54  
55          dList = new DList();
56          addSingleDComponent(dList);
57  
58          Object[] sa = new String[0];
59          dList.setListData(sa);
60  
61          dList.setLabelActor(this);
62      }
63  
64  
65  
66      public void setItems(Object[] sain) {
67          Object[] sa = sain;
68          if (sa == null) {
69              sa = new String[0];
70          }
71          items = sa;
72          dList.setListData(sa);
73          dList.clearBufferedSelection();
74      }
75  
76  
77      public void setItems(ArrayList<? extends Object> obal) {
78          HashSet<Object> oldsels = new HashSet<Object>();
79          for (int i : getSelectedIndexes()) {
80              oldsels.add(items[i]);
81          }
82  
83          Object[] obar = new Object[obal.size()];
84          String[] ott = new String[obal.size()];
85  
86          int iel = 0;
87          for (Object elt : obal) {
88              if (elt instanceof String[]) {
89                  String[] sp = (String[])elt;
90                  obar[iel] = sp[0];
91                  if (sp.length > 1) {
92                      ott[iel] = sp[1]; // tool tip;
93                  }
94  
95              } else {
96                  obar[iel] = elt;
97              }
98              iel += 1;
99          }
100 
101         items = obar;
102         tooltips = ott;
103         dList.setListData(obar);
104         dList.setTooltips(tooltips);
105         /*
106          * E.missing("dlist needs the raw items for the renderer");
107          *
108          * if (oba != null) { String[] sa = new String[oba.size()]; int ic = 0;
109          * for (Object ob : oba) { sa[ic++] = ob.toString(); }
110          * dList.setListData(sa); }
111          */
112 
113 
114         for (int i = 0; i < items.length; i++) {
115             if (oldsels.contains(items[i])) {
116                 dList.selectAt(i);
117             }
118         }
119     }
120 
121     public void setMultiple() {
122         multiple = true;
123         dList.setMultiple();
124     }
125 
126     public void setBg(Color c) {
127         setEtchedBorder(c);
128         // dList.setBackground(c);
129         super.setBg(c);
130     }
131 
132 
133     public void setCellRenderer(DruListCellRenderer obj) {
134         dList.setCellRenderer(obj.getGUIPeer());
135     }
136 
137 
138 
139     public void updateDisplay() {
140         // EFF
141         dList.repaint();
142     }
143 
144 
145     public Object getSelectedItem() {
146         return dList.getSelectedValue();
147     }
148 
149     public int getSelectedIndex() {
150         return dList.getSelectedIndex();
151     }
152 
153     public String getSelectedName() {
154         return "" + getSelectedItem();
155     }
156 
157 
158     public void labelAction(String s, boolean b) {
159         if (s.equals("selected")) {
160             String ssel = getSelectedName();
161 
162             //       E.info("list reporting vc ");
163 
164             valueChange(ssel);
165         }
166     }
167 
168 
169     public void clear() {
170         dList.setListData(new String[0]);
171     }
172 
173 
174     public void selectAt(int i) {
175         dList.selectAt(i);
176     }
177 
178     public void setSelectedItem(Object obj) {
179         setSelected(obj);
180     }
181 
182     public void setSelected(Object obj) {
183         // NB normally the items are the string ids, not hte things themselves if ther is a kl
184         /*
185         if (obj instanceof String && targetKL != null) {
186            if (targetKL.hasItem((String)obj)) {
187 
188               obj = targetKL.get((String)obj);
189            }
190         }
191         */
192 
193 
194         int isel = -1;
195         if (obj != null) {
196             for (int i = 0; i < items.length; i++) {
197                 if (obj.equals(items[i])) {
198                     isel = i;
199                     break;
200                 }
201             }
202             if (isel < 0) {
203                 E.warning(" not in list " + obj);
204             }
205         }
206 
207         dList.selectAt(isel);
208     }
209 
210 
211     public void addClickAction(DruListClickActor actor) {
212         if (clickActors == null) {
213             clickActors = new ArrayList<DruListClickActor>();
214             dList.setClickListener(this);
215         }
216         clickActors.add(actor);
217     }
218 
219 
220     public void pointClicked(int x, int y, int b) {
221         if (clickActors != null) {
222             for (DruListClickActor ca : clickActors) {
223                 ca.clicked(x, this);
224             }
225         }
226 
227     }
228 
229 
230     public void setKeyedList(KeyedList<? extends IDd> idkl) {
231         if (targetKL != null) {
232             targetKL.removeListWatcher(this);
233         }
234         targetKL = idkl;
235         targetKL.addListWatcher(this);
236         syncFromKeyedList();
237     }
238 
239 
240     private void syncFromKeyedList() {
241         Object osel = getSelectedItem();
242 
243 
244         ArrayList<String> als = targetKL.getKeys();
245         String[] sa = als.toArray(new String[0]);
246 
247         if (order == REVERSE_ORDER) {
248             sa = ArrayUtil.reverseStringArray(sa);
249         } else {
250         }
251         setItems(sa);
252 
253         if (osel != null) {
254             setSelectedItem(osel);
255         } else {
256             setSelectedItem(null);
257         }
258     }
259 
260 
261     public void listChanged(Object src) {
262         if (targetKL != null) {
263             syncFromKeyedList();
264         }
265     }
266 
267 
268     public void addLine() {
269         dList.addLine();
270     }
271 
272 
273     public void removeLine() {
274         dList.removeLine();
275     }
276 
277 
278     public void ensureHasSelection() {
279         dList.ensureHasSelection();
280     }
281 
282 
283     public boolean isSelectionEmpty() {
284         return dList.isSelectionEmpty();
285     }
286 
287 
288     public int listSize() {
289         return dList.listSize();
290     }
291 
292 
293 
294     public void able(boolean b) {
295         dList.setEnabled(b);
296     }
297 
298 
299     public void setOrder(int ord) {
300         order = ord;
301     }
302 
303 
304     public int[] getSelectedIndexes() {
305         return dList.getSelectedIndices();
306     }
307 
308 
309 
310     /*
311      *
312      * public Dimension getPreferredScrollableViewportSize() { return
313      * getPreferredSize(); }
314      *
315      * public int getScrollableBlockIncrement(Rectangle visibleRect, int
316      * orientation, int direction) { return 10; }
317      *
318      * public boolean getScrollableTracksViewportHeight() { return false; }
319      *
320      * public boolean getScrollableTracksViewportWidth() { return true; }
321      *
322      * public int getScrollableUnitIncrement(Rectangle visibleRect, int
323      * orientation, int direction) { return 10; }
324      */
325 
326 
327 }