View Javadoc
1 /* 2 * Copyright (c) 2003 Scott Howlett & Paul Libbrecht. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. The end-user documentation included with the redistribution, 18 * if any, must include the following acknowledgment: 19 * "This product includes software developed by the Xwing 20 * Project ( http://xwing.sourceforge.net/ )." 21 * Alternately, this acknowledgment may appear in the software itself, 22 * if and wherever such third-party acknowledgments normally appear. 23 * 24 * 4. The name "Xwing" must not be used to endorse or promote products 25 * derived from this software without prior written permission. For 26 * written permission, please contact the project authors via 27 * the Xwing project site, http://xwing.sourceforge.net/ . 28 * 29 * 5. Products derived from this software may not be called "Xwing", 30 * nor may "Xwing" appear in their name, without prior written 31 * permission of the Xwing project. 32 * 33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 36 * DISCLAIMED. IN NO EVENT SHALL THE XWING AUTHORS OR THE PROJECT 37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44 * SUCH DAMAGE. 45 * 46 * For more information on Xwing, please see 47 * < http://xwing.sourceforge.net/ >. 48 */ 49 50 package net.sourceforge.xwing.swing; 51 52 import java.awt.Component; 53 import java.util.List; 54 import java.util.Vector; 55 56 import javax.swing.JLabel; 57 import javax.swing.JList; 58 import javax.swing.JTable; 59 import javax.swing.ListCellRenderer; 60 import javax.swing.SwingUtilities; 61 import javax.swing.UIManager; 62 import javax.swing.event.TableModelEvent; 63 import javax.swing.table.DefaultTableColumnModel; 64 import javax.swing.table.TableCellRenderer; 65 import javax.swing.table.TableColumn; 66 67 68 import org.apache.commons.lang.StringUtils; 69 import org.w3c.dom.Node; 70 71 public class XTable extends JTable { 72 private boolean changingSel; 73 private MultiLineHeaderRenderer mhr = new MultiLineHeaderRenderer(); 74 75 public XTable() throws Exception { 76 77 MultiLineHeaderRenderer hr = new MultiLineHeaderRenderer(); 78 79 setColumnModel(new DefaultTableColumnModel() { 80 81 public void addColumn(TableColumn aColumn) { 82 aColumn.setPreferredWidth(120); 83 aColumn.setHeaderRenderer(mhr); 84 super.addColumn(aColumn); 85 } 86 87 }); 88 89 setRowHeight(getRowHeight() + 2); 90 91 getTableHeader().setReorderingAllowed(false); 92 setCellSelectionEnabled(true); 93 } 94 95 public List getSelectedRowNodes() { 96 int[] sel = getSelectedRows(); 97 List results = new Vector(); 98 99 XTableModel model = (XTableModel) getModel(); 100 101 for (int i = 0; i < sel.length; ++i) { 102 Object ctx = model.getRowContext(sel[i]); 103 if (ctx != null && ctx instanceof Node) { 104 results.add(ctx); 105 } 106 } 107 return results; 108 } 109 110 public void tableChanged(TableModelEvent e) { 111 112 int idx = -1; 113 if (!changingSel) { 114 if (e.getType() == TableModelEvent.INSERT) { 115 idx = e.getLastRow(); 116 } else if (e.getType() == TableModelEvent.DELETE) { 117 idx = e.getFirstRow(); 118 } 119 int lastRow = getRowCount() - 1; 120 if (idx > lastRow) { 121 idx = lastRow; 122 } 123 } 124 125 removeEditor(); 126 super.tableChanged(e); 127 128 if (idx >= 0) { 129 final int idx2 = idx; 130 SwingUtilities.invokeLater(new Runnable() { 131 public void run() { 132 changeSelection(idx2, 0, false, false); 133 requestFocus(); 134 } 135 }); 136 } 137 } 138 139 public void moveSelectedRowsUp() { 140 XTableModel model = (XTableModel) getModel(); 141 int[] sel = getSelectedRows(); 142 143 // skip contiguous block of selected rows at the top 144 int i; 145 for (i = 0; i < sel.length && sel[i] == i; ++i) { 146 } 147 148 while (i < sel.length) { 149 Node curRow = (Node) model.getRowContext(sel[i]--); 150 Node prevRow = (Node) model.getRowContext(sel[i]); 151 prevRow.getParentNode().insertBefore(curRow, prevRow); 152 ++i; 153 } 154 if (!changingSel) { 155 final int[] cols = this.getSelectedColumns(); 156 final int[] fsel = sel; 157 SwingUtilities.invokeLater(new Runnable() { 158 public void run() { 159 try { 160 changingSel = true; 161 clearSelection(); 162 for (int i = 0; i < fsel.length; ++i) { 163 int maxCol = (i == 0) ? cols.length : 1; 164 for (int j = 0; j < maxCol; ++j) { 165 changeSelection(fsel[i], cols[j], true, false); 166 } 167 } 168 } finally { 169 changingSel = false; 170 } 171 } 172 }); 173 } 174 } 175 public void moveSelectedRowsDown() { 176 XTableModel model = (XTableModel) getModel(); 177 int[] sel = getSelectedRows(); 178 179 // skip contiguous block of selected rows at the bottom 180 int last = model.getRowCount() - 1; 181 int i; 182 for (i = sel.length - 1; i >= 0 && sel[i] == last--; --i) { 183 } 184 185 while (i >= 0) { 186 Node curRow = (Node) model.getRowContext(sel[i]++); 187 Node nextRow = (Node) model.getRowContext(sel[i]); 188 curRow.getParentNode().insertBefore(nextRow, curRow); 189 --i; 190 } 191 if (!changingSel) { 192 final int[] cols = this.getSelectedColumns(); 193 final int[] fsel = sel; 194 SwingUtilities.invokeLater(new Runnable() { 195 public void run() { 196 try { 197 changingSel = true; 198 clearSelection(); 199 for (int i = 0; i < fsel.length; ++i) { 200 int maxCol = (i == 0) ? cols.length : 1; 201 for (int j = 0; j < maxCol; ++j) { 202 changeSelection(fsel[i], cols[j], true, false); 203 } 204 } 205 } finally { 206 changingSel = false; 207 } 208 } 209 }); 210 } 211 } 212 213 public void fillDataDown() { 214 XTableModel model = (XTableModel) getModel(); 215 int[] sel = getSelectedRows(); 216 int[] colSel = this.getSelectedColumns(); 217 218 if (sel.length > 1 && colSel.length > 0) { 219 Object[] topData = new Object[colSel.length]; 220 for (int i = 0; i < colSel.length; ++i) { 221 topData[i] = getValueAt(sel[0], colSel[i]); 222 } 223 224 for (int i = 1; i < sel.length; ++i) { 225 for (int j = 0; j < colSel.length; ++j) { 226 setValueAt(topData[j], sel[i], colSel[j]); 227 } 228 } 229 } 230 } 231 } 232 233 class MultiLineHeaderRenderer extends JList implements TableCellRenderer { 234 public MultiLineHeaderRenderer() { 235 setOpaque(true); 236 setForeground(UIManager.getColor("TableHeader.foreground")); 237 setBackground(UIManager.getColor("TableHeader.background")); 238 setBorder(UIManager.getBorder("TableHeader.cellBorder")); 239 ListCellRenderer renderer = getCellRenderer(); 240 ((JLabel) renderer).setHorizontalAlignment(JLabel.CENTER); 241 setCellRenderer(renderer); 242 } 243 244 public Component getTableCellRendererComponent( 245 JTable table, 246 Object value, 247 boolean isSelected, 248 boolean hasFocus, 249 int row, 250 int column) { 251 String str = (value == null) ? "" : value.toString(); 252 String[] values = StringUtils.split(str, ":"); 253 setListData(values); 254 return this; 255 } 256 }

This page was automatically generated by Maven