KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBFileChooserButton


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import javax.swing.text.JTextComponent JavaDoc;
5 import java.awt.*;
6 import java.awt.event.ActionEvent JavaDoc;
7 import java.awt.event.ActionListener JavaDoc;
8 import java.io.File JavaDoc;
9
10 /**
11  * A small utility file picker button. It works in conjunction with
12  * a JTextComponent. It brings up a 'file' button that the user can
13  * use to select a file, the name + path of which is then set in the
14  * associated text component.
15  *
16  * @see javax.swing.JTextComponent
17  */

18
19 public class CBFileChooserButton extends CBButton implements ActionListener JavaDoc
20 {
21     static String JavaDoc globalLastDirectory = ""; // the last directory accessed
22
String JavaDoc localLastDirectory = ""; // possibly the user wants just this button to remember the last directory used.
23
boolean useLocal = false; // which 'last directory' to use.
24

25     JTextComponent JavaDoc myText;
26     Component JavaDoc parent;
27
28     /**
29      * Constructor takes the JTextComponent it is to be
30      * associated with. The constructor creates a named button
31      * (usually 'File'), and tries to open an image icon
32      * using the file 'open.gif'. (If it's not there, the
33      * button is unadorned.) These defaults can obviously
34      * be reset using setText() and setIcon().
35      *
36      * @param text the JTextComponent to be filled out with
37      * the selected file name after selection.
38      * @param GUIparent the GUI parent to use for the file Chooser
39      * dialog.
40      */

41
42     public CBFileChooserButton(JTextComponent JavaDoc text, Component JavaDoc GUIparent)
43     {
44         this(text, GUIparent, "File");
45     }
46
47     /**
48      * Constructor takes the JTextComponent it is to be
49      * associated with. The constructor creates a named button
50      * (usually 'File'), and tries to open an image icon
51      * using the file 'open.gif'. (If it's not there, the
52      * button is unadorned.) These defaults can obviously
53      * be reset using setText() and setIcon().
54      *
55      * @param text the JTextComponent to be filled out with
56      * the selected file name after selection.
57      * @param GUIparent the GUI parent to use for the file Chooser
58      * dialog.
59      * @param buttonName the name of the button the user pressed to launch
60      * the file chooser.
61      */

62
63     public CBFileChooserButton(JTextComponent JavaDoc text, Component JavaDoc GUIparent, String JavaDoc buttonName)
64     {
65         this(text, GUIparent, buttonName, "");
66     }
67
68     /**
69      * Constructor takes the JTextComponent it is to be
70      * associated with. The constructor creates a named button
71      * (usually 'File'), and tries to open an image icon
72      * using the file 'open.gif'. (If it's not there, the
73      * button is unadorned.) These defaults can obviously
74      * be reset using setText() and setIcon().
75      *
76      * @param text the JTextComponent to be filled out with
77      * the selected file name after selection.
78      * @param GUIparent the GUI parent to use for the file Chooser
79      * dialog.
80      * @param buttonName the name of the button the user pressed to launch
81      * the file chooser.
82      * @param tooltip the tooltip to be added to the button.
83      */

84
85     public CBFileChooserButton(JTextComponent JavaDoc text, Component JavaDoc GUIparent, String JavaDoc buttonName, String JavaDoc tooltip)
86     {
87         super(buttonName, tooltip, new ImageIcon("open.gif"));
88         myText = text;
89         parent = GUIparent;
90         addActionListener(this);
91     }
92
93
94     public void setLocalDirectoryUse(boolean state)
95     {
96         useLocal = state;
97     }
98
99     public boolean getLocalDirectoryUse()
100     {
101         return useLocal;
102     }
103
104     public static void setGlobalDirectory(String JavaDoc dirString)
105     {
106         globalLastDirectory = dirString;
107     }
108
109     public void setStartingDirectory(String JavaDoc dirString)
110     {
111         if (useLocal)
112             localLastDirectory = dirString;
113         else
114             setGlobalDirectory(dirString);
115     }
116
117     public String JavaDoc getStartingDirectory()
118     {
119         return (useLocal ? localLastDirectory : globalLastDirectory);
120     }
121
122     public void actionPerformed(ActionEvent JavaDoc e)
123     {
124         String JavaDoc lastDirectory = (useLocal) ? localLastDirectory : globalLastDirectory;
125
126         /*
127          * Check that the directory exists - if it doesn't the Sun JFileChooser (circa java 1.4)
128          * can throw an uncaught exception and the GUI fails...
129          */

130
131         try
132         {
133             if (new File JavaDoc(lastDirectory).exists() == false)
134             {
135                 lastDirectory = null;
136             }
137         }
138         catch (Exception JavaDoc ex) // something wierd. null is nice and safe (unusually)
139
{
140             lastDirectory = null;
141         }
142
143         JFileChooser chooser = new JFileChooser(lastDirectory);
144         int option = chooser.showOpenDialog(parent);
145
146         if (option == JFileChooser.APPROVE_OPTION) // only do something if user chose 'ok'
147
{
148             setStartingDirectory(chooser.getSelectedFile().getParent());
149             File JavaDoc readFile = chooser.getSelectedFile();
150             myText.setText(readFile.toString());
151         }
152     }
153 }
Popular Tags