Extending Eclipse Java Development Tools (2)

xiaoxiao2021-03-06  91

In the user interface, how to display an extension, where is it displayed?

This is a gentle tip that we have got an answer. We want to display the context menu options for one or more selected methods, which allow us to change the visibility of the method only with one action. We prefer to use these menu options anywhere where you can display methods, such as in the Hierarchy view and Package Explorer. This takes us to the next question.

How often is it to extend the user interface?

By an example to learn more interesting, the Plug-In Project Wizard can help, it provides some sample code, we can modify these code to meet our needs. We will answer several questions in the wizard, which will automatically launch a specialistic perspective for plug-in development, called Plug-in Development Environment (PDE) to prepare tests. The wizard contains many examples that can help us start. In fact, our old friend "Hello World" is there. In order to follow this tradition, we will generate this "Hello World" to view the results to verify that the environment is installed correctly, then modify it to help us answer the current problem and bring us to the next question: Extended the user interface How do I know a basic event similar to "Select"? This is important because we want to apply our newly introduced menu options to the currently selected method.

Note that these instructions assume that you are starting with a new Eclipse installation. If you modify the environment or change the preferences, it may not be entirely as follows. You can consider launching Eclipse from a new work space: Open the command prompt window, change it to

/ Eclipse directory, then start Eclipse using the -Data parameter, as shown in Listing 1.

Listing 1. Start a new Eclipse instance

CD c: /eclipse2.1/eclipse

Eclipse.exe -data WorkspaceDevworks

Create a plugin project from using the New Plug-In Project Wizard. Select File> New> Project. In the New Project dialog, select Plug-in Development and Plug-In Project in the Will List, then select Next. Name the project com.ibm.Lab.Helloworld. The wizard will create a plug-in identity based on this name, so it must be unique in the system (the same is true, the project name, and the plug-in identification). Use the recommended default workspace location below to "Project Contents" is ok; select Next.

On the next page, select Next to accept the default plugin project structure. The plug-in code generator page recommends many samples, wizards can help you further parameterize the project. Select the "Hello, World" option and select Next. Next page (shown in Figure 4) It is recommended to recommend a plug-in name and plugin class name. These names are based on the last word of the plug-in project com.ibm.Lab.Helloworld. This example does not require any plug-in convenience method, so unpacking the selection of three code generation options (as shown in Figure 4), then select Next (not finish; you still have a page).

Figure 4. Simple Plug-in Content

You can specify parameters in the next page (shown in Figure 5), which is unique to "Hello, Worlds" example, such as the message to be displayed.

Figure 5. Sample Action Set To simplify the generated code, change the target package name of the action from com.ibm.Lab.HelloWorld.Ibm.Lab.HelloWorld, which is the same as the name of the project. Although in the actual plug-in, you can choose to group related classes with different packages, but in this case, there are only two classes, so don't do this. This also follows the same practice as the "master" package name and project name. Select Finish now.

You should see a message: "Plug-inscription to Compile Java Classes in this plug-in all currently disabled. The Wizard Will Enable Them To Avoid Compile Errors." Select OK to continue. If this is a new work space, you will also see another information message: "This Kind of Project is associated with the plug-in development personpective. Do you want to switch to this perspection.?". Select YES to switch according to the recommendations of this message.

To verify that all things are set correctly, let us test new plugins. Select Run> Run as> Run-Time Workbench. This will start the second instance of Eclipse, which will contain your plugin. This new instance will create a new space directory called Runtime-Workspace, so don't worry; any tests made to this instance will not affect the development settings. You should see similar to Figure 6, where there is a new drop-down menu, which is the Sample Menu, which has a single option sample action. Select it will display the following message. If you are not starting from a new workspace, you can choose Window> Reset Perspective to view the newly generated drop-down menu; this menu will not be displayed from the existing workspace, because the workbench "remember" What is the last Eclipse runtime? The set is active (you can also add / delete the operation set on the Window> Customize Perspective ... drop-down menu option).

Figure 6. Hello, Eclipse World

Let's quickly browse the plug-in list file plugin.xml. Double-click it to open it in the Plug-In Manifest Editor. This editor provides several pages similar to the wizard and a "original" source page. Transfer to the source code page by selecting the Source tab. You will see the code similar to the code displayed below; we are interested in the part of the bold display.

Listing 2. "Hello, World" plugin.xml

Point = "org.eclipse.ui.actionsets" >>

