Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.73 KB | None | 0 0
  1. package pl.lgbs.daebot.dialogs;
  2.  
  3. import java.io.File;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7.  
  8. import org.dom4j.DocumentException;
  9. import org.eclipse.jface.dialogs.Dialog;
  10. import org.eclipse.jface.dialogs.IDialogConstants;
  11. import org.eclipse.jface.layout.GridDataFactory;
  12. import org.eclipse.swt.SWT;
  13. import org.eclipse.swt.widgets.Table;
  14. import org.eclipse.swt.widgets.TableItem;
  15. import org.eclipse.swt.events.SelectionAdapter;
  16. import org.eclipse.swt.events.SelectionEvent;
  17. import org.eclipse.swt.layout.GridData;
  18. import org.eclipse.swt.layout.GridLayout;
  19. import org.eclipse.swt.widgets.Button;
  20. import org.eclipse.swt.widgets.Composite;
  21. import org.eclipse.swt.widgets.Control;
  22. import org.eclipse.swt.widgets.Event;
  23. import org.eclipse.swt.widgets.FileDialog;
  24. import org.eclipse.swt.widgets.Label;
  25. import org.eclipse.swt.widgets.Listener;
  26. import org.eclipse.swt.widgets.MessageBox;
  27. import org.eclipse.swt.widgets.Shell;
  28. import org.eclipse.swt.widgets.Spinner;
  29. import org.eclipse.swt.widgets.Text;
  30.  
  31. import pl.lgbs.daebot.dao.SettingDao;
  32. import pl.lgbs.daebot.domain.SettingId;
  33. import pl.lgbs.daebot.main.BidHelper;
  34. import pl.lgbs.daebot.main.TaskManager;
  35. import pl.lgbs.exenws.timeseries.ClsTimeSeries;
  36. import pl.lgbs.exenws.timeseries.TimeSeriesDocument;
  37.  
  38.  
  39. public class NewSelectFileDialog extends Dialog {
  40.    
  41.     private Table table;
  42.     private Button btnImport;
  43.     private Label lbImport;
  44.     private Label lbFailureTime;
  45.     private Label lbDifferenceTime;
  46.     private Text text;
  47.     private Spinner incrementBidNrBy;
  48.     private Spinner maxTrays;
  49.     private Spinner failureTime;
  50.     private Spinner differenceTime;
  51.     private Spinner threadGapTime;
  52.     private Spinner maxThreads;
  53.     SettingDao settingDao = new SettingDao();
  54.  
  55.     private Label lbNrOfTries;
  56.  
  57.     private Label lbIncrementBy;
  58.  
  59.     private Label lbMaxThreads;
  60.  
  61.     private Label lbThreadGapTime;
  62.    
  63.     private TimeSeriesDocument document;
  64.     private List<ClsTimeSeries> timeSeriesList;
  65.     private BidHelper bidHelper = new BidHelper();
  66.     private Set<String> items;
  67.     private Set<Integer> sessions = new HashSet<Integer>();
  68.     private Set<String> selectedItems = new HashSet<String>();
  69.     private boolean flag = false;
  70.    
  71.     public NewSelectFileDialog(Shell parentShell) {
  72.         super(parentShell);
  73.     }
  74.  
  75.     @Override
  76.     protected void cancelPressed() {
  77.         super.cancelPressed();
  78.     }
  79.  
  80.     @Override
  81.     protected void okPressed() {
  82.         TaskManager.host = settingDao.load(SettingId.ADDRESS) != null ? settingDao
  83.                 .load(SettingId.ADDRESS).getValue() : null;
  84.         TaskManager.incrementBy = Integer.parseInt(incrementBidNrBy.getText());
  85.         TaskManager.maxTrays = Integer.parseInt(maxTrays.getText());
  86.         TaskManager.path = text.getText();
  87.         TaskManager.failureTime = Integer.parseInt(failureTime.getText());
  88.         TaskManager.differenceTime = Integer.parseInt(differenceTime.getText());
  89.         TaskManager.maxThreads = Integer.parseInt(maxThreads.getText());
  90.         TaskManager.threadGapTime = Integer.parseInt(threadGapTime.getText());
  91.         if (validate()) {
  92.             save();
  93.             super.okPressed();
  94.         }
  95.     }
  96.  
  97.     private void save() {
  98.         settingDao.changeSetting(SettingId.DEFAULT_IMPORT_FILE,
  99.                 TaskManager.path);
  100.         settingDao.changeSetting(SettingId.TASK_START_DIFF,
  101.                 TaskManager.differenceTime.toString());
  102.         settingDao.changeSetting(SettingId.GAP_BETWEEN_ATTEMPTS,
  103.                 TaskManager.failureTime.toString());
  104.         settingDao.changeSetting(SettingId.INCREMENT_BID_NR_BY,
  105.                 TaskManager.incrementBy.toString());
  106.         settingDao.changeSetting(SettingId.MAX_NR_OF_TRIES,
  107.                 TaskManager.maxTrays.toString());
  108.         settingDao.changeSetting(SettingId.MAX_NR_OF_THREADS,
  109.                 TaskManager.maxThreads.toString());
  110.         settingDao.changeSetting(SettingId.GAP_BETWEEN_THREADS,
  111.                 TaskManager.threadGapTime.toString());
  112.     }
  113.  
  114.     private boolean validate() {
  115.         if (TaskManager.host == null) {
  116.             MessageBox mb = new MessageBox(getShell(), SWT.ERROR);
  117.             mb.setText("Error");
  118.             mb.setMessage("Host address not set");
  119.             mb.open();
  120.             return false;
  121.         }
  122.         File p = new File(TaskManager.path);
  123.         if (!p.exists()) {
  124.             MessageBox mb = new MessageBox(getShell(), SWT.ERROR);
  125.             mb.setText("Error");
  126.             mb.setMessage("File doesn't exist or cannot be read!");
  127.             mb.open();
  128.             return false;
  129.         }
  130.         return true;
  131.     }
  132.  
  133.     @Override
  134.     protected Button createButton(Composite parent, int id, String label,
  135.             boolean defaultButton) {
  136.         return super.createButton(parent, id, label, defaultButton);
  137.     }
  138.  
  139.     public void load() {
  140.         try {
  141.             TaskManager.path = settingDao.load(SettingId.DEFAULT_IMPORT_FILE)
  142.                     .getValue();
  143.         } catch (Exception e) {
  144.             TaskManager.path = "";
  145.         }
  146.         try {
  147.             TaskManager.failureTime = Integer.valueOf(settingDao.load(
  148.                     SettingId.GAP_BETWEEN_ATTEMPTS).getValue());
  149.         } catch (Exception e) {
  150.             TaskManager.failureTime = 1000;
  151.         }
  152.         try {
  153.             TaskManager.threadGapTime = Integer.valueOf(settingDao.load(
  154.                     SettingId.GAP_BETWEEN_THREADS).getValue());
  155.         } catch (Exception e) {
  156.             TaskManager.threadGapTime = 1000;
  157.         }
  158.         try {
  159.             TaskManager.differenceTime = Integer.valueOf(settingDao.load(
  160.                     SettingId.TASK_START_DIFF).getValue());
  161.         } catch (Exception e) {
  162.             TaskManager.differenceTime = 0;
  163.         }
  164.         try {
  165.             TaskManager.maxTrays = Integer.valueOf(settingDao.load(
  166.                     SettingId.MAX_NR_OF_TRIES).getValue());
  167.         } catch (Exception e) {
  168.             TaskManager.maxTrays = 1;
  169.         }
  170.         try {
  171.             TaskManager.maxThreads = Integer.valueOf(settingDao.load(
  172.                     SettingId.MAX_NR_OF_THREADS).getValue());
  173.         } catch (Exception e) {
  174.             TaskManager.maxThreads = 20;
  175.         }
  176.         try {
  177.             TaskManager.incrementBy = Integer.valueOf(settingDao.load(
  178.                     SettingId.INCREMENT_BID_NR_BY).getValue());
  179.         } catch (Exception e) {
  180.             TaskManager.incrementBy = 0;
  181.         }
  182.     }
  183.  
  184.     @Override
  185.     protected Control createDialogArea(Composite parent) {
  186.         load();
  187.         Composite composite = new Composite(parent, SWT.NONE);
  188.  
  189.         GridLayout layout = new GridLayout();
  190.         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
  191.         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
  192.         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
  193.         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
  194.         layout.numColumns = 2;
  195.         composite.setLayout(layout);
  196.         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
  197.  
  198.         lbImport = new Label(composite, SWT.NONE);
  199.         lbImport.setText(Messages.SelectFileDialog_0);
  200.         GridDataFactory.fillDefaults().span(2, 1).applyTo(lbImport);
  201.  
  202.         text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
  203.         GridDataFactory.fillDefaults().grab(true, false).applyTo(text);
  204.         text.setText(TaskManager.path);
  205.        
  206.         File f = new File(text.getText());
  207.         if(f.exists())
  208.             flag = true;
  209.            
  210.         btnImport = new Button(composite, SWT.PUSH);
  211.         btnImport.setText(Messages.SelectFileDialog_1);
  212.         btnImport.addSelectionListener(new SelectionAdapter() {
  213.             @Override
  214.             public void widgetSelected(SelectionEvent e) {
  215.                 FileDialog dialog = new FileDialog(getShell());
  216.                 String path = text.getText();
  217.                 if (path != null && !path.isEmpty()) {
  218.                     dialog.setFilterPath(path);
  219.                 }
  220.                 dialog.setFilterExtensions(new String[] { Messages.SelectFileDialog_2 });
  221.                 path = dialog.open();
  222.  
  223.                 if (path != null) {
  224.                     text.setText(path);
  225.                     TaskManager.path = path;
  226.                     loadData();
  227.                 }
  228.             }
  229.         });
  230.        
  231.         lbDifferenceTime = new Label(composite, SWT.NONE);
  232.         lbDifferenceTime.setText(Messages.SelectFileDialog_3);
  233.         GridDataFactory.fillDefaults().span(2, 1).applyTo(lbDifferenceTime);
  234.  
  235.         differenceTime = new Spinner(composite, SWT.BORDER);
  236.         differenceTime.setMinimum(-60);
  237.         differenceTime.setMaximum(60);
  238.         differenceTime.setSelection(TaskManager.differenceTime);
  239.         GridDataFactory.fillDefaults().grab(true, false)
  240.                 .applyTo(differenceTime);
  241.  
  242.         lbFailureTime = new Label(composite, SWT.NONE);
  243.         lbFailureTime.setText(Messages.SelectFileDialog_6);
  244.         GridDataFactory.fillDefaults().span(2, 1).applyTo(lbFailureTime);
  245.  
  246.         failureTime = new Spinner(composite, SWT.BORDER);
  247.         failureTime.setMaximum(120000);
  248.         failureTime.setMinimum(0);
  249.         failureTime.setSelection(TaskManager.failureTime);
  250.         GridDataFactory.fillDefaults().grab(true, false).applyTo(failureTime);
  251.  
  252.         lbNrOfTries = new Label(composite, SWT.NONE);
  253.         lbNrOfTries.setText("Maximum nr of tries per thread:");
  254.         GridDataFactory.fillDefaults().span(2, 1).applyTo(lbNrOfTries);
  255.  
  256.         maxTrays = new Spinner(composite, SWT.BORDER);
  257.         maxTrays.setMinimum(-60);
  258.         maxTrays.setMaximum(60);
  259.         maxTrays.setSelection(TaskManager.maxTrays);
  260.         GridDataFactory.fillDefaults().grab(true, false).applyTo(maxTrays);
  261.  
  262.         lbMaxThreads = new Label(composite, SWT.NONE);
  263.         lbMaxThreads.setText("Maximum nr of threads:");
  264.         GridDataFactory.fillDefaults().span(2, 1).applyTo(lbMaxThreads);
  265.  
  266.         maxThreads = new Spinner(composite, SWT.BORDER);
  267.         maxThreads.setMinimum(0);
  268.         maxThreads.setMaximum(100);
  269.         maxThreads.setSelection(TaskManager.maxThreads);
  270.         GridDataFactory.fillDefaults().grab(true, false).applyTo(maxThreads);
  271.  
  272.         lbThreadGapTime = new Label(composite, SWT.NONE);
  273.         lbThreadGapTime.setText("Time gap between threads start:");
  274.         GridDataFactory.fillDefaults().span(2, 1).applyTo(lbThreadGapTime);
  275.  
  276.         threadGapTime = new Spinner(composite, SWT.BORDER);
  277.         threadGapTime.setMinimum(0);
  278.         threadGapTime.setMaximum(120000);
  279.         threadGapTime.setSelection(TaskManager.threadGapTime);
  280.         GridDataFactory.fillDefaults().grab(true, false).applyTo(threadGapTime);
  281.        
  282.         lbIncrementBy = new Label(composite, SWT.NONE);
  283.         lbIncrementBy.setText("Increment bid nr by:");
  284.         GridDataFactory.fillDefaults().span(2, 1).applyTo(lbIncrementBy);
  285.  
  286.         incrementBidNrBy = new Spinner(composite, SWT.BORDER);
  287.         incrementBidNrBy.setMinimum(0);
  288.         incrementBidNrBy.setMaximum(500);
  289.         incrementBidNrBy.setSelection(TaskManager.incrementBy);
  290.         GridDataFactory.fillDefaults().grab(true, false)
  291.                 .applyTo(incrementBidNrBy);
  292.        
  293.         table = new Table(parent, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL);
  294.         if(flag)
  295.             loadData();
  296.         table.setLayoutData(GridDataFactory.fillDefaults().hint(100, 100).create());
  297.            
  298.         return composite;
  299.     }
  300.    
  301.     private void loadData() {
  302.         selectedItems.clear();
  303.         table.removeAll();
  304.         try {
  305.             document = bidHelper.parse(text.getText());
  306.         } catch (DocumentException e1) {
  307.             e1.printStackTrace();
  308.         }
  309.         if (document != null) {
  310.             timeSeriesList = document.getTimeSeries();
  311.         }
  312.        
  313.         items = new HashSet<String>();
  314.         for (int i = 0; i< timeSeriesList.size(); ++i) {
  315.             items.add(timeSeriesList.get(i).getDimensions().getBusinessDimension().get(0).getV());
  316.             String session = timeSeriesList.get(i).getDimensions().getBusinessDimension().get(1).getV();
  317.             session = session.substring(7);
  318.             sessions.add(Integer.valueOf(session));
  319.         }
  320.        
  321.         for (String s : items) {
  322.               TableItem item = new TableItem(table, SWT.NONE);
  323.               item.setText(translateName(s));
  324.             }
  325.        
  326.         table.addListener(SWT.Selection, new Listener() {
  327.             @Override
  328.             public void handleEvent (Event event) {
  329.                 if (event.detail == SWT.CHECK) {
  330.                     String s = event.item.toString();
  331.                     s = s.substring(s.indexOf("{") + 1);
  332.                     s = s.substring(0, s.indexOf("}"));
  333.                     boolean status = selectedItems.add(reverseName(s));
  334.                     if(!status)
  335.                         selectedItems.remove(reverseName(s));
  336.                 }
  337.             }
  338.         });    
  339.     }
  340.    
  341.     public Set<String> getSelectedItems() {
  342.         return selectedItems;
  343.     }
  344.    
  345.     public Set<Integer> getSessions() {
  346.         return sessions;
  347.     }
  348.    
  349.     public String translateName(String name) {
  350.         name = name.substring(4);
  351.         name = name.replaceAll("_", ">>");
  352.         return name;
  353.     }
  354.    
  355.     public String reverseName(String name) {
  356.         String temp = "CAD_"+name;
  357.         name = temp.replaceAll(">>", "_");
  358.         return name;
  359.     }
  360.    
  361.     public List<ClsTimeSeries> getTimeSeriesList() {
  362.         return timeSeriesList;
  363.     }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement