📄 querydialog.java
字号:
package net.sf.pim.view;
import static net.sf.util.StringUtil.getCurrentDay;
import static net.sf.util.StringUtil.getDeltaDay;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ButtonGroup;
import net.sf.component.calendar.SWTCalendar;
import net.sf.component.calendar.SWTCalendarEvent;
import net.sf.component.calendar.SWTCalendarListener;
import net.sf.component.config.ConfigHelper;
import net.sf.pim.URLUtil;
import net.sf.pim.UiUtil;
import net.sf.pim.model.psp.WorkCondition;
import net.sf.util.StringUtil;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* @author levin
* @fix 20080811 优化界面,增加日期的快速选择
*/
public class QueryDialog extends Dialog {
//条件
private WorkCondition workCondition = new WorkCondition();
//控件,参见WorkCondition
private Text rq1, rq2,key;
private SWTCalendar calendar1;
private SWTCalendar calendar2;
public QueryDialog(Shell shell) {
super(shell);
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("工作记录--查询条件");
Image image = ImageDescriptor.createFromURL(URLUtil.getResourceURL("icons/search3.gif")).createImage(shell.getDisplay());
shell.setImage(image);
image.dispose();
}
protected Control createDialogArea(Composite parent) {
Composite composite=new Composite(parent,SWT.NULL);
GridLayout gl=new GridLayout();
gl.numColumns=3;
gl.verticalSpacing=8;
composite.setLayout(gl);
GridData noteGd=new GridData(GridData.FILL_BOTH);
noteGd.horizontalSpan=3;
Label note=new Label(composite,SWT.NULL);
note.setLayoutData(noteGd);
note.setText("请输入或者选择起始日期,或者通过快速通道选择日期,然后输入关键字进行查询:\n\n");
Label lableInput=new Label(composite,SWT.NULL);
lableInput.setText("输入日期:");
rq1 = new Text(composite, SWT.NULL);
rq1.addVerifyListener(new VerifyListener(){
public void verifyText(VerifyEvent e) {
//几种情况,输入控制键,输入中文,输入字符,输入数字
//整数验证
Pattern pattern = Pattern.compile("[0-9]\\d*");
Matcher matcher = pattern.matcher(e.text);
if(matcher.matches()) //处理数字
e.doit=true;
else if(e.text.length() > 0) //有字符情况,包含中文、空格
e.doit=false;
else //控制键
e.doit=true;
}});
rq1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
UiUtil.syncCalendar(rq1.getText(),calendar1);
}});
rq2 = new Text(composite, SWT.NULL);
rq2.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
UiUtil.syncCalendar(rq2.getText(),calendar2);
}});
String[] s3 = ConfigHelper.getStringArrayProperty("work.search3");
rq1.setText(s3[0]);
rq2.setText(s3[1]);
rq1.setFont(UiUtil.getFont());
rq2.setFont(UiUtil.getFont());
Label labelRq = new Label(composite, SWT.NULL);
labelRq.setText("选择日期:");
calendar1 = new SWTCalendar(composite,SWT.NONE|SWTCalendar.RED_SUNDAY);
calendar1.addSWTCalendarListener(new SWTCalendarListener(){
public void dateChanged(SWTCalendarEvent event) {
rq1.setText(StringUtil.getDefaultDateFormat().format(event.getCalendar().getTime()));
}});
calendar2 = new SWTCalendar(composite,SWT.NONE|SWTCalendar.RED_SUNDAY);
calendar2.addSWTCalendarListener(new SWTCalendarListener(){
public void dateChanged(SWTCalendarEvent event) {
rq2.setText(StringUtil.getDefaultDateFormat().format(event.getCalendar().getTime()));
}});
//赋初值
UiUtil.syncCalendar(s3[0], calendar1);
UiUtil.syncCalendar(s3[1], calendar2);
Label labelQuick = new Label(composite, SWT.NULL);
labelQuick.setText("快速通道:");
Composite quickComposite=new Composite(composite,SWT.NULL);
GridData quickGd=new GridData(GridData.FILL_BOTH);
quickGd.horizontalSpan=2;
quickComposite.setLayoutData(quickGd);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing=8;
quickComposite.setLayout(gridLayout);
String[] quickTips=new String[]{"近一周","近一月","近七天"};
for(String tip:quickTips){
Button b1=new Button(quickComposite,SWT.RADIO);
b1.setText(tip);
b1.setData(tip);
b1.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
syncQuickDate((String) e.widget.getData());
}
});
}
Label labelKey = new Label(composite, SWT.NULL);
labelKey.setText("关键词:");
key = new Text(composite, SWT.NULL);
GridData gd=new GridData(GridData.FILL_BOTH);
gd.horizontalSpan=2;
key.setLayoutData(gd);
key.setFont(UiUtil.getFont());
key.forceFocus();
return composite;
}
protected void okPressed() {
workCondition.setRq1(rq1.getText());
workCondition.setRq2(rq2.getText());
workCondition.setKey(key.getText());
super.okPressed();
}
public WorkCondition getWorkCondition() {
return workCondition;
}
private void syncQuickDate(String choice){
String xtRq = getCurrentDay();
String[] dayRange = new String[]{getDeltaDay(xtRq, -7), xtRq};
if(choice.equals("近一周")){
int dayOfWeek=Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
if(dayOfWeek > 2){ //本周
dayRange = new String[]{getDeltaDay(xtRq, -dayOfWeek+1), xtRq};
if(ConfigHelper.getIntegerProperty("work.firstday") == 6)
dayRange = new String[]{getDeltaDay(xtRq, -dayOfWeek-1), getDeltaDay(xtRq, dayOfWeek > 5?5-dayOfWeek:0)};
}
else{
dayRange = new String[]{getDeltaDay(xtRq, -dayOfWeek-6), getDeltaDay(xtRq, -dayOfWeek)};
if(ConfigHelper.getIntegerProperty("work.firstday") == 6)
dayRange = new String[]{getDeltaDay(xtRq, -dayOfWeek-8), getDeltaDay(xtRq, -dayOfWeek-2)};
}
}else if(choice.equals("近一月")){
int dayOfMonth=Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
if(dayOfMonth > 20) //本月
dayRange = new String[]{getDeltaDay(xtRq, -dayOfMonth+1), xtRq};
else{
String day2= getDeltaDay(xtRq, -dayOfMonth);
String day1= day2.substring(0,6)+"01";
dayRange = new String[]{day1,day2};
}
}
UiUtil.syncCalendar(dayRange[0],calendar1);
UiUtil.syncCalendar(dayRange[1],calendar2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -