📄 wareinputdialog.java
字号:
package com.niis.myprice.views;
import java.util.Date;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.niis.myprice.action.ICommandIds;
import com.niis.myprice.domain.Kind;
import com.niis.myprice.domain.Ware;
import com.niis.myprice.util.message.Messages;
public class WareInputDialog extends Dialog {
private Text descTxt;
private Text priceTxt;
private Text nameTxt;
private Text numTxt;
private int flg;
private TableViewer viewer;
protected Object result;
protected Shell shell;
private Ware ware = null;
/**
* Create the dialog
* @param parent
*/
public WareInputDialog(Shell parent) {
this(parent, SWT.NONE);
}
/**
* Create the dialog
* @param parent
* @param style
*/
public WareInputDialog(Shell parent, int style) {
super(parent, style);
}
public WareInputDialog(Shell parent, int style,Ware ware) {
this(parent, style);
this.ware=ware;
}
public WareInputDialog(Shell parent, int style,TableViewer viewer,int flg) {
this(parent, style);
this.flg=flg;
this.viewer = viewer;
if(flg == ICommandIds.MODIFY){
ISelection selection = viewer.getSelection();
Object obj = ((IStructuredSelection) selection)
.getFirstElement();
if(obj instanceof Ware){
Ware ware = (Ware)obj;
this.ware=ware;
}
}
}
/**
* Open the dialog
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setBounds(240, 220, 303, 380);
// shell.setSize(303, 380);
shell.setText(Messages.getString("WareInputDialog.wareinfo")); //$NON-NLS-1$
final Group wareGroup = new Group(shell, SWT.NONE);
wareGroup.setText(Messages.getString("WareInputDialog.wareinfo")); //$NON-NLS-1$
wareGroup.setBounds(10, 21, 273, 280);
final Label numLab = new Label(wareGroup, SWT.NONE);
numLab.setText(Messages.getString("WareInputDialog.warenum")); //$NON-NLS-1$
numLab.setBounds(16, 29, 50, 22);
numTxt = new Text(wareGroup, SWT.BORDER);
numTxt.setBounds(78, 26, 181, 25);
final Label nameLab = new Label(wareGroup, SWT.NONE);
nameLab.setText(Messages.getString("WareInputDialog.warename")); //$NON-NLS-1$
nameLab.setBounds(16, 61, 50, 21);
nameTxt = new Text(wareGroup, SWT.BORDER);
nameTxt.setBounds(78, 58, 181, 25);
final Label priceLab = new Label(wareGroup, SWT.NONE);
priceLab.setText(Messages.getString("WareInputDialog.wareprice")); //$NON-NLS-1$
priceLab.setBounds(16, 95, 50, 20);
priceTxt = new Text(wareGroup, SWT.BORDER);
priceTxt.setBounds(78, 92, 181, 25);
final Label descLab = new Label(wareGroup, SWT.NONE);
descLab.setText(Messages.getString("WareInputDialog.waredesc")); //$NON-NLS-1$
descLab.setBounds(16, 158, 56, 22);
descTxt = new Text(wareGroup, SWT.BORDER|SWT.WRAP);
descTxt.setBounds(78, 155, 181, 115);
final Button okBtn = new Button(shell, SWT.NONE);
okBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
addWare();
}
});
okBtn.setText(Messages.getString("WareInputDialog.ok")); //$NON-NLS-1$
okBtn.setBounds(127, 319, 62, 22);
final Button calBtn = new Button(shell, SWT.NONE);
calBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
calBtn.setText(Messages.getString("WareInputDialog.cancel")); //$NON-NLS-1$
calBtn.setBounds(195, 319, 66, 22);
//
if(ware != null){
numTxt.setText(ware.getDisplayNum());
nameTxt.setText(ware.getDisplayName());
descTxt.setText(ware.getDisplayDesc());
priceTxt.setText(ware.getDisplayPrice());
}
}
private void addWare(){
try{
int num = Integer.parseInt(numTxt.getText());
double price = Double.parseDouble(priceTxt.getText());
if(flg == ICommandIds.INSERT){
Object obj = viewer.getInput();
if(obj instanceof Kind){
Kind kind = (Kind)obj;
ware = new Ware(kind);
}
}
if(ware == null){
return ;
}
ware.setNum(num);
ware.setName(nameTxt.getText());
ware.setDesc(descTxt.getText());
ware.setPrice(price);
ware.setUpdDate(new Date());
viewer.refresh();
shell.dispose();
}catch(NumberFormatException e){
showMessage(Messages.getString("WareInputDialog.numandpricemustbedigital")); //$NON-NLS-1$
}
}
public void showMessage(String message) {
MessageDialog.openInformation(shell, Messages.getString("WareInputDialog.promptmessage"), message); //$NON-NLS-1$
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -