package com.topsun.posclient.application;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.topsun.posclient.common.LoggerUtil;
import com.topsun.posclient.common.POSClientApp;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.common.core.keyboard.SoftKeyUtil;
import com.topsun.posclient.common.ui.style.CommonCss;
import com.topsun.posclient.common.ui.utils.ImageUtils;
import com.topsun.posclient.datamodel.User;
import com.topsun.posclient.system.service.ILoginService;
import com.topsun.posclient.system.service.impl.LoginServiceImpl;
/**
* 登录窗口
* @author Dong
*
*/
public class LoginDialog extends TitleAreaDialog {
private ILoginService longinsService = new LoginServiceImpl();
private Text passwordText;
private Text userNameText;
/**
* Create the dialog
*
* @param parentShell
*/
public LoginDialog(Shell parentShell) {
super(parentShell);
POSClientApp.get().setData("SHOWMODE", "Touch Mode");
}
/**
* Create contents of the dialog
*
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
POSClientApp.get().setData("SOFTKEY", "false");
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setBackgroundImage(ImageUtils.createImage(TopSunApplicationActivator.PLUGIN_ID, "icons//back.png"));
final GridLayout gridLayout = new GridLayout();
gridLayout.marginLeft = 60;
gridLayout.marginHeight = 40;
gridLayout.marginTop = 30;
gridLayout.marginBottom = 30;
gridLayout.numColumns = 2;
container.setFont(CommonCss.getFontForSize(parent.getDisplay(), 20));
container.setLayout(gridLayout);
GridData data = new GridData();
data.widthHint = 400;
data.heightHint = 400;
container.setLayoutData(data);
final Label label = new Label(container, SWT.RIGHT);
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
label.setForeground( Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
label.setText(MessageResources.message_username+" ");
userNameText = new Text(container, SWT.BORDER);
GridData gd_userNameText = new GridData();
gd_userNameText.widthHint = 180;
gd_userNameText.heightHint = 30;
userNameText.setFont(CommonCss.getFontForSize(parent.getDisplay(), 15));
userNameText.setForeground( Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
userNameText.setLayoutData(gd_userNameText);
SoftKeyUtil.showSoftKey(userNameText);
final Label label_1 = new Label(container, SWT.RIGHT);
label_1.setText(MessageResources.message_password);
GridData data2 = new GridData();
data2.verticalIndent = 20;
label_1.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
label_1.setForeground( Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
label_1.setLayoutData(data2);
passwordText = new Text(container, SWT.BORDER|SWT.PASSWORD);
final GridData gd_passwordText = new GridData();
gd_passwordText.widthHint = 180;
gd_passwordText.heightHint = 30;
gd_passwordText.verticalIndent = 20;
passwordText.setFont(CommonCss.getFontForSize(parent.getDisplay(), 15));
passwordText.setForeground( Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
passwordText.setLayoutData(gd_passwordText);
SoftKeyUtil.showSoftKey(passwordText);
setTitle("豫园POS客户端");
return area;
}
protected void createButtonsForButtonBar(Composite parent) {
int buttonWidth = 80;
int buttonHight = 45;
parent.setLayout(new GridLayout(5,false));
Button okbtn = createButton(parent, IDialogConstants.OK_ID, MessageResources.message_login,true);
GridData btnLayout = new GridData();
btnLayout.widthHint = buttonWidth;
btnLayout.heightHint = buttonHight;
okbtn.setLayoutData(btnLayout);
Button cancelBtn = createButton(parent, IDialogConstants.CANCEL_ID, MessageResources.message_cancel, false);
cancelBtn.setLayoutData(btnLayout);
{
Button keyFlag = createButton(parent, 9999, "软键盘", false);
keyFlag.setText("软键盘");
keyFlag.setLayoutData(btnLayout);
keyFlag.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
Object obj = POSClientApp.get().getData("SOFTKEY");
if(obj instanceof String){
String str = (String)obj;
if(str.equals("true")){
POSClientApp.get().setData("SOFTKEY", "false");
}else{
POSClientApp.get().setData("SOFTKEY", "true");
}
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
}
}
protected Button createButton(Composite parent, int id, String label,
boolean defaultButton) {
return super.createButton(parent, id, label, defaultButton);
}
/**
* Return the initial size of the dialog
*/
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setSize(400,400);
Rectangle screenSize = Display.getDefault().getClientArea();
Rectangle frameSize = newShell.getBounds();
newShell.setLocation(new Point((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2));
newShell.setText(MessageResources.message_title);
newShell.setBackgroundMode(SWT.INHERIT_FORCE);
}
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID) {
try {
if(null == userNameText.getText() || "".equals(userNameText.getText())){
MessageDialog.openError(getShell(), MessageResources.message_tips_loginfailer, "【用户名】不能为空");
userNameText.forceFocus();
return;
}
if(null == passwordText.getText() || "".equals(passwordText.getText())){
MessageDialog.openError(getShell(), MessageResources.message_tips_loginfailer, "【密码】不能为空");
passwordText.forceFocus();
return;
}
boolean flag = login(userNameText.getText(), passwordText.getText());
if(flag){
okPressed();
}else{
MessageDialog.openError(getShell(), MessageResources.message_tips_loginfailer, "登陆失败");
return;
}
} catch (POSException e) {
MessageDialog.openError(getShell(), MessageResources.message_tips_loginfailer, e.getErrorMessage());
return;
}catch (Exception e) {
MessageDialog.openError(getShell(), MessageResources.message_tips_loginfailer, e.getMessage());
return;
}
}
if (buttonId == IDialogConstants.CANCEL_ID) {
cancelPressed();
}
super.buttonPressed(buttonId);
}
private boolean login(String userName, String password) throws Exception {
User operator;
try {
operator = longinsService.getUserData(userName, password);
if (null != operator){
return true;
}else{
LoggerUtil.logError(TopSunApplicationActivator.PLUGIN_ID, MessageResources.message_tips_loginfailer);
throw new Exception(MessageResources.message_tips_loginfailer);
}
} catch (POSException e) {
LoggerUtil.logError(TopSunApplicationActivator.PLUGIN_ID, e.getErrorMessage());
return false;
} catch (Exception e) {
LoggerUtil.logError(TopSunApplicationActivator.PLUGIN_ID, "登陆失败");
return false;
}
}
}