Extending Eclipse Java Development Tools (3)

xiaoxiao2021-03-06  102

How do I extend the user interface of the JDT-specific element (like member in the Outline view)? Extend view or their underlying model?

Simple "Hello, World" example shows that add menu options only need to add a few lines of XML in the plug-in list file.

) And a class that handles actual operations (com.ibm.lab.helloWorld.SampleAction). The addition of the drop-down menu of the view, the toolbar of the public editor, and the pop-up menu are basically simple. The pop-up menu provided is attributed to two categories: a class is only related to the view and the selected object (i.e., the "blank place" blank place "of the view is usually displayed" Default "pop-up menu), Another class is more common, and they are related to options that are applied to the selected object. In our example, we hope that the target is just a specific object, so by defining an extension in the plug-in list file, we will provide the action object to the pop-up menu of these objects (abbreviate the following identifiers, To achieve better format; use '...'), as shown in Listing 8.

Listing 8. Modifier operation

ObjectClass = "org.eclipse.jdt.core.imember"

ID = "... iMember">

Label = "SOLN: MODIFIERS"

Path = "group.reorganize"

ID = "... iMember.modifiers">

Label = "private"

MenuBarpath = "... IMEMBER.MODIFIERS / GROUP1"

Class = "... jdt.excerpt.makeimemberprivate"

ID = "... IMEMBER.MAKEPRIVATE">

Label = "protected"

MenuBarpath = "... IMEMBER.MODIFIERS / GROUP1"

Class = "... jdt.excerpt.makeimemberprotacection"

ID = "... IMEMBER.MAKEPROTECTED">

... all menu choices not shown ...

The extension is named org.eclipse.ui.popupMenus, as the name suggests, which defines the objects provided to the pop-up menu that appears in the workbench. This special example only provides a clearly selected object that implements an object of the IMEMBER interface (please recall the definition in the Java language specification, the member contains methods and fields). Our research has no white fees; we have received the answer to the current problem, we are almost ready to answer the next question.

Before doing this, please note that we are found for simple "Hello, World" operation examples to repeat the other menu operations provided. That is, the selection change will notify the class (through its SelectionChanged method) specified in the class attribute, and will it know when the user selects the menu option (through its RUN method). Our "travel" user interface is coming to end; more difficult part, it is also the same in front of the part we expect to change. As mentioned in the following question, just do one or two observations before you continue. What is the relationship between elements displayed in Package Explorer and other views (such as OUTLINE views)? Does our extension need to know any difference between them?

You may have noticed that when you select a method in the Outline view and Hierarchy view, the class of the selected object is not always the same. For example, if you expand a library (JAR file) in Package Explorer, then select a class (or method), then it will not be the same option in the Outline view of the Java editor. what happened?

Here, we are observing the difference between the "editable" in the JAVA model of the JDT and the only part of the only partial part. Both Java models achieve public interfaces (like Imember), but they have different types of implementation of the underlying restriction. Another example is that there is an implementation class that represents the Java Compile Unit, which derives the .class files of the JAR file displayed from the Package Explorer, and another class represents the compilation unit that is directly derived from the .java file. The latter implementation will allow for some modifications that are not allowed by the former, and their API sharing part is represented by interface ICompilationUnit.

When you have previously edited Java source code, you will be observed: Outline view is updated when you enter method characteristics (if you don't pay attention, try it!). This example illustrates how JDT is suspended in different regions, which is different from the changes that have been saved, compiled, and integrated into the Java model. Some views (OUTLINE views of the Java Editor) know the uncommitted changes and changes that have been submitted, while other views such as Navigator only care about the submitted changes that have been saved to the file system.

Subsequently, what we provide to modify the Java members must (at least some degree) know what context calls it. That is, it must recognize that some selected members are modified (those in the Outline view located in the Java editor), while others cannot modify (stored in the JAR file and displayed in package explorer) Members in the .class file). Remember this, let us continue the next question.

How to change the JDT model by programming?

If you have studied in the previous "travel", you may have noticed that IMEMBER, IJAVAELEMENT, and our operations are not as seen by the selected Java-related items as most interfaces. Setxxx method. So how do you modify these interfaces?

You will find that this is simple, but it may not be so obvious in intuition. JDT's Java model is "read only" in most practices. By collaboration with the Java compiler, the change in the bottom layer Java source code for a given element is synchronized with the changes to the rest of the Java model. In fact, what you have to do is to update the Java source code, and the rest of the necessary changes to the model will be transmitted to any elements that depend on their elements. For example, whenever the Java source code / Java model changes, the JDT index will automatically update, so you can still quickly perform a search, recompile the slave class (indicated by the Java build path specified in the project feature), and so on. Can be great! The following is the reason why the Java model is the key to plug-in integration: The Java model provides a model of common memory throughout the Java environment, its range from one project, until all referenced libraries, all of which don't want you Feel your heart to operate .java files, .class files, .jar files. You can focus on advanced models, and let JDT handle many of the messy details.

Still can't confirm it? Listing 9 contains a small portion of the core code of this solution, which is extracted from the RUN method of providing operations and is slightly simplified for readability:

Listing 9. SelectionChanged method, small solution

Public void SelectionChanged (IADITION Action, ISELECTION Selection) {

IMEMBER MEMBER = (IMEMBER)

(IstructuredSelection selection) .getfirstlement ();

ICompiLationUnit Cu = Member.getCompiLationUnit ();

IF (cu.isworkingcopy ()) {

Ibuffer buffer = cu.getBuffer ();

Buffer.Replace (...);

Cu.Reconcile ();

}

}

It seems a little tiger head snake, isn't it? Provide selected members to request their parent containers (Java .Class or model of Java .class or. Java file, using JDT, compiler), because its parent container management underlying source code, verification Whether the member belongs to "Uncommitted" Java model (in other words, it is currently open in the editor), then modify the source code returned as the buffer. The iBuffer interface is similar to StringBuffer, which is different from that change the corresponding elements of the Java model associated with the compilation unit. Inform the JDT to inform the other related parties (like the Package Explorer view): Your model update is ready for public consumer goods.

You must notice the omitting number in the above code. There, you must analyze the source code itself for modification. Similarly, JDT will help, as we will see in the next question.

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

New Post(0)