Label = "Sample Action SET"

Visible = "True"

ID = "com.ibm.lab.helloWorld.AnctionSet">

Label = "Sample & Menu"

ID = "SampleMenu">

Name = "SampleGroup">

Label = "& sample action" icon = "icons / sample.gif"

Class = "com.ibm.lab.helloworld.sample"

Tooltip = "Hello, Eclipse World"

MenuBarpath = "SampleMenu / SampleGroup"

Toolbarpath = "SampleGroup"

ID = "com.ibm.lab.helloWorld.SampleArt">

It doesn't need to study this code in depth. The purpose of our "travel" first II is just to familiarize yourself with some basic mechanisms, and we can introduce the JDT extension. Here you will see a sample of such a technology: it adds menu and menu options as an operator to the workbench. It uses one with one

The expansion of the tag declaration begins. The workbench user interface plugin defines this extension org.eclipse.ui.actionSets, as well as several similar extensions, and other plugins can be provided to various user interface elements through these extensions.

We have not answered how to add menu options to the context menu of the Java method. A simple example can give us some prompts. First open the class SampleAction that appears "Hello, World" message, please pay attention to its RUN method. It is not particularly interesting; but we also saw another method SelectionChanged. Ah! The answer to the next question is available.

How to know how to know "Select" is similar to "Select"?

The work is notified when the workbench "Select" changes (like the menu drop-down options we provide). This is confirmed in the Javadoc comment in front of the SelectionChanged method. Let us modify this method to inform more information about "Select". First, if you have not closed the work of the workbench, then it is turned off now. Then add the code in Listing 3 to the SelectionChanged method.

Listing 3. SelectionChanged method, first modification

Public void SelectionChanged (IADITION Action, ISELECTION Selection) {

System.out.println ("===========> SelectionChanged");

System.out.println (Selection); SESTEM.

}

With this debug code, we will see what chooses and learned about what makes Eclipse work more. Save the method and restart the Runtime Workbench.

Important: Eclipse has a delayed strategy to load the plugin when the user performs the operation of the plugin code. So you must first select the Sample Action menu option to load your plugin before calling the SelectionChanged method.

Now choose other things, such as text in the editor, files in Navigator, of course, members in the Outline view (Memories: You must create a Java project and sample Java classes to do this because of runtime instances Different work spaces). Listing 4 shows a sample output from which you will see in the console of Eclipse's development instance.

Listing 4. SelectionChanged output, first modified

==========> SelectionChanged [package com.ibm.Lab.soln.jdt.excerpt [in [Working Copy] ChangeimeMberflagAction.java

[in com.ibm.Lab.soln.jdt.excerpt [in src [in com.ibm.Lab.soln.jdt.excerpt]]]]]]]]

==========> SelectionChanged

==========> SelectionChanged

Org.eclipse.jface.text.textselection@9fca283

==========> SelectionChanged

==========> SelectionChanged

[Package Com.ibm.Lab.soln.jdt.excerpt [in [Working Copy] ChangeimemberFlagAction.java

[in com.ibm.Lab.soln.jdt.excerpt [in src [in com.ibm.Lab.soln.jdt.excerpt]]]]]]]]

==========> SelectionChanged

[Imember [] MEMBERS [in change "ChangeimeMberflagAction.java

[in com.ibm.Lab.soln.jdt.Excerpt [in src [in com.ibm.lab.soln.jdt.excerpt]]]]]

==========> SelectionChanged

==========> SelectionChanged

[Changememberflagaction.java [in com.ibm.Lab.soln.jdt.excerpt

[in SRC [in com.ibm.Lab.soln.jdt.excerpt]]]]]

Package com.ibm.Lab.soln.jdt.excerpt

Import org.eclipse.jdt.core.flags

Import org.eclipse.jdt.core.ibuffer

... line omitted ...

Void Selection, IAction, ISELECTION]

==========> SelectionChanged

[Boolean Ischecked (IACTION, IMEMBER) [in ToggleImemberFinalArt)

[in ToggleimeMberfinalAction.java [in com.ibm.Lab.soln.jdt.excerpt

[In SRC [In com.ibm.Lab.soln.jdt.excerpt]]]]]] Oh, this is not as inspiring as we want. Obviously, this "Select" is not as basic as String instance, but it does not express what kind of category involves it, because these classes clearly cover their default toString methods. If you don't have to do more studies, you can understand the information to us, then we are easy, but we have not reached this level. Go back to the SelectionChanged method, browse the hierarchy of the interface ISELECTION for the Selection parameter. His hierarchical indicates that it is not much, only istructuredSerection (for list) and ITextSerability. By outputting selected classes, we can make the SelectionChanged method more smarter. Modify the SelectionChanged method as shown in Listing 5.

