[Eclipse plug-in Development Guide] 2.3 review generated code

Source: Internet
Author: User

Review the code using a simple plug-in provided by the new plug-in project navigation.

● Plug-in list

● Plug-ins

● Common views

 

2.3.1. Plug-In list

The plug-in List editor displays two plug-in list files:META-INF/manifest. MFAndPlugin. xmlThey are used to define how to associate all other things in the system. After you create a plug-in project, this editor will automatically open on the first page (Figure 2-9). If the plug-in List editor is disabled, double-click the fileMETA-INF/manifest. MF orPlugin. XML can be re-opened. Next, we will preview the configuration editor. In chapter 3, we will find more details about the plug-in list.

 

Although this editor can easily modify the description of the plug-in, it can still show us which underlying code is associated with different parts of the editor. Click the manifest. MF label to displayMETA-INF/manifest. MFThis file defines the ease of running the plug-in (Figure 2-7). The first two lines define the plug-in as an osgi configuration file (see Section 3.3, plug-in list ). The following rules specify the plug-in name, version number, identifier, class path, and other plug-ins on which the plug-in depends. All of these can be edited in the plug-in List editor.

Figure 2-7. Plug-in List editor manifest. MF page

Click the plugin. xml tag to display the file.The content of plugin. XML, which is used to define the expansion of the price difference (Figure 2-8 ). The first line indicates that it is an XLM file, and the rest specifies the extension part of the plug-in.

Figure 2-8. Plug-in List editor plugin. XML page

On the overview page of the configuration plug-in, you can see some rough information about the configuration of the plug-in (Figure 2-9 ). This page describes the general information, such as the plug-in identifier (ID), version, name, class, and provider.META-INF/manifest. MFFile.

Bundle-Name: Favorites plug-in bundle-symbolicname: COM. qualityeclipse. favorites; singleton: = true bundle-version: 1.0.0 bundle-Activator: COM. qualityeclipse. favorites. favoritesplugin bundle-vendor: Quality eclipse

Figure 2-9. Plug-in List editor preview page

You can edit the information on the preview page or switch to the manifest. MF page to directly modify the information.

Note:

Changing pages except plugin. xml and manifest. MF may cause the plug-in list to reset the source format. If you have special requirements on the format of a configuration file, you can use only the plugin. xml and manifest. MF pages to edit the file or use other editors.

 

Warning:META-INF/manifest. MFThe format rules include complete non-perceptual rules, which involve the length of a sentence and line breaks. Edit with cautionPlugin. xml andMETA-INF/manifest. MF.

System plug-in dependencies are displayed on the dependencies page of the plug-in List editor (Figure 2-10 ).

Figure 2-10. Plug-in List editor dependencies page

It is equivalentMETA-INF/manifest. MFFileRequire-bundle:

Require-Bundle: org. Eclipse. UI, org. Eclipse. Core. Runtime

For favorites plug-in, this section points out that it depends onOrg. Eclipse. Core. RuntimeAndOrg. Eclipse. UI. This dependency declaration is different from the Java build path of the favorites plug-in project (and the classpath of the compilation period we know) because the Java build path is required during the compilation period, the plug-in dependency also takes effect when the plug-in is executed. Because the project is created as a plug-in project, and it hasOrg. Eclipse. PVDF. pluginnatureSuch a nature (14.3, nature, more project features), any dependency changes will be automatically reflected in the Java build path. If your two plug-ins are not synchronized, you can have a compilation plug-in that can be built, but it may not be executed.

Note: edit the dependency list instead of the Java build path to automatically synchronize the two sides.

The dependency can be expressed as imported packages on the dependencies page of the configuration Editor (Figure 2-10). This can be equivalentMETA-INF/manifest. MFFileImport-PackagePart:

Import-package: org. Eclipse. UI. Views, org. Eclipse. Core. runtime. Model

