Eclipse plug-in development learning note "3"---adding views and perspectives
First, add a view
Views are an important extension point in Eclipse plug-in development, and what we need to do is insert a view into the Eclipse plug-in project.
First, create a new plug-in project named AddView, choose the Hello Word template, and other default settings.
Package Structure:
Double-click the Plugin.xml file, select the Extensions tab, and click Add org.eclipse.ui.views extension point.
Right-click to create a new category and view property:
Right-click src to add Class, enter class name Firstview, Inherit superclass Viewpart, Package addperspective.views, tap finish:
Add the text object in Createpartcontrol, with the following code:
Package addperspective.views;
import org.eclipse.swt.widgets.Composite;
import Org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.*;
import org.eclipse.swt.layout.FillLayout;
import Org.eclipse.swt.SWT;
Public class Firstview extends viewpart {
/**
* The constructor.
*/
Public Firstview () {
}
/**
* This was a callback that would allow us
* To create the viewer and initialize it.
*/
Public void Createpartcontrol (Composite parent) {
Composite topcom = new Composite (parent, SWT. NONE);
Topcom.setlayout (new filllayout ());
Text text = new text (topcom, SWT. BORDER| Swt. MULTI);
Text.settext ("first window");
}
/**
* Passing the focus request to the viewer's control.
*/
Public void SetFocus () {
}
}
Click Run, Effect
At this point, the view is simply a view that needs to be displayed separately when the display is selected. It is then built in a perspective.
Second, add Perspective view
Start by creating a new plug-in project named Addperspective, choose the Hello Word template, and other default settings.
Create two view processes the same as the previous section, no longer repeat, the completed package structure.
Double-click Open plugin.xml file, open the Extensions tab, tap Add, select Org.eclipse.ui.perspectives, change the ID, name, class, and more, as follows:
Right src new class firstperspective, Implement Interface Iperspectivefactory, package addperspective.views, click Finish. Add the View layout code as follows:
Package addperspective.views;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
Public class Firstperspective implements iperspectivefactory {
@Override
Public void createinitiallayout (ipagelayout layout) {
TODO Auto-generated method stubs
String Editorarea = Layout.geteditorarea ();
Ifolderlayout left = Layout.createfolder ("left", Ipagelayout. left, 0.2f,editorarea);
Left.addview ("Addperspective.views.SecondView");
Ifolderlayout buttom = Layout.createfolder ("Buttom", Ipagelayout. BOTTOM, 0.8f,editorarea);
Buttom.addview ("Addperspective.views.FirstView");
}
}
It finds the view and sets his position and proportions by Ipagelayout's CreateFolder method and Iforderlayout's AddView method.
Click Run to open the first perspective, the effect is as follows:
Attach the code for the second view:
Package addperspective.views;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.events.SelectionAdapter;
Import org.eclipse.swt.events.SelectionEvent;
Import Org.eclipse.swt.layout.FillLayout;
Import Org.eclipse.swt.widgets.Composite;
Import org.eclipse.swt.widgets.List;
Import Org.eclipse.swt.widgets.Text;
Import Org.eclipse.ui.IViewPart;
Import Org.eclipse.ui.IWorkbenchPage;
Import Org.eclipse.ui.part.ViewPart;
public class Secondview extends Viewpart {
@Override
public void Createpartcontrol (Composite parent) {
Composite topcom = new Composite (PARENT,SWT. NONE);
Topcom.setlayout (New Filllayout ());
Final list List = new List (TOPCOM,SWT. BORDER);
List.add ("111");
List.add ("222");
List.add ("333");
}
@Override
public void SetFocus () {
TODO auto-generated method stubs
}
}
Written at the end:
Yesterday did not publish the blog, because of the encounter, XX structure of the problem of inconsistency. Finally, the Activator class that did not implement the plugin was found. Remind yourself that you must remember this.
Eclipse plug-in development learning note "3"---adding views and perspectives