View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.table;
21  
22  import java.util.Arrays;
23  import java.util.EventObject;
24  
25  import javax.swing.event.TableModelEvent;
26  
27  
28  /**
29   * <code>TableRowModelEvent</code> contains information about changes in
30   * <code>TableRowModel</code>.
31   *
32   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
33   * @version $Id: TableRowModelEvent.java,v 1.6 2008-04-22 12:28:40 jbobnar Exp $
34   *
35   * @since May 3, 2004.
36   */
37  public class TableRowModelEvent extends EventObject
38  {
39  	private static final long serialVersionUID = 1L;
40  	/** Identifies structural change. */
41  	public final static int HEADER_ROW = TableModelEvent.HEADER_ROW;
42  	/** Identifies element index which applies for all row elements. */
43  	public final static int ALL_VALUES = TableModelEvent.ALL_COLUMNS;
44  
45  	/** Identifies the addtion of new rows. */
46  	public static final int INSERT = TableModelEvent.INSERT;
47  
48  	/** Identifies a change to existing data. */
49  	public static final int UPDATE = TableModelEvent.UPDATE;
50  
51  	/** Identifies the removal of rows. */
52  	public static final int DELETE = TableModelEvent.DELETE;
53  	private int value = ALL_VALUES;
54  	private int type = UPDATE;
55  	private static final TableRow[] DEFAULT_ROWS = new TableRow[0]; 
56  	private TableRow[] rows = DEFAULT_ROWS;
57  	private int rowIndex = -1;
58  
59  	/**
60  	 * Creates new event with row as source, column value is by default
61  	 * <code>ALL_VALUES</code> and type is <code>UPDATE</code>.
62  	 *
63  	 * @param source
64  	 * @param index the index of row in parent model
65  	 */
66  	public TableRowModelEvent(TableRow source, int index)
67  	{
68  		super(source);
69  		rowIndex = index;
70  		rows = new TableRow[]{ source };
71  	}
72  
73  	/**
74  	 * Creates new event with row as source and value index, type is
75  	 * <code>UPDATE</code>..
76  	 *
77  	 * @param source table row as source
78  	 * @param index the index of row in parent model
79  	 * @param value the value index
80  	 *
81  	 * @throws IllegalArgumentException if value isless then -1
82  	 */
83  	public TableRowModelEvent(TableRow source, int index, int value)
84  	{
85  		this(source, index);
86  		this.value = value;
87  
88  		if (value < -1) {
89  			throw new IllegalArgumentException(
90  			    "Argument value is less then -1 :'" + value + "'");
91  		}
92  	}
93  
94  	/**
95  	 * Creates new event.
96  	 *
97  	 * @param source table row as source
98  	 * @param index the index of row in parent model
99  	 * @param value the value index
100 	 * @param type the type of event
101 	 *
102 	 * @throws IllegalArgumentException if type is not one of
103 	 *         <code>INSERT</code>, <code>DELETE</code> or <code>UPDATE</code>
104 	 *         values
105 	 */
106 	public TableRowModelEvent(TableRow source, int index, int value, int type)
107 	{
108 		this(source, index, value);
109 		this.type = type;
110 
111 		if (type < -1 || type > 1) {
112 			throw new IllegalArgumentException(
113 			    "Argument type has illegal value :'" + type + "'");
114 		}
115 	}
116 
117 	/**
118 	 * Creates new event.
119 	 *
120 	 * @param sources table row as source
121 	 * @param value the value index
122 	 * @param type the type of event
123 	 */
124 	public TableRowModelEvent(TableRow[] sources, int value, int type)
125 	{
126 		this(sources[0], -1, value, type);
127 
128 		rows = sources;
129 	}
130 
131 	/**
132 	 * Returns index of value for which event applies
133 	 *
134 	 * @return the value index
135 	 */
136 	public int getValue()
137 	{
138 		return value;
139 	}
140 
141 	/**
142 	 * Returns type of event, valid value is <code>INSERT</code>,
143 	 * <code>DELETE</code> and <code>UPDATE</code>.
144 	 *
145 	 * @return the type of event
146 	 */
147 	public int getType()
148 	{
149 		return type;
150 	}
151 
152 	/**
153 	 * Returns row, which was source of event. If there is more rows as source,
154 	 * first row is returned
155 	 *
156 	 * @return the event source
157 	 */
158 	public TableRow getRow()
159 	{
160 		return rows[0];
161 	}
162 
163 	/**
164 	 * Returns array of rows, which are source of this event.
165 	 *
166 	 * @return the sources of event
167 	 */
168 	public TableRow[] getRows()
169 	{
170 		return rows;
171 	}
172 
173 	/**
174 	 * Returns the index of rowinside parent model.
175 	 *
176 	 * @return the index of rowinside parent model
177 	 */
178 	public int getRowIndex()
179 	{
180 		return rowIndex;
181 	}
182 
183 	/* (non-Javadoc)
184 	 * @see java.util.EventObject#toString()
185 	 */
186 	public String toString()
187 	{
188 		StringBuffer sb = new StringBuffer();
189 		sb.append("TableRowModelEvent = { sources = ");
190 		sb.append(Arrays.asList(getRows()));
191 		sb.append(" rowIndex = ");
192 		sb.append(rowIndex);
193 		sb.append(" value = ");
194 		sb.append(value);
195 		sb.append("' type = ");
196 		sb.append(type);
197 		sb.append(" }");
198 
199 		return sb.toString();
200 	}
201 }
202 
203 /* __oOo__ */