Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.lgbs.daebot.dialogs;
- import java.io.File;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import org.dom4j.DocumentException;
- import org.eclipse.jface.dialogs.Dialog;
- import org.eclipse.jface.dialogs.IDialogConstants;
- import org.eclipse.jface.layout.GridDataFactory;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.Table;
- import org.eclipse.swt.widgets.TableItem;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- 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.Event;
- import org.eclipse.swt.widgets.FileDialog;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Listener;
- import org.eclipse.swt.widgets.MessageBox;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Spinner;
- import org.eclipse.swt.widgets.Text;
- import pl.lgbs.daebot.dao.SettingDao;
- import pl.lgbs.daebot.domain.SettingId;
- import pl.lgbs.daebot.main.BidHelper;
- import pl.lgbs.daebot.main.TaskManager;
- import pl.lgbs.exenws.timeseries.ClsTimeSeries;
- import pl.lgbs.exenws.timeseries.TimeSeriesDocument;
- public class NewSelectFileDialog extends Dialog {
- private Table table;
- private Button btnImport;
- private Label lbImport;
- private Label lbFailureTime;
- private Label lbDifferenceTime;
- private Text text;
- private Spinner incrementBidNrBy;
- private Spinner maxTrays;
- private Spinner failureTime;
- private Spinner differenceTime;
- private Spinner threadGapTime;
- private Spinner maxThreads;
- SettingDao settingDao = new SettingDao();
- private Label lbNrOfTries;
- private Label lbIncrementBy;
- private Label lbMaxThreads;
- private Label lbThreadGapTime;
- private TimeSeriesDocument document;
- private List<ClsTimeSeries> timeSeriesList;
- private BidHelper bidHelper = new BidHelper();
- private Set<String> items;
- private Set<Integer> sessions = new HashSet<Integer>();
- private Set<String> selectedItems = new HashSet<String>();
- private boolean flag = false;
- public NewSelectFileDialog(Shell parentShell) {
- super(parentShell);
- }
- @Override
- protected void cancelPressed() {
- super.cancelPressed();
- }
- @Override
- protected void okPressed() {
- TaskManager.host = settingDao.load(SettingId.ADDRESS) != null ? settingDao
- .load(SettingId.ADDRESS).getValue() : null;
- TaskManager.incrementBy = Integer.parseInt(incrementBidNrBy.getText());
- TaskManager.maxTrays = Integer.parseInt(maxTrays.getText());
- TaskManager.path = text.getText();
- TaskManager.failureTime = Integer.parseInt(failureTime.getText());
- TaskManager.differenceTime = Integer.parseInt(differenceTime.getText());
- TaskManager.maxThreads = Integer.parseInt(maxThreads.getText());
- TaskManager.threadGapTime = Integer.parseInt(threadGapTime.getText());
- if (validate()) {
- save();
- super.okPressed();
- }
- }
- private void save() {
- settingDao.changeSetting(SettingId.DEFAULT_IMPORT_FILE,
- TaskManager.path);
- settingDao.changeSetting(SettingId.TASK_START_DIFF,
- TaskManager.differenceTime.toString());
- settingDao.changeSetting(SettingId.GAP_BETWEEN_ATTEMPTS,
- TaskManager.failureTime.toString());
- settingDao.changeSetting(SettingId.INCREMENT_BID_NR_BY,
- TaskManager.incrementBy.toString());
- settingDao.changeSetting(SettingId.MAX_NR_OF_TRIES,
- TaskManager.maxTrays.toString());
- settingDao.changeSetting(SettingId.MAX_NR_OF_THREADS,
- TaskManager.maxThreads.toString());
- settingDao.changeSetting(SettingId.GAP_BETWEEN_THREADS,
- TaskManager.threadGapTime.toString());
- }
- private boolean validate() {
- if (TaskManager.host == null) {
- MessageBox mb = new MessageBox(getShell(), SWT.ERROR);
- mb.setText("Error");
- mb.setMessage("Host address not set");
- mb.open();
- return false;
- }
- File p = new File(TaskManager.path);
- if (!p.exists()) {
- MessageBox mb = new MessageBox(getShell(), SWT.ERROR);
- mb.setText("Error");
- mb.setMessage("File doesn't exist or cannot be read!");
- mb.open();
- return false;
- }
- return true;
- }
- @Override
- protected Button createButton(Composite parent, int id, String label,
- boolean defaultButton) {
- return super.createButton(parent, id, label, defaultButton);
- }
- public void load() {
- try {
- TaskManager.path = settingDao.load(SettingId.DEFAULT_IMPORT_FILE)
- .getValue();
- } catch (Exception e) {
- TaskManager.path = "";
- }
- try {
- TaskManager.failureTime = Integer.valueOf(settingDao.load(
- SettingId.GAP_BETWEEN_ATTEMPTS).getValue());
- } catch (Exception e) {
- TaskManager.failureTime = 1000;
- }
- try {
- TaskManager.threadGapTime = Integer.valueOf(settingDao.load(
- SettingId.GAP_BETWEEN_THREADS).getValue());
- } catch (Exception e) {
- TaskManager.threadGapTime = 1000;
- }
- try {
- TaskManager.differenceTime = Integer.valueOf(settingDao.load(
- SettingId.TASK_START_DIFF).getValue());
- } catch (Exception e) {
- TaskManager.differenceTime = 0;
- }
- try {
- TaskManager.maxTrays = Integer.valueOf(settingDao.load(
- SettingId.MAX_NR_OF_TRIES).getValue());
- } catch (Exception e) {
- TaskManager.maxTrays = 1;
- }
- try {
- TaskManager.maxThreads = Integer.valueOf(settingDao.load(
- SettingId.MAX_NR_OF_THREADS).getValue());
- } catch (Exception e) {
- TaskManager.maxThreads = 20;
- }
- try {
- TaskManager.incrementBy = Integer.valueOf(settingDao.load(
- SettingId.INCREMENT_BID_NR_BY).getValue());
- } catch (Exception e) {
- TaskManager.incrementBy = 0;
- }
- }
- @Override
- protected Control createDialogArea(Composite parent) {
- load();
- Composite composite = new Composite(parent, SWT.NONE);
- GridLayout layout = new GridLayout();
- layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
- layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
- layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
- layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
- layout.numColumns = 2;
- composite.setLayout(layout);
- composite.setLayoutData(new GridData(GridData.FILL_BOTH));
- lbImport = new Label(composite, SWT.NONE);
- lbImport.setText(Messages.SelectFileDialog_0);
- GridDataFactory.fillDefaults().span(2, 1).applyTo(lbImport);
- text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
- GridDataFactory.fillDefaults().grab(true, false).applyTo(text);
- text.setText(TaskManager.path);
- File f = new File(text.getText());
- if(f.exists())
- flag = true;
- btnImport = new Button(composite, SWT.PUSH);
- btnImport.setText(Messages.SelectFileDialog_1);
- btnImport.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- FileDialog dialog = new FileDialog(getShell());
- String path = text.getText();
- if (path != null && !path.isEmpty()) {
- dialog.setFilterPath(path);
- }
- dialog.setFilterExtensions(new String[] { Messages.SelectFileDialog_2 });
- path = dialog.open();
- if (path != null) {
- text.setText(path);
- TaskManager.path = path;
- loadData();
- }
- }
- });
- lbDifferenceTime = new Label(composite, SWT.NONE);
- lbDifferenceTime.setText(Messages.SelectFileDialog_3);
- GridDataFactory.fillDefaults().span(2, 1).applyTo(lbDifferenceTime);
- differenceTime = new Spinner(composite, SWT.BORDER);
- differenceTime.setMinimum(-60);
- differenceTime.setMaximum(60);
- differenceTime.setSelection(TaskManager.differenceTime);
- GridDataFactory.fillDefaults().grab(true, false)
- .applyTo(differenceTime);
- lbFailureTime = new Label(composite, SWT.NONE);
- lbFailureTime.setText(Messages.SelectFileDialog_6);
- GridDataFactory.fillDefaults().span(2, 1).applyTo(lbFailureTime);
- failureTime = new Spinner(composite, SWT.BORDER);
- failureTime.setMaximum(120000);
- failureTime.setMinimum(0);
- failureTime.setSelection(TaskManager.failureTime);
- GridDataFactory.fillDefaults().grab(true, false).applyTo(failureTime);
- lbNrOfTries = new Label(composite, SWT.NONE);
- lbNrOfTries.setText("Maximum nr of tries per thread:");
- GridDataFactory.fillDefaults().span(2, 1).applyTo(lbNrOfTries);
- maxTrays = new Spinner(composite, SWT.BORDER);
- maxTrays.setMinimum(-60);
- maxTrays.setMaximum(60);
- maxTrays.setSelection(TaskManager.maxTrays);
- GridDataFactory.fillDefaults().grab(true, false).applyTo(maxTrays);
- lbMaxThreads = new Label(composite, SWT.NONE);
- lbMaxThreads.setText("Maximum nr of threads:");
- GridDataFactory.fillDefaults().span(2, 1).applyTo(lbMaxThreads);
- maxThreads = new Spinner(composite, SWT.BORDER);
- maxThreads.setMinimum(0);
- maxThreads.setMaximum(100);
- maxThreads.setSelection(TaskManager.maxThreads);
- GridDataFactory.fillDefaults().grab(true, false).applyTo(maxThreads);
- lbThreadGapTime = new Label(composite, SWT.NONE);
- lbThreadGapTime.setText("Time gap between threads start:");
- GridDataFactory.fillDefaults().span(2, 1).applyTo(lbThreadGapTime);
- threadGapTime = new Spinner(composite, SWT.BORDER);
- threadGapTime.setMinimum(0);
- threadGapTime.setMaximum(120000);
- threadGapTime.setSelection(TaskManager.threadGapTime);
- GridDataFactory.fillDefaults().grab(true, false).applyTo(threadGapTime);
- lbIncrementBy = new Label(composite, SWT.NONE);
- lbIncrementBy.setText("Increment bid nr by:");
- GridDataFactory.fillDefaults().span(2, 1).applyTo(lbIncrementBy);
- incrementBidNrBy = new Spinner(composite, SWT.BORDER);
- incrementBidNrBy.setMinimum(0);
- incrementBidNrBy.setMaximum(500);
- incrementBidNrBy.setSelection(TaskManager.incrementBy);
- GridDataFactory.fillDefaults().grab(true, false)
- .applyTo(incrementBidNrBy);
- table = new Table(parent, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL);
- if(flag)
- loadData();
- table.setLayoutData(GridDataFactory.fillDefaults().hint(100, 100).create());
- return composite;
- }
- private void loadData() {
- selectedItems.clear();
- table.removeAll();
- try {
- document = bidHelper.parse(text.getText());
- } catch (DocumentException e1) {
- e1.printStackTrace();
- }
- if (document != null) {
- timeSeriesList = document.getTimeSeries();
- }
- items = new HashSet<String>();
- for (int i = 0; i< timeSeriesList.size(); ++i) {
- items.add(timeSeriesList.get(i).getDimensions().getBusinessDimension().get(0).getV());
- String session = timeSeriesList.get(i).getDimensions().getBusinessDimension().get(1).getV();
- session = session.substring(7);
- sessions.add(Integer.valueOf(session));
- }
- for (String s : items) {
- TableItem item = new TableItem(table, SWT.NONE);
- item.setText(translateName(s));
- }
- table.addListener(SWT.Selection, new Listener() {
- @Override
- public void handleEvent (Event event) {
- if (event.detail == SWT.CHECK) {
- String s = event.item.toString();
- s = s.substring(s.indexOf("{") + 1);
- s = s.substring(0, s.indexOf("}"));
- boolean status = selectedItems.add(reverseName(s));
- if(!status)
- selectedItems.remove(reverseName(s));
- }
- }
- });
- }
- public Set<String> getSelectedItems() {
- return selectedItems;
- }
- public Set<Integer> getSessions() {
- return sessions;
- }
- public String translateName(String name) {
- name = name.substring(4);
- name = name.replaceAll("_", ">>");
- return name;
- }
- public String reverseName(String name) {
- String temp = "CAD_"+name;
- name = temp.replaceAll(">>", "_");
- return name;
- }
- public List<ClsTimeSeries> getTimeSeriesList() {
- return timeSeriesList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement