KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > range > details > RangeSelectionList


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.metadata.range.swing.range.details;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openharmonise.him.window.messages.*;
29 import org.openharmonise.vfs.*;
30
31
32 /**
33  * @author Matthew Large
34  * @version $Revision: 1.1 $
35  *
36  */

37 public class RangeSelectionList extends JPanel
38                            implements MouseListener, LayoutManager {
39
40     private Object JavaDoc[] m_values = null;
41
42     private JPanel m_list = null;
43     
44     private JScrollPane m_scroller = null;
45     
46     private int m_nHeight = 0;
47     
48     private int m_nWidth = 150;
49     
50     /**
51      *
52      */

53     public RangeSelectionList() {
54         super();
55         this.setup();
56     }
57     
58     public RangeSelectionList(Object JavaDoc[] vals) {
59         super();
60         m_values = vals;
61         this.setup();
62     }
63
64     /**
65      * @param arg0
66      */

67     private RangeSelectionList(boolean arg0) {
68         super(arg0);
69     }
70
71     /**
72      * @param arg0
73      */

74     private RangeSelectionList(LayoutManager arg0) {
75         super(arg0);
76     }
77
78     /**
79      * @param arg0
80      * @param arg1
81      */

82     private RangeSelectionList(LayoutManager arg0, boolean arg1) {
83         super(arg0, arg1);
84     }
85     
86     private void setup() {
87         this.setLayout(new BorderLayout());
88         
89
90         this.setBackground(Color.WHITE);
91         
92         m_list = new JPanel();
93         m_list.setLayout(this);
94         
95         m_scroller = new JScrollPane(m_list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
96         m_scroller.setPreferredSize(new Dimension(270,300));
97         m_scroller.setBorder(BorderFactory.createEmptyBorder());
98
99         this.add(m_scroller);
100         this.validate();
101     }
102     
103     public String JavaDoc getSelectedPath() {
104         for(int i=0; i<m_list.getComponentCount(); i++) {
105             if((m_list.getComponent(i) instanceof RangeSelectionCell)) {
106                 if( ((RangeSelectionCell)m_list.getComponent(i)).isSelected() ) {
107                     return ((RangeSelectionCell)m_list.getComponent(i)).getPath();
108                 }
109             }
110         }
111         return null;
112     }
113
114     /* (non-Javadoc)
115      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
116      */

117     public void removeLayoutComponent(Component arg0) {
118     }
119
120     /* (non-Javadoc)
121      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
122      */

123     public void layoutContainer(Container container) {
124         int nCount = container.getComponentCount();
125         int nHeight = 0;
126         
127         nCount = container.getComponentCount();
128         for(int i=0; i<nCount; i++) {
129             Component comp = container.getComponent(i);
130                 comp.setSize(comp.getPreferredSize());
131                 comp.setLocation(0, nHeight);
132                 nHeight = nHeight + comp.getPreferredSize().height;
133         }
134         if(nHeight!=this.m_nHeight) {
135             this.m_nHeight = nHeight;
136             if(nHeight>300) {
137                 ((JPanel)container).setPreferredSize(new Dimension(m_nWidth, nHeight));
138                 this.m_scroller.validate();
139             } else {
140                 ((JPanel)container).setPreferredSize(new Dimension(m_nWidth,300));
141                 this.m_scroller.validate();
142             }
143         }
144     }
145     
146     
147
148
149     /* (non-Javadoc)
150      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
151      */

152     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
153     }
154
155     /* (non-Javadoc)
156      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
157      */

158     public Dimension minimumLayoutSize(Container arg0) {
159         return new Dimension(m_nWidth, this.m_values.length*20);
160     }
161
162     /* (non-Javadoc)
163      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
164      */

165     public Dimension preferredLayoutSize(Container arg0) {
166         return new Dimension(m_nWidth, this.m_values.length*20);
167     }
168
169
170     /* (non-Javadoc)
171      * @see java.awt.Component#getPreferredSize()
172      */

173     public Dimension getPreferredSize() {
174         return new Dimension(300,300);
175     }
176     
177     public void removeFile(String JavaDoc sPath) {
178         for(int i=0; i<m_list.getComponentCount(); i++) {
179             if((m_list.getComponent(i) instanceof RangeSelectionCell)) {
180                 if( ((RangeSelectionCell)m_list.getComponent(i)).getPath().equals(sPath) ) {
181                     this.m_list.remove(m_list.getComponent(i));
182                 }
183             }
184         }
185         this.validateTree();
186         this.repaint();
187     }
188     
189     public void removeSelected() {
190         for(int i=0; i<m_list.getComponentCount(); i++) {
191             if((m_list.getComponent(i) instanceof RangeSelectionCell)) {
192                 if( ((RangeSelectionCell)m_list.getComponent(i)).isSelected() ) {
193                     this.m_list.remove(m_list.getComponent(i));
194                 }
195             }
196         }
197         this.validateTree();
198         this.repaint();
199     }
200
201     /* (non-Javadoc)
202      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
203      */

204     public void mouseClicked(MouseEvent me) {
205         if(me.getSource() instanceof RangeSelectionCell) {
206             RangeSelectionCell cell = (RangeSelectionCell) me.getSource();
207             for(int i=0; i<m_list.getComponentCount(); i++) {
208                 if((m_list.getComponent(i) instanceof RangeSelectionCell)) {
209                     ((RangeSelectionCell)m_list.getComponent(i)).setSelected(false);
210                 }
211             }
212             if ( (me.getModifiersEx() & (MouseEvent.CTRL_DOWN_MASK)) == MouseEvent.CTRL_DOWN_MASK) {
213                 cell.setSelected(false);
214             } else {
215                 cell.setSelected(true);
216             }
217         }
218         this.validateTree();
219         this.repaint();
220     }
221
222     /* (non-Javadoc)
223      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
224      */

225     public void mouseEntered(MouseEvent arg0) {
226     }
227
228     /* (non-Javadoc)
229      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
230      */

231     public void mouseExited(MouseEvent arg0) {
232     }
233
234     /* (non-Javadoc)
235      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
236      */

237     public void mousePressed(MouseEvent arg0) {
238     }
239
240     /* (non-Javadoc)
241      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
242      */

243     public void mouseReleased(MouseEvent arg0) {
244     }
245     
246     public void addFile(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
247         ArrayList aReasons = new ArrayList();
248         
249         try {
250             RangeSelectionCell cell = new RangeSelectionCell(this, sPath, vfs);
251             boolean bFound = false;
252             for(int i=0; i<m_list.getComponentCount(); i++) {
253                 if((m_list.getComponent(i) instanceof RangeSelectionCell) && ((RangeSelectionCell)m_list.getComponent(i)).equals(cell)) {
254                     bFound=true;
255                 }
256             }
257             if(!bFound) {
258                 cell.addMouseListener(this);
259                 m_list.add(cell);
260             }
261         } catch(NullPointerException JavaDoc npe) {
262             MessageHandler.getInstance().fireMessageEvent("One of the Sections for this Range has not been found.", MessageHandler.TYPE_ERROR);
263         }
264         
265         this.validateTree();
266
267         m_scroller.validate();
268     }
269     
270     public List JavaDoc getPaths() {
271         ArrayList aPaths = new ArrayList();
272         for(int i=0; i<m_list.getComponentCount(); i++) {
273             if((m_list.getComponent(i) instanceof RangeSelectionCell)) {
274                 aPaths.add( ((RangeSelectionCell)m_list.getComponent(i)).getPath() );
275             }
276         }
277         return aPaths;
278     }
279
280 }
281
282
Popular Tags