You are on page 1of 5

private class searchbykeys{

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;

private searchbykeys(jtable tab ) {


tabletobesearched = tab;

tabletobesearched.addkeylistener(new keyadapter(){

public void keypressed(keyevent e){


try {
char ch = e.getkeychar();

if(character.isuppercase(ch))
ch = character.tolowercase(ch);

if( character.isletterordigit(ch) ||
character.isspacechar(ch)){

long systime = system.currenttimemillis();

if(systime - keypresstime > 400 ){


searchkey = ""+ch;
searchkey = searchkey.trim();//remove starting
space

if(resultmap!=null && resultmap.size() > 0 ){

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;

} catch (runtimeexception e1) {

}
system.out.println("constructed search key is: "+
searchkey);
}

public void keyreleased(keyevent e) {

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);

system.out.println("inside else for single key : key being searched by


iterator :"
+ key
+ " . row
index: "
+
rowindextoselect);

}
}
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(){

public void valuechanged(listselectionevent e) {

if(canresetiterator && resultmap!=null){


// system.out.println("****inside list selection
listener*****");
int row = tabletobesearched.getselectedrow();
if(row != -1){
int i;
keysettoselect = resultmap.keyset().iterator();
for (i=0; i <= row ; i++){
if(keysettoselect.hasnext())
keysettoselect.next();
}
system.out.println("value changed listener: "+i);

}
}

);

void constructsearchmap(arraylist<string> list){

if(tabletobesearched!=null && list!=null/* &&


searchcolumn!=null*/ ){
// searchlist = tabletobesearched.getmodel().

resultmap=null;
searchlist = list;
rowindextoselect = 0;
for(int i=0; i < list.size();i++ )
searchmap.put(((string)list.get(i)).trim().tolowercase(),i);
}

// system.out.println("constructed map is: "+searchmap);


}
public string getsearchcolumn() {
return searchcolumn;
}
public void setsearchcolumn(string searchcolumn) {
this.searchcolumn = searchcolumn;
}
public jtable gettabletobesearched() {
return tabletobesearched;
}
public void settabletobesearched(jtable tabletobesearched) {
this.tabletobesearched = tabletobesearched;
}
}
// ends here

You might also like