Listing 5. SelectionChanged method, second modification

Public void SelectionChanged (IADITION Action, ISELECTION Selection) {

System.out.println ("===========> SelectionChanged");

IF (Selection! = NULL) {

IF (Selection InstanceOf IstructureDSelection) {

IstructuredSelection SS = (istructureDselection) Selection;

IF (ss.isempty ())

System.out.println (");

Else

System.out.println ("First SELECTED Element IS" SS.GETFIRSTELEMENT (); getClass ());

} else if (selection instanceof itextselection {

ITEXTSELECTION TS = (ITEXTSELECTION) SELECTION;

System.out.println ("SELECTED TEXT IS <" Ts.getText () ">");

}

} else {

System.out.println (");

}

}

Similarly, keep in mind that the runtime instance is turned off and then restarted. Now when you choose various elements of the user interface, they are not available, as shown in Listing 6.

Listing 6. SelectionChanged output, second modification

???? SELECTED SOME METHODS in The Outline View

==========> SelectionChanged

First SELECTED Element Is Class Org.eclipse.jdt.internal.core.SourceMethod

==========> SelectionChanged

First SELECTED Element Is Class Org.eclipse.jdt.internal.core.SourceMethod

==========> SelectionChanged

Activated the Java Editor

==========> SelectionChanged

SELECTED Text Is

==========> SelectionChanged

SELECTED SAME METHODS AND CLASSES, PACKAGE IN The Package Explorer

==========> SelectionChanged

First SELECTED Element Is Class Org.eclipse.jdt.internal.core.SourceMethod

==========> SelectionChanged

First SELECTED Element is class org.eclipse.jdt.internal.core.sourceType

==========> SelectionChanged

First SELECTED Element is class org.eclipse.jdt.internal.core.PackageFragment

Activated The Navigator View, SELECTED SOME Files, Folders, And Projects

==========> SelectionChanged

First SELECTED Element is class org.eclipse.core.internal.Resources.File

==========> SelectionChanged

==========> SelectionChanged

First SELECTED Element is class org.eclipse.core.internal.Resources.File

==========> SelectionChanged

First SELECTED Element is class org.eclipse.core.internal.Resources.Project

==========> SelectionChanged

First SELECTED Element is class org.eclipse.core.internal.Resources.Folder

==========> SelectionChanged

Reactivated the package explorer, SELECTED SOME CLASSES AND METHODS in Jars of Reference Libraries

==========> SelectionChanged

First SELECTED Element is class org.eclipse.jdt.internal.core.jarpackageFragment

==========> SelectionChanged

First SELECTED Element is class org.eclipse.jdt.internal.core.classfile

==========> SelectionChanged

First SELECTED Element is class org.eclipse.jdt.internal.core.binaryMethod

In particular, we confirm that the things you see in the user interface correspond to the JDT model class. We understand that the displayed model is displayed, not similar to the lower-level basic type of strings and images, and is attributed to another Eclipse framework called JFACE. This framework is mapping between the basic type of the symbol string (near the window widget of the operating system you want to use these basic types) and the higher model object (your code is more willing to use these objects). This article is just a slightly mentioned this topic because the goal we set is to extend JDT. References References have recommended other references about JFACE, which will expand your understanding. This article will only discuss the knowledge necessary for understanding the basis of JDT expansion.

Back to the output, special option results have attracted our attention: they correspond to the selection of Java members in the user interface. They repeated them in Listing 7.

Listing 7. SelectionChanged output, study again

==========> SelectionChanged

First SELECTED Element Is Class Org.eclipse.jdt.internal.core.SourceMethod

==========> SelectionChanged

First SELECTED Element is class org.eclipse.jdt.internal.core.binaryMethod

The internal in the middle of these classes is a bit worried. However, as you usually find, Eclipse will have a public interface that implements classes (internal), as examples herein. Quick class lookup reveals: These classes have realized a group of public interfaces that seem to be this question, which is isourcereference, ijavaElement, especially Imember. At last! Now we can start an extension of hope, which makes us answer the next question.

转载请注明原文地址:https://www.9cbs.com/read-96410.html

New Post(0)