1 50 51 package net.sf.just4log.integration.gui; 52 53 import java.awt.BorderLayout ; 54 import java.awt.GridBagConstraints ; 55 import java.awt.GridBagLayout ; 56 import java.awt.event.ActionEvent ; 57 import java.awt.event.ActionListener ; 58 import java.awt.event.WindowAdapter ; 59 import java.awt.event.WindowEvent ; 60 import java.io.File ; 61 62 import javax.swing.JButton ; 63 import javax.swing.JComponent ; 64 import javax.swing.JFileChooser ; 65 import javax.swing.JFrame ; 66 import javax.swing.JLabel ; 67 import javax.swing.JPanel ; 68 import javax.swing.JTextField ; 69 70 import org.apache.commons.logging.Log; 71 import org.apache.commons.logging.LogFactory; 72 73 77 78 public class Just4LogGui { 79 public static final String CLASSID = 80 "$Id: Just4LogGui.java,v 1.2 2003/07/20 18:02:39 lbruand Exp $"; 81 private static Log logger = LogFactory.getLog(Just4LogGui.class); 82 private static JFrame frame; 83 JTextField source; 84 JTextField target; 85 JButton srcbutton; 86 JButton targetbutton; 87 JButton runbutton; 88 JLabel infolabel; 89 JFileChooser sourcefc; 90 JFileChooser targetfc; 91 94 public Just4LogGui() { 95 super(); 96 } 97 98 public static void main(String [] args) { 99 logger.info("Building up GUI"); 100 frame = new JFrame ("Just4Log"); 101 Just4LogGui app = new Just4LogGui(); 102 103 JComponent contents = app.createComponents(); 104 frame.getContentPane().add(contents, BorderLayout.CENTER); 105 106 frame.addWindowListener(new WindowAdapter () { 107 public void windowClosing(WindowEvent e) { 108 System.exit(0); 109 } 110 }); 111 112 frame.pack(); 113 logger.info("Starting GUI"); 114 frame.setVisible(true); 115 } 116 117 120 121 private static final int FILEFIELDSIZE = 20; 122 private JComponent createComponents() { 123 sourcefc = new JFileChooser (); 124 sourcefc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 125 targetfc = new JFileChooser (); 126 targetfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 127 128 GridBagConstraints constraint; 129 130 JPanel pane = new JPanel (); 131 pane.setLayout(new GridBagLayout ()); 132 133 JLabel srclbl = new JLabel ("Source file/dir:"); 134 constraint = new GridBagConstraints (); 135 constraint.gridx = 0; 136 constraint.gridy = 0; 137 pane.add(srclbl, constraint); 138 139 source = new JTextField (FILEFIELDSIZE); 140 constraint = new GridBagConstraints (); 141 constraint.gridx = 1; 142 constraint.gridy = 0; 143 pane.add(source, constraint); 144 145 srcbutton = new JButton ("Choose"); 146 constraint = new GridBagConstraints (); 147 constraint.gridx = 2; 148 constraint.gridy = 0; 149 pane.add(srcbutton, constraint); 150 151 srcbutton.addActionListener(new ActionListener () { 152 public void actionPerformed(ActionEvent evt) { 153 srcbuttonActionPerformed(evt); 154 } 155 }); 156 157 JLabel targetlbl = new JLabel ("Target file/dir:"); 158 constraint = new GridBagConstraints (); 159 constraint.gridx = 0; 160 constraint.gridy = 1; 161 pane.add(targetlbl, constraint); 162 163 target = new JTextField (FILEFIELDSIZE); 164 constraint = new GridBagConstraints (); 165 constraint.gridx = 1; 166 constraint.gridy = 1; 167 pane.add(target, constraint); 168 169 targetbutton = new JButton ("Choose"); 170 constraint = new GridBagConstraints (); 171 constraint.gridx = 2; 172 constraint.gridy = 1; 173 pane.add(targetbutton, constraint); 174 targetbutton.addActionListener(new ActionListener () { 175 public void actionPerformed(ActionEvent evt) { 176 targetbuttonActionPerformed(evt); 177 } 178 }); 179 180 infolabel = new JLabel ("text"); 181 constraint.gridx = 0; 182 constraint.gridy = 2; 183 constraint.gridwidth = 2; 184 pane.add(infolabel, constraint); 185 186 runbutton = new JButton ("Run!"); 187 constraint.gridx = 2; 188 constraint.gridy = 2; 189 pane.add(runbutton, constraint); 190 runbutton.addActionListener(new ActionListener () { 191 public void actionPerformed(ActionEvent evt) { 192 runbuttonActionPerformed(evt); 193 } 194 }); 195 return pane; 196 197 } 198 199 202 protected void runbuttonActionPerformed(ActionEvent evt) { 203 204 runbutton.setText("Stop"); 205 runbutton.repaint(); 206 } 207 208 211 private void targetbuttonActionPerformed(ActionEvent evt) { 212 int returnVal = targetfc.showOpenDialog(frame); 213 214 if (returnVal == JFileChooser.APPROVE_OPTION) { 215 File file = targetfc.getSelectedFile(); 216 target.setText(file.toString()); 217 logger.info("Opening: " + file.getName() + "."); 218 } else { 219 logger.warn("Open command cancelled by user."); 220 } 221 222 } 223 224 private void srcbuttonActionPerformed(ActionEvent evt) { 225 int returnVal = sourcefc.showOpenDialog(frame); 226 227 if (returnVal == JFileChooser.APPROVE_OPTION) { 228 File file = sourcefc.getSelectedFile(); 229 source.setText(file.toString()); 230 logger.info("Opening: " + file.getName() + "."); 231 } else { 232 logger.warn("Open command cancelled by user."); 233 } 234 } 235 } 236 | Popular Tags |