package com.topsun.posclient.sales.ui.view;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Group;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.common.service.IBaseService;
import com.topsun.posclient.common.service.impl.BaseServiceImpl;
import com.topsun.posclient.common.ui.style.CommonCss;
import com.topsun.posclient.common.ui.utils.TableUtils;
import com.topsun.posclient.common.ui.utils.ThemeUtils;
import com.topsun.posclient.datamodel.GoldPrice;
import com.topsun.posclient.datamodel.dto.GoldPriceDTO;
import com.topsun.posclient.sales.core.service.IGoldPriceService;
import com.topsun.posclient.sales.core.service.impl.GoldPriceServiceImpl;
import com.topsun.posclient.sales.ui.table.GoldPriceCellModify;
import com.topsun.posclient.sales.ui.table.GoldPriceTableContentProvider;
import com.topsun.posclient.sales.ui.table.GoldPriceTableLableProvider;
/**
* 实时金价修改
* @author Dong
*
*/
public class GoldPriceUpdateView extends ViewPart {
public TableViewer goldViewer;//金价列表
private IBaseService baseService = new BaseServiceImpl();
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(1, false));
parent.setBackground(ThemeUtils.getCurrent().getBackGroundColor());
parent.setBackgroundMode(SWT.INHERIT_FORCE);
buildBaseInfo(parent);
buildOperation(parent);
}
/**
* @param parent
*/
private void buildBaseInfo(Composite parent){
GridLayout gridLayout = new GridLayout(2,false);
Group productInfo = new Group(parent, SWT.NONE);
productInfo.setText("实时金价修改");
productInfo.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
productInfo.setLayout(gridLayout);
productInfo.setLayoutData(new GridData(GridData.FILL_BOTH));
goldViewer = new TableViewer(productInfo,SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER|SWT.FULL_SELECTION);
goldViewer.setContentProvider(new GoldPriceTableContentProvider());
goldViewer.setLabelProvider(new GoldPriceTableLableProvider());
String[] cloumsProperties = new String[]{"MtartName","LastSyncTime","Price"};
goldViewer.setColumnProperties(cloumsProperties);
Table table = goldViewer.getTable();
table.setBackground(ThemeUtils.getCurrent().getTableBackGround());
table.setFont(CommonCss.getTableHeaderFont(parent.getDisplay()));
CellEditor[] editors = new CellEditor[3];
editors[0] = new TextCellEditor(table);
editors[1] = new TextCellEditor(table);
editors[2] = new TextCellEditor(table);
goldViewer.setCellEditors(editors);
goldViewer.setCellModifier(new GoldPriceCellModify(goldViewer));
table.setLinesVisible(false);
table.setHeaderVisible(true);
TableUtils.addCellEditSupport(editors[2]);
TableUtils.addTableEditSupport(goldViewer,new int[]{2});
table.setFont(CommonCss.getTableHeaderFont(parent.getDisplay()));
List<GoldPrice> goldPriceList = new ArrayList<GoldPrice>();
try {
GoldPriceDTO dto = baseService.queryAllGoldPrice();
if(null != dto && null != dto.getGoldPriceList()){
goldPriceList = dto.getGoldPriceList();
}
} catch (POSException e) {
MessageDialog.openError(parent.getShell(), "错误", e.getErrorMessage());
}
{
GridData tableData = new GridData(GridData.FILL_BOTH);
table.setLayoutData(tableData);
table.setHeaderVisible(true);
table.setLinesVisible(false);
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(400);
column.setText("物料名称");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(200);
column.setText("最后同步时间");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(300);
column.setText("金价(元)");
}
goldViewer.setInput(goldPriceList);
TableUtils.setTabelHeight(table);
}
/**
* @param parent
*/
private void buildOperation(Composite parent) {
Composite operation = new Composite(parent, SWT.NONE);
operation.setLayout(new GridLayout(2, true));
operation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
{
GridData buttonData = new GridData();
buttonData.heightHint = 45;
buttonData.widthHint = 80;
Button searchBtn = new Button(operation, SWT.NONE);
searchBtn.setText("保存");
searchBtn.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
searchBtn.setLayoutData(buttonData);
searchBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button sureBtn = (Button)e.getSource();
List<GoldPrice> goldPrices = (List<GoldPrice>)goldViewer.getInput();
try {
IGoldPriceService goldPriceService = new GoldPriceServiceImpl();
goldPriceService.updateGoldPriceByMtartCode(goldPrices);
MessageDialog.openInformation(sureBtn.getShell(), "提示", "操作成功");
} catch (POSException e1) {
MessageDialog.openError(sureBtn.getShell(), "错误", e1.getErrorMessage());
return;
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
Button refeshBtn = new Button(operation, SWT.NONE);
refeshBtn.setText("刷新");
refeshBtn.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
refeshBtn.setLayoutData(buttonData);
refeshBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button sureBtn = (Button)e.getSource();
try {
GoldPriceDTO dto = baseService.queryAllGoldPrice();
if(null == dto){
return;
}
List<GoldPrice> goldPrices = dto.getGoldPriceList();
if(null == goldPrices || goldPrices.size() == 0){
return;
}
goldViewer.setInput(goldPrices);
goldViewer.refresh();
} catch (POSException e1) {
e1.printStackTrace();
MessageDialog.openError(sureBtn.getShell(), "错误", e1.getErrorMessage());
return;
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
public void setFocus() {
}
}