Professional Documents
Culture Documents
arraylist<string> searchlist;
string searchcolumn,searchkey="";
jtable tabletobesearched;
long keypresstime;
boolean canbesearched;
treemap<string, integer> searchmap = new treemap<string, integer>();
sortedmap<string, integer> resultmap,tempmap;
iterator<string> keysettoselect;
int rowindextoselect;
boolean canresetiterator = true;
tabletobesearched.addkeylistener(new keyadapter(){
if(character.isuppercase(ch))
ch = character.tolowercase(ch);
if( character.isletterordigit(ch) ||
character.isspacechar(ch)){
if(searchkey.length() != 0 ){
string firstkey = resultmap.firstkey();
if(firstkey.charat(0)!= ch){
resultmap =
searchmap.submap(searchkey,((char)(searchkey.charat(0)+1))+"");
keysettoselect =
resultmap.keyset().iterator();
system.out.println("########## rebuilding
result map for "+ch+" #########");
}
}
}
else if(searchkey.length()!=0){
resultmap =
searchmap.submap(searchkey,((char)(searchkey.charat(0)+1))+"");
keysettoselect =
resultmap.keyset().iterator();
}
keypresstime = systime;
}
else{
searchkey = searchkey + ch ;
keypresstime = systime;
}
canbesearched = true;
}
else
canbesearched = false;
}
system.out.println("constructed search key is: "+
searchkey);
}
try {
if (searchlist != null && canbesearched) {
string k = resultmap.firstkey();
if (searchkey.trim().length() == 1 &&
(k.charat(0) == searchkey.charat(0))) {
if (keysettoselect.hasnext()) {
string key =
keysettoselect.next();
rowindextoselect =
resultmap.get(key);
system.out.println("inside
1st condn for single key : key being searched by iterator :"
+ key
+ " .
row index: "
+
rowindextoselect);
} else {
system.out.println("inside
the else part of single key operation");
keysettoselect =
resultmap.keyset().iterator();
if (keysettoselect.hasnext())
{
string key =
keysettoselect.next();
rowindextoselect =
resultmap.get(key);
}
}
canresetiterator = false;
tabletobesearched.changeselection(rowindextoselect,
0, false, false);
canresetiterator = true;
} else if (searchkey.trim().length() > 1)
{// if
// condition
// for
// starting
// space
// resultmap =
//
searchmap.submap(searchkey,((char)(searchkey.charat(0)+1))+"");
tempmap =
resultmap.submap(searchkey,((char) (searchkey.charat(0) + 1)) + "");
if (tempmap != null &&
tempmap.size() > 0) {
string firstkey =
tempmap.firstkey();
if
(searchkey.trim().length() <= firstkey.length()) {
if
(firstkey.substring(0,searchkey.trim().length())
.
equalsignorecase(searchkey.trim())) {
rowindextoselect = tempmap
.get(firstkey);
tabletobesearched.changeselection(
rowindextoselect, 0, false,
false);
}
}
}
}
} catch (exception e1) {
// todo auto-generated catch block
// e1.printstacktrace();
}
}
});
tabletobesearched.getselectionmodel().addlistselectionlistener(
new listselectionlistener(){
}
}
);
resultmap=null;
searchlist = list;
rowindextoselect = 0;
for(int i=0; i < list.size();i++ )
searchmap.put(((string)list.get(i)).trim().tolowercase(),i);
}