001: package de.java2html.gui;
002:
003: import java.awt.Font;
004: import java.awt.GridBagConstraints;
005: import java.awt.GridBagLayout;
006: import java.awt.Insets;
007: import java.io.IOException;
008: import java.io.StringWriter;
009:
010: import javax.swing.JComponent;
011: import javax.swing.JLabel;
012: import javax.swing.JPanel;
013: import javax.swing.JScrollPane;
014: import javax.swing.JTextArea;
015: import javax.swing.JTextField;
016: import javax.swing.border.EmptyBorder;
017: import javax.swing.event.ChangeEvent;
018: import javax.swing.event.ChangeListener;
019: import javax.swing.event.DocumentEvent;
020: import javax.swing.event.DocumentListener;
021:
022: import de.java2html.JavaSourceConversionSettings;
023: import de.java2html.converter.IJavaSourceConverter;
024: import de.java2html.javasource.JavaSource;
025: import de.java2html.javasource.JavaSourceParser;
026: import de.java2html.util.Ensure;
027:
028: public class DirectTextConversionPanel {
029:
030: private final JComponent content;
031: private final static Font FONT = new Font("Monospaced", Font.PLAIN,
032: 11);
033: private final JTextArea taInput;
034: private final JTextArea taOutput;
035: private final JTextField tfTitle;
036: private final Java2HtmlOptionsPanel optionsPanel;
037: private final IStatisticsView statisticsView;
038:
039: public DirectTextConversionPanel(
040: Java2HtmlOptionsPanel optionsPanel,
041: IStatisticsView statisticsView) {
042: Ensure.ensureArgumentNotNull(optionsPanel);
043: Ensure.ensureArgumentNotNull(statisticsView);
044: this .statisticsView = statisticsView;
045: this .optionsPanel = optionsPanel;
046: //Create UI:
047: JLabel l1 = new JLabel("Title (optional):");
048: tfTitle = new JTextField(30);
049: final DocumentListener documentListener = new DocumentListener() {
050: public void changedUpdate(DocumentEvent e) {
051: convert();
052: }
053:
054: public void removeUpdate(DocumentEvent e) {
055: convert();
056: }
057:
058: public void insertUpdate(DocumentEvent e) {
059: convert();
060: }
061: };
062: tfTitle.getDocument().addDocumentListener(documentListener);
063:
064: taInput = new JTextArea(10, 80);
065: taInput.getDocument().addDocumentListener(documentListener);
066: taInput.setFont(FONT);
067: taOutput = new JTextArea(10, 80);
068: taOutput.setEditable(false);
069: taOutput.setFont(FONT);
070:
071: final JPanel panel = new JPanel(new GridBagLayout());
072: panel.setBorder(new EmptyBorder(5, 6, 5, 6));
073:
074: final GridBagConstraints twoColumnLineConstraints = new GridBagConstraints();
075: twoColumnLineConstraints.anchor = GridBagConstraints.WEST;
076: twoColumnLineConstraints.gridx = 0;
077: twoColumnLineConstraints.gridwidth = 2;
078: twoColumnLineConstraints.fill = GridBagConstraints.HORIZONTAL;
079: twoColumnLineConstraints.weightx = 1.0;
080: twoColumnLineConstraints.insets = new Insets(6, 2, 2, 2);
081:
082: final GridBagConstraints twoColumnBlockConstraints = new GridBagConstraints();
083: twoColumnBlockConstraints.anchor = GridBagConstraints.WEST;
084: twoColumnBlockConstraints.gridx = 0;
085: twoColumnBlockConstraints.gridwidth = 2;
086: twoColumnBlockConstraints.fill = GridBagConstraints.BOTH;
087: twoColumnBlockConstraints.weightx = 1.0;
088: twoColumnBlockConstraints.weighty = 1.0;
089: twoColumnBlockConstraints.insets = new Insets(2, 2, 2, 2);
090:
091: final GridBagConstraints leftColumnConstraints = new GridBagConstraints();
092: leftColumnConstraints.insets = new Insets(2, 2, 2, 2);
093: leftColumnConstraints.gridx = 0;
094: leftColumnConstraints.anchor = GridBagConstraints.EAST;
095:
096: final GridBagConstraints rightColumnConstraints = new GridBagConstraints();
097: rightColumnConstraints.insets = new Insets(2, 2, 2, 2);
098: rightColumnConstraints.gridx = 1;
099: rightColumnConstraints.fill = GridBagConstraints.HORIZONTAL;
100: rightColumnConstraints.anchor = GridBagConstraints.WEST;
101:
102: panel.add(l1, leftColumnConstraints);
103: panel.add(tfTitle, rightColumnConstraints);
104: panel.add(new JLabel(
105: "Java source (paste your source code here):"),
106: twoColumnLineConstraints);
107: panel.add(new JScrollPane(taInput), twoColumnBlockConstraints);
108: panel
109: .add(
110: new JLabel(
111: "Converted source (copy and paste this to where you want it):"),
112: twoColumnLineConstraints);
113: panel.add(new JScrollPane(taOutput), twoColumnBlockConstraints);
114:
115: optionsPanel.addChangeListener(new ChangeListener() {
116: public void stateChanged(ChangeEvent e) {
117: convert();
118: }
119: });
120:
121: this .content = panel;
122: }
123:
124: public JComponent getContent() {
125: return content;
126: }
127:
128: private void convert() {
129: String title = tfTitle.getText();
130: if (title.equals("")) {
131: title = null;
132: }
133:
134: String input = taInput.getText();
135: if (input.trim().equals("")) {
136: taOutput.setText("");
137: statisticsView.setStatistics(null);
138: return;
139: }
140: JavaSourceConversionSettings settings = optionsPanel
141: .getConversionSettings();
142: settings.getConversionOptions().setShowJava2HtmlLink(true);
143: JavaSourceParser parser = new JavaSourceParser(settings
144: .getConversionOptions());
145: JavaSource source = parser.parse(input);
146: source.setFileName(title);
147:
148: //Create the converter
149: IJavaSourceConverter converter = settings.createConverter();
150:
151: StringWriter writer = new StringWriter();
152: try {
153: converter.writeDocumentHeader(writer, settings
154: .getConversionOptions(), title);
155: converter.convert(source, settings.getConversionOptions(),
156: writer);
157: converter.writeDocumentFooter(writer, settings
158: .getConversionOptions());
159: } catch (IOException e) {
160: throw new RuntimeException(e); //should never happen
161: }
162:
163: taOutput.setText(writer.toString());
164: statisticsView.setStatistics(source.getStatistic());
165: taOutput.selectAll();
166: }
167:
168: public void requestFocus() {
169: taInput.requestFocus();
170: }
171: }
|