Eclipse plug-in development learning note "5"---add drop-down menus and buttons to the view
We used the example in the previous article to add two items in the View2 add two buttons and drop-down menus.
First, create a new inherited Actiongroup class, Addactiongroup, with the following code:
/**
************************************
Addactiongroup class
**/
Package viewsconnection.actions;
import org.eclipse.jface.action.Action;
import Org.eclipse.jface.action.IMenuManager;
import Org.eclipse.jface.action.IToolBarManager;
import Org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import Org.eclipse.ui.IActionBars;
import Org.eclipse.ui.actions.ActionGroup;
Import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
Import org.eclipse.ui.internal.WorkbenchImages;
Import Com.sun.javafx.iio.ios.IosImageLoader;
Public class Addactiongroup extends actiongroup {
Public void fillactionbars (iactionbars bars) {
if (bars = = null) return;
Itoolbarmanager manager = Bars.gettoolbarmanager ();
Manager.add (new Action1 () {@Override
Public void Run () {
Messagedialog. openinformation (null, "", "Action1");
}
});
Manager.add (new Action1 () {@Override
Public void Run () {
Messagedialog. openinformation (null, "", "Action2");
}
});
}
Public void fillcontextmenu (Imenumanager menu) {
if (menu = = null) return;
Menu.add (new Action1 () {@Override
Public void Run () {
Messagedialog. openinformation (null, "", "Action1");
}
});
Menu.add (new Action1 () {@Override
Public void Run () {
Messagedialog. openinformation (null, "", "Action2");
}
});
}
Private class Action1 extends action{
Public Action1 () {
Imagedescriptor img = workbenchimages. Getimagedescriptor(iworkbenchgraphicconstants. Img_dtool_new_fastview );
Sethoverimagedescriptor (IMG);
SetText ("Action1");
}
@Override
Public void Run () {
TODO Auto-generated method stubs
Super. Run ();
}
}
Private class Action2 extends action{
Public Action2 () {
Imagedescriptor img = workbenchimages. Getimagedescriptor(iworkbenchgraphicconstants. Img_etool_editor_trimpart );
Sethoverimagedescriptor (IMG);
SetText ("Action2");
}
@Override
Public void Run () {
TODO Auto-generated method stubs
Super. Run ();
}
}
}
/**
************************************
View1 class
**/
Package viewsconnection.views;
Import Java.lang.reflect.Array;
Import java.util.ArrayList;
Import java.util.List;
Import Org.eclipse.jface.viewers.ColumnLayoutData;
Import Org.eclipse.jface.viewers.ColumnWeightData;
Import Org.eclipse.jface.viewers.TableLayout;
Import Org.eclipse.jface.viewers.TableViewer;
Import Org.eclipse.swt.widgets.Composite;
Import org.eclipse.swt.widgets.Table;
Import Org.eclipse.swt.widgets.TableColumn;
Import Org.eclipse.swt.widgets.Text;
Import org.eclipse.ui.part.*;
Import Org.eclipse.swt.layout.FillLayout;
Import Org.eclipse.swt.SWT;
public class View1 extends Viewpart {
/**
* This was a callback that would allow us
* To create the viewer and initialize it.
*/
public void Createpartcontrol (Composite parent) {
Composite topcom = new Composite (PARENT,SWT. NONE);
Topcom.setlayout (New Filllayout ());
Text text = new text (TOPCOM,SWT. BORDER);
Text.settext ("123456");
Tableviewer TV = new Tableviewer (TOPCOM,SWT. full_selection| Swt. border| Swt. MULTI);
Table table = Tv.gettable ();
Table.setheadervisible (TRUE);
Table.setlinesvisible (TRUE);
Tablelayout layout = new Tablelayout ();
Table.setlayout (layout);
Layout.addcolumndata (New Columnweightdata (111));
New TableColumn (TABLE,SWT. NONE). SetText ("ID number");
Layout.addcolumndata (New Columnweightdata (40));
New TableColumn (TABLE,SWT. NONE). SetText ("name");
Layout.addcolumndata (New Columnweightdata (20));
New TableColumn (TABLE,SWT. NONE). SetText ("gender");
Layout.addcolumndata (New Columnweightdata (20));
New TableColumn (TABLE,SWT. NONE). SetText ("Age");
Layout.addcolumndata (New Columnweightdata (60));
New TableColumn (TABLE,SWT. NONE). SetText ("Record settling Time");
List List = new ArrayList ();
List.add ("1");
List.add ("stone");
List.add ("male");
List.add ("35");
List.add ("2016.7.27");
Tv.add (list);
Tv.add (list);
Tv.add (list);
Getsite (). Setselectionprovider (TV);
}
/**
* Passing the focus request to the viewer's control.
*/
public void SetFocus () {
}
}
/**
************************************
View2 class
**/
Package viewsconnection.views;
Import Org.eclipse.jface.action.MenuManager;
Import Org.eclipse.jface.dialogs.MessageDialog;
Import Org.eclipse.jface.internal.MenuManagerEventHelper;
Import org.eclipse.jface.viewers.ISelection;
Import Org.eclipse.swt.events.SelectionAdapter;
Import org.eclipse.swt.events.SelectionEvent;
Import Org.eclipse.swt.layout.FillLayout;
Import Org.eclipse.swt.widgets.Composite;
Import org.eclipse.swt.widgets.List;
Import Org.eclipse.swt.widgets.Menu;
Import Org.eclipse.swt.widgets.Text;
Import Org.eclipse.ui.ISelectionListener;
Import Org.eclipse.ui.IViewPart;
Import Org.eclipse.ui.IWorkbenchPage;
Import Org.eclipse.ui.IWorkbenchPart;
Import org.eclipse.ui.part.*;
Import Org.eclipse.swt.SWT;
Import Viewsconnection.actions.AddActionGroup;
public class View2 extends Viewpart {
/**
* This was a callback that would allow us
* To create the viewer and initialize it.
*/
public void Createpartcontrol (Composite parent) {
Final Composite topcom = new Composite (PARENT,SWT. NONE);
Topcom.setlayout (New Filllayout ());
Final list List = new List (TOPCOM,SWT. BORDER);
List.add ("111");
List.add ("222");
List.add ("333");
Getsite (). GetPage (). Addselectionlistener (New Iselectionlistener () {
public void SelectionChanged (Iworkbenchpart part, iselection selection) {
TODO auto-generated Method Stub
String PartID = Part.getsite (). GetId ();
if (Partid.equals ("Viewsconnection.views.View1")) {
Messagedialog.openinformation (NULL, "Snap TableView", "Capture TableView success!");
List.add (Part.gettitle ());
List.add (Selection.tostring ());
}
}
});
Addactiongroup AG = new Addactiongroup ();
Ag.fillactionbars (Getviewsite (). Getactionbars ());
Ag.fillcontextmenu (Getviewsite (). Getactionbars (). Getmenumanager ());
Menumanager manager = new Menumanager ();
Menu menu = Manager.createcontextmenu (list);
List.setmenu (menu);
Ag.fillcontextmenu (manager);
}
/**
* Passing the focus request to the viewer's control.
*/
public void SetFocus () {
}
}
Click Run, Effect
Eclipse plug-in development learning note "5"---add drop-down menus and buttons to the view