View Javadoc

1   /*
2    * @(#) TableModelEventList.java Nov 7, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.eventlists;
11  
12  import java.util.Arrays;
13  import java.util.Collection;
14  import java.util.Iterator;
15  
16  import javax.swing.table.DefaultTableModel;
17  import javax.swing.table.TableModel;
18  
19  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
20  import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
21  import nl.tudelft.simulation.event.EventType;
22  import nl.tudelft.simulation.logger.Logger;
23  
24  /***
25   * A TableModel implementation of an eventlist is an extionsion of the eventlist
26   * which upholds its own TableModel. This implementation is used to graphically
27   * display the events in the tree.
28   * <p>
29   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
30   * University of Technology </a>, the Netherlands. <br>
31   * See for project information <a href="http://www.simulation.tudelft.nl">
32   * www.simulation.tudelft.nl </a> <br>
33   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
34   * License (GPL) </a>, no warranty <br>
35   * 
36   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
37   *         Jacobs </a>
38   * @version 1.5 2004-03-28
39   * @since 1.0
40   */
41  public class TableModelEventList extends RedBlackTree
42  {
43  	/*** The EVENTLIST_CHANGED_EVENT */
44  	public static final EventType EVENTLIST_CHANCED_EVENT = null;
45  
46  	/*** The tableHeader */
47  	public static final String[] HEADER = {"Time", "Source", "Target", "Method"};
48  
49  	/*** the tableModel */
50  	private DefaultTableModel tableModel = new DefaultTableModel(HEADER, 0);
51  
52  	/**/package-summary/html">show the package information in the tableModel *//package-summary.html">* show the package information in the tableModel */
53  	private boolean showPackage = false;
54  
55  	/***
56  	 * constructs a new TableModelEventList
57  	 * 
58  	 * @param origin the origin
59  	 */
60  	public TableModelEventList(final EventListInterface origin)
61  	{
62  		super();
63  		synchronized (origin)
64  		{
65  			Collection collection = Arrays.asList(origin.toArray());
66  			this.addAll(collection);
67  		}
68  	}
69  
70  	/***
71  	 * returns the TreeMapEventList
72  	 * 
73  	 * @return EventListenerInterface
74  	 */
75  	public synchronized EventListInterface getOrigin()
76  	{
77  		RedBlackTree result = new RedBlackTree();
78  		result.addAll(Arrays.asList(this.toArray()));
79  		return result;
80  	}
81  
82  	/***
83  	 * returns the tableModel
84  	 * 
85  	 * @return TableModel resutl
86  	 */
87  	public TableModel getTableModel()
88  	{
89  		return this.tableModel;
90  	}
91  
92  	/***
93  	 * update the tableModel
94  	 */
95  	private void updateTableModel()
96  	{
97  		try
98  		{
99  			this.tableModel.setRowCount(0);
100 			for (Iterator i = this.iterator(); i.hasNext();)
101 			{
102 				Object[] row = new Object[4];
103 				SimEvent event = (SimEvent) i.next();
104 				row[0] = new Double(event.getAbsoluteExecutionTime());
105 				row[1] = this.formatObject(event.getSource().toString());
106 				row[2] = this.formatObject(event.getTarget().toString());
107 				row[3] = this.formatObject(event.getMethod().toString());
108 				this.tableModel.addRow(row);
109 			}
110 
111 		} catch (Exception exception)
112 		{
113 			Logger.warning(this, "updateTableModel", exception);
114 		}
115 	}
116 
117 	/***
118 	 * formats a label representing an object
119 	 * 
120 	 * @param label the label to format.
121 	 * @return String the label
122 	 */
123 	private String formatObject(final String label)
124 	{
125 		if (label == null)
126 		{
127 			return "null";
128 		}
129 		if (this.showPackage)
130 		{
131 			return label;
132 		}
133 		return label.substring(label.lastIndexOf(".") + 1);
134 	}
135 
136 	/***
137 	 * sets the showPackage
138 	 * 
139 	 * @param showPackage The showPackage to set.
140 	 */
141 	public void setShowPackage(final boolean showPackage)
142 	{
143 		this.showPackage = showPackage;
144 	}
145 
146 	/***
147 	 * @see nl.tudelft.simulation.dsol.eventlists.EventListInterface
148 	 *      #add(nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface)
149 	 */
150 	public synchronized boolean add(final SimEventInterface value)
151 	{
152 		synchronized (this.tableModel)
153 		{
154 			boolean result = super.add(value);
155 			this.updateTableModel();
156 			return result;
157 		}
158 	}
159 
160 	/***
161 	 * @see nl.tudelft.simulation.dsol.eventlists.EventListInterface
162 	 *      #addAll(java.util.Collection)
163 	 */
164 	public synchronized boolean addAll(final Collection collection)
165 	{
166 		synchronized (this.tableModel)
167 		{
168 			boolean result = super.addAll(collection);
169 			this.updateTableModel();
170 			return result;
171 		}
172 	}
173 
174 	/***
175 	 * @see nl.tudelft.simulation.dsol.eventlists.EventListInterface #clear()
176 	 */
177 	public synchronized void clear()
178 	{
179 		synchronized (this.tableModel)
180 		{
181 			super.clear();
182 			this.updateTableModel();
183 		}
184 	}
185 
186 	/***
187 	 * @see nl.tudelft.simulation.dsol.eventlists.EventListInterface
188 	 *      #remove(nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface)
189 	 */
190 	public synchronized boolean remove(final SimEventInterface value)
191 	{
192 		synchronized (this.tableModel)
193 		{
194 			boolean result = super.remove(value);
195 			this.updateTableModel();
196 			return result;
197 		}
198 	}
199 
200 	/***
201 	 * @see nl.tudelft.simulation.dsol.eventlists.EventListInterface
202 	 *      #removeAll(java.util.Collection)
203 	 */
204 	public synchronized boolean removeAll(final Collection collection)
205 	{
206 		synchronized (this.tableModel)
207 		{
208 			boolean result = super.removeAll(collection);
209 			this.updateTableModel();
210 			return result;
211 		}
212 	}
213 
214 	/***
215 	 * @see nl.tudelft.simulation.dsol.eventlists.EventListInterface#removeFirst()
216 	 */
217 	public synchronized SimEventInterface removeFirst()
218 	{
219 		synchronized (this.tableModel)
220 		{
221 			SimEventInterface result = super.removeFirst();
222 			this.updateTableModel();
223 			return result;
224 		}
225 	}
226 
227 	/***
228 	 * @see nl.tudelft.simulation.dsol.eventlists.EventListInterface#removeLast()
229 	 */
230 	public synchronized SimEventInterface removeLast()
231 	{
232 		synchronized (this.tableModel)
233 		{
234 			SimEventInterface result = super.removeLast();
235 			this.updateTableModel();
236 			return result;
237 		}
238 	}
239 }