001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.session.Session;
027: import jacareto.comp.Components;
028: import jacareto.comp.ComponentsEvent;
029: import jacareto.comp.ComponentsListener;
030: import jacareto.comp.select.ComponentSelection;
031: import jacareto.comp.select.ComponentSelectionEvent;
032: import jacareto.comp.select.ComponentSelectionListener;
033: import jacareto.system.Environment;
034: import jacareto.system.Language;
035: import jacareto.test.Test;
036:
037: import java.awt.Component;
038:
039: import java.util.Hashtable;
040: import java.util.Iterator;
041: import java.util.Set;
042: import java.util.TreeSet;
043: import java.util.Vector;
044:
045: import javax.swing.JTable;
046: import javax.swing.ListSelectionModel;
047: import javax.swing.event.ListSelectionEvent;
048: import javax.swing.event.ListSelectionListener;
049: import javax.swing.table.DefaultTableModel;
050:
051: /**
052: * The JTable displaying the components.
053: *
054: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
055: * @version 1.0
056: */
057: public class ComponentsTable extends JTable implements
058: ComponentsListener, ComponentSelectionListener {
059: /** The components instance. */
060: private Components components;
061:
062: /** The component selection. */
063: private ComponentSelection componentSelection;
064:
065: /** The table model. */
066: private DefaultTableModel tableModel;
067:
068: /** The language. */
069: private Language language;
070:
071: /** The column names. */
072: private String[] columnNames;
073:
074: /** The session. */
075: private Session session;
076:
077: /** Whether ot not this instance listens to component selection changes. */
078: private boolean listenToComponentSelectionChanges;
079:
080: /** Whether ot not this instance listens to list selection changes. */
081: private boolean listenToListSelectionChanges;
082:
083: /**
084: * Creates a new table.
085: *
086: * @param env DOCUMENT ME!
087: * @param session DOCUMENT ME!
088: * @param components DOCUMENT ME!
089: */
090: public ComponentsTable(Environment env, Session session,
091: Components components) {
092: super ();
093: this .session = session;
094: this .components = components;
095: componentSelection = components.getComponentSelection();
096: components.addComponentsListener(this );
097: componentSelection.addComponentSelectionListener(this );
098: listenToComponentSelectionChanges = true;
099: listenToListSelectionChanges = true;
100: language = env.getLanguage();
101: tableModel = new DefaultTableModel();
102: columnNames = new String[2];
103: columnNames[0] = language
104: .getString("CleverPHL.ComponentsFrame.JacaretoName");
105: columnNames[1] = language
106: .getString("CleverPHL.ComponentsFrame.UserDefinedName");
107: tableModel.setColumnIdentifiers(columnNames);
108:
109: setModel(tableModel);
110: update();
111: getSelectionModel().addListSelectionListener(
112: new ComponentTableListener());
113: }
114:
115: public void update() {
116: Hashtable userDefinedNames = components.getUserDefinedNames();
117: Set keys = new TreeSet(components.getJacaretoNames());
118: Object[][] data = new Object[keys.size()][2];
119: int i = 0;
120: Iterator it = keys.iterator();
121:
122: while (it.hasNext()) {
123: Object key = it.next();
124: data[i][0] = (String) key;
125:
126: if (userDefinedNames.containsKey(key)) {
127: data[i][1] = (String) userDefinedNames.get(key);
128: } else {
129: data[i][1] = "";
130: }
131:
132: i++;
133: }
134:
135: tableModel.setDataVector(data, columnNames);
136: setModel(tableModel);
137: }
138:
139: public void insertTests(boolean defaultCorrect,
140: boolean defaultIgnore) {
141: int[] selectedRows = getSelectedRows();
142:
143: for (int i = 0; i < selectedRows.length; i++) {
144: String jacaretoName = (String) tableModel.getValueAt(
145: selectedRows[i], 0);
146: java.awt.Component component = components
147: .getComponent(jacaretoName);
148: Test test = session.getTestLoader().getTest(component);
149: test.setCorrecting(defaultCorrect);
150: test.setIgnoring(defaultIgnore);
151: session.insertRecordable(test);
152: }
153: }
154:
155: public boolean isCellEditable(int row, int column) {
156: return false;
157: }
158:
159: /**
160: * Called when the component tree has changed.
161: *
162: * @param event DOCUMENT ME!
163: */
164: public void componentTreeChanged(ComponentsEvent event) {
165: update();
166: }
167:
168: /**
169: * Called when a user defined name has been set.
170: *
171: * @param event DOCUMENT ME!
172: */
173: public void userDefinedNameSet(ComponentsEvent event) {
174: String jacaretoName = event.getJacaretoName();
175: String userDefinedName = event.getUserDefinedName();
176:
177: for (int i = 0; i < tableModel.getRowCount(); i++) {
178: if (((String) tableModel.getValueAt(i, 0))
179: .equals(jacaretoName)) {
180: tableModel.setValueAt(userDefinedName, i, 1);
181:
182: break;
183: }
184: }
185: }
186:
187: /**
188: * Called when the component selection has changed.
189: *
190: * @param event DOCUMENT ME!
191: */
192: public void componentSelectionChanged(ComponentSelectionEvent event) {
193: if (!listenToComponentSelectionChanges) {
194: return;
195: }
196:
197: ListSelectionModel selectionModel = getSelectionModel();
198:
199: switch (event.getID()) {
200: case ComponentSelectionEvent.COMPONENTS_ADDED:
201: listenToListSelectionChanges = false;
202:
203: Component[] addedComponents = event.getComponents();
204:
205: for (int n = 0; n < addedComponents.length; n++) {
206: String componentName = components
207: .getName(addedComponents[n]);
208:
209: if (componentName != null) {
210: for (int i = 0; i < tableModel.getRowCount(); i++) {
211: if (((String) tableModel.getValueAt(i, 0))
212: .equals(componentName)) {
213: selectionModel.addSelectionInterval(i, i);
214:
215: break;
216: }
217: }
218: }
219: }
220:
221: listenToListSelectionChanges = true;
222:
223: break;
224:
225: case ComponentSelectionEvent.COMPONENTS_REMOVED:
226: listenToListSelectionChanges = false;
227:
228: Component[] removedComponents = event.getComponents();
229:
230: for (int n = 0; n < removedComponents.length; n++) {
231: String componentName = components
232: .getName(removedComponents[n]);
233:
234: if (componentName != null) {
235: for (int i = 0; i < tableModel.getRowCount(); i++) {
236: if (((String) tableModel.getValueAt(i, 0))
237: .equals(componentName)) {
238: selectionModel
239: .removeSelectionInterval(i, i);
240:
241: break;
242: }
243: }
244: }
245: }
246:
247: listenToListSelectionChanges = true;
248:
249: break;
250:
251: case ComponentSelectionEvent.SELECTION_CLEARED:
252: listenToListSelectionChanges = false;
253: clearSelection();
254: listenToListSelectionChanges = true;
255:
256: break;
257: }
258: }
259:
260: // internal class
261: class ComponentTableListener implements ListSelectionListener {
262: //~ Methods --------------------------------------------------------------------------------
263:
264: /**
265: * Called when the list selection has changed.
266: *
267: * @param event DOCUMENT ME!
268: */
269: public void valueChanged(ListSelectionEvent event) {
270: if (!listenToListSelectionChanges) {
271: return;
272: }
273:
274: listenToComponentSelectionChanges = false;
275:
276: int[] selectedRows = getSelectedRows();
277:
278: if (!componentSelection.isEmpty()) {
279: componentSelection.clear();
280: }
281:
282: Vector newComponents = new Vector(5, 5);
283:
284: for (int i = 0; i < selectedRows.length; i++) {
285: String componentName = (String) tableModel.getValueAt(
286: selectedRows[i], 0);
287: Component component = components
288: .getComponent(componentName);
289:
290: if (component != null) {
291: newComponents.add(component);
292: }
293: }
294:
295: if (newComponents.size() > 0) {
296: Component[] addedComponents = new Component[newComponents
297: .size()];
298:
299: for (int i = 0; i < newComponents.size(); i++) {
300: addedComponents[i] = (Component) newComponents
301: .get(i);
302: }
303:
304: componentSelection.add(addedComponents);
305: }
306:
307: listenToComponentSelectionChanges = true;
308: }
309: }
310: }
|