Source einer Beispielapplikation
Der folgende Source ist Basis für viele der im RCP-Teil dieses Wikis aufgeführten Codeschippsel:
MyEditor.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.PlatformObject; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IPersistableElement; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.forms.editor.FormEditor; import org.eclipse.ui.forms.editor.IFormPage; public class MyEditor extends FormEditor{ public static final String ID = "com.sowas.javawiki.rcpbaseapplication.MyEditor"; public MyEditor() { super(); } @Override protected void addPages(){ try{ addPage(new MyEditorPage(this, "PageId-1", "MyEditorPage-1")); addPage(new MyEditorPage(this, "PageId-2", "MyEditorPage-2")); }catch (PartInitException e){ e.printStackTrace(); } } @Override public void doSave(IProgressMonitor monitor){ } @Override public void doSaveAs(){ } @Override public boolean isSaveAsAllowed(){ return false; } public static IEditorPart openEditor(final Object myDataObject) { final IWorkbenchPage wbp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); final ObjectHolder editorHolder = new ObjectHolder(); Display.getCurrent().asyncExec(new Runnable() { public void run() { final MyEditorInput input = new MyEditorInput(myDataObject); IEditorPart editor; try{ editor = wbp.openEditor(input, ID); }catch (PartInitException e){ // TODO Auto-generated catch block e.printStackTrace(); editor = null; } editorHolder.setObj(editor); } }); IEditorPart editorPart = (IEditorPart) editorHolder.getObj(); return editorPart; } } class MyEditorInput extends PlatformObject implements IEditorInput { Object obj; public MyEditorInput(Object obj) { this.obj = obj; } public boolean exists(){ return false; } public ImageDescriptor getImageDescriptor(){ return null; } public String getName(){ return obj.toString(); } public IPersistableElement getPersistable(){ return null; } public String getToolTipText(){ return obj.toString(); } } class ObjectHolder { private Object obj = null; public Object getObj(){ return obj; } public void setObj(Object obj){ this.obj = obj; } }
MyEditorPage.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.forms.IManagedForm; import org.eclipse.ui.forms.editor.FormEditor; import org.eclipse.ui.forms.editor.FormPage; import org.eclipse.ui.forms.editor.IFormPage; import org.eclipse.ui.forms.widgets.FormToolkit; public class MyEditorPage extends FormPage implements IFormPage{ public MyEditorPage(FormEditor editor, String pageId, String pageTitle){ super(editor, pageId, pageTitle); } @Override protected void createFormContent(IManagedForm managedForm) { super.createFormContent(managedForm); Composite client = managedForm.getForm().getBody(); client.setLayout(new GridLayout(2, false)); FormToolkit formToolkit = managedForm.getToolkit(); formToolkit.createLabel(client, "Name:"); formToolkit.createText(client, "", SWT.SINGLE|SWT.BORDER); } }
MyView.java:
package com.sowas.javawiki.rcpbaseapplication; import java.util.ArrayList; import java.util.List; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.ITreeSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Tree; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; public class MyView extends ViewPart { public static final String ID = "com.sowas.javawiki.rcpbaseapplication.MyView"; private TreeViewer m_treeViewer; /** * Erstellt die Controls für diesen View, also den Tree */ public void createPartControl(Composite parent) { Tree addressTree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); m_treeViewer = new TreeViewer(addressTree); m_treeViewer.setContentProvider(new AddressContentProvider()); m_treeViewer.setLabelProvider(new ViewLabelProvider()); // Der LabelProvider verwendet die toString()-Methode List<City> cities = new ArrayList<City>(); cities.add(new City()); m_treeViewer.setInput(cities); m_treeViewer.expandAll(); m_treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event){ System.out.println(event.getSelection()); MyEditor.openEditor(((ITreeSelection)event.getSelection()).getFirstElement()); } }); } public void setFocus() { m_treeViewer.getControl().setFocus(); } /** * Wird hier nur verwendet, um Symbole zu liefern */ private class ViewLabelProvider extends LabelProvider implements ILabelProvider { public String getColumnText(Object obj, int index) { return getText(obj); // getText(..) ruft toString() des Objekts auf } public Image getColumnImage(Object obj, int index) { return getImage(obj); } public Image getImage(Object obj) { return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); } } /** * Der Provider für die Inhalte des Tree */ private class AddressContentProvider implements ITreeContentProvider { /** * Liefert die Kinder eines Elements */ public Object[] getChildren(Object parentElement) { if (parentElement instanceof List) return ((List<?>) parentElement).toArray(); if (parentElement instanceof City) return ((City) parentElement).getStreets(); if (parentElement instanceof Street) return ((Street) parentElement).getHouses(); return new Object[0]; } /** * Liefert die Eltern eines Elements */ public Object getParent(Object element) { if (element instanceof Street) return ((Street)element).city; if (element instanceof House) return ((House)element).street; return null; } /** * Liefert, ob ein Element Kinder hat */ public boolean hasChildren(Object element) { if (element instanceof List) return ((List<?>) element).size() > 0; if (element instanceof City) return ((City) element).getStreets().length > 0; if (element instanceof Street) return ((Street) element).getHouses().length > 0; return false; } /** * Liefert die Root-Elemente * Kann daher meist getChildren() aufrufen */ public Object[] getElements(Object inputElement) { return getChildren(inputElement); } public void dispose() { } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } } /** * Definiert eine Stadt, welche mehrere Straßen hat */ private class City{ Street[] streets = new Street[3]; public City(){ for (int i = 0; i<streets.length; i++) streets[i] = new Street(this, i); } public Street[] getStreets(){ return streets; } public String toString(){ return "Küchenhausen"; } } /** * Definiert eine Straße, welche Häuser hat und zu einer Stadt gehört */ private class Street{ City city; House[] houses = new House[2]; int indx; public Street(City city, int index){ this.city = city; indx = index; for (int i = 0; i<houses.length; i++) houses[i] = new House(this, i); } public House[] getHouses(){ return houses; } public String toString(){ switch (indx) { case 0: return "Topfstraße"; case 1: return "Herdweg"; case 2: return "Löffelstraße"; default: throw new RuntimeException("Straße nicht definiert"); } } } /** * Definiert ein Haus, welches zu einer Straße gehört */ private class House{ Street street; int indx; public House(Street street, int i){ this.street = street; indx = i + 1; } public String toString(){ return "Haus " + indx; } } }
MyPerspective.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.ui.IPageLayout; import org.eclipse.ui.IPerspectiveFactory; public class MyPerspective implements IPerspectiveFactory { public void createInitialLayout(IPageLayout layout) { String editorArea = layout.getEditorArea(); layout.setEditorAreaVisible(true); layout.addView(MyView.ID, IPageLayout.LEFT, 0.35f, editorArea); } }
plugin.xml:
<?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.2"?> <plugin> <extension id="application" point="org.eclipse.core.runtime.applications"> <application> <run class="com.sowas.javawiki.rcpbaseapplication.Application"> </run> </application> </extension> <extension point="org.eclipse.ui.perspectives"> <perspective name="Perspective" class="com.sowas.javawiki.rcpbaseapplication.MyPerspective" id="BaseApplication.perspective"> </perspective> </extension> <extension point="org.eclipse.ui.views"> <view name="MyView" class="com.sowas.javawiki.rcpbaseapplication.MyView" id="com.sowas.javawiki.rcpbaseapplication.MyView"> </view> </extension> <extension point="org.eclipse.ui.editors"> <editor class="com.sowas.javawiki.rcpbaseapplication.MyEditor" default="false" id="com.sowas.javawiki.rcpbaseapplication.MyEditor" name="MyEditor"> </editor> </extension> <!-- <extension point="org.eclipse.ui.editors"> <editor class="com.sowas.javawiki.rcpbaseapplication.MyEditor" default="true" id="com.sowas.javawiki.rcpbaseapplication.MyEditor" name="MyEditor"> </editor> </extension> --> </plugin>
Activator.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; /** * The activator class controls the plug-in life cycle */ public class Activator extends AbstractUIPlugin { // The plug-in ID public static final String PLUGIN_ID = "BaseApplication"; // The shared instance private static Activator plugin; /** * The constructor */ public Activator() { } /* * (non-Javadoc) * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) */ public void start(BundleContext context) throws Exception { super.start(context); plugin = this; } /* * (non-Javadoc) * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { plugin = null; super.stop(context); } /** * Returns the shared instance * * @return the shared instance */ public static Activator getDefault() { return plugin; } /** * Returns an image descriptor for the image file at the given * plug-in relative path * * @param path the path * @return the image descriptor */ public static ImageDescriptor getImageDescriptor(String path) { return imageDescriptorFromPlugin(PLUGIN_ID, path); } }
Application.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplicationContext; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.PlatformUI; /** * This class controls all aspects of the application's execution */ public class Application implements IApplication { /* (non-Javadoc) * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext) */ public Object start(IApplicationContext context) { Display display = PlatformUI.createDisplay(); try { int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()); if (returnCode == PlatformUI.RETURN_RESTART) { return IApplication.EXIT_RESTART; } return IApplication.EXIT_OK; } finally { display.dispose(); } } /* (non-Javadoc) * @see org.eclipse.equinox.app.IApplication#stop() */ public void stop() { final IWorkbench workbench = PlatformUI.getWorkbench(); if (workbench == null) return; final Display display = workbench.getDisplay(); display.syncExec(new Runnable() { public void run() { if (!display.isDisposed()) workbench.close(); } }); } }
ApplicationActionBarAdvisor.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.actions.ActionFactory; import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; /** * An action bar advisor is responsible for creating, adding, and disposing of * the actions added to a workbench window. Each window will be populated with * new actions. */ public class ApplicationActionBarAdvisor extends ActionBarAdvisor { // Actions - important to allocate these only in makeActions, and then use // them // in the fill methods. This ensures that the actions aren't recreated // when fillActionBars is called with FILL_PROXY. private IWorkbenchAction exitAction; public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) { super(configurer); } protected void makeActions(final IWorkbenchWindow window) { // Creates the actions and registers them. // Registering is needed to ensure that key bindings work. // The corresponding commands keybindings are defined in the plugin.xml // file. // Registering also provides automatic disposal of the actions when // the window is closed. exitAction = ActionFactory.QUIT.create(window); register(exitAction); } protected void fillMenuBar(IMenuManager menuBar) { MenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE); menuBar.add(fileMenu); fileMenu.add(exitAction); } }
ApplicationWorkbenchAdvisor.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; import org.eclipse.ui.application.WorkbenchAdvisor; import org.eclipse.ui.application.WorkbenchWindowAdvisor; public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor { private static final String PERSPECTIVE_ID = "BaseApplication.perspective"; public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor( IWorkbenchWindowConfigurer configurer) { return new ApplicationWorkbenchWindowAdvisor(configurer); } public String getInitialWindowPerspectiveId() { return PERSPECTIVE_ID; } }
ApplicationWorkbenchWindowAdvisor.java:
package com.sowas.javawiki.rcpbaseapplication; import org.eclipse.swt.graphics.Point; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; import org.eclipse.ui.application.WorkbenchWindowAdvisor; public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { super(configurer); } public ActionBarAdvisor createActionBarAdvisor( IActionBarConfigurer configurer) { return new ApplicationActionBarAdvisor(configurer); } public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(400, 300)); configurer.setShowCoolBar(false); configurer.setShowStatusLine(false); configurer.setTitle("BaseApplication"); } }