KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > report > swing > JComboTree


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.editors.report.swing;
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 import javax.swing.event.*;
28 import javax.swing.tree.*;
29
30 import org.openharmonise.him.*;
31 import org.openharmonise.him.swing.resourcetree.*;
32 import org.openharmonise.him.swing.resourcetree.formresourcetree.*;
33 import org.openharmonise.swing.FontManager;
34 import org.openharmonise.vfs.*;
35 import org.openharmonise.vfs.gui.*;
36 import org.openharmonise.vfs.servers.ServerList;
37
38
39 /**
40  * A component, much like the {@link javax.swing.JComboBox}, which displays
41  * a field, button and drop down. However in this case the drop down
42  * contains a resoruce tree from which the value(s) are selected.
43  *
44  * @author Matthew Large
45  * @version $Revision: 1.2 $
46  *
47  */

48 public class JComboTree
49     extends JPanel
50     implements ActionListener, LayoutManager,
51                 FormResourceTreeListener, MouseListener,
52                 WindowFocusListener, FocusListener {
53
54     /**
55      * Tree selection mode.
56      *
57      * @see TreeSelectionModel
58      */

59     private int m_nSelectionMode = -1;
60     
61     /**
62      * Label do display value.
63      */

64     protected JLabel m_value = null;
65     
66     /**
67      * Open drop down button.
68      */

69     private JToggleButton m_button = null;
70     
71     /**
72      * Screen X position.
73      */

74     private int m_nScreenPosX = 0;
75     
76     /**
77      * Screen Y position.
78      */

79     private int m_nScreenPosY = 0;
80     
81     /**
82      * Window for drop down.
83      */

84     protected JWindow m_nWindow = null;
85     
86     /**
87      * List of paths which are the roots for the tree.
88      */

89     private ArrayList m_aCollectionPaths = new ArrayList();
90     
91     /**
92      * Action command for component.
93      */

94     private String JavaDoc m_sActionCommand = null;
95     
96     /**
97      * List of {@link ActionListener} objects.
98      */

99     private ArrayList m_aActionListeners = new ArrayList();
100     
101     /**
102      * true if leaf nodes are to be displayed.
103      */

104     private boolean m_bShowLeafNodes = true;
105     
106     /**
107      * true if only leaf nodes can be selected.
108      */

109     private boolean m_bSelectedLeafOnly = false;
110     
111     private boolean m_bShowApprovedOnly = false;
112     
113     /**
114      * Selected highlight colour.
115      */

116     private Color m_selectedColor = new Color(173,169,143);
117     
118     /**
119      * List of path values.
120      */

121     private List JavaDoc m_paths = null;
122     
123     /**
124      * true if the user should be able to clear the value(s).
125      */

126     private boolean m_bAllowClear = false;
127     
128     /**
129      * true if the tree is populated.
130      */

131     private boolean m_bIsTreePopulated = false;
132
133     /**
134      * Resource tree.
135      */

136     private FormResourceTree m_tree;
137     
138     private AbstractResourceFilter m_filter = new BasicResourceFilter();
139     
140     private int m_nWidth = 150;
141     private int m_nHeight = 20;
142     
143     private boolean m_bHideReentrantStop = false;
144     
145     /**
146      * Constructs a new combo tree component.
147      *
148      * @param sPath Path value
149      */

150     public JComboTree(String JavaDoc sPath) {
151         super();
152         m_paths = new ArrayList();
153         
154         if(sPath != null) {
155             m_paths.add(sPath);
156         }
157         this.setup();
158     }
159
160     /**
161      *
162      */

163     public JComboTree() {
164         super();
165         this.setup();
166     }
167     
168     /**
169      * Configures this component.
170      *
171      */

172     protected void setup() {
173         this.setLayout(this);
174         this.setBackground(Color.WHITE);
175         
176         this.m_value = new JLabel();
177         this.m_value.setBackground(Color.WHITE);
178         this.m_value.setBorder( BorderFactory.createEtchedBorder());
179         this.m_value.setFont( FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE) );
180         this.add(m_value);
181         
182         this.m_button = new JToggleButton("V");
183         this.m_button.addActionListener(this);
184         this.add(this.m_button);
185     }
186     
187     /**
188      * Returns the value label.
189      *
190      * @return Value label
191      */

192     public JLabel getLabel() {
193         return m_value;
194     }
195     
196     /**
197      * Returns the path value. If there is more than one then the first
198      * is returned.
199      *
200      * @return Path value, or null if there isn't one
201      */

