001: package de.java2html.gui;
002:
003: import java.awt.BorderLayout;
004: import java.awt.FlowLayout;
005: import java.awt.GridLayout;
006: import java.awt.Toolkit;
007: import java.awt.datatransfer.Clipboard;
008: import java.awt.datatransfer.StringSelection;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.io.File;
012: import java.io.IOException;
013: import java.io.StringWriter;
014:
015: import javax.swing.DefaultListModel;
016: import javax.swing.JButton;
017: import javax.swing.JComponent;
018: import javax.swing.JFileChooser;
019: import javax.swing.JLabel;
020: import javax.swing.JList;
021: import javax.swing.JOptionPane;
022: import javax.swing.JPanel;
023: import javax.swing.JScrollPane;
024: import javax.swing.border.EmptyBorder;
025: import javax.swing.event.ListDataEvent;
026: import javax.swing.event.ListDataListener;
027: import javax.swing.event.ListSelectionEvent;
028: import javax.swing.event.ListSelectionListener;
029: import javax.swing.filechooser.FileFilter;
030:
031: import de.java2html.JavaSourceConversionSettings;
032: import de.java2html.converter.IJavaSourceConverter;
033: import de.java2html.javasource.JavaSource;
034: import de.java2html.javasource.JavaSourceParser;
035: import de.java2html.util.Ensure;
036:
037: public class FileConversionPanel implements ActionListener {
038:
039: private final DefaultListModel fileListModel = new DefaultListModel();
040: private final JList list;
041: private final JButton bAdd;
042: private final JButton bRemove;
043: private final JButton bConvert;
044: private final JButton bClear;
045: private File currentDirectory;
046: private final JComponent content;
047: private final Java2HtmlOptionsPanel optionsPanel;
048:
049: public FileConversionPanel(Java2HtmlOptionsPanel optionsPanel) {
050: Ensure.ensureArgumentNotNull(optionsPanel);
051: this .optionsPanel = optionsPanel;
052: list = new JList(fileListModel);
053:
054: bAdd = new JButton("Add...");
055: bAdd.addActionListener(this );
056:
057: bRemove = new JButton("Remove");
058: bRemove.addActionListener(this );
059: bRemove.setEnabled(false);
060:
061: bClear = new JButton("Clear");
062: bClear.addActionListener(this );
063: bClear.setEnabled(false);
064:
065: bConvert = new JButton("Convert");
066: bConvert.addActionListener(this );
067: bConvert.setEnabled(false);
068:
069: final JPanel fileButtonPanel = new JPanel(new GridLayout(0, 1,
070: 6, 6));
071: fileButtonPanel.add(bAdd);
072: fileButtonPanel.add(bRemove);
073: fileButtonPanel.add(bClear);
074:
075: final JPanel buttonPanel = new JPanel(new BorderLayout());
076: buttonPanel.add(fileButtonPanel, BorderLayout.NORTH);
077:
078: final JPanel filesToConvertPanel = new JPanel(new BorderLayout(
079: 4, 4));
080: filesToConvertPanel.add(new JLabel("Files to convert:"),
081: BorderLayout.NORTH);
082: filesToConvertPanel.add(new JScrollPane(list),
083: BorderLayout.CENTER);
084: filesToConvertPanel.add(buttonPanel, BorderLayout.EAST);
085:
086: final JPanel southPanel = new JPanel(new FlowLayout(
087: FlowLayout.LEFT));
088: southPanel.add(bConvert);
089:
090: final JPanel panel = new JPanel(new BorderLayout(4, 4));
091: panel.setBorder(new EmptyBorder(5, 6, 5, 6));
092: panel.add(filesToConvertPanel, BorderLayout.CENTER);
093: panel.add(southPanel, BorderLayout.SOUTH);
094: this .content = panel;
095:
096: list.addListSelectionListener(new ListSelectionListener() {
097: public void valueChanged(ListSelectionEvent e) {
098: updateButtonsEnabled();
099: }
100: });
101: fileListModel.addListDataListener(new ListDataListener() {
102: public void contentsChanged(ListDataEvent e) {
103: updateButtonsEnabled();
104: }
105:
106: public void intervalRemoved(ListDataEvent e) {
107: updateButtonsEnabled();
108: }
109:
110: public void intervalAdded(ListDataEvent e) {
111: updateButtonsEnabled();
112: }
113: });
114: updateButtonsEnabled();
115: }
116:
117: private void updateButtonsEnabled() {
118: bRemove.setEnabled(!list.isSelectionEmpty());
119: bClear.setEnabled(!fileListModel.isEmpty());
120: bConvert.setEnabled(!fileListModel.isEmpty());
121: }
122:
123: private void add() {
124: JFileChooser chooser = new JFileChooser(currentDirectory);
125: chooser.setDialogTitle("Open Java Source");
126: chooser.setFileFilter(new FileFilter() {
127: public String getDescription() {
128: return "*.java";
129: }
130:
131: public boolean accept(File f) {
132: return f.isDirectory()
133: || f.getName().toLowerCase().endsWith(".java");
134: }
135: });
136: int result = chooser.showOpenDialog(content);
137: if (result != JFileChooser.APPROVE_OPTION) {
138: return;
139: }
140: File selectedFile = chooser.getSelectedFile();
141: currentDirectory = selectedFile.getParentFile();
142: fileListModel.addElement(selectedFile);
143: }
144:
145: private void remove() {
146: Object[] files = list.getSelectedValues();
147: for (int i = 0; i < files.length; ++i) {
148: fileListModel.removeElement(files[i]);
149: }
150: }
151:
152: private void clear() {
153: fileListModel.clear();
154: }
155:
156: private void convert() {
157: JavaSourceConversionSettings settings = optionsPanel
158: .getConversionSettings();
159: settings.getConversionOptions().setShowJava2HtmlLink(true);
160:
161: //Collect statistical information
162: final StringBuffer report = new StringBuffer();
163:
164: //Collect conversion-results
165: StringWriter writer = new StringWriter();
166:
167: //Create the converter
168: IJavaSourceConverter converter = settings.createConverter();
169:
170: Object[] files = fileListModel.toArray();
171: try {
172: converter.writeDocumentHeader(writer, settings
173: .getConversionOptions(), "");
174:
175: for (int count = 0; count < files.length; ++count) {
176: final File file = (File) files[count];
177: report.append("<li>File " + (count + 1) + ": "
178: + file.getName() + "<blockquote>");
179:
180: try {
181: if (count > 0) {
182: converter.writeBlockSeparator(writer, settings
183: .getConversionOptions());
184: }
185: JavaSourceParser parser = new JavaSourceParser(
186: settings.getConversionOptions());
187: JavaSource source = parser.parse(file);
188: converter.convert(source, settings
189: .getConversionOptions(), writer);
190:
191: writer.write('\n');
192:
193: report.append(source.getStatistic()
194: .getScreenString("<br>")
195: + "</blockquote></li>");
196: } catch (IOException e) {
197: //TODO Sep 13, 2004 (Markus Gebhard): Error handling!
198: System.err.println("Error converting " + file
199: + ": " + e);
200: }
201: }
202:
203: converter.writeDocumentFooter(writer, settings
204: .getConversionOptions());
205: } catch (IOException e) {
206: // TODO Sep 13, 2004 (Markus Gebhard): Error handling!
207: System.err.println("Error converting files " + e);
208: }
209:
210: //Copy result to system clipboard
211: StringSelection sel = new StringSelection(writer.getBuffer()
212: .toString());
213: Clipboard clipboard = Toolkit.getDefaultToolkit()
214: .getSystemClipboard();
215: clipboard.setContents(sel, sel);
216:
217: String plural = "";
218: if (files.length > 1) {
219: plural = "s";
220: }
221:
222: //Show success-dialog
223:
224: final String title = "File" + plural + " converted";
225: final String text = "<html><b>"
226: + files.length
227: + " File"
228: + plural
229: + " successfully converted.</b>"
230: + "<ul>"
231: + report.toString()
232: + "</ul>"
233: + "<b>The converted source code has been copied the system clipboard</b>"
234: + "</html>";
235: JOptionPane.showMessageDialog(content, text, title,
236: JOptionPane.INFORMATION_MESSAGE);
237: }
238:
239: public JComponent getContent() {
240: return content;
241: }
242:
243: public void actionPerformed(ActionEvent evt) {
244: Object source = evt.getSource();
245: if (source == bAdd) {
246: add();
247: } else if (source == bRemove) {
248: remove();
249: } else if (source == bClear) {
250: clear();
251: } else {
252: convert();
253: }
254: }
255: }
|