1 /* 2 * $Id$ 3 * 4 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 package com.sun.interview.wizard; 28 29 import java.awt.BorderLayout; 30 import java.awt.Dimension; 31 import java.awt.Toolkit; 32 import java.awt.event.ActionEvent; 33 import java.awt.event.ActionListener; 34 import javax.swing.JComponent; 35 import javax.swing.JLabel; 36 import javax.swing.JPanel; 37 import javax.swing.JScrollPane; 38 import javax.swing.JTable; 39 import javax.swing.table.AbstractTableModel; 40 import javax.swing.table.TableColumn; 41 42 import com.sun.interview.ChoiceArrayQuestion; 43 import com.sun.interview.Question; 44 import java.awt.Component; 45 import java.awt.event.KeyAdapter; 46 import java.awt.event.KeyEvent; 47 import java.awt.event.KeyListener; 48 import javax.swing.event.TableModelEvent; 49 import javax.swing.table.TableCellRenderer; 50 import javax.swing.table.TableModel; 51 52 public class ChoiceArrayQuestionRenderer 53 implements QuestionRenderer 54 { 55 56 public JComponent getQuestionRendererComponent(Question qq, ActionListener listener) { 57 q = (ChoiceArrayQuestion)qq; 58 displayChoices = q.getDisplayChoices(); 59 values = q.getValue(); 60 editedListener = listener; 61 62 return createChoiceTable(); 63 } 64 65 public String getInvalidValueMessage(Question q) { 66 return null; 67 } 68 69 protected JComponent createChoiceTable() { 70 AbstractTableModel tm = createTableModel(); 71 JTable tbl = new JTable(tm); 72 73 tbl.setPreferredScrollableViewportSize(new Dimension(DOTS_PER_INCH, DOTS_PER_INCH)); 74 tbl.setShowHorizontalLines(false); 75 tbl.setShowVerticalLines(false); 76 tbl.setTableHeader(null); 77 tbl.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); 78 tbl.setRowSelectionAllowed(false); 79 tbl.setColumnSelectionAllowed(false); 80 tbl.setToolTipText(i18n.getString("chcArr.tbl.tip")); 81 82 tbl.addKeyListener(createKeyListener(tm)); 83 84 TableColumn col0 = tbl.getColumnModel().getColumn(0); 85 col0.setPreferredWidth(24); 86 col0.setMaxWidth(24); 87 col0.setResizable(false); 88 89 TableColumn col1 = tbl.getColumnModel().getColumn(1); 90 col1.setPreferredWidth(getColumnWidth(tbl, 1) + 20); 91 92 final JScrollPane sp = new JScrollPane(tbl); 93 sp.setName("chcArr.sp"); 94 sp.getViewport().setBackground(tbl.getBackground()); 95 96 97 JLabel lbl = new JLabel(i18n.getString("chcArr.tbl.lbl")); 98 lbl.setName("chcArr.tbl.lbl"); 99 lbl.setDisplayedMnemonic(i18n.getString("chcArr.tbl.mne").charAt(0)); 100 lbl.setToolTipText(i18n.getString("chcArr.tbl.tip")); 101 lbl.setLabelFor(sp); 102 103 tbl.setRowHeight(getRowHeight()); 104 105 JPanel result = new JPanel(new BorderLayout()); 106 result.add(lbl, BorderLayout.NORTH); 107 result.add(sp, BorderLayout.CENTER); 108 109 return result; 110 } 111 112 protected int getColumnWidth(JTable table, int colIndex) { 113 int width = -1; 114 115 TableModel model = table.getModel(); 116 117 for(int i = 0; i < model.getRowCount(); i++) { 118 TableCellRenderer r = table.getCellRenderer(i, colIndex); 119 Component c = r.getTableCellRendererComponent(table, 120 model.getValueAt(i, colIndex), 121 false, false, i, colIndex); 122 width = Math.max(width, c.getPreferredSize().width); 123 } 124 125 return width; 126 } 127 protected int getRowHeight() { 128 return 22; 129 } 130 131 132 protected AbstractTableModel createTableModel() { 133 return new TestTableModel(); 134 } 135 136 protected class TestTableModel extends AbstractTableModel { 137 public Class getColumnClass(int c) { 138 return (c == 0 ? Boolean.class : String.class); 139 } 140 141 public int getColumnCount() { 142 return 2; 143 } 144 145 public int getRowCount() { 146 return displayChoices.length; 147 } 148 149 public Object getValueAt(int r, int c) { 150 return c == 0 ? new Boolean(values[r]) : displayChoices[r]; 151 } 152 153 public void setValueAt(Object o, int r, int c) { 154 if (c == 0) { 155 values[r] = ((Boolean) o).booleanValue(); 156 q.setValue(values); 157 fireEditedEvent(this, editedListener); 158 } 159 } 160 161 public boolean isCellEditable(int r, int c) { 162 return (c == 0 ? true : false); 163 } 164 }; 165 166 protected KeyListener createKeyListener(AbstractTableModel tm) { 167 return new TestKeyListener(tm); 168 } 169 170 protected class TestKeyListener extends KeyAdapter { 171 public TestKeyListener(AbstractTableModel tm) { 172 this.tm = tm; 173 } 174 public void keyPressed(KeyEvent e) { 175 if((e.getModifiersEx() & e.CTRL_DOWN_MASK) != 0 && e.getKeyCode() == e.VK_A) { 176 boolean allSelected = true; 177 for(int i = 0; i < tm.getRowCount(); i++) { 178 if(tm.getValueAt(i, 0).equals(new Boolean(false))) { 179 allSelected = false; 180 break; 181 } 182 183 } 184 for(int i = 0; i < tm.getRowCount(); i++) { 185 tm.setValueAt(new Boolean(!allSelected), i, 0); 186 TableModelEvent ev = new TableModelEvent(tm, i, i, 187 TableModelEvent.ALL_COLUMNS, 188 TableModelEvent.UPDATE); 189 tm.fireTableChanged(ev); 190 } 191 } 192 193 } 194 protected AbstractTableModel tm; 195 }; 196 197 protected String[] displayChoices; 198 protected boolean[] values; 199 protected ChoiceArrayQuestion q; 200 protected ActionListener editedListener; 201 202 203 protected void fireEditedEvent(Object src, ActionListener l) { 204 ActionEvent e = new ActionEvent(src, 205 ActionEvent.ACTION_PERFORMED, 206 EDITED); 207 l.actionPerformed(e); 208 } 209 210 private static final I18NResourceBundle i18n = I18NResourceBundle.getDefaultBundle(); 211 protected static final int DOTS_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution(); 212 213 }