202     public String JavaDoc getPath() {
203         String JavaDoc sPath = null;
204         
205         if(m_paths != null && m_paths.size() > 0) {
206             sPath = (String JavaDoc) m_paths.get(0);
207         }
208         
209         return sPath;
210     }
211     
212     /**
213      * Returns a list of path values.
214      *
215      * @return List of path values
216      */

217     public List JavaDoc getPaths() {
218         return new ArrayList(m_paths);
219     }
220
221
222     /* (non-Javadoc)
223      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
224      */

225     public void actionPerformed(ActionEvent arg0) {
226         if(this.m_nWindow==null) {
227             Component comp = this.getParent();
228             while(comp!=null && !(comp instanceof JDialog || comp instanceof JFrame)) {
229                 comp = comp.getParent();
230             }
231             this.m_nWindow = new JWindow(((Window)comp));
232             m_nWindow.addWindowFocusListener(this);
233             
234             JPanel panel = new JPanel();
235             panel.addFocusListener(this);
236             panel.setBorder( BorderFactory.createLineBorder(Color.BLACK));
237             panel.setBackground(Color.WHITE);
238             panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
239
240
241             String JavaDoc fontName = "Dialog";
242             int fontSize = 11;
243             Font font = new Font(fontName, Font.PLAIN, fontSize);
244             
245             if(this.m_bAllowClear) {
246                 JTextField clearLabel = new JTextField("Clear value");
247                 clearLabel.setEditable(false);
248                 clearLabel.setBorder( BorderFactory.createLineBorder(Color.BLACK));
249                 clearLabel.setFont(font);
250                 clearLabel.addMouseListener(this);
251                 panel.add(clearLabel);
252                 clearLabel.setPreferredSize(new Dimension(220,15));
253                 clearLabel.setSize(new Dimension(220,15));
254             }
255         
256             List JavaDoc aPaths = new ArrayList();
257             if(m_paths != null && m_paths.size() > 0) {
258                 aPaths = new ArrayList(m_paths);
259             }
260             m_tree = new FormResourceTree(aPaths);
261             m_tree.setResourceFilter(getResourceTreeFilter());
262             m_tree.addFocusListener(this);
263             if(m_nSelectionMode >= 0) {
264                 m_tree.setSelectionMode(m_nSelectionMode);
265                 if(m_nSelectionMode>1) {
266                     m_tree.setIsMultiSelect(true);
267                 } else {
268                     m_tree.setIsMultiSelect(false);
269                 }
270             } else {
271                 m_tree.setIsMultiSelect(false);
272             }
273             m_tree.setBorder( BorderFactory.createLineBorder(Color.BLACK));
274             m_tree.setShowLeafNodes(m_bShowLeafNodes);
275             m_tree.addFormResourceTreeListener(this);
276             if(m_bShowApprovedOnly){
277                 m_tree.setShowApprovedOnly(true);
278             }
279             AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
280             Iterator itor = this.m_aCollectionPaths.iterator();
281             while (itor.hasNext()) {
282                 String JavaDoc sColPath = (String JavaDoc) itor.next();
283                 VirtualFile vfDir = vfs.getVirtualFile(sColPath).getResource();
284                 if(vfDir!=null) {
285                     m_tree.addCollection(vfDir);
286                 }
287             }
288             
289             this.m_nWindow.getContentPane().setLayout(new BorderLayout());
290             panel.add(m_tree);
291             this.m_nWindow.getContentPane().add(panel);
292             m_bIsTreePopulated = true;
293             
294             addAncestorListener(new AncestorListener(){
295                 public void ancestorAdded(AncestorEvent event){
296                     if (event.getSource()!=this && event.getSource()!=getButton())
297                         hidePopup();
298                 }
299                 public void ancestorRemoved(AncestorEvent event){
300                     if (event.getSource()!=this && event.getSource()!=getButton())
301                         hidePopup();
302                 }
303                 public void ancestorMoved(AncestorEvent event){
304                     if (event.getSource()!=this && event.getSource()!=getButton())
305                         hidePopup();
306                 }});
307             
308         }
309         if(!this.m_bHideReentrantStop && this.m_button.isSelected() && !this.m_nWindow.isVisible()) {
310             this.m_nWindow.setSize(200, 300);
311             this.m_nScreenPosX = this.getLocationOnScreen().x;
312             this.m_nScreenPosY = this.getLocationOnScreen().y;
313             this.m_nWindow.setLocation(this.m_nScreenPosX, this.m_nScreenPosY+20);
314             this.m_nWindow.toFront();
315             this.m_nWindow.setVisible(true);
316
317         } else {
318             this.m_nWindow.setVisible(false);
319             this.m_button.setSelected(false);
320             this.revalidate();
321             this.repaint();
322         }
323         this.m_bHideReentrantStop = false;
324     }
325     
326     protected JToggleButton getButton() {
327         return this.m_button;
328     }
329     
330     /**
331      *
332      */

