/Users/lyon/j4p/src/gui/htmlconverter/J2Html.java
|
1 package gui.htmlconverter;
2
3 import java.awt.*;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.io.*;
7
8 public class J2Html extends Frame implements ActionListener {
9
10 JavaStream js;
11 DataOutputStream dos;
12 FileControlPanel fcp;
13 ColorControlPanel ccp;
14 TargetControlPanel tcp;
15 ButtonControlPanel bcp;
16 Label statusBar;
17 String fileName, pathName;
18 String extention;
19 MenuItem miOpen, miSave, miConvertAll, miQuit;
20
21 boolean miOpenBuf, miSaveBuf, miConvertAllBuf, miQuitBuf;
22
23 static String MI_OPEN = "Open...";
24 static String MI_SAVE = "Save";
25 static String MI_CONVERTALL = "Convert All";
26 static String MI_QUIT = "Quit";
27
28 private static String EXT_JAVA = ".java";
29 private static String EXT_C = ".c";
30 private static String EXT_CPP = ".cpp";
31
32 private static String DIR_OUTPUT = "Html"; // the folder name for output
33
34 // constructor
35
36 public J2Html(String title) {
37 super(title);
38 MenuBar mb = new MenuBar();
39 Menu m = new Menu("File");
40 miOpen = new MenuItem(MI_OPEN);
41 miSave = new MenuItem(MI_SAVE);
42 miConvertAll = new MenuItem(MI_CONVERTALL);
43 miQuit = new MenuItem(MI_QUIT);
44 m.add(miOpen);
45 m.add(miSave);
46 m.add(miConvertAll);
47 m.addSeparator();
48 m.add(miQuit);
49 mb.add(m);
50 // now add the actionListener
51 miOpen.addActionListener(this);
52 miSave.addActionListener(this);
53 miConvertAll.addActionListener(this);
54 miQuit.addActionListener(this);
55 miOpen.setEnabled(true);
56 miSave.setEnabled(false);
57 miConvertAll.setEnabled(false);
58 miQuit.setEnabled(true);
59 setMenuBar(mb);
60 init();
61 }
62
63 // public methods
64
65 public String getExtention() {
66 if (tcp.c.getSelectedItem().equals("Java"))
67 return EXT_JAVA;
68 else if (tcp.c.getSelectedItem().equals("C"))
69 return EXT_C;
70 else
71 return EXT_CPP;
72 }
73
74
75 public void actionPerformed(final ActionEvent evt) {
76 int index;
77 final DataOutputStream indexFile;
78 final Object arg = evt.getActionCommand();
79 final Object target = evt.getSource();
80 //System.out.println("Event="+evt);
81
82 final String label = (String) arg;
83 if (target instanceof MenuItem ||
84 target instanceof Button) {
85 if (label.equals(MI_OPEN) && miOpen.isEnabled()) {
86 setEventMask(true);
87 statusMsg("Open is selected.");
88 final FileDialog fd = new FileDialog(this, "");
89 fd.show();
90 fileName = fd.getFile();
91 pathName = fd.getDirectory();
92 fd.dispose();
93
94 if (fileName != null && pathName != null) {
95 index = fileName.indexOf(".");
96 setEventMask(false);
97 if (index >= 0 && fileName.substring(index).equals(getExtention())) {
98 fileName = fileName.substring(0, index);
99 fcp.setText(fileName);
100 // deprecated DL 4/26/98
101 //miSave.enable();
102 //miConvertAll.enable();
103 miSave.setEnabled(true);
104 miConvertAll.setEnabled(true);
105 } else {
106 fileName = "noname";
107 pathName = "";
108 }
109 } else {
110 setEventMask(false);
111 }
112 } else if (label.equals(MI_SAVE) && miSave.isEnabled()) {
113 setEventMask(true);
114 statusMsg("Save is selected.");
115 final File newDir = new File(pathName + DIR_OUTPUT);
116 newDir.mkdir();
117 convert(MI_SAVE);
118 statusMsg("Converting is done.");
119 setEventMask(false);
120 } else if (label.equals(MI_CONVERTALL) &&
121 miConvertAll.isEnabled()) {
122 setEventMask(true);
123 statusMsg("Convert All is selected.");
124 try {
125 final File dir = new File(pathName);
126 final File newDir = new File(pathName + DIR_OUTPUT);
127 newDir.mkdir();
128 indexFile = new DataOutputStream(
129 new BufferedOutputStream(
130 new FileOutputStream(
131 pathName + DIR_OUTPUT + "/index.gui.html")));
132 indexFile.writeBytes("<HTML>\n");
133 indexFile.writeBytes("<TITLE>Index</TITLE>\n");
134 indexFile.writeBytes("<H3>Index</H3>\n");
135 indexFile.writeBytes("<HR>\n");
136 indexFile.writeBytes("<OL>\n");
137 final String[] files;
138 if (getExtention().equals(EXT_JAVA))
139 files = dir.list(new WildFilter(".java"));
140 else if (getExtention().equals(EXT_C))
141 files = dir.list(new WildFilter(".c"));
142 else
143 files = dir.list(new WildFilter(".cpp"));
144 for (int i = 0; i < files.length; i++) {
145 indexFile.writeBytes("<LI><A HREF=\"" +
146 files[i].substring(0, files[i].indexOf(".")) + ".gui.html\">" +
147 files[i] + "</A>\n");
148 statusMsg("Converting " + files[i]);
149 index = files[i].indexOf(".");
150 if (index >= 0 && files[i].substring(index).equals(getExtention())) {
151 fileName = files[i].substring(0, index);
152 fcp.setText(fileName);
153 convert(MI_CONVERTALL);
154 }
155 }
156 indexFile.writeBytes("</OL>");
157 indexFile.writeBytes("<HR>");
158 indexFile.writeBytes("<H6><A HREF=\"http://www.docjava.com\">This document was generated automatically by Kahindu.</A></H6>\n");
159 indexFile.writeBytes("</HTML>\n");
160 indexFile.close();
161 statusMsg("Converting is done.");
162 } catch (IOException e) {
163 System.out.println("error is occured.");
164 }
165 setEventMask(false);
166
167 } else if (label.equals(MI_QUIT) && miQuit.isEnabled()) {
168 setEventMask(true);
169 statusMsg("Quit is selected.");
170 dispose();
171 setEventMask(false);
172 }
173 }
174 // return true;
175
176 }
177
178 public static void main(String[] args) {
179 J2Html window = new J2Html("HtmlGenerator");
180 // deprecated DL 4/26/98
181 // window.resize(275, 250);
182 window.setSize(350, 350);
183 window.show();
184 }
185
186 // private method
187
188 private void setModifier(final JavaStream js) {
189 js.getMainText().setColorForMainText(ccp.ccf0.c0.getSelectedItem());
190 js.getComments().setColor(ccp.ccf1.c0.getSelectedItem());
191 js.getComments().setStyle(ccp.ccf1.c1.getSelectedItem());
192 js.getStrings().setColor(ccp.ccf2.c0.getSelectedItem());
193 js.getStrings().setStyle(ccp.ccf2.c1.getSelectedItem());
194 js.getKeywords().setColor(ccp.ccf3.c0.getSelectedItem());
195 js.getKeywords().setStyle(ccp.ccf3.c1.getSelectedItem());
196 if (tcp.c.getSelectedItem().equals("Java"))
197 js.setReservedWords(js.javaReservedWords);
198 else if (tcp.c.getSelectedItem().equals("C"))
199 js.setReservedWords(js.cReservedWords);
200 else
201 js.setReservedWords(js.cplusplusReservedWords);
202 }
203
204 private void init() {
205 fcp = new FileControlPanel();
206 tcp = new TargetControlPanel();
207 ccp = new ColorControlPanel(this);
208 bcp = new ButtonControlPanel(this);
209 statusBar = new Label("Please open a " + getExtention() + "file ");
210 setBackground(Color.white);
211
212 setLayout(new FlowLayout(FlowLayout.LEFT));
213 add(tcp); // Add a target control panel
214 add(fcp); // Add a file control panel
215 add(ccp); // Add a color control panel
216 add(bcp); // Add a button control panel
217 add(statusBar); // Add a status bat
218
219 }
220
221 private void writeHtml(final DataOutputStream dos, final JavaStream js)
222 throws IOException {
223 dos.writeBytes("<HTML>\n");
224 dos.writeBytes("<TITLE>" + fcp.fcf1.getText() + "</TITLE>\n");
225 dos.writeBytes(js.getMainText().pref + "\n");
226 dos.writeBytes("<H3>" + fcp.fcf1.getText() + "</H3>\n");
227 dos.writeBytes("<HR><PRE>\n");
228 js.convertToHtml();
229 dos.writeBytes(js.getMainText().postf + "</PRE></BODY>\n");
230 dos.writeBytes("<HR>\n");
231 }
232
233 private void convert(final String convertMode) {
234 try {
235 dos = new DataOutputStream(
236 new BufferedOutputStream(
237 new FileOutputStream(pathName + DIR_OUTPUT
238 + "/" + fcp.fcf2.getText())));
239 js = new JavaStream(
240 new DataInputStream(
241 new FileInputStream(pathName + fcp.fcf1.getText())
242 ), dos);
243 setModifier(js);
244 writeHtml(dos, js);
245 if (convertMode.equals(MI_CONVERTALL)) {
246 dos.writeBytes("<A HREF=\"index.gui.html\">[Back to Index]</A>\n");
247 }
248 final String programName = "This document was generated automatically" +
249 " by Kahindu.</A></H6>\nKahindu";
250 final String homePage = "www.DocJava.com/\">";
251 final String beginHeader = "<H6><A HREF=\"http://";
252 final String bottomMessage =
253 beginHeader + homePage + programName;
254 dos.writeBytes(bottomMessage);
255 dos.writeBytes("</HTML>\n");
256 js.close();
257 dos.close();
258 } catch (FileNotFoundException e) {
259 } catch (IOException e) {
260 }
261 }
262
263 protected void setEventMask(final boolean cond) {
264 if (cond) {
265 miOpenBuf = miOpen.isEnabled();
266 miSaveBuf = miSave.isEnabled();
267 miConvertAllBuf = miConvertAll.isEnabled();
268 miQuitBuf = miQuit.isEnabled();
269 miOpen.setEnabled(false);
270 miSave.setEnabled(false);
271 miConvertAll.setEnabled(false);
272 miQuit.setEnabled(false);
273 } else {
274 miOpen.setEnabled(miOpenBuf);
275 miSave.setEnabled(miSaveBuf);
276 miConvertAll.setEnabled(miConvertAllBuf);
277 miQuit.setEnabled(miQuitBuf);
278 }
279 }
280
281 protected void statusMsg(final String msgText) {
282 statusBar.setText(msgText);
283 }
284
285
286 }
287
288