The runtime page is equivalentMETA-INF/manifest. MFFileBundle-classpathIt defines the libraries used by the plug-in and the libraries used by the plug-in during execution. The prefixes of these packages are used in each library (used to optimize the loading time of the plug-in ). For favorites plug-in, all the code has been included in a separate JAR file namedFavorites. jar, which contains all classes with the "com. qualityeclipse. favorites" Prefix:

Bundle-classpath: favorites. Jar

Figure 2-11. Plug-in List editor runtime page

Favorites plug-in does not import any packages for the plug-in or for expansion.

The extensions page (Figure 2-12) shows how to scale existing plug-ins in the system. They are equivalentPlugin. xmlFile<Extension point = "org. Eclipse. UI. Views">:

1
 

Figure 2-12. Plug-in List editor extension page

 

Favorites plug-in declares the extension fromOrg. Eclipse. UIPlug-ins inOrg. Eclipse. UI. ViewsThe extension point provides an additional view type named quality eclipse and a new view named favorites. Select an option on the left of the tree on the extensions page. Its Attributes are displayed on the right. On the extensions page, select favorites (View) to display the name, ID, class, and more information. They are equivalent to XML attributes.<View>Definition.

Finally, you can define a new extension point on the extension point page of the configuration Editor (Figure 2-13), so that other plug-ins can be extended based on this plug-in. Currently, favorites plug-in does not define any extension points, so it cannot be extended by other plug-ins.

Figure 2-13. Extension point page of the plug-in List editor


 

2.3.2. Plug-in class