333     protected void hidePopup() {
334         if(this.m_nWindow!=null) {
335             this.m_bHideReentrantStop = true;
336             this.m_nWindow.setVisible(false);
337             this.m_button.setSelected(false);
338             this.invalidate();
339         }
340     }
341
342     public void setShowApprovedOnly(boolean show){
343         m_bShowApprovedOnly = show;
344     }
345     
346     /**
347      * Sets whether or not to show leaf nodes.
348      *
349      * @param bShowLeafNodes true if leaf nodes are to be shown.
350      */

351     public void setShowLeafNodes(boolean bShowLeafNodes) {
352         this.m_bShowLeafNodes = bShowLeafNodes;
353     }
354     
355     /**
356      * Sets whether only leaf nodes should be selectable.
357      *
358      * @param bSelectLeafOnly true to set only leaf nodes as selectable
359      */

360     public void setSelectedLeafOnly(boolean bSelectLeafOnly) {
361         this.m_bSelectedLeafOnly = bSelectLeafOnly;
362     }
363     
364     /**
365      * Sets whether or not the user should be able to clear the
366      * value(s).
367      *
368      * @param bAllowClear true to allow the user to clear the value(s)
369      */

370     public void setAllowClear(boolean bAllowClear) {
371     }
372
373     /* (non-Javadoc)
374      * @see java.awt.Component#paint(java.awt.Graphics)
375      */

376     public void paint(Graphics arg0) {
377         super.paint(arg0);
378         if(this.m_nWindow!=null && this.m_nWindow.isVisible() && (this.m_nScreenPosX!=this.getLocationOnScreen().x || this.m_nScreenPosY!=this.getLocationOnScreen().y) ) {
379             this.m_nScreenPosX = this.getLocationOnScreen().x;
380             this.m_nScreenPosY = this.getLocationOnScreen().y;
381             this.m_nWindow.setLocation(this.m_nScreenPosX, this.m_nScreenPosY+20);
382             this.m_nWindow.toFront();
383             this.m_nWindow.repaint();
384         }
385     }
386
387     /* (non-Javadoc)
388      * @see java.awt.Component#doLayout()
389      */

390     public void doLayout() {
391         super.doLayout();
392         if(this.m_nWindow!=null && this.m_nWindow.isVisible() && (this.m_nScreenPosX!=this.getLocationOnScreen().x || this.m_nScreenPosY!=this.getLocationOnScreen().y) ) {
393             this.m_nScreenPosX = this.getLocationOnScreen().x;
394             this.m_nScreenPosY = this.getLocationOnScreen().y;
395             this.m_nWindow.setLocation(this.m_nScreenPosX, this.m_nScreenPosY+20);
396             this.m_nWindow.toFront();
397             this.m_nWindow.repaint();
398         }
399     }
400
401     /* (non-Javadoc)
402      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
403      */

404     public void layoutContainer(Container arg0) {
405         this.m_value.setSize(m_nWidth-20, m_nHeight);
406         this.m_value.setLocation(0, 0);
407         
408         this.m_button.setSize(20, m_nHeight);
409         this.m_button.setLocation(m_nWidth-20, 0);
410     }
411     
412     /* (non-Javadoc)
413      * @see java.awt.Component#setSize(java.awt.Dimension)
414      */

415     public void setSize(Dimension dim) {
416         this.setSize(dim.width, dim.height);
417     }
418     
419     /* (non-Javadoc)
420      * @see java.awt.Component#setSize(int, int)
421      */

422     public void setSize(int nWidth, int nHeight) {
423         super.setSize(nWidth, nHeight);
424         this.m_nWidth = nWidth;
425         this.m_nHeight = nHeight;
426     }
427     
428     
429     
430     /* (non-Javadoc)
431      * @see java.awt.Component#getSize()
432      */

