package com.github.mateuszwenus.template_processor;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;

import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;

public class PasteFromSpreadsheetHandler implements ActionListener {

	private final JTable table;
	private final ResourceBundle resourceBundle;

	private PasteFromSpreadsheetHandler(JTable table, ResourceBundle resourceBundle) {
		this.table = table;
		this.resourceBundle = resourceBundle;
	}

	public static void registerPasteFromSpreadsheetHandler(JTable table, ResourceBundle resourceBundle) {
		KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK);
		table.registerKeyboardAction(new PasteFromSpreadsheetHandler(table, resourceBundle), keyStroke, JComponent.WHEN_FOCUSED);
	}

	public void actionPerformed(ActionEvent e) {
		int startRow = table.getSelectedRow();
		int startCol = table.getSelectedColumn();
		if (startRow != -1 && startCol != -1) {
			try {
				String clipboard = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this)
						.getTransferData(DataFlavor.stringFlavor);
				String[] rows = clipboard.split("\n");
				for (int row = 0; row < Math.min(rows.length, table.getRowCount() - startRow); row++) {
					String[] cols = rows[row].split("\t");
					for (int col = 0; col < Math.min(cols.length, table.getColumnCount() - startCol); col++) {
						table.setValueAt(cols[col], startRow + row, startCol + col);
					}
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, resourceBundle.getString("app.error") + ": " + ex.getMessage());
			}
		}
	}

}

@@ -370,16 +370,60 @@
 yStroke;
+%0Aimport javax.swing.table.DefaultTableModel;
 %0A%0Apublic
@@ -1398,24 +1398,65 @@
 plit(%22%5Cn%22);%0A
+%09%09%09%09ensureTableCapacity(rows, startRow);%0A
 %09%09%09%09for (int
@@ -1471,25 +1471,16 @@
 ; row %3C 
-Math.min(
 rows.len
@@ -1486,41 +1486,8 @@
 ngth
-, table.getRowCount() - startRow)
 ; ro
@@ -1859,10 +1859,283 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private void ensureTableCapacity(String%5B%5D rows, int startRow) %7B%0A%09%09Object%5B%5D rowData = null;%0A%09%09int numMissingRows = rows.length - table.getRowCount() + startRow;%0A%09%09for (int i = 0; i %3C numMissingRows; i++) %7B%0A%09%09%09((DefaultTableModel) table.getModel()).addRow(rowData);%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

create enough table rows to store pasted content