搜索對話框字符串過濾
來源:程序員人生 發布時間:2014-10-08 20:32:01 閱讀次數:2142次
package com.sunsheen.ids.composite.dialog.select;
import java.util.ArrayList;
import java.util.Map;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import com.sunsheen.ids.composite.dialog.FileViewer;
import com.sunsheen.ids.composite.utils.IdsUtils;
import com.sunsheen.jfids.studio.dialog.compoments.listeners.SDialogOnClickListener;
public class onChange implements SDialogOnClickListener {
@Override
public void run(Map<String, Widget> coms, Map<String, Object> params,
Event event, String currentKey) {
Text fileNameText=(Text)coms.get("fileName");
String fileName=fileNameText.getText().trim();
List colum=(List)coms.get("show");
String blank[]=new String[0];
colum.setItems(blank);//清空列表;
Label showText=(Label)coms.get("showText");
showText.setText("");//清空顯示
//show0是臨時添加,用來保存文件列表;
if(params.get("show0")==null)
{
java.util.List<String> list = new FileViewer().getListFiles(params.get("path").toString(), "java", true);
String[] arr = (String[]) list.toArray(new String[list.size()]);
params.put("show0", arr);
}
String []arr=(String[]) params.get("show0");
if(fileName.length()!=0)
{
//在正則表達式中,這些特殊符?,*等,是表示重復次數,前面必須有一個字符。但我們的要求是輸入*就表示任意多個字符,所以要轉化;
if(fileName.contains("?"))
{
// ?本身在正則表達式是為特殊,所以要轉成字符串加? 才表示字符串中的? 。因為replaceAll中兩個參數為正則表達式。
fileName=fileName.replaceAll("?", ".?");
}
if(fileName.contains("*"))
{
fileName=fileName.replaceAll("*", ".*");
}
for(int i=0;i<arr.length;i++)
{
String str=arr[i].substring(arr[i].lastIndexOf("")+1,arr[i].length());
if(str.toLowerCase().matches(".*"+fileName.toLowerCase()+".*"))//matches是全額匹配,所以前后加.*,表示任意多個字符
{
//全路徑為:f:,但js引用是/,所以得轉換
String path=(String) params.get("path");//工程路徑;
String fullPath=arr[i].replaceAll("\", "/");
String path1=fullPath.substring(path.length()+1,fullPath.length());
colum.add(str+" - "+path1);
}
}
}
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