1 22 23 import java.io.*; 24 import java.awt.*; 25 import java.awt.event.*; 26 import javax.swing.*; 27 import javax.swing.border.*; 28 29 import org.netbeans.lib.terminalemulator.*; 30 31 45 46 public class BuildTool extends JFrame implements ActionListener { 47 48 private ActiveTerm term; 49 private JButton b_javac; 50 private JTextField t_files; 51 52 private Object sync = new Object (); 53 54 57 private Image glyph(int dim) { 58 int xpoints[] = {1, 1, dim-1, dim-1}; 59 int ypoints[] = {1, dim-1, dim-1, 1}; 60 Polygon square = new Polygon(xpoints, ypoints, 4); 61 Image i = term.getTopLevelAncestor().createImage(dim, dim); 62 Graphics g = i.getGraphics(); 63 g.setColor(Color.white); 64 g.fillRect(0, 0, dim, dim); 65 g.setColor(Color.red); 66 g.fillPolygon(square); 67 g.setColor(Color.black); 68 g.drawPolygon(square); 69 g.dispose(); 70 return i; 71 } 72 73 private void setup_gui() { 74 75 addWindowListener(new WindowAdapter() { 77 public void windowClosing(WindowEvent e) { 78 System.exit(0); 79 } 80 }); 81 82 term = new ActiveTerm(); 83 term.setFont(new Font("Helvetica", Font.PLAIN, 10)); 84 85 term.setBackground(Color.white); 86 term.setGlyphGutterWidth(20); 87 term.setClickToType(false); 88 term.setHighlightColor(Color.orange); 89 90 term.setActionListener(new ActiveTermListener() { 91 public void action(ActiveRegion r, InputEvent e) { 92 if (r.isLink()) { 93 String text = term.textWithin(r.begin, r.end); 94 JOptionPane.showMessageDialog(term, 95 "Get the editor to show " + text, 96 "Syntax error", 97 JOptionPane.DEFAULT_OPTION); 98 } 99 } 100 } ); 101 102 JToolBar toolbar = new JToolBar(); 103 toolbar.setMargin(new Insets(5, 5, 5, 5)); 104 toolbar.setFloatable(false); 105 b_javac = new JButton("Build"); 106 b_javac.addActionListener(this); 107 toolbar.add(b_javac); 108 109 toolbar.addSeparator(); 110 111 t_files = new JTextField(); 112 t_files.setColumns(10); 113 t_files.setText("<Enter java file names and press Build>"); 114 toolbar.add(t_files); 115 116 getContentPane().add(toolbar, BorderLayout.NORTH); 117 getContentPane().add(term, BorderLayout.CENTER); 118 119 pack(); 120 } 121 122 public void setup() { 123 126 Dimension glyph_cell_size = term.getGlyphCellSize(); 127 term.setGlyphImage(48, glyph(glyph_cell_size.height)); 128 } 129 130 131 public void actionPerformed(ActionEvent e) { 132 133 if (e.getSource() == b_javac) { 134 String what = t_files.getText(); 135 Thread compiler = new Compiler ("javac", what); 136 compiler.start(); 137 } 138 } 139 140 class Compiler extends Thread { 141 String args; 142 String command; 143 Compiler(String command, String args) { 144 this.command = command; 145 this.args = args; 146 } 147 public void run() { 148 run_compile(command, args); 149 } 150 } 151 152 private void run_compile(String cmd, String files) { 153 154 term.clearHistory(); 155 156 term.setAnchored(true); 157 158 String command = cmd + " " + files; 160 term.setAttribute(1); term.appendText(command + "\n", true); 162 term.setAttribute(0); 164 Process proc = null; 165 try { 166 proc = Runtime.getRuntime().exec(command, null, null); 167 } catch (Exception x) { 168 x.printStackTrace(); 169 System.exit(1); 170 } 171 172 173 int nreaders = 0; 174 175 InputStream pout = proc.getInputStream(); 176 Reader out_R = new Reader(pout, this); 177 nreaders++; 178 out_R.start(); 179 180 InputStream perr = proc.getErrorStream(); 181 if (perr != null) { 182 Reader err_R = new Reader(perr, this); 183 nreaders++; 184 err_R.start(); 185 } 186 187 while (nreaders > 0) { 189 try { 190 synchronized(sync) { 191 sync.wait(); 192 } 193 } catch (InterruptedException x) { 194 System.out.println("Compiler wait interrupted"); 195 continue; 196 } 197 nreaders--; 198 } 199 200 term.endRegion(); 201 202 term.setAttribute(1); term.appendText("No more output\n", true); 204 205 try { 207 proc.waitFor(); 208 } catch (Exception x) {} 209 210 term.appendText("Command done\n", true); 211 term.setAttribute(0); } 213 214 215 219 220 class Reader extends Thread { 221 BufferedReader source; 222 BuildTool sink; 223 224 Reader(InputStream src, BuildTool sink) { 225 this.source = new BufferedReader(new InputStreamReader(src)); 226 this.sink = sink; 227 } 228 229 public void run() { 230 while (true) { 231 String line = null; 232 try { 233 line = source.readLine(); 234 } catch (IOException x) { 235 x.printStackTrace(); 236 sink.done(); 237 break; 238 } 239 240 if (line == null) { 241 sink.done(); 242 break; 243 } else { 244 sink.process_line(line); 245 } 246 } 247 } 248 } 249 250 private ActiveRegion region = null; 251 252 synchronized private void process_line(String line) { 253 255 int jx = line.indexOf(".java:"); 256 257 if (jx != -1) { 258 term.setGlyph(48, 0); 260 261 if (region != null) 262 term.endRegion(); 263 region = term.beginRegion(false); 264 region.setFeedbackEnabled(true); 265 region.setSelectable(true); 266 267 int cx1 = line.indexOf(":"); int cx2 = line.indexOf(":", cx1+1); 270 String srcloc = line.substring(0, cx2); 271 ActiveRegion link = term.beginRegion(true); 272 link.setLink(true); 273 link.setFeedbackViaParent(true); 274 term.appendText(srcloc, false); 275 term.endRegion(); 276 277 String rest = line.substring(cx2); 278 term.appendText(rest, true); 279 } else { 280 term.appendText(line, true); 281 } 282 283 term.putChar((char)10); 284 term.putChar((char)13); 285 } 286 287 288 public void done() { 289 synchronized(sync) { 291 sync.notify(); 292 } 293 } 294 295 public static void main(String [] args) { 296 BuildTool app = new BuildTool(); 297 app.setup_gui(); 298 app.setVisible(true); 299 app.setup(); 300 } 301 } 302
| Popular Tags
|