package com.topsun.posclient.system.ui.view;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import com.topsun.posclient.application.WorkbenchUtil;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.common.ui.style.CommonCss;
import com.topsun.posclient.common.ui.utils.ApplicationUtils;
import com.topsun.posclient.common.ui.utils.ThemeUtils;
import com.topsun.posclient.system.SyncProgress;
import com.topsun.posclient.system.service.SyncDataManager;
import com.topsun.posclient.system.ui.MessageResources;
public class SyncDialog implements IStartup {
@Override
public void earlyStartup() {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
Display dis = WorkbenchUtil.getWorkbenchWindow().getWorkbench().getDisplay();
Shell shell = new Shell(dis);
boolean flag = MessageDialog.openConfirm(shell, "同步", "是否需要同步数据");
// SyncComposite dialog = new SyncComposite(shell);
// dialog.open();
if(flag){
String viewId = "com.topsun.posclient.system.ui.SyncDataView";
try {
IViewPart helpView = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.showView(viewId);
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().activate(helpView);
ApplicationUtils.setStatusLine();
if(helpView instanceof SyncDataView){
SyncDataView dataView = (SyncDataView)helpView;
SyncProgress progress = new SyncProgress(dataView.getBar(), dataView.getInfoText());
try {
SyncDataManager.getInstance().doSyncData(progress);
} catch (POSException e) {
MessageDialog.openError(shell, MessageResources.message_tips, e.getErrorMessage());
return;
}
}
} catch (PartInitException e) {
MessageDialog.openError(shell, MessageResources.message_tips, e.getMessage());
return;
}
}
// IToolBarManager barManager = ToolbarContext.getInstance().getManager();
// IContributionItem[] contributionItem = barManager.getItems();
// for (IContributionItem iContributionItem : contributionItem) {
// iContributionItem.setVisible(false);
// }
// barManager.update(true);
// for (IContributionItem iContributionItem : contributionItem) {
// iContributionItem.setVisible(true);
// break;
// }
// barManager.update(true);
}
});
}
}
class SyncComposite extends Dialog{
public ProgressBar bar;
public Text infoText;
public SyncComposite(Shell parentShell) {
super(parentShell);
}
@Override
protected void configureShell(Shell newShell) {
newShell.setText("同步数据");
super.configureShell(newShell);
}
@Override
protected Control createContents(Composite parent) {
parent.setLayout(new GridLayout(1, false));
parent.setBackground(ThemeUtils.getCurrent().getBackGroundColor());
parent.setBackgroundMode(SWT.INHERIT_FORCE);
buildBaseInfo(parent);
buildOperation(parent);
return parent;
}
private void buildBaseInfo(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
{
Label lable = new Label(composite, SWT.NONE);
lable.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
lable.setText("进度:");
GridData data = new GridData();
data.horizontalSpan = 1;
lable.setLayoutData(data);
}
{
bar = new ProgressBar(composite, SWT.HORIZONTAL);
bar.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
bar.setMaximum(100);//设置最大值
bar.setMinimum(0);//设置最小值
GridData data = new GridData();
data.heightHint = 30;
data.widthHint = 750;
bar.setLayoutData(data);
}
{
Label lable = new Label(composite, SWT.NONE);
lable.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
lable.setText("信息:");
GridData data = new GridData();
data.horizontalSpan = 1;
lable.setLayoutData(data);
}
{
infoText = new Text(composite, SWT.MULTI | SWT.BORDER);
infoText.setEditable(false);
infoText.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data = new GridData();
data.heightHint = 400;
data.widthHint = 740;
infoText.setLayoutData(data);
}
}
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));
{
Button button = new Button(operation, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
button.setText("开始同步");
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 100;
button.setLayoutData(data);
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button saveButton = (Button)e.getSource();
try {
SyncProgress progress = new SyncProgress(bar, infoText);
SyncDataManager.getInstance().doSyncData(progress);
} catch (POSException e1) {
MessageDialog.openError(saveButton.getShell(), "提示", e1.getErrorMessage());
return;
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
Button button = new Button(operation, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
button.setText("清除日志");
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 100;
button.setLayoutData(data);
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
infoText.setText("");
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
Button button = new Button(operation, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
button.setText("取消");
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 100;
button.setLayoutData(data);
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
cancelPressed();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
}