KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > blocks > scalable > webmonitor > WebMonitorGui


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47
48  /*=========================================================================================
49    For instructions on how to run this sample please refer to the file
50    sample\blocks\scalable\webmonitor\Readme.txt under the MantaRay installation directory.
51 ==========================================================================================*/

52
53
54 package sample.blocks.scalable.webmonitor;
55
56 import java.awt.BorderLayout JavaDoc;
57 import java.awt.Dimension JavaDoc;
58 import java.awt.GridLayout JavaDoc;
59 import java.awt.event.ActionEvent JavaDoc;
60 import java.awt.event.ActionListener JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63
64 import javax.swing.BorderFactory JavaDoc;
65 import javax.swing.JButton JavaDoc;
66 import javax.swing.JFrame JavaDoc;
67 import javax.swing.JPanel JavaDoc;
68 import javax.swing.JScrollPane JavaDoc;
69 import javax.swing.JTable JavaDoc;
70 import javax.swing.JTextField JavaDoc;
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 /**
77  * @author Amir Shevat
78  * this is the GUI part of the two part, scalable example application
79  */

80 public class WebMonitorGui implements ActionListener JavaDoc, ScalableHandler{
81     // the input text of the url to be checked
82
JTextField JavaDoc urlInput;
83     // the model of the displayed result
84
ResultTable model = new ResultTable();
85
86     /**
87      * the out going stage to the engine
88      */

89     public ScalableStage engineStage;
90     /**
91      * the inbound dispatcher from the engine
92      */

93     public ScalableDispatcher guiDispatcher;
94
95     /**
96      * The GUI gets a reference to the incoming dispatcher and the out going stage,
97      * the distributed parameter is passed to the factory of these objects
98      * @param distributed if this boolean true then this sample is running in a distributed environment and the engine is in a different VM
99      */

100     public WebMonitorGui(boolean distributed){
101
102         // get the stage from the factory
103
engineStage = ScalableFactory.getStage("engine", distributed);
104         // get the dispatcher from the factory
105
guiDispatcher = ScalableFactory.getDispatcher("gui", distributed);
106         // register this object has handler to all incoming events
107
guiDispatcher.addHandler(this);
108     }
109
110     /**
111      * starts the SWING GUI
112      *
113      */

114     public void startGUI(){
115 // Make sure we have nice window decorations.
116
JFrame.setDefaultLookAndFeelDecorated(true);
117
118         //Create and set up the window.
119
JFrame JavaDoc frame = new JFrame JavaDoc("WebSiteMonitor");
120         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
121
122         JTable JavaDoc result = new JTable JavaDoc(model);
123         result.setPreferredScrollableViewportSize(new Dimension JavaDoc(250, 50));
124         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(result);
125
126         JPanel JavaDoc imputPan = new JPanel JavaDoc(new GridLayout JavaDoc(0,2));
127         imputPan.setSize(100, 10);
128         JButton JavaDoc button = new JButton JavaDoc("Add URL to monitor");
129         button.addActionListener(this);
130         button.setMaximumSize(new Dimension JavaDoc(50,25));
131
132         urlInput = new JTextField JavaDoc(20);
133         urlInput.addActionListener(this);
134         urlInput.setMaximumSize(new Dimension JavaDoc(50,25));
135
136
137         imputPan.add(urlInput);
138         imputPan.add(button);
139
140         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
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, //top
147
30, //left
148
10, //bottom
149
30) //right
150
);
151
152
153         frame.setContentPane(panel);
154
155
156         //Display the window.
157
frame.pack();
158         frame.setVisible(true);
159
160
161         JFrame.setDefaultLookAndFeelDecorated(true);
162
163
164         //Display the window.
165
frame.pack();
166         frame.setVisible(true);
167
168
169     }//startGUI
170

171     /**
172      * Called when there is an input from the user
173      * sends the URL input from the user to the engine via the stage
174      */

175     public void actionPerformed(ActionEvent JavaDoc e) {
176         engineStage.queue(urlInput.getText());
177
178     }
179
180     /**
181      * This is an implementation method of ScalableHandler interface
182      * Called by the dispatcher when an event (results) was received from the engine,
183      * updates the display
184      */

185     public void handle(Object JavaDoc event) {
186         HashMap JavaDoc result = (HashMap JavaDoc) event;
187         Iterator JavaDoc urls = result.keySet().iterator();
188         while(urls.hasNext()){
189             String JavaDoc url = (String JavaDoc) urls.next();
190             String JavaDoc status = (String JavaDoc) result.get(url);
191             model.updateURLStatus(url,status);
192         }
193     }
194
195     /**
196      * first argument of this program is "distributed" for a distributed sample or any other string for an in memory one
197      * @param args "distributed" for a distributed sample or any other string for an in memory one
198      * @throws Exception
199      */

200     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
201         boolean distributed = args[0].equalsIgnoreCase("distributed");
202         // if not distributed then start the engine in the same JVM
203
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