KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > installer > editor > FileEditor


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar.installer.editor;
37
38 import java.awt.GridBagConstraints JavaDoc;
39 import java.awt.GridBagLayout JavaDoc;
40 import java.awt.Insets JavaDoc;
41 import java.awt.event.*;
42 import java.io.File JavaDoc;
43
44 import javax.swing.*;
45
46 import org.ungoverned.oscar.installer.Property;
47 import org.ungoverned.oscar.installer.StringProperty;
48
49 public class FileEditor extends JPanel
50 {
51     private StringProperty m_prop = null;
52     private JTextField m_textField = null;
53     private JButton m_browseButton = null;
54     private boolean m_isDirectory = false;
55
56     public FileEditor(StringProperty prop, boolean isDirectory)
57     {
58         super();
59         m_prop = prop;
60         m_isDirectory = isDirectory;
61         init();
62     }
63
64     public Property getProperty()
65     {
66         return m_prop;
67     }
68
69     public void setEnabled(boolean b)
70     {
71         m_textField.setEnabled(b);
72         m_browseButton.setEnabled(b);
73     }
74
75     protected void init()
76     {
77         // Set layout.
78
GridBagLayout JavaDoc grid = new GridBagLayout JavaDoc();
79         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
80         gbc.insets = new Insets JavaDoc(0, 2, 0, 2);
81         setLayout(grid);
82
83         // Add field.
84
gbc.gridx = 0;
85         gbc.gridy = 0;
86         gbc.gridheight = 1;
87         gbc.gridwidth = 2;
88         gbc.anchor = GridBagConstraints.WEST;
89         m_textField = new JTextField(30);
90         m_textField.setText(m_prop.getStringValue());
91         grid.setConstraints(m_textField, gbc);
92         add(m_textField);
93
94         // Add button.
95
gbc.gridx = 2;
96         gbc.gridy = 0;
97         gbc.gridheight = 1;
98         gbc.gridwidth = 1;
99         gbc.anchor = GridBagConstraints.EAST;
100         m_browseButton = new JButton("Browse...");
101         m_browseButton.setMargin(new Insets JavaDoc(1, 1, 1, 1));
102         grid.setConstraints(m_browseButton, gbc);
103         add(m_browseButton);
104
105         // Add focus listener.
106
m_textField.addFocusListener(new FocusListener() {
107             public void focusGained(FocusEvent event)
108             {
109             }
110             public void focusLost(FocusEvent event)
111             {
112                 if (!event.isTemporary())
113                 {
114                     // Set the new value.
115
m_prop.setStringValue(normalizeValue(m_textField.getText()));
116
117                 }
118             }
119         });
120
121         // Add action listener.
122
m_browseButton.addActionListener(new ActionListener() {
123             public void actionPerformed(ActionEvent event)
124             {
125                 JFileChooser fileDlg = new JFileChooser();
126                 if (m_isDirectory)
127                 {
128                     fileDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
129                     fileDlg.setDialogTitle("Please select a directory...");
130                 }
131                 else
132                 {
133                     fileDlg.setFileSelectionMode(JFileChooser.FILES_ONLY);
134                     fileDlg.setDialogTitle("Please select a file...");
135                 }
136                 fileDlg.setApproveButtonText("Select");
137                 if (fileDlg.showOpenDialog(FileEditor.this) ==
138                     JFileChooser.APPROVE_OPTION)
139                 {
140                     m_textField.setText(fileDlg.getSelectedFile().getAbsolutePath());
141                     m_prop.setStringValue(normalizeValue(m_textField.getText()));
142                 }
143             }
144         });
145     }
146
147     private String JavaDoc normalizeValue(String JavaDoc value)
148     {
149         // Make sure that directories never end with a slash,
150
// for consistency.
151
if (m_isDirectory)
152         {
153             if (value.endsWith(File.separator))
154             {
155                 value = value.substring(0, value.length() - 1);
156             }
157         }
158         return value;
159     }
160 }
Popular Tags