Design-Question regarding Actions and th UI [message #466594] |
Sun, 22 April 2007 13:48  |
Eclipse User |
|
|
|
Hi
I have several Action-Classes in my RCP (no local classes) for adding
and removing objects from a ListViewer. My problem is that after I
removed (or added) a Object from (to) the list I need to refresh my
ListViewer but I don't have a good idea how get access to it. I was
thinking of something like a Registry in my Plugin-class (which is
accesseble from everywhere).
Is this a good idea or are there better?
I also don't want to take references to the viewer with me over 3 classes.
And another question: is there another possibility to set a viewers new
content (refresh the old one) instead of calling setInput with a
complete new collection?
|
|
|
|
|
|
Re: Design-Question regarding Actions and th UI [message #466630 is a reply to message #466625] |
Mon, 23 April 2007 07:18   |
Eclipse User |
|
|
|
Hi,
More informations:
http://wiki.eclipse.org/index.php/Platform_Command_Framework
plugin.xml
----------
> <extension
> point="org.eclipse.ui.views">
> <view
> name="View"
> class="testcommandframework.View"
> id="TestCommandFramework.view">
> </view>
> </extension>
> <extension
> point="org.eclipse.ui.menus">
> <menuContribution
> locationURI="menu:file">
> <command
> commandId="TestCommandFramework.addEntry"
> label="Add Entry">
> </command>
> </menuContribution>
> </extension>
> <extension
> point="org.eclipse.ui.commands">
> <command
> defaultHandler="testcommandframework.commands.AddEntryCommandHandler "
> id="TestCommandFramework.addEntry"
> name="Add Entry">
> </command>
> </extension>
View.java:
----------
> public class View extends ViewPart {
> public static final String ID = "TestCommandFramework.view";
>
> private TableViewer viewer;
>
> private class CommandListener implements IExecutionListener {
>
> public void notHandled(String commandId, NotHandledException exception) {
>
> }
>
> public void postExecuteFailure(String commandId,
> ExecutionException exception) {
>
> }
>
> public void postExecuteSuccess(String commandId, Object returnValue) {
> if( commandId.equals(AddEntryCommandHandler.ID) ) {
> viewer.add(returnValue);
> }
> }
>
> public void preExecute(String commandId, ExecutionEvent event) {
>
> }
>
> }
>
> private CommandListener listener = new CommandListener();
>
> class ViewContentProvider implements IStructuredContentProvider {
> public void inputChanged(Viewer v, Object oldInput, Object newInput) {
> }
>
> public void dispose() {
> }
>
> public Object[] getElements(Object parent) {
> return new String[] { "One", "Two", "Three" };
> }
> }
>
> class ViewLabelProvider extends LabelProvider implements
> ITableLabelProvider {
> public String getColumnText(Object obj, int index) {
> return getText(obj);
> }
>
> public Image getColumnImage(Object obj, int index) {
> return getImage(obj);
> }
>
> public Image getImage(Object obj) {
> return PlatformUI.getWorkbench().getSharedImages().getImage(
> ISharedImages.IMG_OBJ_ELEMENT);
> }
> }
>
> public void createPartControl(Composite parent) {
> viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
> | SWT.V_SCROLL);
> viewer.setContentProvider(new ViewContentProvider());
> viewer.setLabelProvider(new ViewLabelProvider());
> viewer.setInput(getViewSite());
>
> ICommandService service = (ICommandService)PlatformUI.getWorkbench().getService(IComma ndService.class);
> Command cmd = service.getCommand(AddEntryCommandHandler.ID);
> cmd.addExecutionListener(listener);
> }
>
> @Override
> public void dispose() {
> ICommandService service = (ICommandService)PlatformUI.getWorkbench().getService(IComma ndService.class);
> Command cmd = service.getCommand(AddEntryCommandHandler.ID);
> cmd.removeExecutionListener(listener);
> }
>
> public void setFocus() {
> viewer.getControl().setFocus();
> }
> }
AddCommandHandler.java
----------------------
> public class AddEntryCommandHandler extends AbstractHandler {
> public static final String ID = "TestCommandFramework.addEntry";
>
> @Override
> public Object execute(ExecutionEvent arg0) throws ExecutionException {
> Object rv;
> if( arg0.getApplicationContext() != null
> && arg0.getApplicationContext() instanceof EvaluationContext
> && (rv = ((EvaluationContext)arg0.getApplicationContext()).getVariabl e(Activator.PLUGIN_ID+ "_ADD_ENTRY")) != null ) {
> return rv;
> } else {
> return "newEntry - " + System.currentTimeMillis();
> }
> }
>
> }
AddAction.java
--------------
> public class AddAction extends Action {
> public AddAction() {
> super("add entry");
> }
>
> @Override
> public void run() {
> ICommandService service = (ICommandService)PlatformUI.getWorkbench().getService(IComma ndService.class);
> Command cmd = service.getCommand(AddEntryCommandHandler.ID);
> EvaluationContext context = new EvaluationContext(null,ISources.ACTIVE_WORKBENCH_WINDOW);
> context.addVariable(ISources.ACTIVE_WORKBENCH_WINDOW+"", PlatformUI.getWorkbench().getActiveWorkbenchWindow());
> context.addVariable(Activator.PLUGIN_ID+"_ADD_ENTRY", "new entry - action - " + System.currentTimeMillis());
>
> ExecutionEvent evt = new ExecutionEvent(cmd,new HashMap<String, String>(),null,context);
>
> try {
> cmd.executeWithChecks(evt);
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
ApplicationActionBarAdvisor.java
--------------------------------
> public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
>
> private IWorkbenchAction exitAction;
>
> public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
> super(configurer);
> }
>
> protected void makeActions(final IWorkbenchWindow window) {
> 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);
> }
>
> @Override
> protected void fillCoolBar(ICoolBarManager coolBar) {
> ToolBarManager mgr = new ToolBarManager();
> mgr.add(new AddAction());
> coolBar.add(mgr);
> }
> }
You notice 2 things:
- I used the new menu-extension-point to directly execute a command with
out the need of an intermediate ActionDelegate
- I show you how you can handle use an Action to execute your command
I'd prefer solution 1 because it is more flexible than using the
Action/ActionDelegate-Interface.
Tom
|
|
|
|
|
Re: Design-Question regarding Actions and th UI [message #466640 is a reply to message #466638] |
Mon, 23 April 2007 11:19   |
Eclipse User |
|
|
|
Hi,
by the way the menu-extension point is the new standard way to add
things to menus, toolbars, ... . But using Actions is not wrong and when
runing on 3.2 you are forced to use this mechanism.
Tom
Tom Schindl schrieb:
> Hi,
>
> that's what I demonstrated for you in the Action which I added manually
> to the ToolBar.
>
> Isn't is enough for you to just copy the contents to your
> toolseye.vehiclemanagement.actions.NewVehicleAction class?
>
> Eventually this code has to be executed:
>
>> ICommandService service =
>> (ICommandService)PlatformUI.getWorkbench().getService(IComma ndService.class);
>>
>> Command cmd = service.getCommand(AddEntryCommandHandler.ID);
>> EvaluationContext context = new
>> EvaluationContext(null,ISources.ACTIVE_WORKBENCH_WINDOW);
>> context.addVariable(ISources.ACTIVE_WORKBENCH_WINDOW+"",
>> PlatformUI.getWorkbench().getActiveWorkbenchWindow());
>> ExecutionEvent evt = new ExecutionEvent(cmd,new
>> HashMap<String, String>(),null,context);
>> try {
>> cmd.executeWithChecks(evt);
>> } catch (Exception e) {
>> e.printStackTrace();
>> }
>
> I'll attach you the whole RCP-App I used to extract what I posted before.
>
> Tom
|
|
|
|
|
|
|
Re: Design-Question regarding Actions and th UI [message #467561 is a reply to message #466775] |
Mon, 07 May 2007 08:41  |
Eclipse User |
|
|
|
What does this mean for the ActionBarContributor interface and using the
contributorClass element of the editor extension point? How does this
still fit in?
Do you handle more sophisticated enablement and disenablement in this
class now and handle (more than before) creating actions and menus etc.
in the XML directly?
Paul Webster wrote:
> Marc Schlegel wrote:
>> I dont know how to use this menuContribution with an <command...> since
>> I am using an actionSet and a popupMenu to extend my New-menu which is
>> provided by my main RCP-plugin.
>>
>> This is a part from my plugin.xml
>> ---------------------------------------
>> <extension
....
>> label="%newVehicleActionLabel"
>> menubarPath="additions"/>
>> </viewerContribution>
>> ---------------------------------------
>
> Like Tom mentioned, if you are in 3.2 you can continue to use actions.
> An actionSets action has access to the IWorkbenchWindow, which means you
> can do a window.getActivePage().findView(*) to find your view.
>
> Your popupMenus action should have been given the active part (your
> view), no?
>
> In 3.3 you can write thing slightly differently using
> org.eclipse.ui.commands to define your command, org.eclipse.ui.handlers
> to provide an implementation, and org.eclipse.ui.menus to provide menu
> items/toolbar items for your command.
>
>
> PW
|
|
|
Powered by
FUDForum. Page generated in 0.05627 seconds