KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > serverconfig > permissions > list > PermissionsSelectionList


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.serverconfig.permissions.list;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.LayoutManager JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28 import java.awt.event.MouseListener JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.swing.BorderFactory JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JScrollPane JavaDoc;
35
36 import org.openharmonise.vfs.*;
37
38
39 /**
40  * List of Roles.
41  *
42  * @author Matthew Large
43  * @version $Revision: 1.1 $
44  *
45  */

46 public class PermissionsSelectionList
47     extends JPanel JavaDoc
48     implements MouseListener JavaDoc, LayoutManager JavaDoc {
49
50         /**
51          * Array of Role cells.
52          */

53         private Object JavaDoc[] m_roleCells = null;
54
55         /**
56          * Inner panel.
57          */

58         private JPanel JavaDoc m_list = null;
59     
60         /**
61          * Scroll pane.
62          */

63         private JScrollPane JavaDoc m_scroller = null;
64     
65         /**
66          * Height of this component.
67          */

68         private int m_nHeight = 0;
69     
70         /**
71          * Width of this component.
72          */

73         private int m_nWidth = 340;
74     
75         /**
76          * List of {@link PermissionsSelectionListener} listeners.
77          */

78         private ArrayList JavaDoc m_listeners = new ArrayList JavaDoc();
79     
80         /**
81          * Constructs a new permissions selection list.
82          */

83         public PermissionsSelectionList() {
84             super();
85             this.setup();
86         }
87     
88         /**
89          *
90          *
91          * @param vals
92          */

93         public PermissionsSelectionList(Object JavaDoc[] vals) {
94             super();
95             m_roleCells = vals;
96             this.setup();
97         }
98
99         /**
100          * @param arg0
101          */

102         private PermissionsSelectionList(boolean arg0) {
103             super(arg0);
104         }
105
106         /**
107          * @param arg0
108          */

109         private PermissionsSelectionList(LayoutManager JavaDoc arg0) {
110             super(arg0);
111         }
112
113         /**
114          * @param arg0
115          * @param arg1
116          */

117         private PermissionsSelectionList(LayoutManager JavaDoc arg0, boolean arg1) {
118             super(arg0, arg1);
119         }
120     
121         private void setup() {
122             this.setLayout(new BorderLayout JavaDoc());
123             this.setBorder( BorderFactory.createLineBorder(Color.BLACK) );
124         
125
126             this.setBackground(Color.WHITE);
127         
128             m_list = new JPanel JavaDoc();
129             m_list.setBackground(Color.WHITE);
130             m_list.setLayout(this);
131         
132             m_scroller = new JScrollPane JavaDoc(m_list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
133             m_scroller.setPreferredSize(new Dimension JavaDoc(270,300));
134         
135             this.add(m_scroller);
136             this.validate();
137         }
138
139         /* (non-Javadoc)
140          * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
141          */

142         public void removeLayoutComponent(Component JavaDoc arg0) {
143         }
144
145         /* (non-Javadoc)
146          * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
147          */

148         public void layoutContainer(Container JavaDoc container) {
149             int nCount = container.getComponentCount();
150             int nHeight = 0;
151         
152             nCount = container.getComponentCount();
153             for(int i=0; i<nCount; i++) {
154                 Component JavaDoc comp = container.getComponent(i);
155                     comp.setSize(comp.getPreferredSize());
156                     comp.setLocation(0, nHeight);
157                     nHeight = nHeight + comp.getPreferredSize().height;
158             }
159             if(nHeight!=this.m_nHeight) {
160                 this.m_nHeight = nHeight;
161                 if(nHeight>300) {
162                     ((JPanel JavaDoc)container).setPreferredSize(new Dimension JavaDoc(m_nWidth, nHeight));
163                     this.m_scroller.validate();
164                 } else {
165                     ((JPanel JavaDoc)container).setPreferredSize(new Dimension JavaDoc(m_nWidth,300));
166                     this.m_scroller.validate();
167                 }
168             }
169         }
170     
171     
172
173
174         /* (non-Javadoc)
175          * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
176          */

177         public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
178         }
179
180         /* (non-Javadoc)
181          * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
182          */

183         public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
184             return new Dimension JavaDoc(m_nWidth, this.m_roleCells.length*20);
185         }
186
187         /* (non-Javadoc)
188          * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
189          */

190         public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
191             return new Dimension JavaDoc(m_nWidth, 100);
192         }
193
194
195         /* (non-Javadoc)
196          * @see java.awt.Component#getPreferredSize()
197          */