Each plug-in can define a class at will and identify the plug-in from a programmable extension point, as shown on the preview page (Figure 2-9. In favorites plug-in, the class name isCom. qualityeclipse. favorites. favoritesplugin.

 

Package COM. qualityeclipse. favorites; </P> <p> Import Org. eclipse. UI. plugin. *; <br/> Import Org. eclipse. jface. resource. imagedescriptor; <br/> Import Org. osgi. framework. bundlecontext; </P> <p>/** <br/> * The main plugin class to be used in the desktop. <br/> */<br/> public class favoritesplugin extends actuiplugin {</P> <p> // The shared instance. <br/> Private Static favoritesplugin plugin; </P> <p>/** <br/> * the constructor. <br/> */<br/> Public favoritesplugin () {<br/> plugin = this; <br/>}</P> <p>/** <br/> * this method is called upon plug-in activation. <br/> */<br/> Public void start (bundlecontext context) throws exception {<br/> super. start (context); <br/>}</P> <p>/** <br/> * this method is called when the plug-in is stopped. <br/> */<br/> Public void stop (bundlecontext context) throws exception {<br/> super. stop (context); <br/> plugin = NULL; <br/>}</P> <p>/** <br/> * returns the shared instance. <br/> */<br/> Public static favoritesplugin getdefault () {<br/> return plugin; <br/>}< br/>/** <br/> * returns an image descriptor for the image file <br/> * at the given plug-in relative path. <br/> * @ Param path <br/> * The path <br/> * @ return the Image Descriptor <br/> */<br/> public static imagedescriptor getimagedescriptor (string path) {<br/> return response actuiplugin. imagedescriptorfromplugin (<br/> "com. qualityeclipse. favorites ", PATH); <br/>}< br/>}

 

When the plug-in is activated, the eclipse system will instantiate the plug-in class and load it before other classes. This separate class instance is used by the eclipse system from start to end.

A typical plug-in class is declared as a static domain to reference this Singleton, so that you can easily share a plug-in. Favorites plug-in definesPluginIn the constructor, you can useGetdefaultMethod to use it.

Note: The Eclipse system always instantiates an instance to assemble a plug-in class. You can add some code to the constructor with caution, but make sure that your code does not create any instances. For example:

 

Public favoritesplugin () {<br/> If (plugin! = NULL) <br/> throw new illegalstateexception (<br/> "plug-in class already exists"); <br/> plugin = This; <br/>}

 

2.3.3. favorites View

In addition to the plug-in list and plug-in classes, the new plug-in project wizard automatically generates a simple view code called favorites. The view creates and displays some information with a simple model. In the following sections, this view will be linked to the favorites model to display information about some options contained in the favorites model.

 

Package COM. qualityeclipse. favorites. views; </P> <p> Import Org. eclipse. SWT. widgets. composite; <br/> Import Org. eclipse. UI. part. *; <br/> Import Org. eclipse. jface. viewers. *; <br/> Import Org. eclipse. SWT. graphics. image; <br/> Import Org. eclipse. jface. action. *; <br/> Import Org. eclipse. jface. dialogs. messagedialog; <br/> Import Org. eclipse. UI. *; <br/> Import Org. eclipse. SWT. widgets. menu; <br/> Import Org. eclipse. SWT. SWT; </P> <p>/** <br/> * This sample class demonstrates how to plug-in a new workbench <br/> * view. the view shows data obtained from the model. the sample <br/> * creates a dummy model on the fly, but a real implementation <br/> * wocould connect to the model available either in this or another <br/> * plug-in (e.g ., the workspace ). the view is connected to the <br/> * model using a content provider. <br/> * </P> <p> * The view uses a label provider to define how Model Objects <br/> * shocould be presented in the view. each view can present the <br/> * same model objects using different labels and icons, if <br/> * needed. alternatively, a single label provider can be shared <br/> * between views in order to ensure that objects of the same type <br/> * are presented in the same way everywhere. <br/> * </P> <p> */<br/> public class favoritesview extends viewpart {<br/> private tableviewer viewer; </P> <p>/* <br/> * the content provider class is responsible for providing <br/> * objects to the view. it can wrap existing objects in <br/> * adapters or simply return objects as-is. these objects may <br/> * be sensitive to the current input of the view, or ignore it <br/> * and always show the same content (task list, for <br/> * example ). <br/> */</P> <p> class viewcontentprovider <br/> implements istructuredcontentprovider <br/>{< br/> Public void inputchanged (<br/> viewer v, object oldinput, object newinput) {<br/>}</P> <p> Public void dispose () {<br/>}</P> <p> Public object [] getelements (Object parent) {<br/> return New String [] {"one ", "two", "three "}; <br/>}< br/>/* <br/> * the label provider class is responsible for translating <br/> * objects into text and images that are displayed <br/> * in the various cells of the table. <br/> */<br/> class viewlabelprovider extends labelprovider <br/> implements itablelabelprovider <br/>{< br/> Public String getcolumntext (Object OBJ, int index) {<br/> return gettext (OBJ); <br/>}</P> <p> public image getcolumnimage (Object OBJ, int index) {<br/> return getimage (OBJ); <br/>}</P> <p> public image getimage (Object OBJ) {<br/> return platformui. getworkbench (). getsharedimages () <br/>. getimage (isharedimages. img_obj_element); <br/>}</P> <p>/** <br/> * the constructor. <br/> */<br/> Public favoritesview () {<br/>}</P> <p>/** <br/> * This is a callback that will allow us to create the viewer <br/> * and initialize it. <br/> */<br/> Public void createpartcontrol (composite parent) {<br/> viewer = new tableviewer (<br/> parent, SWT. multi | SWT. h_scroll | SWT. v_scroll); <br/> viewer. setcontentprovider (New viewcontentprovider (); <br/> viewer. setlabelprovider (New viewlabelprovider (); <br/> viewer. setinput (getviewsite (); <br/>}</P> <p> private void showmessage (string message) {<br/> messagedialog. openinformation (<br/> viewer. getcontrol (). getshell (), "favorites", message ); <br/>}</P> <p>/** <br/> * Passing the focus request to the viewer's control. <br/> */<br/> Public void setfocus () {<br/> viewer. getcontrol (). setfocus (); <br/>}< br/>}

 

This article is the original article of eclipselight.org (eclipse light). For more information, see the source.

Fixed Link: http://www.eclipselight.org/eclipse-plugin-tutorial/594/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.