KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > drafts > AutoFill


1 package thinlet.drafts;
2
3 import java.io.*;
4 import java.text.*;
5 import thinlet.*;
6
7 /**
8  *
9  */

10 public class AutoFill {
11     
12     private String JavaDoc[] autofill;
13     
14     /**
15      *
16      */

17     public void load(Thinlet thinlet, Object JavaDoc localelist, Object JavaDoc shortmonths, Object JavaDoc months,
18             Object JavaDoc shortweekdays, Object JavaDoc weekdays) {
19         DateFormatSymbols symbols = new DateFormatSymbols();
20         fill(thinlet, shortmonths, symbols.getShortMonths());
21         fill(thinlet, months, symbols.getMonths());
22         fill(thinlet, shortweekdays, symbols.getShortWeekdays());
23         fill(thinlet, weekdays, symbols.getWeekdays());
24         
25         autofill = symbols.getWeekdays();
26     }
27     
28     /**
29      *
30      */

31     private void fill(Thinlet thinlet, Object JavaDoc list, String JavaDoc[] values) {
32         for (int i = 0; i < values.length; i++) {
33             Object JavaDoc item = Thinlet.create("item");
34             thinlet.setString(item, "text", values[i]);
35             thinlet.add(list, item);
36         }
37     }
38     
39     /**
40      *
41      */

42     public void autoFill(Thinlet thinlet, Object JavaDoc textfield, String JavaDoc text, int caret) {
43         if (text.length() == caret) {
44             for (int i = 0; i < autofill.length; i++) {
45                 if (autofill[i].startsWith(text)) {
46                     thinlet.setString(textfield, "text", autofill[i]);
47                     thinlet.setInteger(textfield, "start", autofill[i].length());
48                     break;
49                 }
50             }
51         }
52     }
53     
54     /**
55      *
56      */

57     public void initPath(Thinlet thinlet, Object JavaDoc textfield) {
58         thinlet.setString(textfield, "text", System.getProperty("user.dir"));
59     }
60     
61     /**
62      *
63      */

64     public void complementPath(Thinlet thinlet, Object JavaDoc textfield, String JavaDoc text, int caret) {
65         if (text.length() == caret) {
66             int index = text.lastIndexOf(File.separatorChar);
67             if (index == -1) { return; }
68             String JavaDoc foldername = text.substring(0, index + 1);
69             File folder = new File(foldername);
70             String JavaDoc namestart = text.substring(index + 1);
71             String JavaDoc[] list = folder.list();
72             if (list == null) { return; } // e.g. no floppy
73
for (int i = 0; i < list.length; i++) {
74                 if (list[i].startsWith(namestart)) {
75                     String JavaDoc path = foldername + list[i];
76                     if (new File(folder, list[i]).isDirectory()) { path += File.separatorChar; }
77                     thinlet.setString(textfield, "text", path);
78                     thinlet.setInteger(textfield, "end", path.length());
79                     break;
80                 }
81             }
82         }
83     }
84 }
Popular Tags