1 package de.java2html.gui; 2 3 import java.awt.BorderLayout ; 4 import java.awt.FlowLayout ; 5 import java.awt.GridLayout ; 6 import java.awt.Toolkit ; 7 import java.awt.datatransfer.Clipboard ; 8 import java.awt.datatransfer.StringSelection ; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.ActionListener ; 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.StringWriter ; 14 15 import javax.swing.DefaultListModel ; 16 import javax.swing.JButton ; 17 import javax.swing.JComponent ; 18 import javax.swing.JFileChooser ; 19 import javax.swing.JLabel ; 20 import javax.swing.JList ; 21 import javax.swing.JOptionPane ; 22 import javax.swing.JPanel ; 23 import javax.swing.JScrollPane ; 24 import javax.swing.border.EmptyBorder ; 25 import javax.swing.event.ListDataEvent ; 26 import javax.swing.event.ListDataListener ; 27 import javax.swing.event.ListSelectionEvent ; 28 import javax.swing.event.ListSelectionListener ; 29 import javax.swing.filechooser.FileFilter ; 30 31 import de.java2html.JavaSourceConversionSettings; 32 import de.java2html.converter.IJavaSourceConverter; 33 import de.java2html.javasource.JavaSource; 34 import de.java2html.javasource.JavaSourceParser; 35 import de.java2html.util.Ensure; 36 37 public class FileConversionPanel implements ActionListener { 38 39 private final DefaultListModel fileListModel = new DefaultListModel (); 40 private final JList list; 41 private final JButton bAdd; 42 private final JButton bRemove; 43 private final JButton bConvert; 44 private final JButton bClear; 45 private File currentDirectory; 46 private final JComponent content; 47 private final Java2HtmlOptionsPanel optionsPanel; 48 49 public FileConversionPanel(Java2HtmlOptionsPanel optionsPanel) { 50 Ensure.ensureArgumentNotNull(optionsPanel); 51 this.optionsPanel = optionsPanel; 52 list = new JList (fileListModel); 53 54 bAdd = new JButton ("Add..."); 55 bAdd.addActionListener(this); 56 57 bRemove = new JButton ("Remove"); 58 bRemove.addActionListener(this); 59 bRemove.setEnabled(false); 60 61 bClear = new JButton ("Clear"); 62 bClear.addActionListener(this); 63 bClear.setEnabled(false); 64 65 bConvert = new JButton ("Convert"); 66 bConvert.addActionListener(this); 67 bConvert.setEnabled(false); 68 69 final JPanel fileButtonPanel = new JPanel (new GridLayout (0, 1, 6, 6)); 70 fileButtonPanel.add(bAdd); 71 fileButtonPanel.add(bRemove); 72 fileButtonPanel.add(bClear); 73 74 final JPanel buttonPanel = new JPanel (new BorderLayout ()); 75 buttonPanel.add(fileButtonPanel, BorderLayout.NORTH); 76 77 final JPanel filesToConvertPanel = new JPanel (new BorderLayout (4, 4)); 78 filesToConvertPanel.add(new JLabel ("Files to convert:"), BorderLayout.NORTH); 79 filesToConvertPanel.add(new JScrollPane (list), BorderLayout.CENTER); 80 filesToConvertPanel.add(buttonPanel, BorderLayout.EAST); 81 82 final JPanel southPanel = new JPanel (new FlowLayout (FlowLayout.LEFT)); 83 southPanel.add(bConvert); 84 85 final JPanel panel = new JPanel (new BorderLayout (4, 4)); 86 panel.setBorder(new EmptyBorder (5, 6, 5, 6)); 87 panel.add(filesToConvertPanel, BorderLayout.CENTER); 88 panel.add(southPanel, BorderLayout.SOUTH); 89 this.content = panel; 90 91 list.addListSelectionListener(new ListSelectionListener () { 92 public void valueChanged(ListSelectionEvent e) { 93 updateButtonsEnabled(); 94 } 95 }); 96 fileListModel.addListDataListener(new ListDataListener () { 97 public void contentsChanged(ListDataEvent e) { 98 updateButtonsEnabled(); 99 } 100 101 public void intervalRemoved(ListDataEvent e) { 102 updateButtonsEnabled(); 103 } 104 105 public void intervalAdded(ListDataEvent e) { 106 updateButtonsEnabled(); 107 } 108 }); 109 updateButtonsEnabled(); 110 } 111 112 private void updateButtonsEnabled() { 113 bRemove.setEnabled(!list.isSelectionEmpty()); 114 bClear.setEnabled(!fileListModel.isEmpty()); 115 bConvert.setEnabled(!fileListModel.isEmpty()); 116 } 117 118 private void add() { 119 JFileChooser chooser = new JFileChooser (currentDirectory); 120 chooser.setDialogTitle("Open Java Source"); 121 chooser.setFileFilter(new FileFilter () { 122 public String getDescription() { 123 return "*.java"; 124 } 125 126 public boolean accept(File f) { 127 return f.isDirectory() || f.getName().toLowerCase().endsWith(".java"); 128 } 129 }); 130 int result = chooser.showOpenDialog(content); 131 if (result != JFileChooser.APPROVE_OPTION) { 132 return; 133 } 134 File selectedFile = chooser.getSelectedFile(); 135 currentDirectory = selectedFile.getParentFile(); 136 fileListModel.addElement(selectedFile); 137 } 138 139 private void remove() { 140 Object [] files = list.getSelectedValues(); 141 for (int i = 0; i < files.length; ++i) { 142 fileListModel.removeElement(files[i]); 143 } 144 } 145 146 private void clear() { 147 fileListModel.clear(); 148 } 149 150 private void convert() { 151 JavaSourceConversionSettings settings = optionsPanel.getConversionSettings(); 152 settings.getConversionOptions().setShowJava2HtmlLink(true); 153 154 final StringBuffer report = new StringBuffer (); 156 157 StringWriter writer = new StringWriter (); 159 160 IJavaSourceConverter converter = settings.createConverter(); 162 163 Object [] files = fileListModel.toArray(); 164 try { 165 converter.writeDocumentHeader(writer, settings.getConversionOptions(), ""); 166 167 for (int count = 0; count < files.length; ++count) { 168 final File file = (File ) files[count]; 169 report.append("<li>File " + (count + 1) + ": " + file.getName() + "<blockquote>"); 170 171 try { 172 if (count > 0) { 173 converter.writeBlockSeparator(writer, settings.getConversionOptions()); 174 } 175 JavaSourceParser parser = new JavaSourceParser(settings.getConversionOptions()); 176 JavaSource source = parser.parse(file); 177 converter.convert(source, settings.getConversionOptions(), writer); 178 179 writer.write('\n'); 180 181 report.append(source.getStatistic().getScreenString("<br>") + "</blockquote></li>"); 182 } 183 catch (IOException e) { 184 System.err.println("Error converting " + file + ": " + e); 186 } 187 } 188 189 converter.writeDocumentFooter(writer, settings.getConversionOptions()); 190 } 191 catch (IOException e) { 192 System.err.println("Error converting files " + e); 194 } 195 196 StringSelection sel = new StringSelection (writer.getBuffer().toString()); 198 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 199 clipboard.setContents(sel, sel); 200 201 String plural = ""; 202 if (files.length > 1) { 203 plural = "s"; 204 } 205 206 208 final String title = "File" + plural + " converted"; 209 final String text = "<html><b>" 210 + files.length 211 + " File" 212 + plural 213 + " successfully converted.</b>" 214 + "<ul>" 215 + report.toString() 216 + "</ul>" 217 + "<b>The converted source code has been copied the system clipboard</b>" 218 + "</html>"; 219 JOptionPane.showMessageDialog(content, text, title, JOptionPane.INFORMATION_MESSAGE); 220 } 221 222 public JComponent getContent() { 223 return content; 224 } 225 226 public void actionPerformed(ActionEvent evt) { 227 Object source = evt.getSource(); 228 if (source == bAdd) { 229 add(); 230 } 231 else if (source == bRemove) { 232 remove(); 233 } 234 else if (source == bClear) { 235 clear(); 236 } 237 else { 238 convert(); 239 } 240 } 241 } | Popular Tags |