Changes

Jump to: navigation, search

Teams Winter 2011/team2/lab2

5,531 bytes added, 23:18, 17 February 2011
no edit summary
}
</pre>
**Save and close both files and now open the manifest file
*Click on the dependencies tab then click on add in the "import packages" panel.
*In the search field, enter "ecl", then click on "ecl.team2.lab2.weather", and afterwords click okay
==Creating and Implementing the Client Bundle==
*Follow the same steps for creating a new plug-in projects but instead supply the name "ecl.team2.lab2.weathersystem.client"
*Remove all contents within the src->current package and add a class file called "Activator" and another class file called "
*Copy and paste the following code to their respective file
'''Activator'''
<pre>
package ecl.team2.lab2.weathersystem.client;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
 
import ecl.team2.lab2.weather.WeatherSystem;
 
public class Activator implements BundleActivator {
 
private static BundleContext context;
private WeatherSystem sws;
 
static BundleContext getContext() {
return context;
}
 
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
ServiceReference reference = context.getServiceReference(WeatherSystem.class.getName());
if(reference != null){
sws = (WeatherSystem) context.getService(reference);
if(sws != null) {
WeatherDialog.runDialog(sws);
context.ungetService(reference);
} else
System.err.println("The Weather Station is down !!!");
} else
System.err.println("The Weather Station can not be found !!!");
}
 
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
System.err.println("Client Not Interested !");
}
}
</pre>
'''WeatherDialog'''
<pre>
package ecl.team2.lab2.weathersystem.client;
import javax.swing.*;
 
import ecl.team2.lab2.weather.WeatherSystem;
 
import java.awt.*;
import java.awt.event.*;
 
 
public class WeatherDialog extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton btnGo;
private JComboBox combo;
private JLabel labelTitle;
private JLabel labelTemperature;
private JLabel labelRain;
private JLabel labelSnow;
private JLabel labelWindDirection;
private JLabel labelWindSpeed;
private WeatherSystem sws;
String[] cities = {"Toronto","New York","Calgary","Ottawa","Vancouver",
"Regina","Winnipeg","St. John","Fredericton",
"Halifax","Quebec City", "Whitehorse","Yellowknife"
};
 
public WeatherDialog(){
panel = new JPanel();
panel.setPreferredSize(new Dimension(200,200));
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
combo = new JComboBox();
for(int i=0;i<cities.length;i++){
combo.addItem(cities[i]);
}
panel.add(combo);
btnGo = new JButton("Go");
panel.add(btnGo);
btnGo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String chosencity = (String)combo.getSelectedItem();
ShowWeather(chosencity);
}
}
);
this.labelTitle = new JLabel();
this.labelTemperature = new JLabel();
this.labelRain = new JLabel();
this.labelSnow = new JLabel();
this.labelWindDirection = new JLabel();
this.labelWindSpeed = new JLabel();
panel.add(labelTitle);panel.add(labelTemperature);panel.add(labelRain);
panel.add(labelSnow);panel.add(labelWindDirection);panel.add(labelWindSpeed);
this.setTitle("Todays Weather");
// Display the window.
this.setContentPane(panel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLocationRelativeTo(null);
}
public void ShowWeather(String city){
try {
float temp = sws.getWeather(city).getTempInCelcius();
float rain =sws.getWeather(city).getRainInMM();
float snow = sws.getWeather(city).getSnowInMM();
char direction = sws.getWeather(city).getWindDirection();
float speed = sws.getWeather(city).getWindspeedInKM();
this.labelTitle.setText("Todays weather in "+ city);
this.labelTemperature.setText("Temperature: "+ String.format("%.2f", temp) + " Celcius");
this.labelRain.setText("Rain: " + String.format("%.2f", rain) + " mm");
this.labelSnow.setText("Snow: " + String.format("%.2f", snow) + " mm");
this.labelWindDirection.setText("Wind Direction: " + direction);
this.labelWindSpeed.setText("Speed: " + String.format("%.2f", speed) + " km/hr");
}
catch (Exception ex){
System.err.println(ex.getMessage());
}
}
private static void createAndShowGUI(WeatherSystem ws) {
WeatherDialog frame = new WeatherDialog();
frame.sws = ws;
}
public static void runDialog(final WeatherSystem ws) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(ws);
}
});
}
}
</pre>
*Save and close both files and now open the manifest file
*Click on the dependencies tab then click on add in the "import packages" panel.
*In the search field, enter "ecl", then click on "ecl.team2.lab2.weather", and afterwords click okay
=INCOMPLETE, WILL FINISH BY TOMORROW=
1
edit

Navigation menu