View Javadoc

1   /*
2    *  File: JaretTableSelectionModelImpl.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui.table.model;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  import java.util.Vector;
16  
17  /***
18   * Implementation of the JaretTableSelectionModel.
19   * 
20   * @author Peter Kliem
21   * @version $Id: JaretTableSelectionModelImpl.java 180 2007-01-07 18:44:01Z olk $
22   */
23  public class JaretTableSelectionModelImpl implements IJaretTableSelectionModel {
24      /*** listeners to inform. */
25      protected List<IJaretTableSelectionModelListener> _listeners;
26  
27      /*** true for allowance of full row selection. */
28      protected boolean _fullRowSelectionAllowed = true;
29  
30      /*** true for allowance of full column selection. */
31      protected boolean _fullColumnSelectionAllowed = true;
32  
33      /*** true for allowance of single cell selection. */
34      protected boolean _cellSelectioAllowed = true;
35  
36      /*** true if multiple selection of more than one elemnt is allowed. */
37      protected boolean _multipleSelectionAllowed = true;
38  
39      /*** true if only row selections are allowed. */
40      protected boolean _onlyRowSelectionAllowed = false;
41  
42      /*** the selection data store. */
43      protected IJaretTableSelection _selection = new JaretTableSelectionImpl();
44  
45      /***
46       * {@inheritDoc}
47       */
48      public void clearSelection() {
49          List<IRow> l = new ArrayList<IRow>();
50          l.addAll(_selection.getSelectedRows());
51          for (IRow row : l) {
52              remSelectedRow(row);
53          }
54          List<IColumn> c = new ArrayList<IColumn>();
55          c.addAll(_selection.getSelectedColumns());
56          for (IColumn col : c) {
57              remSelectedColumn(col);
58          }
59          List<IJaretTableCell> t = new ArrayList<IJaretTableCell>();
60          t.addAll(_selection.getSelectedCells());
61          for (IJaretTableCell cell : t) {
62              remSelectedCell(cell);
63          }
64      }
65  
66      /***
67       * {@inheritDoc}
68       */
69      public boolean isFullRowSelectionAllowed() {
70          return _fullRowSelectionAllowed;
71      }
72  
73      /***
74       * {@inheritDoc}
75       */
76      public void setFullRowSelectionAllowed(boolean allowed) {
77          _fullRowSelectionAllowed = allowed;
78      }
79  
80      /***
81       * {@inheritDoc}
82       */
83      public boolean isFullColumnSelectionAllowed() {
84          return _fullColumnSelectionAllowed;
85      }
86  
87      /***
88       * {@inheritDoc}
89       */
90      public void setFullColumnSelectionAllowed(boolean allowed) {
91          _fullColumnSelectionAllowed = allowed;
92      }
93  
94      /***
95       * {@inheritDoc}
96       */
97      public boolean isCellSelectionAllowed() {
98          return _cellSelectioAllowed;
99      }
100 
101     /***
102      * {@inheritDoc}
103      */
104     public void setCellSelectionAllowed(boolean allowed) {
105         _cellSelectioAllowed = allowed;
106     }
107 
108     /***
109      * {@inheritDoc}
110      */
111     public boolean isMultipleSelectionAllowed() {
112         return _multipleSelectionAllowed;
113     }
114 
115     /***
116      * {@inheritDoc}
117      */
118     public void setMultipleSelectionAllowed(boolean allowed) {
119         _multipleSelectionAllowed = allowed;
120     }
121 
122     /***
123      * {@inheritDoc}
124      */
125     public void addSelectedRow(IRow row) {
126         if (!_selection.getSelectedRows().contains(row)) {
127             _selection.addRow(row);
128             fireRowSelectionAdded(row);
129         }
130     }
131 
132     /***
133      * {@inheritDoc}
134      */
135     public void remSelectedRow(IRow row) {
136         if (_selection.getSelectedRows().contains(row)) {
137             _selection.remRow(row);
138             fireRowSelectionRemoved(row);
139         }
140     }
141 
142     /***
143      * {@inheritDoc}
144      */
145     public void addSelectedColumn(IColumn column) {
146         if (!_selection.getSelectedColumns().contains(column)) {
147             _selection.addColumn(column);
148             fireColumnSelectionAdded(column);
149         }
150     }
151 
152     /***
153      * {@inheritDoc}
154      */
155     public void remSelectedColumn(IColumn column) {
156         if (_selection.getSelectedColumns().contains(column)) {
157             _selection.remColumn(column);
158             fireColumnSelectionRemoved(column);
159         }
160     }
161 
162     /***
163      * {@inheritDoc}
164      */
165     public void addSelectedCell(IJaretTableCell cell) {
166         if (!_selection.getSelectedCells().contains(cell)) {
167             _selection.addCell(cell);
168             fireCellSelectionAdded(cell);
169         }
170     }
171 
172     /***
173      * {@inheritDoc}
174      */
175     public void remSelectedCell(IJaretTableCell cell) {
176         if (_selection.getSelectedCells().contains(cell)) {
177             _selection.remCell(cell);
178             fireCellSelectionRemoved(cell);
179         }
180     }
181 
182     /***
183      * {@inheritDoc}
184      */
185     public IJaretTableSelection getSelection() {
186         return _selection;
187     }
188 
189 
190     
191     private void fireRowSelectionAdded(IRow row) {
192         if (_listeners != null) {
193             for (IJaretTableSelectionModelListener listener : _listeners) {
194                 listener.rowSelectionAdded(row);
195             }
196         }
197     }
198 
199     private void fireRowSelectionRemoved(IRow row) {
200         if (_listeners != null) {
201             for (IJaretTableSelectionModelListener listener : _listeners) {
202                 listener.rowSelectionRemoved(row);
203             }
204         }
205     }
206 
207     private void fireColumnSelectionAdded(IColumn column) {
208         if (_listeners != null) {
209             for (IJaretTableSelectionModelListener listener : _listeners) {
210                 listener.columnSelectionAdded(column);
211             }
212         }
213     }
214 
215     private void fireColumnSelectionRemoved(IColumn column) {
216         if (_listeners != null) {
217             for (IJaretTableSelectionModelListener listener : _listeners) {
218                 listener.columnSelectionRemoved(column);
219             }
220         }
221     }
222 
223     private void fireCellSelectionAdded(IJaretTableCell cell) {
224         if (_listeners != null) {
225             for (IJaretTableSelectionModelListener listener : _listeners) {
226                 listener.cellSelectionAdded(cell);
227             }
228         }
229     }
230 
231     private void fireCellSelectionRemoved(IJaretTableCell cell) {
232         if (_listeners != null) {
233             for (IJaretTableSelectionModelListener listener : _listeners) {
234                 listener.cellSelectionRemoved(cell);
235             }
236         }
237     }
238 
239     /***
240      * {@inheritDoc}
241      */
242     public synchronized void addTableSelectionModelListener(IJaretTableSelectionModelListener jtsm) {
243         if (_listeners == null) {
244             _listeners = new Vector<IJaretTableSelectionModelListener>();
245         }
246         _listeners.add(jtsm);
247 
248     }
249 
250     /***
251      * {@inheritDoc}
252      */
253     public void removeTableSelectionModelListener(IJaretTableSelectionModelListener jtsm) {
254         if (_listeners != null) {
255             _listeners.remove(jtsm);
256         }
257     }
258 
259     /***
260      * {@inheritDoc}
261      */
262     public boolean isOnlyRowSelectionAllowed() {
263         return _onlyRowSelectionAllowed;
264     }
265 
266     /***
267      * {@inheritDoc}
268      */
269     public void setOnlyRowSelectionAllowed(boolean allowed) {
270         _onlyRowSelectionAllowed = allowed;
271     }
272 
273 }