KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > resourcehandling > ResourceSelectionCell


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.resourcehandling;
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.*;
29 import org.openharmonise.swing.FontManager;
30 import org.openharmonise.vfs.*;
31 import org.openharmonise.vfs.gui.*;
32 import org.openharmonise.vfs.metadata.value.*;
33 import org.openharmonise.vfs.servers.ServerList;
34
35 /**
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.2 $
39  *
40  */

41 public class ResourceSelectionCell extends JPanel implements LayoutManager, ActionListener, MouseListener {
42
43     private Reason m_reason = null;
44     
45     private Color m_selectedColor = new Color(173, 169, 143);
46     
47     private String JavaDoc m_sFilePath = null;
48     private AbstractVirtualFileSystem m_vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
49     
50     private ResourceSelectionList m_list = null;
51     
52     private JLabel m_resourceLabel = null;
53     private JLabel m_errorLabel = null;
54     private JComboBox m_reasonComboBox = null;
55
56     private int m_nWidth = 270;
57     
58     private boolean m_bInSetup = true;
59
60     /**
61      * @param list
62      * @param vfFile
63      */

64     public ResourceSelectionCell(ResourceSelectionList list, VirtualFile vfFile) {
65         super();
66         this.m_list = list;
67         this.m_sFilePath = vfFile.getFullPath();
68         this.setup();
69     }
70
71     /**
72      * @param value
73      * @param reason
74      * @param list
75      */

76     public ResourceSelectionCell(ResourceValue value, Reason reason, ResourceSelectionList list) {
77         super();
78         this.m_list = list;
79         this.m_sFilePath = value.getValue();
80         this.m_reason = reason;
81         this.setup();
82     }
83     
84     private void setup() {
85         this.setLayout(this);
86         this.setBackground(Color.WHITE);
87         
88         this.m_errorLabel = new JLabel();
89         this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif"));
90         this.m_errorLabel.addMouseListener(this);
91         this.add(this.m_errorLabel);
92         
93         VirtualFile vfFile = this.m_vfs.getVirtualFile(m_sFilePath).getResource();
94         this.m_resourceLabel = new JLabel(this.m_vfs.getVirtualFileSystemView().getDisplayName(vfFile));
95         this.m_resourceLabel.setToolTipText(this.m_vfs.getVirtualFileSystemView().getDisplayName(vfFile));
96         this.m_resourceLabel.setIcon( this.m_vfs.getVirtualFileSystemView().getIcon(vfFile) );
97         this.m_resourceLabel.setForeground(Color.RED);
98         this.m_resourceLabel.addMouseListener(this);
99         this.m_resourceLabel.setFont( FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE) );
100         this.add(this.m_resourceLabel);
101         
102         this.refreshReasons();
103         this.add(this.m_reasonComboBox);
104         this.checkValid();
105         this.m_bInSetup = false;
106     }
107     
108     private void checkValid() {
109         if(this.m_reason!=null && this.m_reason.isValid()) {
110             this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-confirm.gif"));
111             this.m_resourceLabel.setForeground(Color.BLACK);
112         } else {
113             this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif"));
114             this.m_resourceLabel.setForeground(Color.RED);
115         }
116     }
117     
118     protected boolean isSelected() {
119         if(this.getBackground()==this.m_selectedColor) {
120             return true;
121         } else {
122             return false;
123         }
124     }
125     
126     protected void prepareToRemove() {
127         if(this.m_reason!=null) {
128             this.m_reason.removeValue(m_sFilePath);
129         }
130     }
131     
132     protected void setCellSelected(boolean bSelected) {
133         if(!bSelected) {
134             this.setBackground(Color.WHITE);
135             this.m_resourceLabel.setBackground(Color.WHITE);
136             this.m_errorLabel.setBackground(Color.WHITE);
137             this.m_reasonComboBox.setBackground(Color.WHITE);
138         } else {
139             this.setBackground(this.m_selectedColor);
140             this.m_resourceLabel.setBackground(this.m_selectedColor);
141             this.m_errorLabel.setBackground(this.m_selectedColor);
142             this.m_reasonComboBox.setBackground(this.m_selectedColor);
143         }
144         this.validateTree();
145     }
146
147     private Reason getReasonByName(String JavaDoc sReasonName) {
148         List JavaDoc reasons = this.m_list.getReasons();
149         Iterator itor = reasons.iterator();
150         while (itor.hasNext()) {
151             Reason tempReason = (Reason) itor.next();
152             if(tempReason.getDisplayName().equals(sReasonName)) {
153                 return tempReason;
154             }
155         }
156         return null;
157     }
158     
159     protected void setReason(Reason newReason) {
160         if(this.m_reason!=null && newReason!=null && this.m_reason!=newReason && !this.m_reason.getDisplayName().equalsIgnoreCase(newReason.getDisplayName())) {
161             this.m_reason.removeValue(this.m_sFilePath);
162         }
163         if(newReason!=null && (this.m_reason==null || this.m_reason!=newReason) && (this.m_reason==null || !this.m_reason.getDisplayName().equalsIgnoreCase(newReason.getDisplayName()) )) {
164             newReason.addValue(this.m_sFilePath);
165             this.m_reason = newReason;
166         }
167     }
168
169     /* (non-Javadoc)
170      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
171      */

