public void valueChanged(ListSelectionEvent e) { ListSelectionModel lsm = (ListSelectionModel) e.getSource(); int firstIndex = e.getFirstIndex(); int lastIndex = e.getLastIndex(); boolean isAdjusting = e.getValueIsAdjusting(); output.append( "Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting + "; selected indexes:"); if (lsm.isSelectionEmpty()) { output.append(" <none>"); } else { // Find out which indexes are selected. int minIndex = lsm.getMinSelectionIndex(); int maxIndex = lsm.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (lsm.isSelectedIndex(i)) { output.append(" " + i); } } } output.append(newline); output.setCaretPosition(output.getDocument().getLength()); }
/** * Moves the column and heading at <code>columnIndex</code> to <code>newIndex</code>. The old * column at <code>columnIndex</code> will now be found at <code>newIndex</code>. The column that * used to be at <code>newIndex</code> is shifted left or right to make room. This will not move * any columns if <code>columnIndex</code> equals <code>newIndex</code>. This method also posts a * <code>columnMoved</code> event to its listeners. * * @param columnIndex the index of column to be moved * @param newIndex new index to move the column * @exception IllegalArgumentException if <code>column</code> or <code>newIndex</code> are not in * the valid range */ public void moveColumn(int columnIndex, int newIndex) { if ((columnIndex < 0) || (columnIndex >= getColumnCount()) || (newIndex < 0) || (newIndex >= getColumnCount())) throw new IllegalArgumentException("moveColumn() - Index out of range"); TableColumn aColumn; // If the column has not yet moved far enough to change positions // post the event anyway, the "draggedDistance" property of the // tableHeader will say how far the column has been dragged. // Here we are really trying to get the best out of an // API that could do with some rethinking. We preserve backward // compatibility by slightly bending the meaning of these methods. if (columnIndex == newIndex) { fireColumnMoved(new TableColumnModelEvent(this, columnIndex, newIndex)); return; } aColumn = (TableColumn) tableColumns.elementAt(columnIndex); tableColumns.removeElementAt(columnIndex); boolean selected = selectionModel.isSelectedIndex(columnIndex); selectionModel.removeIndexInterval(columnIndex, columnIndex); tableColumns.insertElementAt(aColumn, newIndex); selectionModel.insertIndexInterval(newIndex, 1, true); if (selected) { selectionModel.addSelectionInterval(newIndex, newIndex); } else { selectionModel.removeSelectionInterval(newIndex, newIndex); } fireColumnMoved(new TableColumnModelEvent(this, columnIndex, newIndex)); }
/** * Recursive traverse of tree to determine selections A leaf is selected if rsm is selected. * Nonleaf nodes are selected if all children are selected. * * @param tn node in the tree for which to determine selection * @param nodeidx the ordinal postion in the segments array * @param gsm the graph segments selection model * @param rsm the table row selection model * @return true if given node tn is selected, else false */ private boolean selTraverse( TreeNode tn, int[] nodeidx, ListSelectionModel gsm, ListSelectionModel rsm) { boolean selected = true; if (!tn.isLeaf()) { // A nonleaf node is selected if all its children are selected. for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); selected &= selTraverse(cn, nodeidx, gsm, rsm); } } else { if (tn instanceof RowCluster) { // get the row index of the leaf node int ri = ((RowCluster) tn).getIndex(); // A leaf is selected if its row is selected in the row selection rsm. selected = rsm.isSelectedIndex(ri); } } // Get the offset into the segments array int idx = nodeidx[0] * segOffset; if (selected) { gsm.addSelectionInterval(idx, idx + (segOffset - 1)); } else { gsm.removeSelectionInterval(idx, idx + (segOffset - 1)); } // Increment the nodeidx in the tree nodeidx[0]++; return selected; }
/** * Returns the number of columns selected. * * @return the number of columns selected */ public int getSelectedColumnCount() { if (selectionModel != null) { int iMin = selectionModel.getMinSelectionIndex(); int iMax = selectionModel.getMaxSelectionIndex(); int count = 0; for (int i = iMin; i <= iMax; i++) { if (selectionModel.isSelectedIndex(i)) { count++; } } return count; } return 0; }
private List<Node> getSelectedNodes() { List<Node> selectedNodes = new Vector<Node>(); NodeStatusTableModel myTableModel = (NodeStatusTableModel) getModel(); ListSelectionModel lsm = getSelectionModel(); int minIndex = lsm.getMinSelectionIndex(); int maxIndex = lsm.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (lsm.isSelectedIndex(i)) { // System.out.println("row " + i + " is selected"); Node currNode = myTableModel.getNode(i); if (currNode != null) selectedNodes.add(currNode); } } return selectedNodes; }
/** * Returns an array of selected columns. If <code>selectionModel</code> is <code>null</code>, * returns an empty array. * * @return an array of selected columns or an empty array if nothing is selected or the <code> * selectionModel</code> is <code>null</code> */ public int[] getSelectedColumns() { if (selectionModel != null) { int iMin = selectionModel.getMinSelectionIndex(); int iMax = selectionModel.getMaxSelectionIndex(); if ((iMin == -1) || (iMax == -1)) { return new int[0]; } int[] rvTmp = new int[1 + (iMax - iMin)]; int n = 0; for (int i = iMin; i <= iMax; i++) { if (selectionModel.isSelectedIndex(i)) { rvTmp[n++] = i; } } int[] rv = new int[n]; System.arraycopy(rvTmp, 0, rv, 0, n); return rv; } return new int[0]; }