KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > gui > FileConversionPanel


1 package de.java2html.gui;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.FlowLayout JavaDoc;
5 import java.awt.GridLayout JavaDoc;
6 import java.awt.Toolkit JavaDoc;
7 import java.awt.datatransfer.Clipboard JavaDoc;
8 import java.awt.datatransfer.StringSelection JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.ActionListener JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.StringWriter JavaDoc;
14
15 import javax.swing.DefaultListModel JavaDoc;
16 import javax.swing.JButton JavaDoc;
17 import javax.swing.JComponent JavaDoc;
18 import javax.swing.JFileChooser JavaDoc;
19 import javax.swing.JLabel JavaDoc;
20 import javax.swing.JList JavaDoc;
21 import javax.swing.JOptionPane JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23 import javax.swing.JScrollPane JavaDoc;
24 import javax.swing.border.EmptyBorder JavaDoc;
25 import javax.swing.event.ListDataEvent JavaDoc;
26 import javax.swing.event.ListDataListener JavaDoc;
27 import javax.swing.event.ListSelectionEvent JavaDoc;
28 import javax.swing.event.ListSelectionListener JavaDoc;
29 import javax.swing.filechooser.FileFilter JavaDoc;
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 JavaDoc {
38
39   private final DefaultListModel JavaDoc fileListModel = new DefaultListModel JavaDoc();
40   private final JList JavaDoc list;
41   private final JButton JavaDoc bAdd;
42   private final JButton JavaDoc bRemove;
43   private final JButton JavaDoc bConvert;
44   private final JButton JavaDoc bClear;
45   private File JavaDoc currentDirectory;
46   private final JComponent JavaDoc content;
47   private final Java2HtmlOptionsPanel optionsPanel;
48
49   public FileConversionPanel(Java2HtmlOptionsPanel optionsPanel) {
50     Ensure.ensureArgumentNotNull(optionsPanel);
51     this.optionsPanel = optionsPanel;
52     list = new JList JavaDoc(fileListModel);
53
54     bAdd = new JButton JavaDoc("Add...");
55     bAdd.addActionListener(this);
56
57     bRemove = new JButton JavaDoc("Remove");
58     bRemove.addActionListener(this);
59     bRemove.setEnabled(false);
60
61     bClear = new JButton JavaDoc("Clear");
62     bClear.addActionListener(this);
63     bClear.setEnabled(false);
64
65     bConvert = new JButton JavaDoc("Convert");
66     bConvert.addActionListener(this);
67     bConvert.setEnabled(false);
68
69     final JPanel JavaDoc fileButtonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(0, 1, 6, 6));
70     fileButtonPanel.add(bAdd);
71     fileButtonPanel.add(bRemove);
72     fileButtonPanel.add(bClear);
73
74     final JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
75     buttonPanel.add(fileButtonPanel, BorderLayout.NORTH);
76
77     final JPanel JavaDoc filesToConvertPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(4, 4));
78     filesToConvertPanel.add(new JLabel JavaDoc("Files to convert:"), BorderLayout.NORTH);
79     filesToConvertPanel.add(new JScrollPane JavaDoc(list), BorderLayout.CENTER);
80     filesToConvertPanel.add(buttonPanel, BorderLayout.EAST);
81
82     final JPanel JavaDoc southPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.LEFT));
83     southPanel.add(bConvert);
84
85     final JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(4, 4));
86     panel.setBorder(new EmptyBorder JavaDoc(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 JavaDoc() {
92       public void valueChanged(ListSelectionEvent JavaDoc e) {
93         updateButtonsEnabled();
94       }
95     });
96     fileListModel.addListDataListener(new ListDataListener JavaDoc() {
97       public void contentsChanged(ListDataEvent JavaDoc e) {
98         updateButtonsEnabled();
99       }
100
101       public void intervalRemoved(ListDataEvent JavaDoc e) {
102         updateButtonsEnabled();
103       }
104
105       public void intervalAdded(ListDataEvent JavaDoc 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 JavaDoc chooser = new JFileChooser JavaDoc(currentDirectory);
120     chooser.setDialogTitle("Open Java Source");
121     chooser.setFileFilter(new FileFilter JavaDoc() {
122       public String JavaDoc getDescription() {
123         return "*.java";
124       }
125
126       public boolean accept(File JavaDoc 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 JavaDoc selectedFile = chooser.getSelectedFile();
135     currentDirectory = selectedFile.getParentFile();
136     fileListModel.addElement(selectedFile);
137   }
138
139   private void remove() {
140     Object JavaDoc[] 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     //Collect statistical information
155
final StringBuffer JavaDoc report = new StringBuffer JavaDoc();
156
157     //Collect conversion-results
158
StringWriter JavaDoc writer = new StringWriter JavaDoc();
159
160     //Create the converter
161
IJavaSourceConverter converter = settings.createConverter();
162
163     Object JavaDoc[] files = fileListModel.toArray();
164     try {
165       converter.writeDocumentHeader(writer, settings.getConversionOptions(), "");
166
167       for (int count = 0; count < files.length; ++count) {
168         final File JavaDoc file = (File JavaDoc) 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 JavaDoc e) {
184           //TODO Sep 13, 2004 (Markus Gebhard): Error handling!
185
System.err.println("Error converting " + file + ": " + e);
186         }
187       }
188
189       converter.writeDocumentFooter(writer, settings.getConversionOptions());
190     }
191     catch (IOException JavaDoc e) {
192       // TODO Sep 13, 2004 (Markus Gebhard): Error handling!
193
System.err.println("Error converting files " + e);
194     }
195
196     //Copy result to system clipboard
197
StringSelection JavaDoc sel = new StringSelection JavaDoc(writer.getBuffer().toString());
198     Clipboard JavaDoc clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
199     clipboard.setContents(sel, sel);
200
201     String JavaDoc plural = "";
202     if (files.length > 1) {
203       plural = "s";
204     }
205
206     //Show success-dialog
207

208     final String JavaDoc title = "File" + plural + " converted";
209     final String JavaDoc 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 JavaDoc getContent() {
223     return content;
224   }
225
226   public void actionPerformed(ActionEvent JavaDoc evt) {
227     Object JavaDoc 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