private String extractContent(
      DataGridPanel grid, String delimiter, boolean saveHeader, boolean ecloseWithDowbleQuotes) {
    JTable _table = grid.getTable();

    int numcols = _table.getSelectedColumnCount();
    int numrows = _table.getSelectedRowCount();

    if (numcols > 0 && numrows > 0) {
      StringBuffer sbf = new StringBuffer();
      int[] rowsselected = _table.getSelectedRows();
      int[] colsselected = _table.getSelectedColumns();

      if (saveHeader) {
        // put header name list
        for (int j = 0; j < numcols; j++) {
          String text =
              (String)
                  _table
                      .getTableHeader()
                      .getColumnModel()
                      .getColumn(colsselected[j])
                      .getHeaderValue();
          sbf.append(text);
          if (j < numcols - 1) sbf.append(delimiter);
        }
        sbf.append("\n");
      }

      // put content
      for (int i = 0; i < numrows; i++) {
        for (int j = 0; j < numcols; j++) {
          Object value = _table.getValueAt(rowsselected[i], colsselected[j]);
          String _value = "";
          if (value != null) {
            _value = value.toString();
          }
          if (ecloseWithDowbleQuotes) {
            sbf.append("\"").append(_value).append("\"");
          } else {
            sbf.append(_value);
          }

          if (j < numcols - 1) sbf.append(delimiter);
        }
        sbf.append("\n");
      }

      //            StringSelection stsel = new StringSelection(sbf.toString());
      //            Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard();
      //            system.setContents(stsel, stsel);
      return sbf.toString();
    }

    return null;
  }
예제 #2
0
 /**
  * This method is activated on the Keystrokes we are listening to in this implementation. Here it
  * listens for Copy and Paste ActionCommands. Selections comprising non-adjacent cells result in
  * invalid selection and then copy action cannot be performed. Paste is done by aligning the upper
  * left corner of the selection with the 1st element in the current selection of the JTable.
  */
 public void actionPerformed(ActionEvent e) {
   if (e.getActionCommand().compareTo("Copy") == 0) {
     StringBuffer sbf = new StringBuffer();
     // Check to ensure we have selected only a contiguous block of
     // cells
     int numcols = jTable1.getSelectedColumnCount();
     int numrows = jTable1.getSelectedRowCount();
     int[] rowsselected = jTable1.getSelectedRows();
     int[] colsselected = jTable1.getSelectedColumns();
     if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0]
             && numrows == rowsselected.length)
         && (numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0]
             && numcols == colsselected.length))) {
       JOptionPane.showMessageDialog(
           null, "Invalid Copy Selection", "Invalid Copy Selection", JOptionPane.ERROR_MESSAGE);
       return;
     }
     for (int i = 0; i < numrows; i++) {
       for (int j = 0; j < numcols; j++) {
         sbf.append(jTable1.getValueAt(rowsselected[i], colsselected[j]));
         if (j < numcols - 1) sbf.append("\t");
       }
       sbf.append("\n");
     }
     stsel = new StringSelection(sbf.toString());
     system = Toolkit.getDefaultToolkit().getSystemClipboard();
     system.setContents(stsel, stsel);
   }
   if (e.getActionCommand().compareTo("Paste") == 0) {
     System.out.println("Trying to Paste");
     int startRow = (jTable1.getSelectedRows())[0];
     int startCol = (jTable1.getSelectedColumns())[0];
     try {
       String trstring =
           (String) (system.getContents(this).getTransferData(DataFlavor.stringFlavor));
       System.out.println("String is:" + trstring);
       StringTokenizer st1 = new StringTokenizer(trstring, "\n");
       for (int i = 0; st1.hasMoreTokens(); i++) {
         rowstring = st1.nextToken();
         StringTokenizer st2 = new StringTokenizer(rowstring, "\t");
         for (int j = 0; st2.hasMoreTokens(); j++) {
           value = (String) st2.nextToken();
           if (startRow + i < jTable1.getRowCount() && startCol + j < jTable1.getColumnCount())
             jTable1.setValueAt(value, startRow + i, startCol + j);
           System.out.println(
               "Putting " + value + "at row = " + startRow + i + "column = " + startCol + j);
         }
       }
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   }
 }
    public void setSelected(AnActionEvent event, boolean b) {
      switch (command) {
        case REFRESH_RESULTSET:
          {
            QueryResultPanel resultPanel = getSelectedResultPanel();
            if (isConnected
                && resultPanel instanceof DataGridPanel
                && resultPanel.isRefreshSupported()) {
              try {
                resultPanel.refresh();
              } catch (DBException e) {
              }
            }
            break;
          }
        case CLOSE_PANEL:
          close();
          break;
        case STICKY_OPTION:
          selected ^= true;
          break;
        case EXPORT_DATA:
          {
            QueryResultPanel resultPanel = getSelectedResultPanel();
            if (resultPanel instanceof DataGridPanel) {
              DataGridPanel grid = (DataGridPanel) resultPanel;
              JTable _table = grid.getTable();

              int numcols = _table.getSelectedColumnCount();
              int numrows = _table.getSelectedRowCount();

              if (numcols == 0 && numrows == 0) {
                // nothing to copy
                return;
              }

              ExportSettings settings = new ExportSettings();
              settings.show();
              if (!settings.isOK()) {
                return;
              }

              File fileToSave = null;
              if (settings.saveToFile()) {
                String path = settings.getFilePathToSave();
                if (path == null || path.length() == 0) {
                  Messages.showInfoMessage(
                      "File not specified, content will be saved in Clipboard",
                      "File not specified");
                  //                            } else if (new File(path).getParentFile().exists())
                  // {
                } else {
                  // todo --
                  fileToSave = new File(path);
                }
              }

              String delimiter;
              switch (settings.getDelimiter()) {
                case TAB:
                  delimiter = "\t";
                  break;
                case COMMA:
                  delimiter = ",";
                  break;
                default: //  SEMICOLON:
                  delimiter = ";";
                  break;
              }

              String content =
                  extractContent(
                      grid,
                      delimiter,
                      settings.saveColumnHeaders(),
                      settings.encloseFieldsInDowubleQuotes());
              if (content != null) {
                if (fileToSave != null) {
                  // save in the file
                  try {
                    StringUtils.string2file(content, fileToSave);
                  } catch (IOException e) {
                    Messages.showErrorDialog(e.getMessage(), "Export failed");
                  }
                } else {
                  // save in Clipboard
                  StringSelection stsel = new StringSelection(content);
                  Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard();
                  system.setContents(stsel, stsel);
                }
              }
            }
            break;
          }
      }
      // notifyListeners(command);
    }