Changes

Jump to: navigation, search

Teams Winter 2011/team2/lab3

5,442 bytes added, 01:43, 23 March 2011
no edit summary
<source lang=java>
package ecl.team2.lab3.weathermodel;
 
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Random;
public class Weather {
private float windspeedInKM;
private char windDirection;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public Weather()
{
}
public Weather(String cit)
{
city=cit;
Random randomGenerator = new Random();
tempInCelcius = randomGenerator.nextFloat()+35;
rainInMM = randomGenerator.nextFloat()+10;
snowInMM = randomGenerator.nextFloat()+10;
windspeedInKM = randomGenerator.nextFloat()+50;
int r = randomGenerator.nextInt(4);
switch(r){
case 0:
windDirection = 'E';
break;
case 1:
windDirection = 'W';
break;
case 2:
windDirection = 'N';
break;
case 3:
windDirection = 'S';
break;
}
}
public Weather(String pcity, float ptemp, float prain, float psnow, float pwspeed, char pwdirection){
public void setWindDirection(char windDirection) {
propertyChangeSupport.firePropertyChange("", this.windDirection, this.windDirection = windDirection);
}
public void setWindspeedInKM(float windspeedInKM) {
propertyChangeSupport.firePropertyChange("", this.windspeedInKM, this.windspeedInKM = windspeedInKM);
}
public void setSnowInMM(float snowInMM) {
propertyChangeSupport.firePropertyChange("", this.snowInMM, this.snowInMM = snowInMM);
}
public void setRainInMM(float rainInMM) {
propertyChangeSupport.firePropertyChange("", this.rainInMM, this.rainInMM = rainInMM);
}
public void setTempInCelcius(float tempInCelcius) {
propertyChangeSupport.firePropertyChange("", this.tempInCelcius, this.tempInCelcius = tempInCelcius);
}
public void setCity(String city) {
propertyChangeSupport.firePropertyChange("", this.city, this.city = city);
}
public String getCity() {
return city;
}
public void addPropertyChangeListener(String name, PropertyChangeListener listener)
{
propertyChangeSupport.addPropertyChangeListener(name,listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener)
{
propertyChangeSupport.addPropertyChangeListener(listener);
}
}
return found;
}
public void addCity(String nCity)
{
Random randomGenerator = new Random();
float temp = randomGenerator.nextFloat()+35;
float rain = randomGenerator.nextFloat()+10;
float snow = randomGenerator.nextFloat()+10;
float windspeed = randomGenerator.nextFloat()+50;
char winddirection = '.';
int r = randomGenerator.nextInt(4);
switch(r){
case 0:
winddirection = 'E';
break;
case 1:
winddirection = 'W';
break;
case 2:
winddirection = 'N';
break;
case 3:
winddirection = 'S';
break;
}
Weather w = new Weather(nCity, temp, rain, snow, windspeed, winddirection);
this.weathers.add(w);
System.out.println(w.toString());
}
public void removeCity(String oCity)
{
this.weathers.remove(oCity);
}
*These classes will be used later on into the tutorial.
==Creating and Using Commands/Views=====Commands===
*Commands are in the most laymen terms, actions. What we mean by action is that we are insisting through some physical representation, whether it be a button or plain-typed text, we are calling an event. That event can be to exit the program or even perform a feature within the application.
*Let's create our first menu for the menu bar that will contain a command to exit the application.
[[File:FWindowpop3T2.png]]
*Now click on "File" within the menu bar and click "Exit", this will close the program based on the small application we created for that command.
'''Pre-emptive work for later on'''*Another way Given that you now have context of how to create a command, we will now progress to creating commands and classes on the fly is is by doing it through the manifest file. Lets do this other method for that will be used later within our print weather commandapplication. Follow the same directions previously of creating a command and enter Please create the following information within commands along with the "Extension Element Details" view.required items:[[File:CommT2.png]]*Now click on the "defaultHandler" link, this should pop up java class creation wizard.'''AddCityHandler'''[[File:Comm2T2.png]]For Commmand*Once the "New Java Class" Window is active click on highlighted button below.<pre>[[Fileid:NjwinT2.png]]*This should pop up another window containing every package (including default and empty) available within the project. Double-click "ecl.team2.lab3.commands", this should enter automatically to the text box that dictates where you want the class to be storedrcpExample.AddCity[[Filename:Njwin2T2.png]]Add City*After completing the previous step, you will be back on the previous window (New Java Class)defaultHandler: ecl. Since we require this java class to be used for a command, we must have this class inherit the AbstractHandler classteam2. We do this by clicking the "Browselab3.commands.." button under the "Superclass" category.AddCityHandler</pre> For Menu Command<pre>*A window to select your superclass appearscommandId: ecl. In the textbox, please enter "AbstractHandler"team2. The textbox provides a means of searching for a particular class. From the search results, select the "AbstractHandler" that's located in "orglab3.eclipse.core.commands". The reason for selecting this specific class is because it is the most up-to-date standard for creating handlersrcpExample.AddCity[[Filelabel:ScT2.png]]Add City*To ensure we include all methods necessary for our handler to function properly, we will now add an interface called "IHandler". Click the button "Add" near "Interfaces" category. The process of adding an interface is very similar to adding tooltip: Adds a superclass except we must highlight the interface we want, click add (clicking add allows you city to create a list of interfaces to included in your application), and then click okay. After searching for "IHandler", ensure the interface is located in "org.eclipse.core.commands".*You are now back to your original window. As you may have noticed, you still cannot create the class. The reason is incredibly quite simple, you haven't named the class yet. Next to name, enter the following in the textbox "PrintWeather". You should have something that looks like the image below:</pre> [[File:Classl3-2T2For AddCityHandler.png]]*The java file is now in your editor and will contain something like this:
<source lang=java>
package ecl.team2.lab3.commands;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
//This will allow us to edit the content using the editor windowimport ecl.team2.lab3.editor.WeatherEditor;import ecl.team2.lab3.editor.WeatherInput; public class PrintWeather AddCityHandler extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); IWorkbenchPage page = window.getActivePage(); WeatherInput input = new WeatherInput("-1"); try { page.openEditor(input, WeatherEditor.ID); } catch (PartInitException e) { System.out.println(e.getMessage()); } return null; } }</source><br/ TODO Auto-generated method stub><br/> '''RemoveCityHandler'''For Commmand<pre>id: ecl.team2.lab3.rcpExample.RemoveCityname: Add CitydefaultHandler: ecl.team2.lab3.commands.RemoveCityHandler</pre> For Menu Command<pre>commandId: ecl.team2.lab3.rcpExample.RemoveCitylabel: Remove Citytooltip: Removes a city or multiple cities based on selection</pre> For RemoveCityHandler.java<source lang=java>package ecl.team2.lab3.commands; import java.util.ArrayList;import java.util.Iterator; import org.eclipse.core.commands.AbstractHandler;import org.eclipse.core.commands.ExecutionEvent;import org.eclipse.core.commands.ExecutionException;import org.eclipse.core.commands.IHandler;import org.eclipse.jface.viewers.ISelection;import org.eclipse.jface.viewers.IStructuredSelection;import org.eclipse.ui.IWorkbenchPage;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.handlers.HandlerUtil; import ecl.team2.lab3.rcpexample.WeatherView;import ecl.team2.lab3.weathermodel.*; public class RemoveCityHandler extends AbstractHandler implements IHandler {  @Override public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); IWorkbenchPage page = window.getActivePage(); WeatherView view = (WeatherView) page.findView(WeatherView.ID); ISelection select = view.getSite().getSelectionProvider().getSelection(); if(select!=null && select instanceof IStructuredSelection) { IStructuredSelection sel = (IStructuredSelection)select; for(Iterator<Weather> iter = sel.iterator(); iter.hasNext();) { Weather temp = iter.next(); SimpleWeatherSystem.INSTANCE.removeCity(temp.getCity()); } view.getViewer().refresh(); }
return null;
}
}
</source>
<br/>===Views===Since most of the back-end coding has been completed through the "Commands" section, this portion will be brief.*In order to view and edit our content, we must create a view. Creating a view is very similar to creating a command, except you must add the extension "org.eclipse.ui.views" in our MANIFEST file.*You should have something that looks akin to this[[File:UiviewsT2.png]]*Now that we've included the extension, we should create a new view so we may view and edit our content. We do this by right-clicking our recent addition, scroll to "New", and then click on "view".*Enter the following information below and once that's completed click on "class*". Clicking on "class*" will now change this allow us to create classes on the snippet fly using the "New Java Class" wizard. Most of the mandatory components for creating a view java class has already been added, so all we have to do is click "Finish"[[File:Ext4T2.png]]*Replace code belowinside "WeatherView.java" with:
<source lang=java>
package ecl.team2.lab3.rcpexample;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
public class WeatherView extends ViewPart
{
public static final String ID = "ecl.team2.lab3.rcpexample.WeatherView";
@Override
public void createPartControl(Composite parent)
{
this.setPartName("Weather");
Text text = new Text(parent, SWT.BORDER);
text.setText("Weather content will be displayed here");
}
 
@Override
public void setFocus()
{
// TODO Auto-generated method stub
}
}
</source>
<br/>
*We must now add our recently created view to the perspective, we do this by again engaging with the extensions in the MANIFEST file. We must include the "org.eclipse.ui.perspectiveExtensions" extension. After it is added, we will add a "view" to the last branch dubbed "*(perspectiveExtensions).
*Mimic the contents in the picture below
[[File:Ext5T2.png]]
*Launch the application, should get something akin to this
[[File:AppT2.png]]
<br/>
==Define and Use Editor==
1
edit

Navigation menu