Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes

sponsored links
-------------------------------------------------- --------------------------------------------

Study: http://www.blogjava.net/youxia/archive/2006/11/20/82388.html

-------------------------------------------------- --------------------------------------------

1. To edit ApplicationActionBarAdvisor.java way this class:

A) preparation of the menu action categories:

package hellorcp.action;

import hellorcp.dialog.FirstDialog;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

public class FirstAction extends Action implements IWorkbenchAction {
        private IWorkbenchWindow workbenchWindow;

        public FirstAction(IWorkbenchWindow window) {
                if (window == null) {
                        throw new IllegalArgumentException();
                }

                this.workbenchWindow = window;
        }

        public void run() {
                // make sure action is not disposed
                if (workbenchWindow != null) {
                        //  Add functionality here.  
                        FirstDialog dg = new FirstDialog(workbenchWindow.getShell());
                        dg.open();

                }
        }

        public void dispose() {
                workbenchWindow = null;

        }

}


2) The menu action to open the dialog box, it needs to write dialog class:

package hellorcp.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FirstDialog extends Dialog {
        protected Shell shell;

        private int result;

        public FirstDialog(Shell parent, int style) {
                super(parent, style);
        }

        public FirstDialog(Shell parent) {
                this(parent, SWT.NONE);
        }

        public int open() {
                createContents();
                shell.open();
                shell.layout();
                Display display = getParent().getDisplay();
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch())
                                display.sleep();
                }
                return result;
        }

        protected void createContents() {
                shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                shell.setSize(150, 70);
                shell.setText("  The first dialog box  ");

                final Button okButton = new Button(shell, SWT.NONE);
                okButton.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                result = 1;
                                shell.dispose();
                        }
                });
                okButton.setText(" OK ");
                okButton.setBounds(10, 10, 48, 22);

                final Button cancelButton = new Button(shell, SWT.NONE);
                cancelButton.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                result = 2;
                                shell.dispose();
                        }
                });
                cancelButton.setText(" Cancel ");
                cancelButton.setBounds(89, 10, 48, 22);
        }

}


3) The editing method makeActions, registration of the menu action; and then edit the way fillMenuBar draw menu:

package hellorcp;

import hellorcp.action.FirstAction;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

        private IWorkbenchAction action1;
        private IWorkbenchAction action2;

        public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
                super(configurer);
        }

        protected void makeActions(IWorkbenchWindow window) {
                action1 = new FirstAction(window);
                action1.setText("FirstMenu1");
                action1.setId(action1.getClass().getName());
                register(action1);
        
                action2 = new FirstAction(window);
                action2.setText("FirstMenu2");
                action2.setId(action2.getClass().getName());
                register(action2);
        }

        protected void fillMenuBar(IMenuManager menuBar) {
                MenuManager newMenu = new MenuManager(" The first menu  ", "hellorcp.firstmenu1");
                menuBar.add(newMenu);
                newMenu.add(action1);

                MenuManager newMenu2 = new MenuManager(" The first menu  ", "hellorcp.firstmenu2");
                menuBar.add(newMenu2);
                newMenu.add(action2);
        }

}


4) Run the following diagram results:

Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes
Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes


2. A way to edit the plugin.xml to add the menu:

A) preparation of the menu action categories:

package hellorcp.action;

import hellorcp.dialog.FirstDialog;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class Action2 implements IWorkbenchWindowActionDelegate {
        private IWorkbenchWindow window;

        public void dispose() {
                // TODO

        }

        public void init(IWorkbenchWindow window) {
                // TODO
                this.window = window;

        }

        public void run(IAction action) {
                // TODO
                FirstDialog dg = new FirstDialog(window.getShell());
                dg.open();

        }

        public void selectionChanged(IAction action, ISelection selection) {
                // TODO

        }

}


2) Double-click the plugin.xml to open Extensions editing window, click the "All Extensions" under the "Add ..." to open Extension selection window, and select org.eclipse.ui.actionSets, the following diagram:

Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes


3) Back to the Extensions editing interface, select the "org.eclipse.ui.actionSets", and on the right "Extension Details" of the "ID", "Name", enter the content, save the following diagram:

Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes


4) Click on the "plugin.xml" tab, you can see the file appears in the following code:

   <extension
        
         name=" Menu  "
         point="org.eclipse.ui.actionSets">
   </extension>


The fragment directly edit the contents as follows:

   <extension
        
         name=" Menu  "
         point="org.eclipse.ui.actionSets">
         <actionSet
                        description =" The first expansion  " 
            id ="RCP_Start.actionSet1" 
            label ="RCP_Start.actionSet1" 
            visible ="true"> 
                <action
                                class ="hellorcp.action.Action2" 
                icon ="icons/alt_window_16.gif" 
                id ="RCP_Start.action2" 
                    label =" The second menu item  " 
                                menubarPath ="hellorcp.firstmenu/additions" 
                style ="push" 
                toolbarPath ="additions" 
                tooltip =" The second menu item button  " /> 
       </actionSet> 
   </extension>


5) once again switch to the "Extensions" tab, you can see the corresponding content is there, and the following figure:

Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes


6) run the program, the effect the following figure:

Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes


Note: The need to type ApplicationWorkbenchWindowAdvisor method preWindowOpen () in configurer.setShowCoolBar (false); replaced configurer.setShowCoolBar (true); to appear in the icon menu.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb
Tags:

Related Posts of Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries