boolean value = ClipBoardExcel.pasteFromExcel(spreadsheetView);
//If no excel format is found, we go with the normal behavior.
if (!value) {
super.pasteClipboard();
}
/**
* Try to find an Excel format in the clipboard.
*
* @param formats
* @return the DataFormat identified, or null if it has not been found.
*/
private static DataFormat findExcelFormat(Set<DataFormat> formats) {
for (DataFormat format : formats) {
if (format.getIdentifiers().contains(EXCEL_IDENTIFIER)) {
return format;
}
}
return null;
}
/**
* Return whether the past from Excel has succeed.
*
* @param spv
* @return
*/
public static boolean pasteFromExcel(SpreadsheetView spv) {
if (!spv.isEditable()) {
return false;
}
final Clipboard clipboard = Clipboard.getSystemClipboard();
DataFormat excelFormat = findExcelFormat(clipboard.getContentTypes());
if (excelFormat == null) {
return false;
}
Workbook wb;
try {
wb = new HSSFWorkbook(new ByteArrayInputStream(((ByteBuffer) clipboard.getContent(excelFormat)).array()));
} catch (IOException ex) {
LOGGER.error("Problem in ClipBoardExcel when trying to get the clipboard content.", ex);
return false;
}
final TablePosition<?, ?> focusedCell = spv.getSelectionModel().getFocusedCell();
/**
* We need to keep records of the different indexes we are considering.
* Then we can detect if we have some gap between the indexes in order
* to respect these gap when pasting in Spv.
*/
int currentRow = focusedCell.getRow();
final int baseColumn = focusedCell.getColumn();
int currentColumn = focusedCell.getColumn();
if (currentRow == -1 || currentColumn == -1) {
return false;
}
pasteIntoSpreadsheetView(wb, currentRow, baseColumn, currentColumn, spv);
return true;
}
private static void pasteIntoSpreadsheetView(Workbook wb, int currentRow, int baseColumn, int currentColumn, SpreadsheetView spv) {
int oldIndex = 0;
int newIndex = 0;
final Grid grid = spv.getGrid();
final List<ObservableList<SpreadsheetCell>> rows = grid.getRows();
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
/**
* The idea here is to keep track of the old indexes in order to see
* if we have some gap between rows in Excel.
*
* So if the next row is situated 3 rows ahead, we will have a
* newIndex-oldIndex = 3 and that will help us target the right row
* in our own grid, instead of pasting everything next to each
* other.
*/
newIndex = row.getRowNum();
oldIndex = oldIndex == 0 ? row.getRowNum() - 1 : oldIndex;
currentRow += newIndex - oldIndex - 1;
if (currentRow >= grid.getRowCount()) {
continue;
}
for (Cell cell : row) {
final SpreadsheetView.SpanType type = spv.getSpanType(currentRow, currentColumn);
if (type == SpreadsheetView.SpanType.NORMAL_CELL || type == SpreadsheetView.SpanType.ROW_VISIBLE) {
SpreadsheetCell spc = rows.get(currentRow).get(currentColumn);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
boolean succeedString = spc.getCellType().match(cell.getRichStringCellValue().getString());
if (succeedString) {
grid.setCellValue(spc.getRow(), spc.getColumn(),
spc.getCellType().convertValue(cell.getRichStringCellValue().getString()));
}
break;
case Cell.CELL_TYPE_NUMERIC:
case Cell.CELL_TYPE_FORMULA:
//FIXME Verify DateTime here.
if (DateUtil.isCellDateFormatted(cell)) {
boolean succeedDate = spc.getCellType().match((double) cell.getDateCellValue().getTime());
if (succeedDate) {
grid.setCellValue(spc.getRow(), spc.getColumn(),
spc.getCellType().convertValue((double) cell.getDateCellValue().getTime()));
}
} else {
boolean succeedNumber = spc.getCellType().match(cell.getNumericCellValue());
if (succeedNumber) {
grid.setCellValue(spc.getRow(), spc.getColumn(),
spc.getCellType().convertValue(cell.getNumericCellValue()));
}
}
break;
case Cell.CELL_TYPE_BOOLEAN:
// System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK:
spv.getGrid().setCellValue(spc.getRow(), spc.getColumn(), null);
break;
default:
}
}
currentColumn++;
}
//Reset the column
currentColumn = baseColumn;
currentRow++;
oldIndex = row.getRowNum();
}
}Hi,
scene.getAccelerators().put(new KeyCodeCombination(KeyCode.X, KeyCombination.CONTROL_ANY), new Runnable() { @Override
public void run() {
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
ObservableList<TablePosition> focusedCell = spv.getSelectionModel().getSelectedCells(); int currentRow = 0;
int currentColumn = 0;
String contentText = ""; for (final TablePosition<?, ?> p : focusedCell) {
int tempRow = currentRow;
int tempColumn = currentColumn; currentRow = p.getRow();
currentColumn = p.getColumn(); String spcText = rows.get(currentRow).get(currentColumn).getText();
contentText += spcText; } content.putString(contentText);
clipboard.setContent(content); System.out.println("clipboard.getString() : " + clipboard.getString());
}
});new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN)if (clipboard.getContent(fmt) != null) {
@SuppressWarnings("unchecked")
final ArrayList<GridChange> list = (ArrayList<GridChange>) clipboard.getContent(fmt);
if (list.size() == 1) {
pasteOneValue(list.get(0));
} else if (selectedCells.size() > 1) {
pasteMixedValues(list);
} else {
pasteSeveralValues(list);
}if ((fmt = DataFormat.lookupMimeType("SpreadsheetView")) == null) { //$NON-NLS-1$
fmt = new DataFormat("SpreadsheetView"); //$NON-NLS-1$
} boolean value = ClipBoardExcel.pasteFromExcel(spreadsheetView);
//If no excel format is found, we go with the normal behavior.
if (!value) {
super.pasteClipboard();
}final Clipboard clipboard = Clipboard.getSystemClipboard();
DataFormat excelFormat = findExcelFormat(clipboard.getContentTypes());
if (excelFormat == null) {
return false;
}
/**
* Try to find an Excel format in the clipboard.
*
* @param formats
* @return the DataFormat identified, or null if it has not been found.
*/
private static DataFormat findExcelFormat(Set<DataFormat> formats) {
for (DataFormat format : formats) {
if (format.getIdentifiers().contains(EXCEL_IDENTIFIER)) {
return format;
}
}
return null;
}
private static final String EXCEL_IDENTIFIER = "Biff8";