172     public void actionPerformed(ActionEvent ae) {
173         if(ae.getActionCommand().equals("COMBO")) {
174             Reason newReason = null;
175             String JavaDoc sReasonName = (String JavaDoc) this.m_reasonComboBox.getSelectedItem();
176
177             newReason = this.getReasonByName(sReasonName);
178             if(newReason!=null) {
179                 this.setReason(newReason);
180                 this.m_reasonComboBox.setSelectedItem(this.m_reason.getDisplayName());
181             } else {
182                 if(this.m_reason!=null) {
183                     this.m_reasonComboBox.setSelectedItem(this.m_reason.getDisplayName());
184                 }
185             }
186             this.checkValid();
187         }
188     }
189     
190     public void refreshReasons() {
191         List JavaDoc aUseableReasons = new ArrayList();
192         aUseableReasons.add("");
193         List JavaDoc reasons = this.m_list.getReasons();
194         for(int i=0; i<reasons.size(); i++) {
195             Reason reason = (Reason) reasons.get(i);
196             if(reason.isValidReasonForResource(m_vfs, m_sFilePath)) {
197                 aUseableReasons.add( reason.getDisplayName() );
198             }
199         }
200         String JavaDoc[] aReasons = new String JavaDoc[aUseableReasons.size()];
201         for(int i=0; i<aUseableReasons.size(); i++) {
202             aReasons[i] = (String JavaDoc) aUseableReasons.get(i);
203         }
204
205         String JavaDoc sSelectedReason = "";
206         if(this.m_reasonComboBox==null) {
207             if(this.m_reason==null && aReasons.length==2) {
208                 sSelectedReason = aReasons[1];
209                 
210             } else if(this.m_reason!=null) {
211                 sSelectedReason = this.m_reason.getDisplayName();
212             }
213             this.m_reasonComboBox = this.getCombo(sSelectedReason, aReasons);
214         } else {
215             this.remove(this.m_reasonComboBox);
216             sSelectedReason = (String JavaDoc) this.m_reasonComboBox.getSelectedItem();
217             if(this.m_reason==null && sSelectedReason.equals("") && aReasons.length==2) {
218                 sSelectedReason = aReasons[1];
219             } else if(this.m_reason!=null) {
220                 sSelectedReason = this.m_reason.getDisplayName();
221             }
222             this.m_reasonComboBox = this.getCombo(sSelectedReason, aReasons);
223         }
224         this.setReason(this.getReasonByName(sSelectedReason));
225         this.add(m_reasonComboBox);
226         this.checkValid();
227         this.revalidate();
228     }
229
230     /* (non-Javadoc)
231      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
232      */

233     public void layoutContainer(Container arg0) {
234         this.m_resourceLabel.setSize(m_nWidth-170, 20);
235         this.m_resourceLabel.setLocation(0,0);
236         this.m_errorLabel.setSize(20,20);
237         this.m_errorLabel.setLocation(m_nWidth-170, 0);
238         this.m_reasonComboBox.setSize(148, 18);
239         this.m_reasonComboBox.setLocation(m_nWidth-149, 1);
240     }
241     
242     private JComboBox getCombo(String JavaDoc sSelectedItem, String JavaDoc[] aReasons) {
243         JComboBox combo = new JComboBox(aReasons);
244         combo.setSelectedItem(sSelectedItem);
245         combo.addActionListener(this);
246         combo.setActionCommand("COMBO");
247         return combo;
248     }
249
250     /* (non-Javadoc)
251      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
252      */

253     public void mouseClicked(MouseEvent me) {
254         if(me.getSource()==this.m_resourceLabel || me.getSource()==this.m_errorLabel) {
255             this.m_list.cellSelected(this);
256         }
257     }
258
259     /* (non-Javadoc)
260      * @see java.awt.Component#getPreferredSize()
261      */

262     public Dimension getPreferredSize() {
263         return new Dimension(m_nWidth, 20);
264     }
265
266     /* (non-Javadoc)
267      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
268      */

269     public Dimension minimumLayoutSize(Container arg0) {
270         return this.getPreferredSize();
271     }
272
273     /* (non-Javadoc)
274      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
275      */

276     public Dimension preferredLayoutSize(Container arg0) {
277         return this.getPreferredSize();
278     }
279
280     /**
281      *
282      */

283     public ResourceSelectionCell() {
284         super();
285     }
286
287     /**
288      * @param arg0
289      */

290     private ResourceSelectionCell(boolean arg0) {
291         super(arg0);
292     }
293
294     /**
295      * @param arg0
296      */

297     private ResourceSelectionCell(LayoutManager arg0) {
298         super(arg0);
299     }
300
301     /**
302      * @param arg0
303      * @param arg1
304      */

305     private ResourceSelectionCell(LayoutManager arg0, boolean arg1) {
306         super(arg0, arg1);
307     }
308
309     /* (non-Javadoc)
310      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
311      */

312     public void removeLayoutComponent(Component arg0) {
313     }
314
315     /* (non-Javadoc)
316      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
317      */

318     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
319     }
320
321     /* (non-Javadoc)
322      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
323      */

324     public void mouseEntered(MouseEvent arg0) {
325     }
326
327     /* (non-Javadoc)
328      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
329      */

330     public void mouseExited(MouseEvent arg0) {
331     }
332
333     /* (non-Javadoc)
334      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
335      */

336     public void mousePressed(MouseEvent arg0) {
337     }
338
339     /* (non-Javadoc)
340      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
341      */

342     public void mouseReleased(MouseEvent arg0) {
343     }
344
345 }
346
Popular Tags