1 46 47 48 52 53 54 package sample.blocks.scalable.webmonitor; 55 56 import java.awt.BorderLayout ; 57 import java.awt.Dimension ; 58 import java.awt.GridLayout ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 64 import javax.swing.BorderFactory ; 65 import javax.swing.JButton ; 66 import javax.swing.JFrame ; 67 import javax.swing.JPanel ; 68 import javax.swing.JScrollPane ; 69 import javax.swing.JTable ; 70 import javax.swing.JTextField ; 71 72 import org.mr.api.blocks.ScalableDispatcher; 73 import org.mr.api.blocks.ScalableFactory; 74 import org.mr.api.blocks.ScalableHandler; 75 import org.mr.api.blocks.ScalableStage; 76 80 public class WebMonitorGui implements ActionListener , ScalableHandler{ 81 JTextField urlInput; 83 ResultTable model = new ResultTable(); 85 86 89 public ScalableStage engineStage; 90 93 public ScalableDispatcher guiDispatcher; 94 95 100 public WebMonitorGui(boolean distributed){ 101 102 engineStage = ScalableFactory.getStage("engine", distributed); 104 guiDispatcher = ScalableFactory.getDispatcher("gui", distributed); 106 guiDispatcher.addHandler(this); 108 } 109 110 114 public void startGUI(){ 115 JFrame.setDefaultLookAndFeelDecorated(true); 117 118 JFrame frame = new JFrame ("WebSiteMonitor"); 120 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 121 122 JTable result = new JTable (model); 123 result.setPreferredScrollableViewportSize(new Dimension (250, 50)); 124 JScrollPane scrollPane = new JScrollPane (result); 125 126 JPanel imputPan = new JPanel (new GridLayout (0,2)); 127 imputPan.setSize(100, 10); 128 JButton button = new JButton ("Add URL to monitor"); 129 button.addActionListener(this); 130 button.setMaximumSize(new Dimension (50,25)); 131 132 urlInput = new JTextField (20); 133 urlInput.addActionListener(this); 134 urlInput.setMaximumSize(new Dimension (50,25)); 135 136 137 imputPan.add(urlInput); 138 imputPan.add(button); 139 140 JPanel panel = new JPanel (new BorderLayout ()); 141 panel.add(imputPan,BorderLayout.NORTH); 142 panel.add(scrollPane,BorderLayout.SOUTH); 143 144 panel.setBorder(BorderFactory.createEmptyBorder()); 145 panel.setBorder(BorderFactory.createEmptyBorder( 146 10, 30, 10, 30) ); 151 152 153 frame.setContentPane(panel); 154 155 156 frame.pack(); 158 frame.setVisible(true); 159 160 161 JFrame.setDefaultLookAndFeelDecorated(true); 162 163 164 frame.pack(); 166 frame.setVisible(true); 167 168 169 } 171 175 public void actionPerformed(ActionEvent e) { 176 engineStage.queue(urlInput.getText()); 177 178 } 179 180 185 public void handle(Object event) { 186 HashMap result = (HashMap ) event; 187 Iterator urls = result.keySet().iterator(); 188 while(urls.hasNext()){ 189 String url = (String ) urls.next(); 190 String status = (String ) result.get(url); 191 model.updateURLStatus(url,status); 192 } 193 } 194 195 200 public static void main(String [] args) throws Exception { 201 boolean distributed = args[0].equalsIgnoreCase("distributed"); 202 if(!distributed){ 204 WebMonitorEngine engine = new WebMonitorEngine(distributed); 205 engine.start(); 206 } 207 208 WebMonitorGui checker = new WebMonitorGui(distributed); 209 checker.startGUI(); 210 } 211 } 212 | Popular Tags |