433     public Dimension getSize() {
434         return new Dimension(this.m_nWidth, this.m_nHeight);
435     }
436     /**
437      * Adds a list of root collection paths for the tree.
438      *
439      * @param paths List of full paths
440      */

441     public void addCollectionPaths(List JavaDoc paths) {
442         this.m_aCollectionPaths.addAll(paths);
443     }
444     
445     /**
446      * Adds a root colllection path for the tree.
447      *
448      * @param sPath Full path
449      */

450     public void addCollectionPath(String JavaDoc sPath) {
451         this.m_aCollectionPaths.add(sPath);
452     }
453     
454     /**
455      * Removes a root collection path from the tree.
456      *
457      * @param sPath Full path
458      */

459     public void removeCollectionPath(String JavaDoc sPath) {
460         this.m_aCollectionPaths.remove(sPath);
461     }
462     
463     /**
464      * Returns a list of the root collection paths for the tree.
465      *
466      * @return List of full paths
467      */

468     public List JavaDoc getCollectionPaths() {
469         return (List JavaDoc) this.m_aCollectionPaths.clone();
470     }
471
472     /* (non-Javadoc)
473      * @see java.awt.Component#getPreferredSize()
474      */

475     public Dimension getPreferredSize() {
476         return new Dimension(170, 20);
477     }
478
479     /**
480      * @param arg0
481      */

482     private JComboTree(boolean arg0) {
483         super(arg0);
484     }
485
486     /**
487      * @param arg0
488      */

489     private JComboTree(LayoutManager arg0) {
490         super(arg0);
491     }
492
493     /**
494      * @param arg0
495      * @param arg1
496      */

497     private JComboTree(LayoutManager arg0, boolean arg1) {
498         super(arg0, arg1);
499     }
500
501     /* (non-Javadoc)
502      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
503      */

504     public void removeLayoutComponent(Component arg0) {
505     }
506
507     /* (non-Javadoc)
508      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
509      */

510     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
511     }
512
513     /* (non-Javadoc)
514      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
515      */

516     public Dimension minimumLayoutSize(Container arg0) {
517         return this.getPreferredSize();
518     }
519
520     /* (non-Javadoc)
521      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
522      */

523     public Dimension preferredLayoutSize(Container arg0) {
524         return this.getPreferredSize();
525     }
526
527     /**
528      * Adds an action listener.
529      *
530      * @param listener Listener to add
531      */

532     public void addActionListener(ActionListener listener) {
533         this.m_aActionListeners.add(listener);
534     }
535     
536     /**
537      * Removes an action listener.
538      *
539      * @param listener Listener to remove
540      */

541     public void removeActionListener(ActionListener listener) {
542         this.m_aActionListeners.remove(listener);
543     }
544     
545     /**
546      * Fires an action event to all the action listeners.
547      *
548      */

549     private void fireActionEvent() {
550         ActionEvent ae = new ActionEvent(this, 0, this.m_sActionCommand);
551         Iterator itor = this.m_aActionListeners.iterator();
552         while (itor.hasNext()) {
553             ActionListener listener = (ActionListener) itor.next();
554             listener.actionPerformed(ae);
555         }
556     }
557     
558     /**
559      * Sets the action command for this component.
560      *
561      * @param sActionCommand Action command
562      */

563     public void setActionCommand(String JavaDoc sActionCommand) {
564         this.m_sActionCommand = sActionCommand;
565     }
566     
567     /**
568      * Clears the value(s) and hides the drop down.
569      *
570      */

571     public void clear() {
572         if(this.m_paths!=null) {
573             this.m_paths.clear();
574         }
575         this.m_value.setIcon(IconManager.getInstance().getIcon("16-blank.gif"));
576         this.m_value.setText("");
577         this.revalidate();
578         this.m_nWindow.setVisible(false);
579         this.m_tree.clearSelection();
580     }
581
582     /* (non-Javadoc)
583      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
584      */

585     public void mouseClicked(MouseEvent me) {
586         clear();
587     }
588     
589     /**
590      * Addsa path value.
591      *
592      * @param vfs Virtual file system
593      * @param sPath Full path
594      */

595     public void addPath(AbstractVirtualFileSystem vfs, String JavaDoc sPath) {
596         if(m_paths == null) {
597             m_paths = new ArrayList();
598         }
599         
600         m_paths.add(sPath);
601     
602         setPaths(vfs,m_paths);
603     }
604     
605     /**
606      * Removes a path value.
607      *
608      * @param vfs Virtual file system
609      * @param sPath Full path
610      */

