Gets the data selected by the user in this datatable. This method is modified from the org.opensourcephysics.display.DataTableFrame getSelectedData method. @param asFormatted true to retain table formatting @return a StringBuffer containing the data.
(boolean asFormatted)
| 2622 | * @return a StringBuffer containing the data. |
| 2623 | */ |
| 2624 | public StringBuffer getData(boolean asFormatted) { |
| 2625 | StringBuffer buf = new StringBuffer(); |
| 2626 | // get selected data |
| 2627 | int[] selectedRows = getSelectedRows(); |
| 2628 | int[] selectedColumns = getSelectedColumns(); |
| 2629 | // if no data is selected, select all |
| 2630 | int[] restoreRows = null; |
| 2631 | int[] restoreColumns = null; |
| 2632 | if (selectedRows.length == 0) { |
| 2633 | selectAll(); |
| 2634 | restoreRows = selectedRows; |
| 2635 | restoreColumns = selectedColumns; |
| 2636 | selectedRows = getSelectedRows(); |
| 2637 | selectedColumns = getSelectedColumns(); |
| 2638 | } |
| 2639 | if (includeHeadersInCopiedData) { |
| 2640 | // copy column headings |
| 2641 | for (int j = 0; j < selectedColumns.length; j++) { |
| 2642 | // ignore row heading |
| 2643 | if (isRowNumberVisible() && selectedColumns[j] == 0) |
| 2644 | continue; |
| 2645 | String name = getColumnName(selectedColumns[j]); |
| 2646 | name = TeXParser.removeSubscripting(name); |
| 2647 | // if (name.startsWith(FunctionEditor.THETA)) { |
| 2648 | // for (int i = 0; i < selectedRows.length; i++) { |
| 2649 | // Object val = getFormattedValueAt(selectedRows[i], selectedColumns[j]); |
| 2650 | // if (val != null && val.toString().contains(FunctionEditor.DEGREES)) { |
| 2651 | // name += "(" + FunctionEditor.DEGREES + ")"; |
| 2652 | // break; |
| 2653 | // } |
| 2654 | // } |
| 2655 | // } |
| 2656 | buf.append(name); |
| 2657 | if (j < selectedColumns.length - 1) |
| 2658 | buf.append(VideoIO.getDelimiter()); // add delimiter after each column except the last |
| 2659 | } |
| 2660 | buf.append(XML.NEW_LINE); |
| 2661 | } |
| 2662 | java.text.DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(); |
| 2663 | nf.applyPattern("0.000000E0"); //$NON-NLS-1$ |
| 2664 | nf.setDecimalFormatSymbols(OSPRuntime.getDecimalFormatSymbols()); |
| 2665 | java.text.DateFormat df = java.text.DateFormat.getInstance(); |
| 2666 | for (int i = 0; i < selectedRows.length; i++) { |
| 2667 | for (int j = 0; j < selectedColumns.length; j++) { |
| 2668 | int temp = convertColumnIndexToModel(selectedColumns[j]); |
| 2669 | if (isRowNumberVisible()) { |
| 2670 | if (temp == 0) { // don't copy row numbers |
| 2671 | continue; |
| 2672 | } |
| 2673 | } |
| 2674 | Object value = null; |
| 2675 | if (asFormatted) { |
| 2676 | value = getFormattedValueAt(selectedRows[i], selectedColumns[j]); |
| 2677 | } else { |
| 2678 | value = getValueAt(selectedRows[i], selectedColumns[j]); |
| 2679 | if (value != null) { |
| 2680 | if (value instanceof Number) { |
| 2681 | value = nf.format(value); |
no test coverage detected