1 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 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 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 prompt, String 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 fileURL = file.toURL().toString(); 148 _address.setText( fileURL ); 149 } 151 catch( MalformedURLException mex ) 152 { 153 Status.error( "*** cannot create URL for file: " + mex.toString() ); 154 } 155 } 156 157 158 public void setAddress( String value ) 159 { 160 _address.setText( value ); 161 } 162 163 164 public String getAddress() 165 { 166 return _address.getText(); 167 } 168 169 170 public void setPrompt( String value ) 171 { 172 _prompt.setText( value ); 173 } 174 } 175 | Popular Tags |