Exemplo n.º 1
0
  public Shell open(Display display) {
    shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.addShellListener(
        new ShellAdapter() {
          @Override
          public void shellClosed(ShellEvent e) {
            e.doit = closeAddressBook();
          }
        });

    createMenuBar();

    searchDialog = new SearchDialog(shell);
    searchDialog.setSearchAreaNames(columnNames);
    searchDialog.setSearchAreaLabel(resAddressBook.getString("Column"));
    searchDialog.addFindListener(
        new FindListener() {
          public boolean find() {
            return findEntry();
          }
        });

    table = new Table(shell, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setMenu(createPopUpMenu());
    table.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetDefaultSelected(SelectionEvent e) {
            TableItem[] items = table.getSelection();
            if (items.length > 0) editEntry(items[0]);
          }
        });
    for (int i = 0; i < columnNames.length; i++) {
      TableColumn column = new TableColumn(table, SWT.NONE);
      column.setText(columnNames[i]);
      column.setWidth(150);
      final int columnIndex = i;
      column.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              sort(columnIndex);
            }
          });
    }

    newAddressBook();

    shell.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
    shell.open();
    return shell;
  }
  public Shell open(Display display) {

    // Window dressing - the icon
    windowIcon =
        new Image(display, getClass().getClassLoader().getResourceAsStream("icons/joanju.gif"));

    shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.addShellListener(
        new ShellAdapter() {
          public void shellClosed(ShellEvent e) {
            e.doit = closeAddressBook();
          }
        });

    shell.setImage(windowIcon);

    createMenuBar();

    searchDialog = new SearchDialog(shell);
    searchDialog.setSearchAreaNames(columnNames);
    searchDialog.setSearchAreaLabel(resMessages.getString("Column"));
    searchDialog.addFindListener(
        new FindListener() {
          public boolean find() {
            return findEntry();
          }
        });

    table = new Table(shell, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setMenu(createPopUpMenu());
    table.addSelectionListener(
        new SelectionAdapter() {
          public void widgetDefaultSelected(SelectionEvent e) {
            TableItem[] items = table.getSelection();
            if (items.length > 0) launchEditor(items[0]);
          }
        });
    int[] widths = {150, 50, 200, 200};
    for (int i = 0; i < columnNames.length; i++) {
      TableColumn column = new TableColumn(table, SWT.NONE);
      column.setText(columnNames[i]);
      column.setWidth(widths[i]);
      final int columnIndex = i;
      column.addSelectionListener(
          new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
              sort(columnIndex);
            }
          });
    }

    newAddressBook();

    shell.addDisposeListener(
        new DisposeListener() {
          public void widgetDisposed(DisposeEvent e) {
            windowIcon.dispose();
          }
        });

    shell.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
    shell.open();
    return shell;
  }
  private void openFile(String fileName, Table table) {
    String outStr;
    try {

      CSVReader reader = new CSVReader(new FileReader(fileName), ',', '"');
      String[] strLineArr;

      // Open the file that is the first
      // command line parameter
      FileInputStream fstream = new FileInputStream(fileName);
      // Get the object of DataInputStream
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;

      table.clearAll();
      table.removeAll();
      table.redraw();

      int length = 0;
      boolean first = true;
      // Read File Line By Line
      while ((strLineArr = reader.readNext()) != null) {
        // while ((strLine = br.readLine()) != null)   {
        // Print the content on the console
        // outStr = strLine;
        TableItem item = null;

        if (first) {
          length = strLineArr.length;
        } else {
          item = new TableItem(table, SWT.NONE);
        }

        int thisLen = strLineArr.length;
        if (thisLen <= length) {
          // String[] line = new String[length];
          for (int i = 0; i < thisLen; i++) {
            // line[i] = st.nextToken();
            // item.setText (i, st.nextToken());
            if (first) {
              TableColumn column = new TableColumn(table, SWT.NONE);
              column.setText(strLineArr[i]);
            } else {

              // System.out.println("-- "+i+" -- " + strLineArr[i]);
              item.setText(i, strLineArr[i]);
            }
          }
        }
        first = false;
      }

      // System.out.println("Finisehd file");

      for (int i = 0; i < length; i++) {
        table.getColumn(i).pack();
      }
      // System.out.println("finished packing");
      // Close the input stream
      in.close();
    } catch (Exception e) { // Catch exception if any
      System.err.println("Error: " + e.getMessage());
      e.printStackTrace();
    }
  }