package com.topsun.posclient.system.service;
import java.util.Stack;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.system.MessageResources;
import com.topsun.posclient.system.SyncProgress;
import com.topsun.posclient.system.service.impl.local.LocalSyncItemDataServiceImpl;
import com.topsun.posclient.webservice.POSServerCaller;
public class AgentSyncDataManager implements ISyncDataListener{
public Stack<ISyncDataService> syncStack = new Stack<ISyncDataService>();
public SyncProgress progress;
private static AgentSyncDataManager dataManager;
public static AgentSyncDataManager getInstance(){
if(dataManager == null){
dataManager = new AgentSyncDataManager();
return dataManager;
}
return dataManager;
}
private AgentSyncDataManager(){
SyncDataListenerManager.getInstance().addKeyListener(this);
}
public void doSyncData(final SyncProgress progress) throws POSException{
if(POSServerCaller.isConnected()){
this.progress = progress;
checkRegistrySyncType();
int size = syncStack.size();
int count = 0;
progress.getInfoText().setText("");
progress.getBar().setVisible(true);
for (int i = 0; i < syncStack.size(); i++) {
count = count + (100/size);
syncStack.get(i).syncData(progress,count);
}
syncStack.clear();
Display.getDefault().asyncExec(new Runnable() {
public void run() {
Shell shell = progress.getBar().getShell();
progress.getBar().setSelection(100);
MessageDialog.openInformation(shell, MessageResources.message_tips, "数据同步完成");
progress.getBar().setSelection(0);
}
});
}else{
throw new POSException(MessageResources.message_error_notconnection);
}
}
private void regsitryService(ISyncDataService dataService){
syncStack.push(dataService);
}
public void checkRegistrySyncType(){
regsitryService(new LocalSyncItemDataServiceImpl());
}
@Override
public void onChange(final String message,int count) {
String str = progress.getInfoText().getText() + "\r\n";
progress.getInfoText().setText(str + message);
progress.getBar().setSelection(count);
}
@Override
public void onChange(String message) {
String str = progress.getInfoText().getText() + "\r\n";
progress.getInfoText().setText(str + message);
}
}