Exemple #1
0
  /** Export the selected values from the table to a csv file */
  private void export() {

    FileDialog dialog = new FileDialog(shell, SWT.SAVE);
    String[] filterNames = new String[] {"CSV Files", "All Files (*)"};
    String[] filterExtensions = new String[] {"*.csv;", "*"};
    String filterPath = "/";
    String platform = SWT.getPlatform();
    if (platform.equals("win32") || platform.equals("wpf")) {
      filterNames = new String[] {"CSV Files", "All Files (*.*)"};
      filterExtensions = new String[] {"*.csv", "*.*"};
      filterPath = "c:\\";
    }
    dialog.setFilterNames(filterNames);
    dialog.setFilterExtensions(filterExtensions);
    dialog.setFilterPath(filterPath);
    try {
      dialog.setFileName(this.getCurrentKey() + ".csv");
    } catch (Exception e) {
      dialog.setFileName("export.csv");
    }
    String fileName = dialog.open();

    FileOutputStream fos;
    OutputStreamWriter out;
    try {
      fos = new FileOutputStream(fileName);
      out = new OutputStreamWriter(fos, "UTF-8");
    } catch (Exception e) {
      MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
      messageBox.setMessage("Error creating export file " + fileName + " : " + e.getMessage());
      messageBox.open();
      return;
    }

    Vector<TableItem> sel = new Vector<TableItem>(this.dataTable.getSelection().length);

    sel.addAll(Arrays.asList(this.dataTable.getSelection()));
    Collections.reverse(sel);

    for (TableItem item : sel) {
      String date = item.getText(0);
      String value = item.getText(1);
      try {
        out.write(date + ";" + value + "\n");
      } catch (IOException e) {
        continue;
      }
    }

    try {
      out.close();
      fos.close();
    } catch (IOException e) {
      MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
      messageBox.setMessage("Error writing export file " + fileName + " : " + e.getMessage());
      messageBox.open();
      e.printStackTrace();
    }
  }
  /**
   * Handle the create button selection event.
   *
   * @param event org.eclipse.swt.events.SelectionEvent
   */
  void createButtonSelected(SelectionEvent event) {

    /* Compute the appropriate dialog style */
    int style = getDefaultStyle();
    if (okButton.getEnabled() && okButton.getSelection()) style |= SWT.OK;
    if (cancelButton.getEnabled() && cancelButton.getSelection()) style |= SWT.CANCEL;
    if (yesButton.getEnabled() && yesButton.getSelection()) style |= SWT.YES;
    if (noButton.getEnabled() && noButton.getSelection()) style |= SWT.NO;
    if (retryButton.getEnabled() && retryButton.getSelection()) style |= SWT.RETRY;
    if (abortButton.getEnabled() && abortButton.getSelection()) style |= SWT.ABORT;
    if (ignoreButton.getEnabled() && ignoreButton.getSelection()) style |= SWT.IGNORE;
    if (iconErrorButton.getEnabled() && iconErrorButton.getSelection()) style |= SWT.ICON_ERROR;
    if (iconInformationButton.getEnabled() && iconInformationButton.getSelection())
      style |= SWT.ICON_INFORMATION;
    if (iconQuestionButton.getEnabled() && iconQuestionButton.getSelection())
      style |= SWT.ICON_QUESTION;
    if (iconWarningButton.getEnabled() && iconWarningButton.getSelection())
      style |= SWT.ICON_WARNING;
    if (iconWorkingButton.getEnabled() && iconWorkingButton.getSelection())
      style |= SWT.ICON_WORKING;
    if (primaryModalButton.getEnabled() && primaryModalButton.getSelection())
      style |= SWT.PRIMARY_MODAL;
    if (applicationModalButton.getEnabled() && applicationModalButton.getSelection())
      style |= SWT.APPLICATION_MODAL;
    if (systemModalButton.getEnabled() && systemModalButton.getSelection())
      style |= SWT.SYSTEM_MODAL;
    if (saveButton.getEnabled() && saveButton.getSelection()) style |= SWT.SAVE;
    if (openButton.getEnabled() && openButton.getSelection()) style |= SWT.OPEN;
    if (multiButton.getEnabled() && multiButton.getSelection()) style |= SWT.MULTI;

    /* Open the appropriate dialog type */
    String name = dialogCombo.getText();

    if (name.equals(ControlExample.getResourceString("ColorDialog"))) {
      ColorDialog dialog = new ColorDialog(shell, style);
      dialog.setRGB(new RGB(100, 100, 100));
      dialog.setText(ControlExample.getResourceString("Title"));
      RGB result = dialog.open();
      textWidget.append(ControlExample.getResourceString("ColorDialog") + Text.DELIMITER);
      textWidget.append(
          ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER);
      textWidget.append("getRGB() = " + dialog.getRGB() + Text.DELIMITER + Text.DELIMITER);
      return;
    }

    if (name.equals(ControlExample.getResourceString("DirectoryDialog"))) {
      DirectoryDialog dialog = new DirectoryDialog(shell, style);
      dialog.setMessage(ControlExample.getResourceString("Example_string"));
      dialog.setText(ControlExample.getResourceString("Title"));
      String result = dialog.open();
      textWidget.append(ControlExample.getResourceString("DirectoryDialog") + Text.DELIMITER);
      textWidget.append(
          ControlExample.getResourceString("Result", new String[] {"" + result})
              + Text.DELIMITER
              + Text.DELIMITER);
      return;
    }

    if (name.equals(ControlExample.getResourceString("FileDialog"))) {
      FileDialog dialog = new FileDialog(shell, style);
      dialog.setFileName(ControlExample.getResourceString("readme_txt"));
      dialog.setFilterNames(FilterNames);
      dialog.setFilterExtensions(FilterExtensions);
      dialog.setText(ControlExample.getResourceString("Title"));
      String result = dialog.open();
      textWidget.append(ControlExample.getResourceString("FileDialog") + Text.DELIMITER);
      textWidget.append(
          ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER);
      textWidget.append("getFileNames() =" + Text.DELIMITER);
      if ((dialog.getStyle() & SWT.MULTI) != 0) {
        String[] files = dialog.getFileNames();
        for (int i = 0; i < files.length; i++) {
          textWidget.append("\t" + files[i] + Text.DELIMITER);
        }
      }
      textWidget.append(
          "getFilterIndex() = " + dialog.getFilterIndex() + Text.DELIMITER + Text.DELIMITER);
      return;
    }

    if (name.equals(ControlExample.getResourceString("FontDialog"))) {
      FontDialog dialog = new FontDialog(shell, style);
      dialog.setText(ControlExample.getResourceString("Title"));
      FontData result = dialog.open();
      textWidget.append(ControlExample.getResourceString("FontDialog") + Text.DELIMITER);
      textWidget.append(
          ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER);
      textWidget.append("getFontList() =" + Text.DELIMITER);
      FontData[] fonts = dialog.getFontList();
      if (fonts != null) {
        for (int i = 0; i < fonts.length; i++) {
          textWidget.append("\t" + fonts[i] + Text.DELIMITER);
        }
      }
      textWidget.append("getRGB() = " + dialog.getRGB() + Text.DELIMITER + Text.DELIMITER);
      return;
    }

    if (name.equals(ControlExample.getResourceString("PrintDialog"))) {
      PrintDialog dialog = new PrintDialog(shell, style);
      dialog.setText(ControlExample.getResourceString("Title"));
      PrinterData result = dialog.open();
      textWidget.append(ControlExample.getResourceString("PrintDialog") + Text.DELIMITER);
      textWidget.append(
          ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER);
      textWidget.append("getScope() = " + dialog.getScope() + Text.DELIMITER);
      textWidget.append("getStartPage() = " + dialog.getStartPage() + Text.DELIMITER);
      textWidget.append("getEndPage() = " + dialog.getEndPage() + Text.DELIMITER);
      textWidget.append(
          "getPrintToFile() = " + dialog.getPrintToFile() + Text.DELIMITER + Text.DELIMITER);
      return;
    }

    if (name.equals(ControlExample.getResourceString("MessageBox"))) {
      MessageBox dialog = new MessageBox(shell, style);
      dialog.setMessage(ControlExample.getResourceString("Example_string"));
      dialog.setText(ControlExample.getResourceString("Title"));
      int result = dialog.open();
      textWidget.append(ControlExample.getResourceString("MessageBox") + Text.DELIMITER);
      /*
       * The resulting integer depends on the original
       * dialog style.  Decode the result and display it.
       */
      switch (result) {
        case SWT.OK:
          textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.OK"}));
          break;
        case SWT.YES:
          textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.YES"}));
          break;
        case SWT.NO:
          textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.NO"}));
          break;
        case SWT.CANCEL:
          textWidget.append(
              ControlExample.getResourceString("Result", new String[] {"SWT.CANCEL"}));
          break;
        case SWT.ABORT:
          textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.ABORT"}));
          break;
        case SWT.RETRY:
          textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.RETRY"}));
          break;
        case SWT.IGNORE:
          textWidget.append(
              ControlExample.getResourceString("Result", new String[] {"SWT.IGNORE"}));
          break;
        default:
          textWidget.append(ControlExample.getResourceString("Result", new String[] {"" + result}));
          break;
      }
      textWidget.append(Text.DELIMITER + Text.DELIMITER);
    }
  }
 public Gfui_dlg_file Init_file_(String v) {
   under.setFileName(v);
   return this;
 }