KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > windows > ScriptedListDialog


1 package rero.gui.windows;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5 import javax.swing.table.*;
6
7 import java.awt.*;
8 import java.awt.event.*;
9
10 import java.util.*;
11 import rero.util.*;
12
13 import contrib.javapro.*; // sorted JTable code...
14

15 import rero.ircfw.interfaces.*;
16
17 import rero.config.*;
18 import rero.client.*;
19
20 import rero.gui.*;
21 import rero.gui.windows.*;
22 import rero.gui.toolkit.*;
23
24 import text.*;
25
26 import rero.dcc.*;
27 import rero.client.output.*;
28 import rero.util.*;
29
30 import sleep.bridges.*;
31 import sleep.runtime.*;
32 import rero.script.*;
33
34 public class ScriptedListDialog extends GeneralListDialog
35 {
36    public ScriptedListDialog(String JavaDoc title, String JavaDoc hook, Object JavaDoc data, LinkedList cols)
37    {
38       super(title, hook, new ScriptedListModel(cols, data));
39    }
40
41    public void refreshData()
42    {
43       model.fireTableDataChanged();
44    }
45
46    public void init()
47    {
48       ((ScriptedListModel)model).install(popupHook, capabilities.getScriptCore());
49    }
50
51    public static class ScriptedCompare implements Comparator
52    {
53       private int col;
54       private boolean rev;
55
56       public ScriptedCompare(int column, boolean reverse)
57       {
58          rev = reverse;
59          col = column;
60       }
61
62       public int compare(Object JavaDoc a, Object JavaDoc b)
63       {
64          if (rev)
65          {
66             Object JavaDoc c = b;
67             b = a;
68             a = c;
69          }
70
71          String JavaDoc[] sa = a.toString().toLowerCase().split("\t");
72          String JavaDoc[] sb = b.toString().toLowerCase().split("\t");
73          
74          try
75          {
76             int na = Integer.parseInt(sa[col]);
77             int nb = Integer.parseInt(sb[col]);
78
79             return na - nb;
80          }
81          catch (Exception JavaDoc ex) { }
82        
83          return sa[col].compareTo(sb[col]);
84       }
85    }
86
87    private static class ScriptedListModel extends GeneralListModel
88    {
89       private Scalar data;
90       private LinkedList cols;
91       private ScriptCore script;
92       private String JavaDoc popupHook;
93
94       public void install(String JavaDoc popup, ScriptCore s) { script = s; popupHook = popup; }
95
96       public ScriptedListModel(LinkedList headers, Object JavaDoc scalar)
97       {
98          cols = headers;
99
100          data = (Scalar)scalar;
101       }
102
103       public void sortColumn(int col, boolean ascending)
104       {
105 /* String function = "&" + popupHook + "_sort";
106
107          Stack temp = new Stack();
108          temp.push(SleepUtils.getScalar((ascending ? 1 : 0)));
109          temp.push(SleepUtils.getScalar(col));
110
111          script.callFunction(function, temp); */

112
113          data.getArray().sort(new ScriptedCompare(col, ascending));
114          fireTableDataChanged();
115       }
116
117       public HashMap getEventHashMap(int row)
118       {
119          return ClientUtils.getEventHashMap(row+"", "");
120       }
121
122       public int getRowCount()
123       {
124          return data.getArray().size();
125       }
126
127       public int getColumnCount()
128       {
129          return cols.size();
130       }
131
132       public int getColumnWidth(int col)
133       {
134          return (int)(TextSource.fontMetrics.stringWidth(cols.get(col).toString()) * 1.5);
135       }
136
137       public String JavaDoc getColumnName(int col)
138       {
139          return cols.get(col).toString();
140       }
141
142       public Object JavaDoc getValueAt(int row, int col)
143       {
144          if (row < getRowCount() && col < getColumnCount())
145          {
146             String JavaDoc temp = data.getArray().getAt(row).toString();
147             String JavaDoc blah[] = temp.split("\t");
148
149             if (col < blah.length)
150             {
151                AttributedString tempa = AttributedString.CreateAttributedString(blah[col]);
152                tempa.assignWidths();
153  
154                return tempa;
155             }
156          }
157
158          return null;
159       }
160
161       public boolean isSortable(int col)
162       {
163          return true;
164       }
165    }
166 }
167
Popular Tags