Source code: dr/davmgr/swingview/internalframes/views/UrlsTableViewModel.java
1 package dr.davmgr.swingview.internalframes.views;
2
3 import javax.swing.table.*;
4 import java.util.*;
5 import dr.davmgr.protocol.*;
6
7 public class UrlsTableViewModel
8 extends AbstractTableModel
9 {
10 Urls urls = null;
11 Vector rows = null;
12 int colcount = 0;
13
14 boolean showLocked = false;
15
16 boolean reverseOrder=false;
17 String orderColumnName=null;
18 AttributeComparator lastComparator = null;
19
20
21 Hashtable columnNames = null;
22
23 private final static String LANGBUNDLE = "dr.davmgr.swingview.internalframes.views.properties.UrlsTableViewModelLang";
24 private final static String STRUCTBUNDLE = "dr.davmgr.swingview.internalframes.views.properties.UrlsTableViewModel";
25
26 ProtocolHandler protocolHandler = null;
27 public UrlsTableViewModel(Urls urls) {
28 this.urls=urls;
29 rows=buildRows(urls);
30
31 protocolHandler=urls.getProtocolHandler();
32
33 init(urls);
34 }
35 public boolean showLocked() { return showLocked; }
36 private Vector buildRows(Urls urls, AttributeComparator comparator) {
37 List dirs=null, files=null;
38 Vector rows=new Vector();
39 if ((dirs=urls.getCollections())!=null) {
40 if (comparator!=null) Collections.sort(dirs, comparator);
41 else Collections.sort(dirs);
42 rows.addAll(dirs);
43 }
44 if ((files=urls.getFiles())!=null) {
45 if (comparator!=null) Collections.sort(files, comparator);
46 else Collections.sort(files);
47 rows.addAll(files);
48 }
49 return rows;
50 }
51 private Vector buildRows(Urls urls) {
52 return buildRows(urls, null);
53 }
54 public void removeUrls(List urlsList) {
55 ListIterator li = urlsList.listIterator();
56 for (;li.hasNext();) {
57 Urls urls = (Urls)li.next();
58 }
59 }
60 public Urls getUrls() {
61 return urls;
62 }
63 public void setUrls(Urls urls) {
64 this.urls=urls;
65 if ((lastComparator!=null)&&
66 (protocolHandler==urls.getProtocolHandler()))
67 rows=buildRows(urls, lastComparator);
68 else
69 rows=buildRows(urls);
70
71 if (protocolHandler!=urls.getProtocolHandler()) {
72 protocolHandler=urls.getProtocolHandler();
73 init(urls);
74 fireTableStructureChanged();
75 } else fireTableDataChanged();
76 }
77 Vector attributesToShow = null;
78 Hashtable attributeNames = null;
79 private void init(Urls urls) {
80 String protocol=urls.getURL().getProtocol();
81 String whatToShow;
82 if ((whatToShow=getStructRes(protocol+".show"))!=null) {
83 StringTokenizer st = new StringTokenizer(whatToShow,",");
84 attributesToShow=new Vector();
85 while (st.hasMoreTokens()) {
86 attributesToShow.add(st.nextToken());
87 }
88 } else {
89 attributesToShow =
90 new Vector(urls.getProtocolHandler().getAttributeNames());
91 }
92 colcount=attributesToShow.size();
93 attributeNames=new Hashtable();
94 columnNames = new Hashtable();
95 for (Enumeration e=attributesToShow.elements(); e.hasMoreElements();) {
96 String name=(String)e.nextElement(), realname;
97 if ((realname=getLangRes(protocol+"."+name))==null)
98 realname=name;
99 // System.err.println(name+"="+realname);
100 attributeNames.put(name, realname);
101 columnNames.put(realname, name);
102 }//for
103 if (getStructRes(protocol+".show.islocked")!=null)
104 showLocked=getStructRes(protocol+".show.islocked").toLowerCase().equals("true");
105 else showLocked=false;
106 }//init
107
108 public int getRowCount() {
109 if (rows==null) return 0;
110 return rows.size();
111 }
112 public int getColumnCount() {
113 if (showLocked) return colcount+1;
114 return colcount;
115 }
116
117 public String getColumnName(int column) {
118 if ((showLocked)&&(column==0)) return "";
119 else if (showLocked) column--;
120 return (String)attributeNames.get(attributesToShow.elementAt(column));
121 }
122
123
124 public Object getValueAt(int row, int column) {
125 if (rows==null) {
126 if (column==0) return "no files";
127 else return "";
128 }
129 if (row>=rows.size()) return "";
130 if (showLocked) {
131 if (column>colcount) return "";
132 } else if (column>=colcount) return "";
133 if ((column==0)&&(showLocked))
134 return ((Urls)rows.get(row)).getAttribute(Urls.A_LOCKED);
135 else if (showLocked) column--;
136 if (attributesToShow.elementAt(column).equals("urls"))
137 return rows.get(row);
138 else
139 return ((Urls)rows.get(row)).getAttribute(attributesToShow.elementAt(column));
140 }
141 public boolean isCellEditable(int row, int column) {
142 return false;
143 //return attributesToShow.elementAt(column).equals("urls") &&
144 // ((Urls)rows.get(row)).isWritable();
145 }
146
147 private static ResourceBundle langresources = null;
148 private static ResourceBundle structresources = null;
149
150 private static String getLangRes(String key) {
151 try {
152 if (langresources==null)
153 langresources=ResourceBundle.getBundle(LANGBUNDLE);
154 return langresources.getString(key);
155 } catch(MissingResourceException mre) {
156 System.err.println(mre.toString());
157 }
158 return null;
159 }
160 public static String getStructRes(String key) {
161 try {
162 if (structresources==null)
163 structresources=ResourceBundle.getBundle(STRUCTBUNDLE);
164 return structresources.getString(key);
165 } catch(MissingResourceException mre) {
166 System.err.println(mre.toString());
167 }
168 return null;
169 }
170
171 public Urls getRow(int idx) {
172 if (rows==null) return null;
173 if ((idx<0)||(idx>=rows.size())) return null;
174 return (Urls)rows.get(idx);
175 }
176
177 public void newOrder(String columnName) {
178 if (urls==null) return;
179 List l;
180 if ((l=urls.getAll())==null) return;
181 //rows=l.toArray();
182
183 if (columnName.equals(orderColumnName)) reverseOrder=!reverseOrder;
184 else reverseOrder=false;
185
186 int a2=0;
187 if (getColumnName(0).equals(columnName)) a2=1;
188
189 rows=buildRows(urls,
190 lastComparator=new AttributeComparator(
191 (String)columnNames.get(columnName),
192 (String)columnNames.get(getColumnName(a2)),
193 reverseOrder));
194 fireTableDataChanged();
195 orderColumnName=columnName;
196 }
197 public void newOrder(String columnName, int order) {
198 if (order==View.ASCENDENT_ORDER) reverseOrder=false;
199 else if (order==View.DESCENDENT_ORDER) reverseOrder=true;
200 if (columnName.equals(orderColumnName) && reverseOrder)
201 reverseOrder=false;
202 newOrder(columnName);
203 }
204 public void finalize() {
205 urls=null; rows=null;
206 lastComparator=null;
207 }
208
209 class AttributeComparator implements Comparator {
210 String attribute =null, attribute2=null;
211 boolean reverse = false;
212 public AttributeComparator(String attribute) {
213 this.attribute=attribute;
214 }
215 public AttributeComparator(String attribute, String attribute2, boolean reverse) {
216 this.attribute=attribute;
217 this.attribute2=attribute2;
218 this.reverse=reverse;
219 }
220 public String getAttribute() {
221 return attribute;
222 }
223 public int compare(Object o1, Object o2) {
224 Urls u1 = (Urls)o1, u2 = (Urls)o2;
225 Object a1 = u1.getAttribute(attribute),
226 a2 = u2.getAttribute(attribute);
227 int sign=reverse?-1:1;
228 if (a1==null) a1=u1.toString();
229 if (a1==null) return sign;
230 if (a2==null) a2=u2.toString();
231 if (a2==null) return sign;
232 int comp=0;
233 if (a1 instanceof Comparable)
234 comp = sign * ((Comparable)a1).compareTo(a2);
235 else {
236 if ((a1.toString()==null)||(a2.toString()==null)) return sign;
237 comp = sign * a1.toString().compareTo(a2.toString());
238 }
239 if ((comp==0)&&(attribute2!=null)) {
240 Object a3=u1.getAttribute(attribute2),
241 a4=u2.getAttribute(attribute2);
242 if ((a3==null)||(a4==null)) {
243 a3=u1.toString();
244 a4=u2.toString();
245 }
246 comp = sign * a3.toString().compareTo(a4.toString());
247 }
248 return comp;
249 }
250 public boolean equals(Object o) {
251 return super.equals(o);
252 }
253 }
254
255 }