/** * 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(); } } }
/** Update state from Infostat. */ public void updateStatus(String msg) { // Messages.postDebug("VLcStatusChart.updateStatus(" + msg + ")");/*CMP*/ if (msg == null) { return; } StringTokenizer tok = new StringTokenizer(msg); if (tok.hasMoreTokens()) { String key = tok.nextToken(); String val = ""; if (tok.hasMoreTokens()) { val = tok.nextToken("").trim(); // Get remainder of msg } if (key.equals(statkey)) { if (val != null && !val.equals("-")) { valstr = val; setState(state); } /*System.out.println("Chart: statkey=" + statkey + ", val=" + val);/*CMP*/ } if (key.equals(statpar)) { if (val != null && !val.equals("-")) { /*System.out.println("Chart statpar=" + statpar + ", value=" + value);/*CMP*/ setState(state); } } if (key.equals(statset)) { if (val != null && !val.equals("-")) { setval = val; try { String num = val.substring(0, val.indexOf(' ')); setStatusValue(Double.parseDouble(num)); } catch (NumberFormatException nfe) { Messages.postDebug("VLcStatusChart.updateStatus(): " + "Non-numeric value: " + msg); } catch (StringIndexOutOfBoundsException sioobe) { setStatusValue(0); // No value found } /*System.out.println("Chart statset=" + statset + ", setval=" + setval);/*CMP*/ setState(state); } } } repaint(); }
private String subString(String s, String m, String d) { String r = ""; StringTokenizer tok = new StringTokenizer(s, " \t\':+-*/\"/()=,\0", true); while (tok.hasMoreTokens()) { String t = tok.nextToken(); if (t.equals(m)) r += d; else r += t; } return r; }