📄 shellcalccomposite.java
字号:
package net.sf.pim.calc;
import net.sf.util.StringUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import bsh.Interpreter;
/**
* 计算组件,对用户的文本输入,实现如下三种计算
* 1.列求和,常用于周报中时间汇种
* 2.表达式求值,也可替代计算器的功能
* 3.groovy代码片段执行
* 其中,2,3均采用groovy实现
* 因为beanshell的性能比groovy好,而且更小巧,用beanshell替代groovy
* @author levin
*
*/
public class ShellCalcComposite extends Composite{
//输入
private Text input;
//输出,只读
private Text output;
public ShellCalcComposite(Composite parent, int style) {
super(parent, style);
create();
}
private void create(){
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns=3;
setLayout(gridLayout);
input = new Text(this,SWT.WRAP | SWT.V_SCROLL);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.verticalSpan=3;
input.setLayoutData(gridData);
Composite group=new Composite(this,SWT.NULL);
RowLayout rowLayout=new RowLayout(SWT.VERTICAL);
rowLayout.spacing = 5;
group.setLayout(rowLayout);
Button sum=new Button(group,SWT.FLAT);
sum.setText("求和");
sum.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
if(input.getText().equals("")) return ;
try{
String[] ss=StringUtil.toArray(input.getText(), "\r\n");
double total=0.0;
for(int i=0;i<ss.length;i++)
total += StringUtil.parseDouble(convert(ss[i]));
output.setText(String.valueOf(total));
}catch(Exception ex){
output.setText("求和失败!"+ex);
}
}});
Button eval=new Button(group,SWT.FLAT);
eval.setText("运算");
eval.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
if(input.getText().equals("")) return ;
BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
public void run() {
try {
Interpreter shell = new Interpreter();
Object value = shell.eval(input.getText());
output.setText(String.valueOf(value));
} catch (Exception ex) {
output.setText("运算失败!" + ex);
}
}
});
}
});
Button run=new Button(group,SWT.FLAT);
run.setText("执行");
run.setEnabled(false);
Button clear=new Button(group,SWT.FLAT);
clear.setText("清除");
clear.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
output.setText("");
}});
output = new Text(this,SWT.WRAP | SWT.V_SCROLL|SWT.READ_ONLY);
output.setLayoutData(gridData);
output.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
}
//对字符串进行处理
private static String convert(String string) {
String str=string;
if(StringUtil.isNull(string))
str="";
//去掉前或后问号
String mask="??%%";
if(mask.indexOf(string.charAt(0)) != -1)
str=string.substring(1);
if(mask.indexOf(string.charAt(string.length()-1)) != -1)
str=string.substring(0,string.length()-1);
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -