KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > richtext > WebAddressDialog


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22 package salsa.richtext;
23
24 import java.io.*;
25 import java.net.*;
26 import java.awt.*;
27 import java.awt.event.*;
28 import javax.swing.*;
29
30 import salsa.util.*;
31
32 import houston.*;
33
34 public class WebAddressDialog extends JDialog
35 {
36    static Logger T = Logger.getLogger( WebAddressDialog.class );
37
38    JLabel _prompt;
39    JTextField _address;
40
41    JButton _browseButton;
42    JButton _okButton;
43    JButton _cancelButton;
44
45    JFrame _owner;
46    JFileChooser _chooser;
47
48
49    public WebAddressDialog( JFrame owner, String JavaDoc title, JFileChooser chooser )
50    {
51       super( owner, title, true );
52
53       _owner = owner;
54       _chooser = chooser;
55
56       _prompt = new JLabel( "Please enter the web address (URL):" );
57
58       _address = new JTextField( "" );
59       _address.setColumns( 30 );
60
61       _browseButton = new JButton( "Choose File..." );
62       _browseButton.setRequestFocusEnabled( false );
63       _browseButton.addActionListener(
64          new ActionListener()
65          {
66             public void actionPerformed( ActionEvent ev )
67             {
68                onBrowse();
69             }
70          } );
71
72       JPanel centerInner = new JPanel();
73       centerInner.setLayout( new BoxLayout( centerInner, BoxLayout.X_AXIS ) );
74       centerInner.add( _address );
75       centerInner.add( _browseButton );
76
77       JPanel center = new JPanel( new FlowLayout( FlowLayout.LEFT ) );
78       center.add( centerInner );
79
80       _okButton = new JButton( "Ok" );
81       _okButton.addActionListener(
82          new ActionListener()
83          {
84             public void actionPerformed( ActionEvent ev )
85             {
86                _result = JOptionPane.OK_OPTION;
87                setVisible( false );
88             }
89          } );
90
91       _cancelButton = new JButton( "Cancel" );
92       _cancelButton.addActionListener(
93          new ActionListener()
94          {
95             public void actionPerformed( ActionEvent ev )
96             {
97                _result = JOptionPane.CANCEL_OPTION;
98                setVisible( false );
99             }
100          } );
101
102       JPanel bottomInner = new JPanel( new GridLayout( 1, 2 ) );
103       // rows, cols
104
bottomInner.add( _okButton );
105       bottomInner.add( _cancelButton );
106       JPanel bottom = new JPanel( new FlowLayout( FlowLayout.RIGHT ) );
107       bottom.add( bottomInner );
108
109       getContentPane().setLayout( new BorderLayout() );
110       getContentPane().add( _prompt, BorderLayout.NORTH );
111       getContentPane().add( center, BorderLayout.CENTER );
112       getContentPane().add( bottom, BorderLayout.SOUTH );
113
114       pack();
115       setResizable( false );
116    }
117
118
119    int _result;
120
121
122    public int showDialog( String JavaDoc prompt, String JavaDoc address )
123    {
124       setPrompt( prompt );
125       setAddress( address );
126
127       _result = JOptionPane.CANCEL_OPTION;
128
129       WindowUtils.centerOnWindow( _owner, this );
130       show();
131
132       return _result;
133    }
134
135
136    private void onBrowse()
137    {
138       T.debug( "onBrowse()" );
139
140       if( _chooser.showOpenDialog( _owner ) != JFileChooser.APPROVE_OPTION )
141          return;
142
143       File file = _chooser.getSelectedFile();
144
145       try
146       {
147          String JavaDoc fileURL = file.toURL().toString();
148          _address.setText( fileURL );
149          // op.setInitialSelectionValue(str);
150
}
151       catch( MalformedURLException mex )
152       {
153          Status.error( "*** cannot create URL for file: " + mex.toString() );
154       }
155    }
156
157
158    public void setAddress( String JavaDoc value )
159    {
160       _address.setText( value );
161    }
162
163
164    public String JavaDoc getAddress()
165    {
166       return _address.getText();
167    }
168
169
170    public void setPrompt( String JavaDoc value )
171    {
172       _prompt.setText( value );
173    }
174 }
175
Popular Tags