198         public Dimension JavaDoc getPreferredSize() {
199             return new Dimension JavaDoc(300,300);
200         }
201     
202         public void removeFile(String JavaDoc sPath) {
203             for(int i=0; i<m_list.getComponentCount(); i++) {
204                 if((m_list.getComponent(i) instanceof PermissionsSelectionCell)) {
205                     if( ((PermissionsSelectionCell)m_list.getComponent(i)).getPath().equals(sPath) ) {
206                         this.m_list.remove(m_list.getComponent(i));
207                     }
208                 }
209             }
210             this.validateTree();
211             this.repaint();
212         }
213     
214         public void removeSelected() {
215             for(int i=0; i<m_list.getComponentCount(); i++) {
216                 if((m_list.getComponent(i) instanceof PermissionsSelectionCell)) {
217                     if( ((PermissionsSelectionCell)m_list.getComponent(i)).isSelected() ) {
218                         this.m_list.remove(m_list.getComponent(i));
219                     }
220                 }
221             }
222             this.validateTree();
223             this.repaint();
224         }
225     
226         public String JavaDoc getSelectedPath() {
227             for(int i=0; i<m_list.getComponentCount(); i++) {
228                 if((m_list.getComponent(i) instanceof PermissionsSelectionCell)) {
229                     if( ((PermissionsSelectionCell)m_list.getComponent(i)).isSelected() ) {
230                         return ((PermissionsSelectionCell)m_list.getComponent(i)).getPath();
231                     }
232                 }
233             }
234             return null;
235         }
236
237         /* (non-Javadoc)
238          * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
239          */

240         public void mouseClicked(MouseEvent JavaDoc me) {
241             if(me.getSource() instanceof PermissionsSelectionCell) {
242                 PermissionsSelectionCell cell = (PermissionsSelectionCell) me.getSource();
243                 this.fireSelectionEvent( cell.getPath() );
244                 for(int i=0; i<m_list.getComponentCount(); i++) {
245                     if((m_list.getComponent(i) instanceof PermissionsSelectionCell)) {
246                         ((PermissionsSelectionCell)m_list.getComponent(i)).setSelected(false);
247                     }
248                 }
249                 if ( (me.getModifiersEx() & (MouseEvent.CTRL_DOWN_MASK)) == MouseEvent.CTRL_DOWN_MASK) {
250                     cell.setSelected(false);
251                 } else {
252                     cell.setSelected(true);
253                 }
254             }
255             this.validateTree();
256             this.repaint();
257         }
258         
259         public void selectPermission(String JavaDoc sPath) {
260             PermissionsSelectionCell cell = null;
261             for(int i=0; i<m_list.getComponentCount(); i++) {
262                 if((m_list.getComponent(i) instanceof PermissionsSelectionCell)) {
263                     PermissionsSelectionCell tempCell = ((PermissionsSelectionCell)m_list.getComponent(i));
264                     tempCell.setSelected(false);
265                     if(tempCell.getPath().equals(sPath)) {
266                         cell = tempCell;
267                     }
268                 }
269             }
270             if(cell!=null) {
271                 cell.setSelected(true);
272                 this.fireSelectionEvent(sPath);
273             }
274         }
275
276         /* (non-Javadoc)
277          * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
278          */

279         public void mouseEntered(MouseEvent JavaDoc arg0) {
280         }
281
282         /* (non-Javadoc)
283          * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
284          */

285         public void mouseExited(MouseEvent JavaDoc arg0) {
286         }
287
288         /* (non-Javadoc)
289          * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
290          */

291         public void mousePressed(MouseEvent JavaDoc arg0) {
292         }
293
294         /* (non-Javadoc)
295          * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
296          */

297         public void mouseReleased(MouseEvent JavaDoc arg0) {
298         }
299     
300         public void addFile(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
301             ArrayList JavaDoc aReasons = new ArrayList JavaDoc();
302         
303             PermissionsSelectionCell cell = new PermissionsSelectionCell(this, sPath, vfs);
304             boolean bFound = false;
305             for(int i=0; i<m_list.getComponentCount(); i++) {
306                 if((m_list.getComponent(i) instanceof PermissionsSelectionCell) && ((PermissionsSelectionCell)m_list.getComponent(i)).equals(cell)) {
307                     bFound=true;
308                 }
309             }
310             if(!bFound) {
311                 cell.addMouseListener(this);
312                 m_list.add(cell);
313             }
314         
315             this.validateTree();
316
317             m_scroller.validate();
318         }
319     
320         public void addPermissionsSelectionListener(PermissionsSelectionListener listener) {
321             if(!this.m_listeners.contains(listener)) {
322                 this.m_listeners.add(listener);
323             }
324         }
325     
326         public void removePermissionsSelectionListener(PermissionsSelectionListener listener) {
327             this.m_listeners.remove(listener);
328         }
329     
330         private void fireSelectionEvent(String JavaDoc sPath) {
331             Iterator JavaDoc itor = this.m_listeners.iterator();
332             while (itor.hasNext()) {
333                 PermissionsSelectionListener element = (PermissionsSelectionListener) itor.next();
334                 element.permissionSelected(sPath);
335             }
336         }
337
338     }
339
340
Popular Tags