001 /* 002 * This program is free software; you can redistribute it and/or modify 003 * it under the terms of the GNU General Public License as published by 004 * the Free Software Foundation; version 2 of the License. 005 * 006 * This program is distributed in the hope that it will be useful, 007 * but WITHOUT ANY WARRANTY; without even the implied warranty of 008 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 009 * GNU General Public License for more details. 010 * 011 * You should have received a copy of the GNU General Public License along 012 * with this program; if not, write to the Free Software Foundation, Inc., 013 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 014 */ 015 package org.tastybug.bugwerk.bugtrail.widget.queue; 016 017 import java.util.Arrays; 018 import java.util.Iterator; 019 020 import javax.swing.JTable; 021 import javax.swing.ListSelectionModel; 022 023 import org.tastybug.bugwerk.blueprint.event.TicketChangeListener; 024 import org.tastybug.bugwerk.blueprint.model.Incident; 025 import org.tastybug.bugwerk.blueprint.model.Ticket; 026 import org.tastybug.bugwerk.blueprint.model.TicketQueue; 027 import org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker; 028 import org.tastybug.bugwerk.bugtrap.base.worker.QueueWorkerListener; 029 030 /** 031 * This table shows the content of a <code>TicketQueue</code>, listing all 032 * contained <code>Ticket</code> instances. 033 * <br> 034 * The table is a <code>QueueWorker</code> implementation and thus listens for 035 * activity on the queue, displaying changes upon necessity. 036 * <br> 037 * Use it like this: 038 * <pre> 039 * TicketQueueTable table = new TicketQueueTable(); 040 * table.connectQueue(Adapters.getAdapter("myProject").getTicketQueue()); 041 * 042 * // get informed about ticket selections: 043 * table.getSelectionModel().addListSelectionListener(yourSelectionListener); 044 * </pre> 045 * <br> 046 * 047 * <hr> 048 * Copyright 2006 Philipp Bartsch.<br> 049 * <a href="http://www.tastybug.com">www.tastybug.com</a><br> 050 * <hr> 051 * Created on Feb 8, 2006<br> 052 * @author Philipp Bartsch, philipp.bartsch{at}tastybug{dot}com</a> 053 */ 054 public class TicketQueueTable extends JTable implements QueueWorker, TicketChangeListener { 055 056 /**The <code>serialVersionUID</code>*/ 057 private static final long serialVersionUID = 536742784785681779L; 058 059 private TicketQueue ticketQueue; 060 private TicketQueueTableModel tableModel; 061 private TicketQueueTableRenderer tableRenderer; 062 063 /** 064 * Initialises the table, using an empty model that is replaced as soon as a 065 * ticket queue is connected. 066 */ 067 public TicketQueueTable () { 068 super (new Object[0][0], TicketQueueTableModel.columns); 069 setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 070 } 071 072 /** 073 * Updates table model and UI. 074 * 075 * @see org.tastybug.bugwerk.blueprint.event.TicketQueueChangeListener#performTicketRemovedEvent(org.tastybug.bugwerk.blueprint.event.TicketQueueChangeListener.TicketQueueEvent) 076 */ 077 public void performTicketRemovedEvent(TicketQueueEvent event) { 078 tableModel.update(); 079 tableRenderer.update(); 080 updateUI(); 081 } 082 083 /** 084 * Updates table model and UI. 085 * 086 * @see org.tastybug.bugwerk.blueprint.event.TicketQueueChangeListener#performTicketAddedEvent(org.tastybug.bugwerk.blueprint.event.TicketQueueChangeListener.TicketQueueEvent) 087 */ 088 public void performTicketAddedEvent(TicketQueueEvent event) { 089 tableModel.update(); 090 tableRenderer.update(); 091 updateUI(); 092 } 093 094 /** 095 * Updates table model and UI. 096 * 097 * @see org.tastybug.bugwerk.blueprint.event.TicketChangeListener#performIncidentRemovedEvent(org.tastybug.bugwerk.blueprint.event.TicketChangeListener.IncidentEvent) 098 */ 099 public void performIncidentRemovedEvent(IncidentEvent event) { 100 updateUI(); 101 } 102 103 /** 104 * Updates table model and UI. 105 * 106 * @see org.tastybug.bugwerk.blueprint.event.TicketChangeListener#performIncidentAddedEvent(org.tastybug.bugwerk.blueprint.event.TicketChangeListener.IncidentEvent) 107 */ 108 public void performIncidentAddedEvent(IncidentEvent event) { 109 updateUI(); 110 } 111 112 /** 113 * Updates table model and UI. 114 * 115 * @see org.tastybug.bugwerk.blueprint.event.TicketChangeListener#performTicketChangedEvent(org.tastybug.bugwerk.blueprint.event.TicketChangeListener.TicketChangeEvent) 116 */ 117 public void performTicketChangedEvent(TicketChangeEvent event) { 118 updateUI(); 119 } 120 121 /** 122 * Connects a ticket queue; results in the (re-)creation of the table model. 123 * 124 * @see org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker#connectQueue(org.tastybug.bugwerk.blueprint.model.TicketQueue) 125 */ 126 public void connectQueue(TicketQueue queue) { 127 if (ticketQueue != null) 128 for (Iterator tickets = Arrays.asList(ticketQueue.getTickets()).iterator(); tickets.hasNext();) 129 ((Ticket)tickets.next()).removeTicketChangeListener(this); 130 131 ticketQueue = queue; 132 tableModel = new TicketQueueTableModel(ticketQueue); 133 tableRenderer = new TicketQueueTableRenderer(ticketQueue); 134 135 setModel(tableModel); 136 setDefaultRenderer(String.class, tableRenderer); 137 138 // als listener an der queue anmelden 139 ticketQueue.addTicketQueueChangeListener(this); 140 141 for (Iterator tickets = Arrays.asList(ticketQueue.getTickets()).iterator(); tickets.hasNext();) 142 ((Ticket)tickets.next()).addTicketChangeListener(this); 143 144 updateUI(); 145 } 146 147 /** 148 * Does nothing, you cannot listen at this worker. 149 * 150 * @see org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker#addQueueWorkerListener(org.tastybug.bugwerk.bugtrap.base.worker.QueueWorkerListener) 151 */ 152 public void addQueueWorkerListener(QueueWorkerListener qwListener) { 153 // 154 } 155 156 /** 157 * Returns always <code>false</code> as you cannot listen at this worker. 158 * 159 * @see org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker#removeQueueWorkerListener(org.tastybug.bugwerk.bugtrap.base.worker.QueueWorkerListener) 160 */ 161 public boolean removeQueueWorkerListener(QueueWorkerListener qwListener) { 162 return false; 163 } 164 165 /** 166 * Returns always an empty array as you cannot listen at this worker. 167 * 168 * @see org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker#removeQueueWorkerListener(org.tastybug.bugwerk.bugtrap.base.worker.QueueWorkerListener) 169 */ 170 public QueueWorkerListener[] getQueueWorkerListeners() { 171 return new QueueWorkerListener[0]; 172 } 173 174 /** 175 * Updates table model and UI. 176 * 177 * @see org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker#processQueue(boolean) 178 */ 179 public void processQueue(boolean isUserDriven) { 180 updateUI(); 181 } 182 183 /** 184 * Does nothing. 185 * 186 * @see org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker#fireIncidentProcessedEvent(org.tastybug.bugwerk.blueprint.model.Incident, boolean, java.lang.String) 187 */ 188 public void fireIncidentProcessedEvent(Incident arg0, boolean arg1, String arg2) { 189 // 190 } 191 192 /** 193 * Does nothing. 194 * 195 * @see org.tastybug.bugwerk.bugtrap.base.worker.QueueWorker#fireTicketProcessedEvent(org.tastybug.bugwerk.blueprint.model.Ticket, boolean, java.lang.String) 196 */ 197 public void fireTicketProcessedEvent(Ticket arg0, boolean arg1, String arg2) { 198 updateUI(); 199 } 200 }