Open main menu

CDOT Wiki β

Changes

Teams Winter 2011/team1/RCP/Define and use commands

9,885 bytes added, 12:31, 9 March 2011
Run your app with view(s)
<br/>
Add the command you just created to the menuContribution you have in the extension point (the same one you added Print command to). <br/>
 [[Image: RCPCommand15RCPCommand16.jpg | 700px]]<br/>
<br/>
[[Image'''Implementing the AddStudent command'''<br/>Please do this part, after the view, Jface viewer and Editor view is added and working properly.<br/>1. Implementing the execute method of the command handler class:<br/>This code will get access to the page and creates an empty editor input (sending it the id of string "0" indicating that it belongs to no student), then tries to open the editor with that input id.<code>AddStudent.java </code><source lang=java>package cs.ecl.rcp.simplercp.commands; 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.ui.IWorkbenchPage;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.PartInitException;import org.eclipse.ui.handlers.HandlerUtil; import cs.ecl.rcp.simplercp.editor.MyStudentEditor;import cs.ecl.rcp.simplercp.editor.MyStudentEditorInput;  public class AddStudent extends AbstractHandler implements IHandler {  @Override public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); IWorkbenchPage page = window.getActivePage(); MyStudentEditorInput input = new MyStudentEditorInput("0"); try { page.openEditor(input, MyStudentEditor.ID);  } catch (PartInitException e) { throw new RuntimeException(e); }  return null; } } </source><br/><br/>2. Modifying the MyStudentEditorInput class: RCPCommand16<br/>we will override the "equals" method to recognize if the input object to the method equals this object (EditorInput).This ensures that if you click on Add Student command several times, only one editor window will open and consequent calls to the command will only set focus on the open editor view.<code>MyStudentEditorInput.java </code><source lang=java>/ Added For NewStudent Command @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MyStudentEditorInput other = (MyStudentEditorInput) obj; if (id != other.id) return false; return true; }</source> <br/><br/>3. Modifying the editor view (MyStudentEditor class).jpg | 700px]]<br/>Here we do the following changes: a) make the Text objects, a member of the class, so that they can be used in other methods. <code>MyStudentEditor.java </code><source lang=java>public class MyStudentEditor extends EditorPart { public static final String ID = "cs.ecl.rcp.simplercp.editor.studenteditor"; private Student student; private MyStudentEditorInput input; // Added For NewStudent Command private Text text; private Text text1; private Text text2; private Text text3;</source><br/> b) Modify the "init" method so it distinguishes between a new and existing student input object, and creates an empty student object for the input with id "0": <code>MyStudentEditor.java </code><source lang=java>public void init(IEditorSite site, IEditorInput input) throws PartInitException { if (!(input instanceof MyStudentEditorInput)) { throw new RuntimeException("Wrong input"); }  MyStudentEditorInput new_name = (MyStudentEditorInput) input; this.input = (MyStudentEditorInput) input; setSite(site); setInput(input); // Added For NewStudent Command if(this.input.getId()=="0"){ student = new Student (); setPartName("New Student"); } else { student = ModelProvider.INSTANCE.getPersonById(this.input.getId()); setPartName("Student " + student.getLastName()); } } </source> 
<br/>
c)Modify the "createPartControl" method to 1)check if the student object properties are null to prevent referencing null when the student object is a new one. 2)To add a save button to the window with the listener. when clicked, the save button will set the text field inputs as the student property and adds the student to the ModelProvider class, then refreshes the tableViewer and closes the editor window. The save button only appears for the new student editor.<br/>
 
<code>MyStudentEditor.java </code>
<source lang=java>
 
