KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > selection > AbstractSelectionModel


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.selection;
14
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import com.tonbeller.wcf.controller.RequestContext;
20
21 /**
22  * simplifies implementation of the SelectionModel
23  *
24  * @author av
25  */

26 public abstract class AbstractSelectionModel implements SelectionModel {
27   private int mode;
28   private SelectionChangeSupport changeSupport;
29
30   public abstract Set JavaDoc getSelection();
31   public abstract void add(Object JavaDoc obj);
32   public abstract void remove(Object JavaDoc obj);
33   public abstract void clear();
34
35   protected AbstractSelectionModel(int mode) {
36     this.mode = mode;
37     changeSupport = new SelectionChangeSupport(this);
38   }
39
40   public int getMode() {
41     return mode;
42   }
43
44   public void setMode(int mode) {
45     this.mode = mode;
46   }
47
48
49   public void addAll(Collection JavaDoc c) {
50     for (Iterator JavaDoc it = c.iterator(); it.hasNext();)
51       add(it.next());
52   }
53
54   public boolean contains(Object JavaDoc obj) {
55     return getSelection().contains(obj);
56   }
57
58   public Object JavaDoc getSingleSelection() {
59     Set JavaDoc selection = getSelection();
60     if (selection.size() == 0)
61       return null;
62     if (selection.size() == 1)
63       return selection.iterator().next();
64     throw new IllegalStateException JavaDoc("single selection contains " + selection.size() + " elements");
65   }
66
67   public void setSingleSelection(Object JavaDoc obj) {
68     clear();
69     add(obj);
70   }
71
72   public boolean isEmpty() {
73     return getSelection().isEmpty();
74   }
75
76   /**
77    * returns true for all objects except Unselectable
78    * @see Unselectable
79    */

80   public boolean isSelectable(Object JavaDoc item) {
81     if (item instanceof Unselectable)
82       return false;
83     return true;
84   }
85
86   public void fireSelectionChanged(RequestContext context) {
87     changeSupport.fireSelectionChanged(context);
88   }
89
90   public void addSelectionListener(SelectionChangeListener l) {
91     changeSupport.addSelectionListener(l);
92   }
93
94   public void removeSelectionListener(SelectionChangeListener l) {
95     changeSupport.removeSelectionListener(l);
96   }
97
98 }
99
Popular Tags