001/* 002 * Copyright (c) 2004-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.sdm.spa.actors.piw.viz; 031 032import java.awt.BorderLayout; 033import java.awt.Color; 034import java.awt.Component; 035import java.awt.Dimension; 036import java.awt.FlowLayout; 037import java.awt.event.ActionEvent; 038import java.awt.event.ActionListener; 039 040import javax.swing.AbstractCellEditor; 041import javax.swing.BorderFactory; 042import javax.swing.JCheckBox; 043import javax.swing.JComboBox; 044import javax.swing.JPanel; 045import javax.swing.JScrollPane; 046import javax.swing.JTable; 047import javax.swing.border.Border; 048import javax.swing.table.AbstractTableModel; 049import javax.swing.table.TableCellEditor; 050import javax.swing.table.TableCellRenderer; 051 052/** 053 * @author xiaowen 054 * 055 */ 056public class SitesPanel extends JPanel { 057 058 private static final long serialVersionUID = 3977297702998454328L; 059 060 private final SequenceCollection _sequenceCollection; 061 private final SiteCollection _siteCollection; 062 private final ActionListener _listenerSiteChange; 063 064 public SitesPanel(SequenceCollection sequenceCollection, 065 SiteCollection siteCollection, ActionListener listenerSiteChange) { 066 this._sequenceCollection = sequenceCollection; 067 this._siteCollection = siteCollection; 068 this._listenerSiteChange = listenerSiteChange; 069 070 TableModelFrequency tableModel = new TableModelFrequency(); 071 JTable table = new JTable(tableModel); 072 table.setDefaultRenderer(SiteCollection.Site.class, new SiteRenderer()); 073 table.setDefaultEditor(SiteCollection.Site.class, new SiteEditor()); 074 table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 075 table.getColumnModel().getColumn(1).setMaxWidth(75); 076 table.getColumnModel().getColumn(2).setMaxWidth(75); 077 table.setRowHeight(18); 078 079 // Pull down menu containing selection actions. 080 JComboBox comboBox = new JComboBox(new String[] { "Select ...", 081 "Select All", "Deselect All", "Select Marked", 082 "Deselect Marked" }); 083 comboBox.addActionListener(new ListenerComboBoxSelect(table)); 084 085 this.setLayout(new BorderLayout()); 086 this.add(comboBox, BorderLayout.NORTH); 087 088 JScrollPane scrollPane = new JScrollPane(table); 089 scrollPane.setWheelScrollingEnabled(true); 090 this.add(scrollPane, BorderLayout.CENTER); 091 this.setPreferredSize(new Dimension(50, 50)); 092 } 093 094 private class TableModelFrequency extends AbstractTableModel { 095 private static final long serialVersionUID = 3257850999766069561L; 096 private String[] columnNames = { "TFBS", "Shown", "Frequency" }; 097 098 public int getColumnCount() { 099 return columnNames.length; 100 } 101 102 public int getRowCount() { 103 return _siteCollection.size(); 104 } 105 106 public String getColumnName(int col) { 107 return columnNames[col]; 108 } 109 110 public Object getValueAt(int row, int col) { 111 if (0 == col) { 112 return _siteCollection.getSite(row).name; 113 } else if (1 == col) { 114 return _siteCollection.getSite(row); 115 } else if (2 == col) { 116 return new Double(_siteCollection.getSite(row).frequency); 117 } 118 return null; 119 } 120 121 public Class getColumnClass(int c) { 122 return getValueAt(0, c).getClass(); 123 } 124 125 public boolean isCellEditable(int row, int col) { 126 return 1 == col; 127 } 128 129 public void setValueAt(Object value, int row, int col) { 130 if (1 == col) { 131 _siteCollection.getSite(row).selected = ((Boolean) value) 132 .booleanValue(); 133 _listenerSiteChange 134 .actionPerformed(new ActionEvent(this, 0, "")); 135 } 136 } 137 } 138 139 private class SiteRenderer extends JPanel implements TableCellRenderer { 140 141 private static final long serialVersionUID = 3258410646906615096L; 142 143 Border unselectedBorder = null; 144 Border selectedBorder = null; 145 JCheckBox checkbox = null; 146 147 public SiteRenderer() { 148 setOpaque(true); 149 } 150 151 public Component red() { 152 this.setBackground(Color.RED); 153 this.setForeground(Color.RED); 154 return this; 155 } 156 157 public Component getTableCellRendererComponent(JTable table, Object s, 158 boolean isSelected, boolean hasFocus, int row, int column) { 159 160 SiteCollection.Site site = _siteCollection.getSite(row); 161 162 if (null == checkbox) { 163 checkbox = new JCheckBox(); 164 checkbox.setBorder(BorderFactory.createEmptyBorder()); 165 this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); 166 this.add(checkbox); 167 } 168 169 checkbox.setSelected(site.selected); 170 171 setBackground(site.color); 172 if (isSelected) { 173 if (selectedBorder == null) { 174 selectedBorder = BorderFactory.createMatteBorder(2, 3, 2, 175 3, table.getSelectionBackground()); 176 } 177 setBorder(selectedBorder); 178 } else { 179 if (unselectedBorder == null) { 180 unselectedBorder = BorderFactory.createMatteBorder(2, 3, 2, 181 3, table.getBackground()); 182 } 183 setBorder(unselectedBorder); 184 } 185 186 return this; 187 } 188 } 189 190 private class SiteEditor extends AbstractCellEditor implements 191 TableCellEditor { 192 193 private static final long serialVersionUID = 3256727290376433720L; 194 private SiteRenderer _siteRenderer; 195 196 /** 197 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, 198 * java.lang.Object, boolean, int, int) 199 */ 200 public Component getTableCellEditorComponent(JTable table, 201 Object value, boolean isSelected, int row, int column) { 202 203 if (null == _siteRenderer) { 204 _siteRenderer = new SiteRenderer(); 205 } 206 207 Component component = _siteRenderer.getTableCellRendererComponent( 208 table, value, true, true, row, column); 209 210 _siteRenderer.checkbox.addActionListener(new ActionListener() { 211 public void actionPerformed(ActionEvent e) { 212 SiteEditor.this.stopCellEditing(); 213 } 214 }); 215 216 return component; 217 } 218 219 /** 220 * @see javax.swing.CellEditor#getCellEditorValue() 221 */ 222 public Object getCellEditorValue() { 223 return new Boolean(_siteRenderer.checkbox.isSelected()); 224 } 225 } 226 227 private class ListenerComboBoxSelect implements ActionListener { 228 229 private final JTable _table; 230 231 public ListenerComboBoxSelect(JTable table) { 232 this._table = table; 233 } 234 235 public void actionPerformed(ActionEvent e) { 236 JComboBox cb = (JComboBox) e.getSource(); 237 int index = cb.getSelectedIndex(); 238 239 if (1 == index) { 240 // Selected all. 241 for (int i = 0; i < _siteCollection.size(); i++) { 242 _siteCollection.getSite(i).selected = true; 243 } 244 } else if (2 == index) { 245 // Deselect all. 246 for (int i = 0; i < _siteCollection.size(); i++) { 247 _siteCollection.getSite(i).selected = false; 248 } 249 } else if (3 == index) { 250 // Select marked. 251 int[] selected = _table.getSelectedRows(); 252 for (int i = 0; i < selected.length; i++) { 253 _siteCollection.getSite(selected[i]).selected = true; 254 } 255 } else if (4 == index) { 256 // Deselect marked. 257 int[] selected = _table.getSelectedRows(); 258 for (int i = 0; i < selected.length; i++) { 259 _siteCollection.getSite(selected[i]).selected = false; 260 } 261 } 262 cb.setSelectedIndex(0); 263 _listenerSiteChange.actionPerformed(new ActionEvent(this, 0, "")); 264 } 265 } 266 267}