611     public void removePath(AbstractVirtualFileSystem vfs, String JavaDoc sPath) {
612         if(m_paths != null) {
613             if(m_paths.contains(sPath)) {
614                 m_paths.remove(sPath);
615                 setPaths(vfs, m_paths);
616             }
617         }
618     }
619     
620     /**
621      * Sets the path value.
622      *
623      * @param vfs Virtual file system
624      * @param sPath full path
625      */

626     public void setPath(AbstractVirtualFileSystem vfs, String JavaDoc sPath) {
627         ArrayList list = new ArrayList();
628         list.add(sPath);
629         
630         setPaths(vfs,list);
631     }
632     
633     /**
634      * Sets a list of paths.
635      *
636      * @param vfs Virtual file system
637      * @param paths List of full paths
638      */

639     public void setPaths(AbstractVirtualFileSystem vfs, List JavaDoc paths) {
640         VirtualFileSystemView vfsView = vfs.getVirtualFileSystemView();
641         Iterator iter = paths.iterator();
642         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
643         Icon icon = null;
644         m_paths = new ArrayList();
645         while (iter.hasNext()) {
646             String JavaDoc sPath = (String JavaDoc) iter.next();
647             
648             VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
649             if(m_value != null
650                     && (!this.m_bSelectedLeafOnly
651                         || (this.m_bSelectedLeafOnly
652                                 && !vfFile.isDirectory()) )) {
653                 m_paths.add(sPath);
654
655                 icon = vfsView.getIcon(vfFile);
656                 if(sbuf.length() > 0) {
657                     sbuf.append(";");
658                 }
659                 sbuf.append(vfsView.getDisplayName(vfFile));
660             }
661         }
662         this.m_value.setIcon(icon);
663         this.m_value.setText( sbuf.toString() );
664     }
665
666     /* (non-Javadoc)
667      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
668      */

669     public void mouseEntered(MouseEvent arg0) {
670         
671     }
672
673     /* (non-Javadoc)
674      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
675      */

676     public void mouseExited(MouseEvent arg0) {
677         
678     }
679
680     /* (non-Javadoc)
681      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
682      */

683     public void mousePressed(MouseEvent arg0) {
684         
685     }
686
687     /* (non-Javadoc)
688      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
689      */

690     public void mouseReleased(MouseEvent arg0) {
691         
692     }
693     
694     /**
695      * Sets the tree selection mode.
696      *
697      * @param nMode Tree selection mode
698      */

699     public void setSelectionMode(int nMode) {
700         m_nSelectionMode = nMode;
701     }
702
703     /* (non-Javadoc)
704      * @see com.simulacramedia.contentmanager.swing.resourcetree.formresourcetree.FormResourceTreeListener#pathValuesChanged(java.util.List)
705      */

706     public void pathValuesChanged(List JavaDoc pathValues) {
707         if(m_bIsTreePopulated == true) {
708             
709             setPaths(ServerList.getInstance().getHarmoniseServer().getVFS(), m_tree.getPathValues());
710             
711             this.revalidate();
712             if(this.m_nWindow!=null
713                     && (m_nSelectionMode == -1 || m_nSelectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION)) {
714                 this.m_nWindow.setVisible(false);
715             }
716             this.fireActionEvent();
717         }
718     }
719
720     /* (non-Javadoc)
721      * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
722      */

723     public void focusGained(FocusEvent arg0) {
724     }
725
726     /* (non-Javadoc)
727      * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
728      */

729     public void focusLost(FocusEvent arg0) {
730         this.hidePopup();
731     }
732
733     /* (non-Javadoc)
734      * @see java.awt.event.WindowFocusListener#windowGainedFocus(java.awt.event.WindowEvent)
735      */

736     public void windowGainedFocus(WindowEvent arg0) {
737     }
738
739     /* (non-Javadoc)
740      * @see java.awt.event.WindowFocusListener#windowLostFocus(java.awt.event.WindowEvent)
741      */

742     public void windowLostFocus(WindowEvent arg0) {
743         this.hidePopup();
744     }
745     public void setResourceTreeFilter(AbstractResourceFilter filter) {
746         m_filter = filter;
747     }
748     protected AbstractResourceFilter getResourceTreeFilter() {
749         return m_filter;
750     }
751     public void setScopeEnabled(boolean isEnabled){
752         this.m_value.setEnabled(isEnabled);
753         this.m_button.setEnabled(isEnabled);
754     }
755 }
756
Popular Tags