1 23 package org.enhydra.kelp.common.swing; 24 25 import org.enhydra.kelp.common.Constants; 27 import org.enhydra.kelp.common.event.WriteEvent; 28 import org.enhydra.kelp.common.event.WriteListener; 29 import org.enhydra.kelp.common.node.OtterProject; 30 31 import org.enhydra.tool.common.PathHandle; 33 34 import javax.swing.*; 36 import javax.swing.text.BadLocationException ; 37 import java.awt.*; 38 import java.awt.event.*; 39 import java.beans.Beans ; 40 import java.io.File ; 41 import java.io.FileWriter ; 42 import java.lang.ref.WeakReference ; 43 import java.util.ResourceBundle ; 44 45 public class OutputPanel extends JPanel implements WriteListener { 47 48 public static ResourceBundle res = 50 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private JScrollPane scrollOutput; 52 private JTextArea textAreaOutput; 53 private BorderLayout layoutMain; 54 private WeakReference projectRef; 55 private int waitCount = 0; 56 57 61 public OutputPanel() { 62 try { 63 jbInit(); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 69 public void clearAll() { 70 clearOutput(); 71 if (projectRef != null) { 72 projectRef.clear(); 73 projectRef = null; 74 } 75 removeAll(); 76 } 77 78 84 public void onWrite(WriteEvent event) { 85 String out = event.getString(); 86 87 if (event.getType() == WriteEvent.CLEAR) { 88 clearOutput(); 89 } else { 90 if (File.separatorChar == '\\') { 91 92 if (out.indexOf(":\\") > 0) { 94 out = out.replace('\\', '/'); 95 } 96 } 97 addOutput(out); 98 } 99 } 100 101 public void clearOutput() { 102 OutputClear clearRun = null; 103 104 clearRun = new OutputClear(textAreaOutput, scrollOutput); 105 try { 106 if (SwingUtilities.isEventDispatchThread()) { 107 clearRun.run(); 108 } else { 109 SwingUtilities.invokeAndWait(clearRun); 110 } 111 } catch (Exception e) { 112 e.printStackTrace(); 113 } 114 } 115 116 public void scrollToBottom() { 117 OutputScroll runSwing = null; 118 119 runSwing = new OutputScroll(scrollOutput); 120 try { 121 if (SwingUtilities.isEventDispatchThread()) { 122 runSwing.run(); 123 } else { 124 SwingUtilities.invokeAndWait(runSwing); 125 } 126 } catch (Exception e) { 127 e.printStackTrace(); 128 } 129 try { 130 Thread.sleep(500); 131 } catch (InterruptedException e) { 132 133 } 135 } 136 137 public OtterProject getProject() { 138 OtterProject project = null; 139 140 if (projectRef != null) { 141 project = (OtterProject) projectRef.get(); 142 } 143 return project; 144 } 145 146 public void setProject(OtterProject project) { 147 projectRef = new WeakReference (project); 148 } 149 150 public int getRows() { 151 return textAreaOutput.getRows(); 152 } 153 154 public void setRows(int i) { 155 textAreaOutput.setRows(i); 156 } 157 158 private void addOutput(final String output) { 162 OutputAdd adder = null; 163 164 adder = new OutputAdd(textAreaOutput, scrollOutput, output); 165 try { 166 if (SwingUtilities.isEventDispatchThread()) { 167 adder.run(); 168 } else { 169 waitCount++; 170 if (waitCount == 10) { 171 waitCount = 0; 172 SwingUtilities.invokeAndWait(adder); 173 } else { 174 SwingUtilities.invokeLater(adder); 175 } 176 } 177 } catch (Exception e) { 178 e.printStackTrace(System.err); 179 } 180 projectLog(output); 181 } 182 183 private void projectLog(final String output) { 184 PathHandle log = null; 185 186 if (getProject() != null) { 187 log = 188 PathHandle.createPathHandle(getProject().getOutputFilename()); 189 if (log.hasExtension("txt") && log.getFile().canWrite()) { 190 try { 191 FileWriter writer = null; 192 193 writer = new FileWriter (log.getPath(), true); 194 writer.write(output); writer.close(); 196 } catch (java.io.IOException e) { 197 e.printStackTrace(System.err); 198 } 199 } 200 } 201 } 202 203 209 private void jbInit() throws Exception { 210 textAreaOutput = 211 (JTextArea) Beans.instantiate(getClass().getClassLoader(), 212 JTextArea.class.getName()); 213 textAreaOutput.setEditable(false); 214 textAreaOutput.setRows(15); 215 scrollOutput = new JScrollPane(textAreaOutput); 216 layoutMain = 217 (BorderLayout) Beans.instantiate(getClass().getClassLoader(), 218 BorderLayout.class.getName()); 219 this.setLayout(layoutMain); 220 this.add(scrollOutput, BorderLayout.CENTER); 221 } 222 223 private class OutputAdd implements Runnable { 225 private JTextArea textArea = null; 226 private JScrollPane scroll = null; 227 private String out = null; 228 229 protected OutputAdd(JTextArea ta, JScrollPane sc, String o) { 230 textArea = ta; 231 scroll = sc; 232 out = o; 233 } 234 235 synchronized public void run() { 236 int start = 0; 237 int end = 0; 238 239 if (textArea.getLineCount() > 2000) { 240 try { 241 start = textArea.getLineStartOffset(0); 242 end = textArea.getLineEndOffset(0); 243 } catch (BadLocationException e) { 244 start = 0; 245 end = 0; 246 } 247 textArea.replaceRange(null, start, end); 248 } 249 textArea.append(out); 250 scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum()); 251 textArea.repaint(); 252 repaint(); 253 } 254 255 } 256 257 private class OutputClear implements Runnable { 259 private JTextArea textArea = null; 260 private JScrollPane scroll = null; 261 262 protected OutputClear(JTextArea ta, JScrollPane sc) { 263 textArea = ta; 264 scroll = sc; 265 } 266 267 synchronized public void run() { 268 textArea.setText(new String ()); 269 scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum()); 270 textArea.repaint(); 271 repaint(); 272 } 273 274 } 275 276 private class OutputScroll implements Runnable { 278 private JScrollPane scroll = null; 279 280 protected OutputScroll(JScrollPane sc) { 281 scroll = sc; 282 } 283 284 synchronized public void run() { 285 scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum()); 286 } 287 288 } 289 } 290 | Popular Tags |