summaryrefslogtreecommitdiff
path: root/javax/swing/tree/DefaultTreeSelectionModel.java
blob: de27dad04f9978985617894ef31e3e896a0c648f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/* DefaultTreeSelectionModel.java 
   Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
   
This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package javax.swing.tree;

import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.EventListener;
import java.util.Vector;

import javax.swing.DefaultListSelectionModel;
import javax.swing.event.EventListenerList;
import javax.swing.event.SwingPropertyChangeSupport;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

/**
 * DefaultTreeSelectionModel
 * 
 * @author Andrew Selkirk
 */
public class DefaultTreeSelectionModel
		implements Cloneable, Serializable, TreeSelectionModel
{
	static final long serialVersionUID = 3288129636638950196L;

	/**
	 * SELECTION_MODE_PROPERTY
	 */
	public static final String SELECTION_MODE_PROPERTY = "selectionMode";

	/**
	 * Our Swing property change support.
	 */
	protected SwingPropertyChangeSupport changeSupport;

	/**
	 * The current selection.
	 */
	protected TreePath[] selection;

	/**
	 * Our TreeSelectionListeners.
	 */
	protected EventListenerList listenerList;

	/**
	 * The current RowMapper.
	 */
	protected transient RowMapper rowMapper;

	/**
	 * The current listSelectionModel.
	 */
	protected DefaultListSelectionModel listSelectionModel;

	/**
	 * The current selection mode.
	 */
	protected int selectionMode;

	/**
	 * The path that has been added last.
	 */
	protected TreePath leadPath;

	/**
	 * The index of the last added path.
	 */
	protected int leadIndex;

	/**
	 * The row of the last added path according to the RowMapper.
	 */
	protected int leadRow;

	/**
	 * Constructs a new DefaultTreeSelectionModel.
	 */
	public DefaultTreeSelectionModel()
	{
		setSelectionMode(DISCONTIGUOUS_TREE_SELECTION);
		listenerList = new EventListenerList();
	}

	/**
	 * Creates a clone of this DefaultTreeSelectionModel with the same
	 * selection.
	 * 
	 * @exception CloneNotSupportedException should not be thrown here
	 * 
	 * @return a clone of this DefaultTreeSelectionModel
	 */
	public Object clone() throws CloneNotSupportedException
	{
		return null; // TODO
	}

	/**
	 * Returns a string that shows this object's properties.
	 * 
	 * @return a string that shows this object's properties
	 */
	public String toString()
	{
		return null; // TODO
	}

	/**
	 * writeObject
	 * 
	 * @param value0 TODO
	 * @exception IOException TODO
	 */
	private void writeObject(ObjectOutputStream value0) throws IOException
	{
		// TODO
	}

	/**
	 * readObject
	 * 
	 * @param value0 TODO
	 * @exception IOException TODO
	 * @exception ClassNotFoundException TODO
	 */
	private void readObject(ObjectInputStream value0) throws IOException,
			ClassNotFoundException
	{
		// TODO
	}

	/**
	 * Sets the RowMapper that should be used to map between paths and their
	 * rows.
	 * 
	 * @param rowMapper the RowMapper to set
	 * 
	 * @see RowMapper
	 */
	public void setRowMapper(RowMapper rowMapper)
	{
		// TODO
	}

	/**
	 * Returns the RowMapper that is currently used to map between paths and
	 * their rows.
	 * 
	 * @return the current RowMapper
	 * 
	 * @see RowMapper
	 */
	public RowMapper getRowMapper()
	{
		return rowMapper;
	}

	/**
	 * Sets the current selection mode. Possible values are
	 * {@link #SINGLE_TREE_SELECTION}, {@link #CONTIGUOUS_TREE_SELECTION} and
	 * {@link #DISCONTIGUOUS_TREE_SELECTION}.
	 * 
	 * @param mode the selection mode to be set
	 * 
	 * @see #getSelectionMode
	 * @see #SINGLE_TREE_SELECTION
	 * @see #CONTIGUOUS_TREE_SELECTION
	 * @see #DISCONTIGUOUS_TREE_SELECTION
	 */
	public void setSelectionMode(int mode)
	{
		selectionMode = mode;
	}

	/**
	 * Returns the current selection mode.
	 * 
	 * @return the current selection mode
	 * 
	 * @see #setSelectionMode
	 * @see #SINGLE_TREE_SELECTION
	 * @see #CONTIGUOUS_TREE_SELECTION
	 * @see #DISCONTIGUOUS_TREE_SELECTION
	 */
	public int getSelectionMode()
	{
		return selectionMode;
	}

	/**
	 * Sets this path as the only selection.
	 * 
	 * If this changes the selection the registered TreeSelectionListeners are
	 * notified.
	 * 
	 * @param path the path to set as selection
	 */
	public void setSelectionPath(TreePath path)
	{
		selection = new TreePath[] {
			path };
	}

	/**
	 * Sets the paths as selection. This method checks for duplicates and
	 * removes them.
	 * 
	 * If this changes the selection the registered TreeSelectionListeners are
	 * notified.
	 * 
	 * @param paths the paths to set as selection
	 */
	public void setSelectionPaths(TreePath[] paths)
	{
		// TODO
	}

	/**
	 * Adds a path to the list of selected paths. This method checks if the path
	 * is already selected and doesn't add the same path twice.
	 * 
	 * If this changes the selection the registered TreeSelectionListeners are
	 * notified.
	 * 
	 * @param path the path to add to the selection
	 */
	public void addSelectionPath(TreePath path)
	{
		if (!isPathSelected(path))
		{
			if (isSelectionEmpty())
				setSelectionPath(path);
			else
			{
				TreePath[] temp = new TreePath[selection.length + 1];
				System.arraycopy(selection, 0, temp, 0, selection.length);
				temp[temp.length - 1] = path;
				selection = new TreePath[temp.length];
				System.arraycopy(temp, 0, selection, 0, temp.length);
			}
			leadPath = path;
			fireValueChanged(new TreeSelectionEvent(this, path, true,
					leadPath, path));
		}
	}

	/**
	 * Adds the paths to the list of selected paths. This method checks if the
	 * paths are already selected and doesn't add the same path twice.
	 * 
	 * If this changes the selection the registered TreeSelectionListeners are
	 * notified.
	 * 
	 * @param paths the paths to add to the selection
	 */
	public void addSelectionPaths(TreePath[] paths)
	{
		if (paths != null)
		{
			TreePath v0 = null;
			for (int i = 0; i < paths.length; i++)
			{
				v0 = paths[i];
				if (!isPathSelected(v0))
				{
					if (isSelectionEmpty())
						setSelectionPath(v0);
					else
					{
						TreePath[] temp = new TreePath[selection.length + 1];
						System.arraycopy(selection, 0, temp, 0,
								selection.length);
						temp[temp.length - 1] = v0;
						selection = new TreePath[temp.length];
						System.arraycopy(temp, 0, selection, 0, temp.length);
					}
					leadPath = paths[paths.length - 1];
					fireValueChanged(new TreeSelectionEvent(this, v0, true,
							leadPath, paths[0]));
				}
			}
		}
	}

	/**
	 * Removes the path from the selection.
	 * 
	 * If this changes the selection the registered TreeSelectionListeners are
	 * notified.
	 * 
	 * @param path the path to remove
	 */
	public void removeSelectionPath(TreePath path)
	{
		int index = -1;
		if (isPathSelected(path))
		{
			for (int i = 0; i < selection.length; i++)
			{
				if (selection[i].equals(path))
				{
					index = i;
					break;
				}
			}
			TreePath[] temp = new TreePath[selection.length - 1];
			System.arraycopy(selection, 0, temp, 0, index);
			System.arraycopy(selection, index + 1, temp, index,
					selection.length - index - 1);
			selection = new TreePath[temp.length];
			System.arraycopy(temp, 0, selection, 0, temp.length);

			fireValueChanged(new TreeSelectionEvent(this, path, false,
					leadPath, path));
		}
	}

	/**
	 * Removes the paths from the selection.
	 * 
	 * If this changes the selection the registered TreeSelectionListeners are
	 * notified.
	 * 
	 * @param paths the paths to remove
	 */
	public void removeSelectionPaths(TreePath[] paths)
	{
		if (paths != null)
		{
			int index = -1;
			TreePath v0 = null;
			for (int i = 0; i < paths.length; i++)
			{
				v0 = paths[i];
				if (isPathSelected(v0))
				{
					for (int x = 0; x < selection.length; x++)
					{
						if (selection[i].equals(v0))
						{
							index = x;
							break;
						}
					}
					TreePath[] temp = new TreePath[selection.length - 1];
					System.arraycopy(selection, 0, temp, 0, index);
					System.arraycopy(selection, index + 1, temp, index,
							selection.length - index - 1);
					selection = new TreePath[temp.length];
					System.arraycopy(temp, 0, selection, 0, temp.length);

					fireValueChanged(new TreeSelectionEvent(this, v0, false,
							leadPath, paths[0]));
				}
			}
		}
	}

	/**
	 * Returns the first path in the selection. This is especially useful when
	 * the selectionMode is {@link #SINGLE_TREE_SELECTION}.
	 * 
	 * @return the first path in the selection
	 */
	public TreePath getSelectionPath()
	{
		if ((selection == null) || (selection.length == 0))
			return null;
		else
			return selection[0];
	}

	/**
	 * Returns the complete selection.
	 * 
	 * @return the complete selection
	 */
	public TreePath[] getSelectionPaths()
	{
		return selection;
	}

	/**
	 * Returns the number of paths in the selection.
	 * 
	 * @return the number of paths in the selection
	 */
	public int getSelectionCount()
	{
		if (selection == null)
			return 0;
		else
			return selection.length;
	}

	/**
	 * Checks if a given path is in the selection.
	 * 
	 * @param path the path to check
	 * 
	 * @return <code>true</code> if the path is in the selection,
	 *         <code>false</code> otherwise
	 */
	public boolean isPathSelected(TreePath path)
	{
		if (selection == null)
			return false;

		for (int i = 0; i < selection.length; i++)
		{
			if (selection[i].equals(path))
				return true;
		}
		return false;
	}

	/**
	 * Checks if the selection is empty.
	 * 
	 * @return <code>true</code> if the selection is empty, <code>false</code>
	 *         otherwise
	 */
	public boolean isSelectionEmpty()
	{
		return ((selection == null) || (selection.length == 0));
	}

	/**
	 * Removes all paths from the selection.
	 */
	public void clearSelection()
	{
		leadPath = null;
		selection = null;
	}

	/**
	 * Adds a <code>TreeSelectionListener</code> object to this model.
	 * 
	 * @param listener the listener to add
	 */
	public void addTreeSelectionListener(TreeSelectionListener listener)
	{
		listenerList.add(TreeSelectionListener.class, listener);
	}

	/**
	 * Removes a <code>TreeSelectionListener</code> object from this model.
	 * 
	 * @param listener the listener to remove
	 */
	public void removeTreeSelectionListener(TreeSelectionListener listener)
	{
		listenerList.remove(TreeSelectionListener.class, listener);
	}

	/**
	 * Returns all <code>TreeSelectionListener</code> added to this model.
	 * 
	 * @return an array of listeners
	 * 
	 * @since 1.4
	 */
	public TreeSelectionListener[] getTreeSelectionListeners()
	{
		return (TreeSelectionListener[]) 
				getListeners(TreeSelectionListener.class);
	}

	/**
	 * fireValueChanged
	 * 
	 * @param event the event to fire.
	 */
	protected void fireValueChanged(TreeSelectionEvent event)
	{
		TreeSelectionListener[] listeners = getTreeSelectionListeners();

		for (int i = 0; i < listeners.length; ++i)
			listeners[i].valueChanged(event);
	}

	/**
	 * Returns all added listeners of a special type.
	 * 
	 * @param listenerType the listener type
	 * 
	 * @return an array of listeners
	 * 
	 * @since 1.3
	 */
	public EventListener[] getListeners(Class listenerType)
	{
		return listenerList.getListeners(listenerType);
	}

	/**
	 * Returns the currently selected rows.
	 * 
	 * @return the currently selected rows
	 */
	public int[] getSelectionRows()
	{
		if (rowMapper == null)
			return null;
		else
			return rowMapper.getRowsForPaths(selection);
	}

	/**
	 * Returns the smallest row index from the selection.
	 * 
	 * @return the smallest row index from the selection
	 */
	public int getMinSelectionRow()
	{
		if ((rowMapper == null) || (selection == null)
				|| (selection.length == 0))
			return -1;
		else
		{
			int[] rows = rowMapper.getRowsForPaths(selection);
			int minRow = Integer.MAX_VALUE;
			for (int index = 0; index < rows.length; index++)
				minRow = Math.min(minRow, rows[index]);
			return minRow;
		}
	}

	/**
	 * Returns the largest row index from the selection.
	 * 
	 * @return the largest row index from the selection
	 */
	public int getMaxSelectionRow()
	{
		if ((rowMapper == null) || (selection == null)
				|| (selection.length == 0))
			return -1;
		else
		{
			int[] rows = rowMapper.getRowsForPaths(selection);
			int maxRow = -1;
			for (int index = 0; index < rows.length; index++)
				maxRow = Math.max(maxRow, rows[index]);
			return maxRow;
		}
	}

	/**
	 * Checks if a particular row is selected.
	 * 
	 * @param row the index of the row to check
	 * 
	 * @return <code>true</code> if the row is in this selection,
	 *         <code>false</code> otherwise
	 */
	public boolean isRowSelected(int row)
	{
		return false; // TODO
	}

	/**
	 * Updates the mappings from TreePaths to row indices.
	 */
	public void resetRowSelection()
	{
		// TODO
	}

	/**
	 * getLeadSelectionRow
	 * 
	 * @return int
	 */
	public int getLeadSelectionRow()
	{
		if ((rowMapper == null) || (leadPath == null))
			return -1;
		else
			return rowMapper.getRowsForPaths(new TreePath[] {
				leadPath })[0];
	}

	/**
	 * getLeadSelectionPath
	 * 
	 * @return TreePath
	 */
	public TreePath getLeadSelectionPath()
	{
		return leadPath;
	}

	/**
	 * Adds a <code>PropertyChangeListener</code> object to this model.
	 * 
	 * @param listener the listener to add.
	 */
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		changeSupport.addPropertyChangeListener(listener);
	}

	/**
	 * Removes a <code>PropertyChangeListener</code> object from this model.
	 * 
	 * @param listener the listener to remove.
	 */
	public void removePropertyChangeListener(PropertyChangeListener listener)
	{
		changeSupport.removePropertyChangeListener(listener);
	}

	/**
	 * Returns all added <code>PropertyChangeListener</code> objects.
	 * 
	 * @return an array of listeners.
	 * 
	 * @since 1.4
	 */
	public PropertyChangeListener[] getPropertyChangeListeners()
	{
		return changeSupport.getPropertyChangeListeners();
	}

	/**
	 * Makes sure the currently selected paths are valid according to the
	 * current selectionMode.
	 * 
	 * If the selectionMode is set to {@link #CONTIGUOUS_TREE_SELECTION} and the
	 * selection isn't contiguous then the selection is reset to the first set
	 * of contguous paths.
	 * 
	 * If the selectionMode is set to {@link #SINGLE_TREE_SELECTION} and the
	 * selection has more than one path, the selection is reset to the contain
	 * only the first path.
	 */
	protected void insureRowContinuity()
	{
		// TODO
	}

	/**
	 * Returns <code>true</code> if the paths are contiguous or we have no
	 * RowMapper assigned.
	 * 
	 * @param paths the paths to check for continuity
	 * @return <code>true</code> if the paths are contiguous or we have no
	 *         RowMapper assigned
	 */
	protected boolean arePathsContiguous(TreePath[] paths)
	{
		return false; // TODO
	}

	/**
	 * Checks if the paths can be added. This returns <code>true</code> if:
	 * <ul>
	 * <li><code>paths</code> is <code>null</code> or empty</li>
	 * <li>we have no RowMapper assigned</li>
	 * <li>nothing is currently selected</li>
	 * <li>selectionMode is {@link #DISCONTIGUOUS_TREE_SELECTION}</li>
	 * <li>adding the paths to the selection still results in a contiguous set
	 * of paths</li>
	 * 
	 * @param paths the paths to check
	 * 
	 * @return <code>true</code> if the paths can be added with respect to the
	 *         selectionMode
	 */
	protected boolean canPathsBeAdded(TreePath[] paths)
	{
		return false; // TODO
	}

	/**
	 * Checks if the paths can be removed without breaking the continuity of the
	 * selection according to selectionMode.
	 * 
	 * @param paths the paths to check
	 * @return <code>true</code> if the paths can be removed with respect to
	 *         the selectionMode
	 */
	protected boolean canPathsBeRemoved(TreePath[] paths)
	{
		return false; // TODO
	}

	/**
	 * notifyPathChange
	 * 
	 * @param value0 TODO
	 * @param value1 TODO
	 */
	protected void notifyPathChange(Vector value0, TreePath value1)
	{
		// TODO
	}

	/**
	 * Updates the lead index instance field.
	 */
	protected void updateLeadIndex()
	{
		// TODO
	}

	/**
	 * Deprecated and not used.
	 */
	protected void insureUniqueness()
	{
		// TODO
	}
}