// Modified For NewStudent Command
@Override
public void createPartControl(Composite parent) {
GridLayout layout = new GridLayout();
layout.numColumns = 2;
parent.setLayout(layout);
Label label = new Label(parent, SWT.BORDER);
label.setText("ID");
text = new Text(parent, SWT.BORDER);
if (student.getId()!=null)
text.setText(student.getId());
text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
Label label1 = new Label(parent, SWT.BORDER);
label1.setText("First Name");
text1 = new Text(parent, SWT.BORDER);
if (student.getFirstName()!=null)
text1.setText(student.getFirstName());
text1.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
Label label2 = new Label(parent, SWT.BORDER);
label2.setText("Last Name");
text2 = new Text(parent, SWT.BORDER);
if (student.getLastName()!=null)
text2.setText(student.getLastName());
text2.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
Label label3 = new Label(parent, SWT.BORDER);
label3.setText("Program");
text3 = new Text(parent, SWT.BORDER);
if (student.getProgram()!=null)
text3.setText(student.getProgram());
text3.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
// Added For NewStudent Command
Button btnSave = new Button(parent, SWT.PUSH);
btnSave.setText("Save");
btnSave.setVisible(false);
if (this.input.getId()=="0") btnSave.setVisible(true);
btnSave.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (text.getText().length() != 0
&& text1.getText().length() != 0
&& text2.getText().length() != 0
&& text3.getText().length() != 0) {
student.setId(text.getText());
student.setFirstName(text1.getText());
student.setLastName(text2.getText());
student.setProgram(text3.getText());
ModelProvider students =ModelProvider.INSTANCE;
students.getStudents().add(student);
IWorkbenchWindow window = getSite().getWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
StudentsView view = (StudentsView) page.findView(StudentsView.ID);
view.getViewer().refresh();
getSite().getPage().closeEditor(getSite().getPage().findEditor(getEditorInput()) , false);
}
}
});
}
</source><br/>
When modifying the code, add necessary imports when the eclipse code editor creates error of not finding some classes.<br/>
==== "Delete Student" Command ====
=== Define Views in The Application ===
In this part of the tutorial we are going to add a view to our application. We have to create a view and add it to the perspective and make it visible.<br/>
 
==== Create Views====
Click on the Extensions tab in the plugin.xml window in the project and add a new Extension Point as "org.eclipse.ui.views".
<br/>
[[Image: RCPView3.jpg | 400px]]<br/>
<br/>
Right click on the newly added Extension Point and choose: New > View.<br/>
Then in the details section give the view a Name("StudentView"), id ("cs.ecl.rcp.simpleRCP.StudentView")and a class ("cs.ecl.rcp.simplercp.StudentView").<br/>
[[Image: RCPView4.jpg | 700px]]<br/>
<br/>
Double click on the class link and the New Java Class window will open. Notice that the StudentView class is a subclass of "org.eclipse.ui.part.ViewPart" class.<br/>
 
[[Image: RCPView1.jpg | 400px]]<br/>
<br/>
Go to the project in project explorer and find the StudentView in the "cs.ecl.rcp.simplercp" package in the src folder. Here is how it looks like ( we added a text to this view).
<br/>
[[Image: RCPView2.jpg | 700px]]<br/>
 
==== Add view(s) to perspective via code ====
Now we have to add our view to the perspective.We could do this in Extensions window, but here we want to show you how to do it programmatically.<br/>
Find the "Perspective" class in src folder of your project in the "cs.ecl.rco.simplercp" package.Here is how it looks like:<br/>
[[Image: RCPView5.jpg | 700px]]<br/>
<br/>
Add the following code to add view to the perspective. we also are making the view non-closeable, so that user cannot close it.
<code>Perspective.java</code>
<source lang="java">
package cs.ecl.rcp.simplercp;
 
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
 
 
public class Perspective implements IPerspectiveFactory {
 
public void createInitialLayout(IPageLayout layout) {
layout.setEditorAreaVisible(true);
layout.addView(StudentsView.ID, IPageLayout.LEFT, 1.0f, layout.getEditorArea());
//make the view fixed
layout.setFixed(true);
//make the view non-closeable
layout.getViewLayout(StudentsView.ID).setCloseable(false);
}
}
</source>
 
==== Run your app with view(s)====
Run the project now. Here is what you will see:
<br/>
[[Image: RCPView7.jpg | 400px]]<br/>
 
<br/>
[[Teams_Winter_2011/team1| Index Page]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [[Teams_Winter_2011/team1/RCP/Define_and_use_editors| Next>>]]
1
edit