1 31 package org.objectweb.proactive.examples; 32 33 import java.text.*; 34 import javax.swing.*; 35 import javax.swing.text.*; 36 import javax.swing.border.*; 37 38 import org.apache.log4j.Logger; 39 40 49 50 public abstract class StandardFrame extends javax.swing.JFrame { 51 52 static Logger logger = Logger.getLogger(StandardFrame.class.getName()); 53 54 protected final static int MESSAGE_ZONE_HEIGHT = 250; 55 protected String name; 56 protected int width; 57 protected int height; 58 protected DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); 59 protected transient JTextPane messageArea; 60 protected transient Style regularStyle; 61 protected javax.swing.JSplitPane verticalSplitPane; 62 63 64 68 public StandardFrame(String name, int width, int height) { 69 super(name); 70 this.name = name; 71 init(width, height); 72 } 73 74 public StandardFrame(String name) { 75 super(name); 76 this.name = name; 77 } 78 79 public StandardFrame(){} 80 81 85 public void receiveMessage(final String message) { 86 final String date = dateFormat.format(new java.util.Date ()); 87 final String threadName = Thread.currentThread().getName(); 88 SwingUtilities.invokeLater(new Runnable () { 89 public void run() { 90 Document doc = messageArea.getDocument(); 91 try { 92 doc.insertString(doc.getLength(), date + " (" + threadName 93 + ")\n => " + message + "\n", regularStyle); 94 } 95 catch (Exception e) { 96 logger.error("Couldn't insert initial text."); 97 } 98 } 99 }); 100 } 101 102 103 public void receiveMessage(final String message, final java.awt.Color color) { 105 final String date = dateFormat.format(new java.util.Date ()); 106 final String threadName = Thread.currentThread().getName(); 107 final Style s = messageArea.addStyle("colored", regularStyle); 108 StyleConstants.setForeground(s, color); 109 javax.swing.SwingUtilities.invokeLater(new Runnable () { 110 public void run() { 111 Document doc = messageArea.getDocument(); 112 try { 113 doc.insertString(doc.getLength(), date + " (" + threadName + ")\n => ", 114 regularStyle); 115 doc.insertString(doc.getLength(), message + "\n", s); 116 } 117 catch (Exception e) { 118 logger.error("Couldn't insert initial text."); 119 } 120 } 121 }); 122 } 123 124 125 126 130 protected void init(int width, int height) { 131 initFrame(name, width, height); 132 setVisible(true); 133 start(); 134 } 135 136 137 protected abstract void start(); 138 139 140 protected abstract javax.swing.JPanel createRootPanel(); 141 142 143 protected javax.swing.JPanel createMessageZonePanel(final javax.swing.JTextPane area) { 144 Style styleDef = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 145 regularStyle = area.addStyle("regular", styleDef); 146 area.setFont(new java.awt.Font ("SansSerif", java.awt.Font.PLAIN, 10)); 147 148 JPanel panel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 149 javax.swing.border.TitledBorder border = new javax.swing.border.TitledBorder ("Messages"); 150 panel.setBorder(border); 151 javax.swing.JPanel topPanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 152 javax.swing.JButton clearLogButton = new javax.swing.JButton ("clear messages"); 154 clearLogButton.addActionListener(new java.awt.event.ActionListener () { 155 public void actionPerformed(java.awt.event.ActionEvent e) { 156 area.setText(""); 157 } 158 }); 159 topPanel.add(clearLogButton, java.awt.BorderLayout.WEST); 160 panel.add(topPanel, java.awt.BorderLayout.NORTH); 161 javax.swing.JScrollPane pane = new javax.swing.JScrollPane (area); 162 panel.add(pane, java.awt.BorderLayout.CENTER); 163 return panel; 164 } 165 166 167 171 private void initFrame(String name, int width, int height) { 172 java.awt.Container c = getContentPane(); 173 c.setLayout(new java.awt.GridLayout (1, 1)); 174 175 JPanel topPanel = new JPanel(new java.awt.GridLayout (1, 1)); 177 TitledBorder border = new javax.swing.border.TitledBorder (name); 178 topPanel.setBorder(border); 179 topPanel.add(createRootPanel()); 180 181 messageArea = new javax.swing.JTextPane (); 183 messageArea.setEditable(false); 184 javax.swing.JPanel bottomPanel = createMessageZonePanel(messageArea); 185 186 verticalSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 188 verticalSplitPane.setDividerLocation(height); 189 verticalSplitPane.setTopComponent(topPanel); 190 verticalSplitPane.setBottomComponent(bottomPanel); 191 c.add(verticalSplitPane); 192 193 setSize(width, height + MESSAGE_ZONE_HEIGHT); 194 setLocation(30, 30); 195 addWindowListener(new java.awt.event.WindowAdapter () { 196 public void windowClosing(java.awt.event.WindowEvent e) { 197 System.exit(0); 198 } 199 }); 200 } 201 202 203 } 204 | Popular Tags |