14. Exercise: Adding menus
: 실습 : 메뉴 추가
In this exercise you create commands and handlers for your application. Afterwards you will create menu entries using these commands.
14.1. Create command model elements
Open the Application.e4xmi file of your com.vogella.tasks.ui plug-in and select the Commands entry. This selection is highlighted in the following screenshot.
사진 설명을 입력하세요.
Via the Add… button you can create new commands. The name and the ID are the important fields. Create the following commands.
Table 12. Commands
- 0열 선택0열 다음에 열 추가
- 1열 선택1열 다음에 열 추가
- 0행 선택0행 다음에 행 추가
- 1행 선택1행 다음에 행 추가
- 2행 선택2행 다음에 행 추가
- 3행 선택3행 다음에 행 추가
- 4행 선택4행 다음에 행 추가
- 5행 선택5행 다음에 행 추가
ID
|
Name
|
org.eclipse.ui.file.saveAll
|
Save
|
org.eclipse.ui.file.exit
|
Exit
|
com.vogella.tasks.ui.command.new
|
Create task
|
com.vogella.tasks.ui.command.remove
|
Remove task
|
com.vogella.tasks.ui.command.test
|
For testing
|
14.2. Creating the handler classes
Create the com.vogella.tasks.ui.handlers package for your handler classes.
All handler classes implement an execute() method annotated with @Execute.
package com.vogella.tasks.ui.handlers;
import org.eclipse.e4.core.di.annotations.Execute;
public class SaveAllHandler {
@Execute
public void execute() {
System.out.println((this.getClass().getSimpleName() + " called"));
}
}
Create now the the following classes by copying the above class.
SaveAllHandler
ExitHandler
NewTaskHandler
RemoveTaskHandler
TestHandler
14.3. Creating handler model elements
Select the application-scoped Handlers entry in your application model and create the handlers from the following table for your commands. For the definition of handlers the ID, command and class are the relevant information.
Use the com.vogella.tasks.ui.handler prefix for all IDs of the handlers.
Table 13. Handlers
Handler ID
|
Command
|
Class
|
.saveall
|
Save
|
SaveAllHandler
|
.exit
|
Exit
|
ExitHandler
|
.new
|
Create task
|
NewTaskHandler
|
.remove
|
Remove task
|
RemoveTaskHandler
|
.test
|
For testing
|
TestHandler
|
The application model editor shows both the name and the ID of the command. The class URI follows the bundleclass:// schema, the table only defines the class name to make the table more readable. For example, for the handler with the com.vogella.tasks.ui.handler.saveall id uses the following entry to point to his handler class.
bundleclass://com.vogella.tasks.ui/com.vogella.tasks.ui.handlers.SaveAllHandler'
사진 설명을 입력하세요.
14.4. Adding a menu
In your Application.e4xmi file select your TrimmedWindow entry in the model and flag the Main Menu attribute.
사진 설명을 입력하세요.
Assign the org.eclipse.ui.main.menu ID to your main menu.
주의 : Ensure that this ID of the main menu is correct. You use it later to contribute another menu entry via another plug-in.
Add two menus, one with the name "File" and the other one with the name "Edit" in the Label attribute.
Also set the org.eclipse.ui.file.menu ID for the File menu. Use com.vogella.tasks.ui.menu.edit as ID for the Edit menu.
사진 설명을 입력하세요.
Add a Handled MenuItem model element to the File menu. This item should point to the Save command via the Command attribute.
사진 설명을 입력하세요.
Add a Separator after the Save menu item and after that add an entry for the Exit command.
Add all other commands to the Edit menu.
14.5. Implement a handler class for exit
To test if your handler is working, change your ExitHandler class, so that it closes your application, once selected.
package com.vogella.tasks.ui.handlers;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.workbench.IWorkbench;
public class ExitHandler {
@Execute
public void execute(IWorkbench workbench) {
workbench.close();
}
}
14.6. Validate
Validate that your save handlers are called if you select them in the menu.
14.7. Possible issue: Exit menu entry on a MacOS
If you use the org.eclipse.ui.file.exit ID for your exit command, the Eclipse framework maps the menu entry to its default menu location on the MacOS. If you don’t see your exit menu, in its defined position, check this location.