1 package net.sourceforge.importscrubber; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.io.IOException ; 6 import java.util.*; 7 import javax.swing.*; 8 import javax.swing.event.DocumentEvent ; 9 import javax.swing.event.DocumentListener ; 10 import javax.swing.text.Document ; 11 12 public class ImportScrubberGUI implements IProgressMonitor, ActionListener 13 { 14 15 private JTextField sourceFilesField, classFilesField; 16 private JFrame mainFrame; 17 private JCheckBox recurseCheckbox; 18 private JCheckBox classFromSourceCheckbox; 19 private JCheckBox sortHighCheckbox; 20 private JList fileList = new JList(new UniqueListModel()); 21 private ImportScrubber scrubber; 22 private JComboBox formats; 23 private ImportScrubberMenu menu; 24 private Settings settings; 25 private JButton goButton; 26 private JButton addFilesButton, clearFilesButton; 27 JTextField thresholdField = new JTextField(2); 28 JCheckBox thresholdCheckbox; 29 30 public ImportScrubberGUI() 31 { 32 settings = new Settings("importscrubber"); 33 34 ResourceBundle res = ResourceBundle.getBundle("net.sourceforge.importscrubber.Resources"); 35 36 mainFrame = new JFrame(res.getString(Resources.VERSION_ID)); 37 mainFrame.setLocation(200, 150); 38 mainFrame.setSize(600,520); 39 mainFrame.addWindowListener(new BasicWindowMonitor()); 40 mainFrame.getContentPane().setLayout(new BorderLayout()); 41 42 recurseCheckbox = new JCheckBox(res.getString(Resources.RECURSE_LABEL)); 43 if (settings.get(ImportScrubber.RECURSE)!=null) { 44 recurseCheckbox.setSelected(Boolean.valueOf(settings.get(ImportScrubber.RECURSE)).booleanValue()); 45 } 46 sortHighCheckbox = new JCheckBox(res.getString(Resources.SORT_JAVA_LIBS_LABEL)); 47 if (settings.get(ImportScrubber.SORT_STD_HIGH)!=null) { 48 sortHighCheckbox.setSelected(Boolean.valueOf(settings.get(ImportScrubber.SORT_STD_HIGH)).booleanValue()); 49 } 50 classFromSourceCheckbox = new JCheckBox("Class files are in source directory"); 51 if (settings.get(ImportScrubber.SYNC_CLASS_TO_SOURCE)!=null) { 52 classFromSourceCheckbox.setSelected(Boolean.valueOf(settings.get(ImportScrubber.SYNC_CLASS_TO_SOURCE)).booleanValue()); 53 } 54 classFromSourceCheckbox.addActionListener(new ActionListener() { 55 public void actionPerformed(ActionEvent e) { 56 maybeSyncClassToSource(); 57 } 58 } 59 ); 60 61 sourceFilesField = new JTextField(43); 62 classFilesField = new JTextField(43); 63 BrowseButton sourceBrowseButton = new BrowseButton(sourceFilesField, res.getString(Resources.BROWSE_LABEL)); 64 BrowseButton classBrowseButton = new BrowseButton(classFilesField, res.getString(Resources.BROWSE_LABEL)); 65 if(settings.get(ImportScrubber.START_DIRECTORY_KEY) != null) { 66 sourceFilesField.setText(settings.get(ImportScrubber.START_DIRECTORY_KEY)); 67 } 68 if(settings.get(ImportScrubber.CLASS_DIRECTORY_KEY) != null) { 69 classFilesField.setText(settings.get(ImportScrubber.CLASS_DIRECTORY_KEY)); 70 } 71 sourceFilesField.getDocument().addDocumentListener(new DocumentListener () { 72 private void update() { 73 maybeSyncClassToSource(); 74 } 75 public void removeUpdate(DocumentEvent e) { 76 update(); 77 } 78 public void insertUpdate(DocumentEvent e) { 79 update(); 80 } 81 public void changedUpdate(DocumentEvent e) {} 82 } 83 ); 84 maybeSyncClassToSource(); 85 86 JLabel sourceFilesLabel = new JLabel("Source File/Directory"); 87 JPanel sourceFilesPanel = new JPanel(new FlowLayout()); 88 sourceFilesPanel.add(sourceFilesField); 89 sourceFilesPanel.add(sourceBrowseButton); 90 91 JLabel classFilesLabel = new JLabel("Class Directory"); 92 JPanel classFilesPanel = new JPanel(new FlowLayout()); 93 classFilesPanel.add(classFilesField); 94 classFilesPanel.add(classBrowseButton); 95 96 clearFilesButton = new JButton(res.getString(Resources.CLEAR_FILES_LABEL)); 97 clearFilesButton.setMnemonic('C'); 98 clearFilesButton.addActionListener(this); 99 100 addFilesButton = new JButton(res.getString(Resources.FIND_FILES_LABEL)); 101 addFilesButton.setMnemonic('A'); 102 addFilesButton.addActionListener(this); 103 104 goButton = new JButton(res.getString(Resources.GO_LABEL)); 105 goButton.setMnemonic('G'); 106 goButton.addActionListener(this); 107 goButton.setEnabled(false); 108 109 JPanel buttonsPanel = new JPanel(new FlowLayout()); 110 buttonsPanel.add(addFilesButton); 111 buttonsPanel.add(clearFilesButton); 112 113 JScrollPane listPane = new JScrollPane(fileList); 114 JPanel listPanel = new JPanel(new BorderLayout()); 115 listPanel.add(listPane,BorderLayout.CENTER); 116 117 JPanel filesPanel = new JPanel(new GridBagLayout()); 118 filesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Files")); 119 120 GridBagConstraints c = new GridBagConstraints(); 121 c.anchor = GridBagConstraints.NORTHWEST; 122 c.weightx = 1; 123 c.gridx = 0; 124 c.gridy = 0; 125 c.fill = GridBagConstraints.NONE; 126 c.gridwidth = 1; 127 filesPanel.add(recurseCheckbox, c); 128 129 c.gridy = 1; 130 filesPanel.add(classFromSourceCheckbox, c); 131 132 c.gridy = 2; 133 filesPanel.add(sourceFilesLabel, c); 134 135 c.gridy = 3; 136 c.fill = GridBagConstraints.HORIZONTAL; 137 c.gridwidth = 2; 138 filesPanel.add(sourceFilesPanel, c); 139 140 c.gridy = 4; 141 c.fill = GridBagConstraints.NONE; 142 c.gridwidth = 1; 143 filesPanel.add(classFilesLabel, c); 144 145 c.gridy = 5; 146 c.fill = GridBagConstraints.HORIZONTAL; 147 c.gridwidth = 2; 148 filesPanel.add(classFilesPanel, c); 149 150 c.gridy = 6; 151 c.fill = GridBagConstraints.NONE; 152 c.gridwidth = 1; 153 filesPanel.add(buttonsPanel, c); 154 155 c.gridy = 7; 156 c.fill = GridBagConstraints.HORIZONTAL; 157 c.gridwidth = 2; 158 filesPanel.add(listPanel, c); 159 160 formats = new JComboBox(); 161 for (Iterator i = StatementFormat.formatIterator(); i.hasNext(); ) { 162 formats.addItem(i.next()); 163 } 164 if (settings.get(ImportScrubber.BREAK_STYLE) != null) { 165 formats.setSelectedItem(settings.get(ImportScrubber.BREAK_STYLE)); 166 } 167 168 JLabel thresholdLabel = new JLabel("Import package.* if more than this many classes used: "); 169 if (settings.get(ImportScrubber.THRESHOLD) != null) { 170 thresholdField.setText(settings.get(ImportScrubber.THRESHOLD)); 171 } else { 172 thresholdField.setText("0"); 173 } 174 thresholdField.setMaximumSize(thresholdField.getPreferredSize()); 175 thresholdCheckbox = new JCheckBox("Only apply to standard libraries"); 176 if (settings.get(ImportScrubber.THRESHOLD_STD_ONLY)!=null) { 177 thresholdCheckbox.setSelected(Boolean.valueOf(settings.get(ImportScrubber.THRESHOLD_STD_ONLY)).booleanValue()); 178 } 179 JPanel thresholdPanel = new JPanel(); 180 thresholdPanel.setLayout(new BoxLayout(thresholdPanel, BoxLayout.X_AXIS)); 181 thresholdPanel.add(thresholdLabel); 182 thresholdPanel.add(thresholdField); 183 thresholdPanel.add(thresholdCheckbox); 184 185 JPanel optionsPanel = new JPanel(); 186 optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.Y_AXIS)); 187 optionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Options")); 188 thresholdPanel.setAlignmentX(JDialog.LEFT_ALIGNMENT); 189 optionsPanel.add(thresholdPanel); 190 sortHighCheckbox.setAlignmentX(JDialog.LEFT_ALIGNMENT); 191 optionsPanel.add(sortHighCheckbox); 192 formats.setAlignmentX(JDialog.LEFT_ALIGNMENT); 193 formats.setMaximumSize(formats.getPreferredSize()); 194 optionsPanel.add(formats); 195 196 menu = new ImportScrubberMenu(this); 197 mainFrame.setJMenuBar(menu); 198 mainFrame.getContentPane().add(filesPanel,BorderLayout.NORTH); 199 mainFrame.getContentPane().add(optionsPanel,BorderLayout.CENTER); 200 mainFrame.getContentPane().add(goButton, BorderLayout.SOUTH); 201 202 scrubber = new ImportScrubber(System.getProperty("file.encoding")); 203 204 mainFrame.setVisible(true); 205 } 206 207 private void maybeSyncClassToSource() 208 { 209 classFilesField.setEnabled(!classFromSourceCheckbox.isSelected()); 210 if (classFromSourceCheckbox.isSelected()) { 211 classFilesField.setText(ImportScrubber.getDirectory(sourceFilesField.getText())); 212 } 213 } 214 215 public void go() 216 { 217 if (fileList.getModel().getSize() == 0) { 218 return; 219 } 220 221 setBusy(true); 222 223 ResourceBundle res = ResourceBundle.getBundle("net.sourceforge.importscrubber.Resources"); 224 225 try { 226 scrubber.setFormat(new StatementFormat(sortHighCheckbox.isSelected(), 227 StatementFormat.valueToKey(formats.getSelectedItem()), 228 new Integer (thresholdField.getText()).intValue(), 229 thresholdCheckbox.isSelected())); 230 int count = scrubber.buildTasks(new Iterator() { 231 Enumeration e = ((UniqueListModel)fileList.getModel()).elements(); 232 public Object next() { 233 return e.nextElement(); 234 } 235 public boolean hasNext() { 236 return e.hasMoreElements(); 237 } 238 public void remove 239 () {} 240 } 241 ); 242 scrubber.runTasks(this); 243 setBusy(false); 244 245 JOptionPane.showMessageDialog(null, res.getString(Resources.ALL_DONE) + " - processed " + count + " files", res.getString(Resources.APP_NAME), JOptionPane.INFORMATION_MESSAGE); 246 } catch (Exception e) { 247 setBusy(false); 248 JOptionPane.showMessageDialog(null, res.getString(Resources.ERR_UNABLE_TO_FINISH) + e.getMessage(), res.getString(Resources.APP_NAME), JOptionPane.ERROR_MESSAGE); 249 e.printStackTrace(); 250 } 251 } 252 253 public void find() 254 { 255 setBusy(true); 256 try { 257 scrubber.setFileRoot(sourceFilesField.getText(), classFilesField.getText(), recurseCheckbox.isSelected()); 258 UniqueListModel model = (UniqueListModel)fileList.getModel(); 259 for (Iterator i = scrubber.getFilesIterator(); i.hasNext();) { 260 FilePair fp = (FilePair)i.next(); 261 model.addElement(fp); 262 } 263 } catch (Exception e) { 264 e.printStackTrace(); 265 } 266 setBusy(false); 267 } 269 270 public void destroy() 271 { 272 settings.put(ImportScrubber.START_DIRECTORY_KEY, sourceFilesField.getText()); 273 settings.put(ImportScrubber.CLASS_DIRECTORY_KEY, classFilesField.getText()); 274 settings.put(ImportScrubber.RECURSE, String.valueOf(recurseCheckbox.isSelected())); 275 settings.put(ImportScrubber.SORT_STD_HIGH, String.valueOf(sortHighCheckbox.isSelected())); 276 settings.put(ImportScrubber.SYNC_CLASS_TO_SOURCE, String.valueOf(classFromSourceCheckbox.isSelected())); 277 settings.put(ImportScrubber.THRESHOLD, thresholdField.getText()); 278 settings.put(ImportScrubber.THRESHOLD_STD_ONLY, String.valueOf(thresholdCheckbox.isSelected())); 279 settings.put(ImportScrubber.BREAK_STYLE, (String )formats.getSelectedItem()); 280 try { 281 settings.save(); 282 } catch (IOException ioe) { 283 ioe.printStackTrace(); 284 } 285 286 mainFrame.setVisible(false); 287 288 mainFrame.dispose(); 289 System.exit(0); 290 } 291 292 public void actionPerformed(java.awt.event.ActionEvent actionEvent) 293 { 294 if(actionEvent.getSource() == clearFilesButton) { 295 UniqueListModel model = (UniqueListModel)fileList.getModel(); 296 model.removeAllElements(); 297 setBusy(false); } else if(actionEvent.getSource() == addFilesButton) { 299 Thread fc = new Thread () { 300 public void run() { 301 setBusy(true); 302 find(); 303 setBusy(false); 304 } 305 }; 306 fc.setPriority(Thread.MIN_PRIORITY); 308 fc.start(); 309 } else if(actionEvent.getSource() == goButton) { 310 Thread go = new Thread () { 311 public void run() { 312 go(); 313 } 314 }; 315 go.setPriority(Thread.MIN_PRIORITY); 317 go.start(); 318 } 319 } 320 321 public void setBusy(boolean busy) 322 { 323 javax.swing.SwingUtilities.invokeLater(new SetBusy(busy)); 324 } 325 326 public class SetBusy implements Runnable 327 { 328 boolean busy; 329 public SetBusy(boolean busy) 330 { 331 this.busy = busy; 332 } 333 public void run() 334 { 335 addFilesButton.setEnabled(!busy); 336 clearFilesButton.setEnabled(!busy); 337 goButton.setEnabled(!busy && fileList.getModel().getSize() > 0); 338 339 if(busy) { 340 mainFrame.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR)); 341 } else { 342 mainFrame.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR)); 343 } 344 } 345 } 346 347 public void taskStarted(ScrubTask task) 349 { 350 UniqueListModel model = (UniqueListModel)fileList.getModel(); 351 for (Enumeration e = model.elements(); e.hasMoreElements();) { 352 FilePair pair = (FilePair)e.nextElement(); 353 if (pair.getSourceFile().getAbsolutePath().equals(task.getSourcePath())) { 354 fileList.setSelectedValue(pair, true); 355 } 356 } 357 } 358 359 public void taskComplete(ScrubTask task) 360 { 361 fileList.clearSelection(); 362 } 363 365 public static void main(String [] args) 366 { 367 ImportScrubberGUI gui = new ImportScrubberGUI(); 368 } 369 370 private class BrowseButton extends JButton 371 { 372 private JTextField _field; 373 374 public BrowseButton(final JTextField field, String label) 375 { 376 super(label); 377 _field = field; 378 addActionListener(new ActionListener() { 379 public void actionPerformed(ActionEvent e) { 380 JFileChooser chooser = new JFileChooser(field.getText()); 381 382 chooser.setDialogTitle(ResourceBundle.getBundle("net.sourceforge.importscrubber.Resources").getString(Resources.FILE_BROWSER_TITLE)); 383 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 384 chooser.showOpenDialog(mainFrame); 385 386 if (chooser.getSelectedFile() != null) { 387 _field.setText(chooser.getSelectedFile().getAbsolutePath()); 388 } 389 } 390 } 391 ); 392 } 393 } 394 395 private class BasicWindowMonitor extends WindowAdapter 396 { 397 public void windowClosing(WindowEvent we) 398 { 399 destroy(); 400 } 401 } 402 403 private class UniqueListModel extends DefaultListModel 404 { 405 private HashMap contents = new HashMap(1000); 406 private Boolean t = new Boolean (true); 407 408 public UniqueListModel() 409 { 410 super(); 411 } 412 413 public void addElement(Object o) 414 { 415 if (!contents.containsKey(o)) { 416 super.addElement(o); 417 contents.put(o, t); 418 } 419 } 420 421 public void removeAllElements() 422 { 423 super.removeAllElements(); 424 contents.clear(); 425 } 426 } 427 } 428 | Popular Tags |