View Javadoc

1   
2   package org.catacomb.druid.swing;
3   
4   import org.catacomb.druid.event.LabelActor;
5   
6   import java.awt.BorderLayout;
7   import java.awt.Color;
8   import java.awt.Component;
9   import java.awt.Dimension;
10  import java.util.ArrayList;
11  
12  import javax.swing.JMenu;
13  import javax.swing.JPanel;
14  import javax.swing.JPopupMenu;
15  import javax.swing.MenuElement;
16  
17  
18  
19  public class DPopLabel extends JPanel implements LabelActor {
20  
21      private static final String SHOW_MENU = "showMenu";
22  
23      static final long serialVersionUID = 1001;
24  
25      int type = 0;
26      boolean fixedSize = false;
27  
28      public final static int SIMPLE = 0;
29      public final static int CHECKBOX = 1;
30  
31      JPopupMenu mymenu;
32      DButton popB;
33  
34  
35      String latestChoice;
36  
37      LabelActor ml;
38  
39      Menued menued;
40      String[] options;
41  
42  
43  
44      public DPopLabel(String s) {
45          this(s, s);
46      }
47  
48      public DPopLabel(String s, String itxt) {
49          popB = new DButton(itxt);
50          popB.setActionCommand(SHOW_MENU);
51  
52          mymenu = new JPopupMenu();
53          add(mymenu);
54          popB.setLabelActor(this);
55  
56          setLayout(new BorderLayout());
57          add("Center", popB);
58      }
59  
60  
61  
62      public void setBg(Color c) {
63          popB.setBg(c);
64          setBackground(c);
65      }
66  
67  
68      public void clear() {
69          // popB.remove(mymenu);
70          // mymenu = new JPopupMenu();
71          // popB.add(mymenu);
72  
73          mymenu.removeAll();
74      }
75  
76  
77      public void setLabel(String s) {
78          popB.setText(s);
79      }
80  
81  
82      public void setEnabled(boolean b) {
83          popB.setEnabled(b);
84          mymenu.setEnabled(b);
85      }
86  
87  
88      public void setLabelActor(LabelActor ml) {
89          this.ml = ml;
90      }
91  
92  
93      public void setFixedSize(boolean b) {
94          fixedSize = b;
95      }
96  
97  
98      public Dimension getPreferredSize() {
99          if (fixedSize) {
100             return new Dimension(60, 20);
101         } else {
102             return popB.getPreferredSize();
103         }
104     }
105 
106 
107     public void setType(int ityp) {
108         type = ityp;
109     }
110 
111 
112     public void setText(String sin) {
113         String s = sin;
114         if (s == null)
115             s = "";
116         if (!s.equals(popB.getText())) {
117             popB.setText(s);
118             popB.invalidate();
119             validate();
120         }
121     }
122 
123 
124     public String getText() {
125         return popB.getText();
126     }
127 
128 
129     public String getLabel() {
130         return popB.getText();
131     }
132 
133 
134     public void myShowMenu() {
135 
136         if (menued != null) {
137             String[] sa = menued.getMenuOptions();
138             if (sa != null && sa != options) {
139                 setMenuOptions(sa);
140             }
141         }
142 
143         checkNames();
144         mymenu.show(this, 0, 25);
145     }
146 
147 
148     public void showMenu() {
149         myShowMenu();
150     }
151 
152 
153     public void hideMenu() {
154         mymenu.setVisible(false);
155     }
156 
157 
158 
159     public void armSelection(String s) {
160         Component[] cs = mymenu.getComponents();
161         for (int i = 0; i < cs.length; i++) {
162             if (cs[i] instanceof DMenuItem) {
163                 DMenuItem cmi = (DMenuItem)cs[i];
164                 if (cmi.actionCommand.equals(s)) {
165                     cmi.setArmed(true);
166                 } else {
167                     cmi.setArmed(false);
168                 }
169             }
170         }
171 
172     }
173 
174 
175     public void repaintMenu() {
176         mymenu.repaint();
177     }
178 
179 
180 
181     public void labelAction(String sarg, boolean b) {
182 
183         if (sarg.equals(SHOW_MENU)) {
184             myShowMenu();
185 
186         } else {
187             deliverLabelAction(sarg, b);
188         }
189     }
190 
191 
192 
193     public void deliverLabelAction(String sarg, boolean b) {
194         latestChoice = sarg;
195         if (ml != null)
196             ml.labelAction(sarg, b);
197     }
198 
199 
200 
201 
202     public void checkNames() {
203 
204 
205         MenuElement[] jme = mymenu.getSubElements();
206         for (int i = 0; i < jme.length; i++) {
207             Object ob = jme[i];
208             if (ob instanceof DMenuItem) {
209                 ((DMenuItem)ob).checkName();
210             }
211         }
212     }
213 
214 
215     public String[] getNames() {
216         MenuElement[] jme = mymenu.getSubElements();
217         String[] sa = new String[jme.length];
218         for (int i = 0; i < jme.length; i++) {
219             Object ob = jme[i];
220             if (ob instanceof DMenuItem) {
221                 sa[i] = ((DMenuItem)(mymenu.getComponent(i))).getText();
222             }
223         }
224         return sa;
225     }
226 
227 
228     public boolean[] getStates() {
229         MenuElement[] jme = mymenu.getSubElements();
230         boolean[] ba = new boolean[jme.length];
231         for (int i = 0; i < jme.length; i++) {
232             Object mi = jme[i];
233             ba[i] = false;
234             if (mi instanceof DCheckboxMenuItem) {
235                 DCheckboxMenuItem tmi = (DCheckboxMenuItem)mi;
236                 ba[i] = tmi.getState();
237             }
238         }
239         return ba;
240     }
241 
242 
243     public void setStates(String[] onm, boolean ob[]) {
244         MenuElement[] jme = mymenu.getSubElements();
245         for (int i = 0; i < jme.length; i++) {
246             Object mi = jme[i];
247             if (mi instanceof DCheckboxMenuItem) {
248                 DCheckboxMenuItem tmi = (DCheckboxMenuItem)mi;
249                 String s = tmi.getText();
250                 for (int k = 0; k < onm.length; k++) {
251                     if (s.equals(onm[k])) {
252                         tmi.setState(ob[k]);
253                     }
254                 }
255             }
256         }
257     }
258 
259 
260     public void setState(String s, boolean b) {
261         MenuElement[] jme = mymenu.getSubElements();
262         for (int i = 0; i < jme.length; i++) {
263             Object mi = jme[i];
264             if (mi instanceof DCheckboxMenuItem) {
265                 DCheckboxMenuItem tmi = (DCheckboxMenuItem)mi;
266                 String sm = tmi.getText();
267                 if (sm.equals(s))
268                     tmi.setState(b);
269             }
270         }
271 
272     }
273 
274 
275     public void addItem(String s) {
276         addItem(s, false);
277     }
278 
279 
280     public void addItem(String s, boolean b) {
281         if (type == SIMPLE) {
282             DMenuItem tmi = new DMenuItem(s);
283             // tmi.setLabelActor(this);
284             mymenu.add(tmi);
285 
286         } else if (type == CHECKBOX) {
287             DCheckboxMenuItem tmi = new DCheckboxMenuItem(s, this, b);
288             // tmi.setLabelActor(this);
289             mymenu.add(tmi);
290         }
291     }
292 
293 
294     public void addItems(String[] sa) {
295         if (sa == null)
296             return;
297         for (int i = 0; i < sa.length; i++)
298             addItem(sa[i]);
299     }
300 
301 
302     public void addCheckboxItem(String s, boolean b) {
303         DCheckboxMenuItem tmi = new DCheckboxMenuItem(s, this, b);
304         tmi.setLabelActor(this);
305         tmi.setActionCommand(s);
306         // tmi.addActionListener (this);
307         // tmi.addItemListener (this);
308         mymenu.add(tmi);
309     }
310 
311 
312     public void addSeparator() {
313         mymenu.addSeparator();
314     }
315 
316 
317     public void removeAll() {
318         options = null;
319         mymenu.removeAll();
320     }
321 
322 
323     public void addHierarchical(String[] sain, boolean b) {
324         String[] sa = sain;
325         if (sa == null)
326             return;
327         if (b)
328             sa = myStringSort(sa);
329         addHierarchical(sa);
330     }
331 
332 
333     public void addOptions(ArrayList<String> v) {
334         String[] sa = new String[v.size()];
335         int iin = 0;
336         for (String s : v) {
337             sa[iin++] = s;
338         }
339         addMenuOptions(sa);
340     }
341 
342 
343     public void addMenuOptions(String[] sa) {
344         addHierarchical(sa);
345     }
346 
347 
348     public void setMenued(Menued md) {
349         menued = md;
350         setMenuOptions(menued.getMenuOptions());
351     }
352 
353 
354     public void setOptions(ArrayList<String> v) {
355         setMenuOptions(v);
356     }
357 
358 
359     public void setMenuOptions(ArrayList<String> v) {
360         String[] sa = new String[v.size()];
361         int iin = 0;
362         for (String s : v) {
363             sa[iin++] = s;
364         }
365         setMenuOptions(sa);
366     }
367 
368 
369 
370     public void setMenuOptions(String[] sa) {
371         removeAll();
372         options = sa;
373         addHierarchical(sa);
374     }
375 
376 
377     public void addHierarchical(String[] sa) {
378         growMenu(mymenu, "", sa, 0);
379     }
380 
381 
382     private int growMenu(Object tpm, String schop, String[] sa, int i0) {
383         int nend = sa.length;
384         int nchop = schop.length();
385         int nn = 0;
386         while (i0 + nn < nend && sa[i0 + nn] != null && sa[i0 + nn].startsWith(schop)) {
387             String ss = sa[i0 + nn];
388             String srest = ss.substring(nchop, ss.length());
389             int inxt = srest.indexOf(":");
390             if (inxt < 0)
391                 inxt = srest.indexOf("/");
392 
393             if (inxt > 1) {
394                 JMenu ttpm = new JMenu(srest.substring(0, inxt));
395 
396                 if (tpm instanceof JPopupMenu)
397                     ((JPopupMenu)tpm).add(ttpm);
398                 if (tpm instanceof JMenu)
399                     ((JMenu)tpm).add(ttpm);
400 
401 
402                 nn += growMenu(ttpm, schop + srest.substring(0, inxt + 1), sa, i0 + nn);
403             } else {
404                 if (srest.equals("SEPARATOR")) {
405                     if (tpm instanceof JPopupMenu) {
406                         ((JPopupMenu)tpm).addSeparator();
407                     } else if (tpm instanceof JMenu) {
408                         ((JMenu)tpm).addSeparator();
409                     }
410 
411                 } else {
412                     DMenuItem tmi = new DMenuItem(srest);
413                     tmi.setActionCommand(schop + srest);
414                     // tmi.setLabelActor (this);
415                     if (tpm instanceof JPopupMenu)
416                         ((JPopupMenu)tpm).add(tmi);
417                     if (tpm instanceof JMenu)
418                         ((JMenu)tpm).add(tmi);
419                 }
420                 nn++;
421             }
422         }
423         return nn;
424     }
425 
426 
427 
428     public void addAlphabeticizedSubsetted(String[] sain) {
429         String[] sa = sain;
430         sa = myStringSort(sa);
431         // chop inti submenus of no more than nmax elements, giving
432         // each a title like "a-e";
433 
434         int nmin = 5;
435         int nmax = 15;
436         int ndone = 0;
437 
438         int ntot = sa.length;
439 
440         while (ndone < ntot) {
441 
442             // find how many letters need to get a change in the right place;
443             int nlet = 0;
444             boolean ok = false;
445 
446             int lstcng = 0;
447             while (!ok && nlet < 3) {
448                 nlet++;
449                 lstcng = 0;
450                 String s0 = sa[ndone].substring(0, nlet);
451 
452                 for (int i = 0; i < nmax && ndone + i < ntot; i++) {
453                     String sn = sa[ndone + i].substring(0, nlet);
454                     if (!(sn.equals(s0))) {
455                         lstcng = i - 1;
456                         s0 = sn;
457                     } else if (ndone + i == ntot - 1) {
458                         lstcng = i;
459                     }
460                 }
461                 if (ndone + lstcng == ntot - 1 || lstcng >= nmin) {
462                     ok = true;
463                 }
464             }
465 
466 
467             if (!ok) {
468                 lstcng = nmax; // couldnt find a break, just use nmax;
469                 if (ndone + lstcng >= ntot)
470                     lstcng = ntot - ndone - 1;
471             }
472 
473             String shead = (sa[ndone].substring(0, nlet) + "-" + sa[ndone + lstcng].substring(0, nlet) + ":");
474             for (int i = 0; i <= lstcng; i++) {
475                 sa[ndone + i] = shead + sa[ndone + i];
476             }
477             ndone += lstcng;
478             ndone++;
479         }
480         addHierarchical(sa);
481     }
482 
483 
484 
485     private String[] myStringSort(String[] sa) {
486         int n = sa.length;
487         String[] ss = new String[n];
488         for (int i = 0; i < n; i++) {
489             String sin = sa[i];
490             int j = 0;
491             for (; j < i && ss[j].compareTo(sin) < 0; j++)
492                 ;
493             for (int k = i; k > j; k--)
494                 ss[k] = ss[k - 1];
495             ss[j] = sin;
496         }
497         return ss;
498     }
499 }
500 
501 
502 
503 // add should just add a menu item and set its action listener to this
504 // add (string1, string2) should add an item called string1 with the
505 // action command set to string1 + " " + string2
506 
507 
508