1 50 package com.lowagie.tools.plugins; 51 52 import java.awt.event.ActionEvent; 53 import java.awt.event.ActionListener; 54 import java.awt.event.KeyEvent; 55 import java.io.File; 56 import java.io.IOException; 57 import java.io.PipedInputStream; 58 import java.io.PipedOutputStream; 59 import java.io.PrintStream; 60 import java.util.ArrayList; 61 import java.util.Iterator; 62 63 import javax.swing.JInternalFrame; 64 import javax.swing.JMenu; 65 import javax.swing.JMenuBar; 66 import javax.swing.JMenuItem; 67 import javax.swing.JOptionPane; 68 import javax.swing.JScrollPane; 69 import javax.swing.JTextArea; 70 71 import com.lowagie.tools.Executable; 72 import com.lowagie.tools.ToolMenuItems; 73 import com.lowagie.tools.arguments.ToolArgument; 74 75 78 public abstract class AbstractTool implements ToolMenuItems, ActionListener { 79 80 83 public class Console { 84 PipedInputStream piOut; 85 PipedInputStream piErr; 86 PipedOutputStream poOut; 87 PipedOutputStream poErr; 88 JTextArea textArea = new JTextArea(); 89 90 96 public Console(int columns, int rows) throws IOException { 97 piOut = new PipedInputStream(); 99 poOut = new PipedOutputStream(piOut); 100 System.setOut(new PrintStream(poOut, true)); 101 102 piErr = new PipedInputStream(); 104 poErr = new PipedOutputStream(piErr); 105 System.setErr(new PrintStream(poErr, true)); 106 107 textArea.setEditable(false); 109 textArea.setRows(rows); 110 textArea.setColumns(columns); 111 112 new ReaderThread(piOut).start(); 114 new ReaderThread(piErr).start(); 115 } 116 117 class ReaderThread extends Thread { 118 PipedInputStream pi; 119 120 ReaderThread(PipedInputStream pi) { 121 this.pi = pi; 122 } 123 124 127 public void run() { 128 final byte[] buf = new byte[1024]; 129 try { 130 while (true) { 131 final int len = pi.read(buf); 132 if (len == -1) { 133 break; 134 } 135 textArea.append(new String(buf, 0, len)); 136 textArea.setCaretPosition(textArea.getDocument().getLength()); 137 } 138 } catch (IOException e) { 139 } 140 } 141 } 142 } 143 144 145 146 protected JInternalFrame internalFrame = null; 147 148 protected ArrayList arguments = new ArrayList(); 149 150 protected int menuoptions = MENU_EXECUTE; 151 152 public static final int MENU_EXECUTE = 1; 153 154 public static final int MENU_EXECUTE_SHOW = 2; 155 156 public static final int MENU_EXECUTE_PRINT = 4; 157 158 public static final int MENU_EXECUTE_PRINT_SILENT = 8; 159 160 164 public void setArguments(ArrayList arguments) { 165 this.arguments = arguments; 166 } 167 168 172 public void setArguments(String[] args) { 173 int counter = 0; 174 ToolArgument argument; 175 for (Iterator i = arguments.iterator(); i.hasNext(); ) { 176 argument = (ToolArgument) i.next(); 177 if (args.length > counter) { 178 argument.setValue(args[counter]); 179 } 180 else { 181 break; 182 } 183 counter++; 184 } 185 } 186 187 191 public ArrayList getArguments() { 192 return arguments; 193 } 194 195 201 public Object getValue(String name) throws InstantiationException { 202 ToolArgument argument; 203 for (Iterator i = arguments.iterator(); i.hasNext(); ) { 204 argument = (ToolArgument) i.next(); 205 if (name.equals(argument.getName())) { 206 return argument.getArgument(); 207 } 208 } 209 return null; 210 } 211 212 216 public void setInternalFrame(JInternalFrame internalFrame) { 217 this.internalFrame = internalFrame; 218 } 219 220 224 public JInternalFrame getInternalFrame() { 225 if (internalFrame == null) { 226 createFrame(); 227 } 228 return internalFrame; 229 } 230 231 235 public JMenuBar getMenubar() { 236 JMenuBar menubar = new JMenuBar(); 237 JMenu tool = new JMenu(TOOL); 238 tool.setMnemonic(KeyEvent.VK_F); 239 JMenuItem usage = new JMenuItem(USAGE); 240 usage.setMnemonic(KeyEvent.VK_U); 241 usage.addActionListener(this); 242 tool.add(usage); 243 JMenuItem args = new JMenuItem(ARGUMENTS); 244 args.setMnemonic(KeyEvent.VK_A); 245 args.addActionListener(this); 246 tool.add(args); 247 if ((menuoptions & MENU_EXECUTE) > 0) { 248 JMenuItem execute = new JMenuItem(EXECUTE); 249 execute.setMnemonic(KeyEvent.VK_E); 250 execute.addActionListener(this); 251 tool.add(execute); 252 } 253 if ((menuoptions & MENU_EXECUTE_SHOW) > 0) { 254 JMenuItem execute = new JMenuItem(EXECUTESHOW); 255 execute.addActionListener(this); 256 tool.add(execute); 257 } 258 if ((menuoptions & MENU_EXECUTE_PRINT) > 0) { 259 JMenuItem execute = new JMenuItem(EXECUTEPRINT); 260 execute.addActionListener(this); 261 tool.add(execute); 262 } 263 if ((menuoptions & MENU_EXECUTE_PRINT_SILENT) > 0) { 264 JMenuItem execute = new JMenuItem(EXECUTEPRINTSILENT); 265 execute.addActionListener(this); 266 tool.add(execute); 267 } 268 JMenuItem close = new JMenuItem(CLOSE); 269 close.setMnemonic(KeyEvent.VK_C); 270 close.addActionListener(this); 271 tool.add(close); 272 menubar.add(tool); 273 if (arguments.size() > 0) { 274 JMenu params = new JMenu(ARGUMENTS); 275 tool.setMnemonic(KeyEvent.VK_T); 276 JMenuItem item; 277 ToolArgument argument; 278 for (Iterator i = arguments.iterator(); i.hasNext(); ) { 279 argument = (ToolArgument)i.next(); 280 item = new JMenuItem(argument.getName()); 281 item.setToolTipText(argument.getDescription()); 282 item.addActionListener(argument); 283 params.add(item); 284 } 285 menubar.add(params); 286 } 287 return menubar; 288 } 289 290 296 public JScrollPane getConsole(int columns, int rows) { 297 try { 298 Console console = new Console(columns, rows); 299 return new JScrollPane(console.textArea); 300 } 301 catch(IOException ioe) { 302 ioe.printStackTrace(); 303 return null; 304 } 305 } 306 307 311 public String getUsage() { 312 StringBuffer buf = new StringBuffer("java "); 313 buf.append(getClass().getName()); 314 ToolArgument argument; 315 for (Iterator i = arguments.iterator(); i.hasNext(); ) { 316 argument = (ToolArgument) i.next(); 317 buf.append(" "); 318 buf.append(argument.getName()); 319 } 320 buf.append("\n"); 321 for (Iterator i = arguments.iterator(); i.hasNext(); ) { 322 argument = (ToolArgument) i.next(); 323 buf.append(argument.getUsage()); 324 } 325 return buf.toString(); 326 } 327 328 332 public String getArgs() { 333 StringBuffer buf = new StringBuffer("Current arguments:\n"); 334 ToolArgument argument; 335 for (Iterator i = arguments.iterator(); i.hasNext(); ) { 336 argument = (ToolArgument) i.next(); 337 buf.append(" "); 338 buf.append(argument.getName()); 339 if (argument.getValue() == null) { 340 buf.append(" = null\n"); 341 } 342 else { 343 buf.append(" = '"); 344 buf.append(argument.getValue()); 345 buf.append("'\n"); 346 } 347 } 348 return buf.toString(); 349 } 350 351 354 public void actionPerformed(ActionEvent evt) { 355 if (CLOSE.equals(evt.getActionCommand())) { 356 internalFrame.dispose(); 357 } 358 if (USAGE.equals(evt.getActionCommand())) { 359 JOptionPane.showMessageDialog(internalFrame, getUsage()); 360 } 361 if (ARGUMENTS.equals(evt.getActionCommand())) { 362 JOptionPane.showMessageDialog(internalFrame, getArgs()); 363 } 364 if (EXECUTE.equals(evt.getActionCommand())) { 365 this.execute(); 366 } 367 if (EXECUTESHOW.equals(evt.getActionCommand())) { 368 this.execute(); 369 try { 370 Executable.openDocument(getDestPathPDF()); 371 } catch (Exception e) { 372 System.err.println(e.getMessage()); 373 } 374 } 375 if (EXECUTEPRINT.equals(evt.getActionCommand())) { 376 this.execute(); 377 try { 378 Executable.printDocument(getDestPathPDF()); 379 } catch (Exception e) { 380 System.err.println(e.getMessage()); 381 } 382 } 383 if (EXECUTEPRINTSILENT.equals(evt.getActionCommand())) { 384 this.execute(); 385 try { 386 Executable.printDocumentSilent(getDestPathPDF()); 387 } catch (Exception e) { 388 System.err.println(e.getMessage()); 389 } 390 } 391 } 392 393 398 protected abstract File getDestPathPDF() throws InstantiationException; 399 400 403 protected abstract void createFrame(); 404 405 408 public abstract void execute(); 409 410 414 public abstract void valueHasChanged(ToolArgument arg); 415 } | Popular Tags |