01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.examples.contributions.view;
11:
12: import org.eclipse.core.commands.AbstractHandler;
13: import org.eclipse.core.commands.ExecutionEvent;
14: import org.eclipse.core.commands.ExecutionException;
15: import org.eclipse.jface.viewers.ISelection;
16: import org.eclipse.jface.viewers.IStructuredSelection;
17: import org.eclipse.ui.IWorkbenchWindow;
18: import org.eclipse.ui.PartInitException;
19: import org.eclipse.ui.examples.contributions.Activator;
20: import org.eclipse.ui.examples.contributions.ContributionMessages;
21: import org.eclipse.ui.examples.contributions.model.PersonInput;
22: import org.eclipse.ui.handlers.HandlerUtil;
23:
24: /**
25: * open an element for editing.
26: *
27: * @since 3.3
28: */
29: public class EditInfoHandler extends AbstractHandler {
30:
31: private static final String EDITOR_ID = "org.eclipse.ui.examples.contributions.editor"; //$NON-NLS-1$
32:
33: /*
34: * (non-Javadoc)
35: *
36: * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
37: */
38: public Object execute(ExecutionEvent event)
39: throws ExecutionException {
40: IWorkbenchWindow window = HandlerUtil
41: .getActiveWorkbenchWindow(event);
42: if (window == null) {
43: return null;
44: }
45: // this is written to check a context menu selection, and if not take
46: // the standard application context selection.
47: ISelection sel = HandlerUtil.getActiveMenuSelection(event);
48: if (sel == null) {
49: sel = HandlerUtil.getCurrentSelection(event);
50: }
51: if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
52: IStructuredSelection selection = (IStructuredSelection) sel;
53: int personIndex = Activator.getModel().indexOf(
54: selection.getFirstElement());
55: PersonInput input = new PersonInput(personIndex);
56: try {
57: window.getActivePage().openEditor(input, EDITOR_ID);
58: } catch (PartInitException e) {
59: throw new ExecutionException(
60: ContributionMessages.EditInfoHandler_failed_to_open,
61: e);
62: }
63: }
64: return null;
65: }
66:
67: }
|