1 32 33 package com.nqadmin.swingSet; 34 35 import java.awt.Toolkit ; 36 import java.awt.event.KeyAdapter ; 37 import java.awt.event.KeyEvent ; 38 import java.awt.datatransfer.Clipboard ; 39 import java.awt.datatransfer.StringSelection ; 40 import java.awt.datatransfer.DataFlavor ; 41 import java.awt.datatransfer.Transferable ; 42 import java.awt.datatransfer.UnsupportedFlavorException ; 43 import java.util.StringTokenizer ; 44 import javax.swing.JTable ; 45 import javax.swing.JOptionPane ; 46 import java.lang.reflect.Constructor ; 47 import java.io.IOException ; 48 import java.io.Serializable ; 49 50 61 public class SSTableKeyAdapter extends KeyAdapter implements Serializable { 62 63 66 protected int onMask = KeyEvent.CTRL_DOWN_MASK; 67 68 71 protected int offMask = KeyEvent.ALT_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK; 72 73 76 protected boolean allowInsertion = false; 77 78 81 protected boolean forSSDataGrid = false; 82 83 88 public SSTableKeyAdapter(JTable _jTable) { 89 init(_jTable); 90 } 91 92 99 public void setAllowInsertion(boolean _allowInsertion) { 100 allowInsertion = _allowInsertion; 101 } 102 103 110 public void setForSSDataGrid(boolean _forSSDataGrid) { 111 forSSDataGrid = _forSSDataGrid; 112 } 113 114 117 protected void init(JTable _jTable) { 118 _jTable.addKeyListener(this); 119 } 120 121 124 public void keyReleased(KeyEvent ke) { 125 StringBuffer strBuf = new StringBuffer (); 126 127 JTable jTable = (JTable )ke.getSource(); 128 131 if (((ke.getModifiersEx() & (onMask | offMask)) == onMask) && ke.getKeyCode() == KeyEvent.VK_C) { 132 135 138 int numRows = jTable.getSelectedRowCount(); 140 int numColumns = 0; 141 if (jTable instanceof SSDataGrid) { 142 numColumns = ((SSDataGrid)jTable).getSelectedColumnCount(); 143 } else { 144 numColumns = jTable.getSelectedColumnCount(); 145 } 146 147 if (numRows < 1 || numColumns < 1) { 150 return; 151 } 152 153 int[] selectedRows = jTable.getSelectedRows(); 155 int[] selectedColumns = null; 156 if (jTable instanceof SSDataGrid) { 157 selectedColumns = ((SSDataGrid)jTable).getSelectedColumns(); 158 } else { 159 selectedColumns = jTable.getSelectedColumns(); 160 } 161 162 for (int i=0; i<selectedRows.length; i++) { 165 for (int j=0; j<selectedColumns.length; j++) { 166 strBuf.append(jTable.getValueAt(selectedRows[i], selectedColumns[j])); 167 if (j < (numColumns -1)) { 168 strBuf.append("\t"); 169 } 170 } 171 strBuf.append("\n"); 172 } 173 174 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 176 177 StringSelection stringSelection = new StringSelection (strBuf.toString()); 179 180 clipboard.setContents(stringSelection,stringSelection); 182 183 } else if (((ke.getModifiersEx() & (onMask | offMask)) == onMask) && ke.getKeyCode() == KeyEvent.VK_V) { 184 187 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 189 190 Transferable transferable = clipboard.getContents(this); 192 193 if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) { 195 String strData = ""; 197 try { 198 strData = (String )transferable.getTransferData(DataFlavor.stringFlavor); 199 System.out.println(strData); 200 } catch(UnsupportedFlavorException ufe) { 201 ufe.printStackTrace(); 202 return; 203 } catch(IOException ioe) { 204 ioe.printStackTrace(); 205 return; 206 } 207 208 if (jTable.getSelectedRowCount() < 1 || jTable.getSelectedColumnCount() < 1) { 210 return; 211 } 212 213 int[] selectedRows = jTable.getSelectedRows(); 215 int[] selectedColumns = jTable.getSelectedColumns(); 216 217 StringTokenizer rowTokens = new StringTokenizer (strData, "\n", false); 219 220 int numRows = 0; 222 int numColumns = 0; 223 224 StringTokenizer columnTokens; 225 226 if (rowTokens.hasMoreTokens()) { 229 numRows++; 230 columnTokens = new StringTokenizer (rowTokens.nextToken(), "\t", false); 231 for (; columnTokens.hasMoreTokens(); numColumns++) { 233 columnTokens.nextToken(); 234 } 235 } 236 237 for(; rowTokens.hasMoreTokens(); numRows++){ 239 rowTokens.nextToken(); 240 } 241 242 int rowCount = jTable.getRowCount(); 244 int columnCount = jTable.getColumnCount(); 245 246 if (forSSDataGrid || jTable instanceof SSDataGrid) { 247 rowCount--; 248 } 249 250 if (columnCount < (numColumns + selectedColumns[0])) { 255 JOptionPane.showMessageDialog(jTable, "There are not enough columns in the table to copy into.\n"); 256 return; 257 } 258 259 int numRowsToCopy = numRows; 260 261 if (rowCount < (numRows + selectedRows[0])) { 263 JOptionPane.showMessageDialog(jTable, "There are not enough rows in the table to copy into.\n"); 264 if (allowInsertion) { 265 int option = JOptionPane.showConfirmDialog(jTable, "Do you want to insert new rows?", "Add Rows", JOptionPane.YES_NO_OPTION); 266 if (option == JOptionPane.YES_OPTION) { 267 numRowsToCopy = numRows; 270 } else if (JOptionPane.showConfirmDialog(jTable, "Do you want to copy data into just the rows present?", "Limited Copy", JOptionPane.YES_NO_OPTION) 271 == JOptionPane.YES_OPTION) { 272 numRowsToCopy = rowCount - selectedRows[0]; 275 } 276 } else if (JOptionPane.showConfirmDialog(jTable, "Do you want to copy data into just the rows present?", "Limited Copy", JOptionPane.YES_NO_OPTION) 277 == JOptionPane.YES_OPTION) { 278 numRowsToCopy = rowCount - selectedRows[0]; 281 } 282 283 } 284 285 try { 287 rowTokens = new StringTokenizer (strData, "\n", false); 289 290 for (int i=0;rowTokens.hasMoreTokens() && i<numRowsToCopy; i++) { 293 columnTokens = new StringTokenizer (rowTokens.nextToken(), "\t", false); 295 for (int j=0; columnTokens.hasMoreTokens(); j++) { 297 Object newValue = getObjectToSet(jTable, selectedColumns[0]+j, columnTokens.nextToken()); 299 jTable.setValueAt(newValue, selectedRows[0]+i, selectedColumns[0]+j); 301 } 302 } 303 304 jTable.updateUI(); 307 308 } catch(NoSuchMethodException nsme) { 309 nsme.printStackTrace(); 310 JOptionPane.showMessageDialog(jTable, "One of the column class does not provide a constructor" 311 + "that takes a single String Argument"); 312 } catch(SecurityException se) { 313 se.printStackTrace(); 314 JOptionPane.showMessageDialog(jTable, "One of the column class does not provide a constructor" 315 + "that takes a single String Argument"); 316 } catch(InstantiationException ie) { 317 ie.printStackTrace(); 318 JOptionPane.showMessageDialog(jTable, "Failed to copy data. Error occured while instantiating" 319 +"a single String argument constructor for a column "); 320 } catch(Exception e) { 321 e.printStackTrace(); 322 JOptionPane.showMessageDialog(jTable, "Failed to copy data."); 323 } 324 325 } 327 } 329 } 331 342 protected Object getObjectToSet(JTable _jTable, int _column, String _value) throws Exception { 343 Class objectClass = _jTable.getColumnClass(_column); 345 Object newValue = null; 346 try { 347 Constructor constructor = objectClass.getConstructor(new Class []{String .class}); 349 350 newValue = constructor.newInstance(new Object []{_value}); 352 353 } catch(NoSuchMethodException nsme) { 354 newValue = _value; 355 } 356 357 return newValue; 359 360 } 361 362 } 364 | Popular Tags |