001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.viewers.ISelectionProvider;
015: import org.eclipse.ui.IEditorPart;
016: import org.eclipse.ui.IViewPart;
017: import org.eclipse.ui.IWorkbenchPage;
018: import org.eclipse.ui.IWorkbenchPart;
019: import org.eclipse.ui.IWorkbenchWindow;
020: import org.eclipse.ui.PartInitException;
021: import org.eclipse.ui.internal.util.Util;
022: import org.eclipse.ui.part.IShowInSource;
023: import org.eclipse.ui.part.IShowInTarget;
024: import org.eclipse.ui.part.ShowInContext;
025: import org.eclipse.ui.views.IViewDescriptor;
026:
027: /**
028: * Action for a particular target in the Show In menu.
029: */
030: public class ShowInAction extends Action {
031: private IWorkbenchWindow window;
032:
033: private IViewDescriptor desc;
034:
035: /**
036: * Creates a new <code>ShowInAction</code>.
037: */
038: protected ShowInAction(IWorkbenchWindow window, IViewDescriptor desc) {
039: super (desc.getLabel());
040: setImageDescriptor(desc.getImageDescriptor());
041: window.getWorkbench().getHelpSystem().setHelp(this ,
042: IWorkbenchHelpContextIds.SHOW_IN_ACTION);
043: this .window = window;
044: this .desc = desc;
045: }
046:
047: /**
048: * Shows the current context in this action's view.
049: */
050: public void run() {
051: IWorkbenchPage page = window.getActivePage();
052: if (page == null) {
053: beep();
054: return;
055: }
056:
057: IWorkbenchPart sourcePart = page.getActivePart();
058: if (sourcePart == null) {
059: beep();
060: return;
061: }
062:
063: ShowInContext context = getContext(sourcePart);
064: if (context == null) {
065: beep();
066: return;
067: }
068:
069: try {
070: IViewPart view = page.showView(desc.getId());
071: IShowInTarget target = getShowInTarget(view);
072: if (target != null && target.show(context)) {
073: // success
074: } else {
075: beep();
076: }
077: ((WorkbenchPage) page).performedShowIn(desc.getId()); // TODO: move back up
078: } catch (PartInitException e) {
079: WorkbenchPlugin
080: .log(
081: "Error showing view in ShowInAction.run", e.getStatus()); //$NON-NLS-1$
082: }
083: }
084:
085: /**
086: * Returns the <code>IShowInSource</code> provided by the source part,
087: * or <code>null</code> if it does not provide one.
088: *
089: * @param sourcePart the source part
090: * @return an <code>IShowInSource</code> or <code>null</code>
091: */
092: private IShowInSource getShowInSource(IWorkbenchPart sourcePart) {
093: return (IShowInSource) Util.getAdapter(sourcePart,
094: IShowInSource.class);
095: }
096:
097: /**
098: * Returns the <code>IShowInTarget</code> for the given part,
099: * or <code>null</code> if it does not provide one.
100: *
101: * @param targetPart the target part
102: * @return the <code>IShowInTarget</code> or <code>null</code>
103: */
104: private IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
105: return (IShowInTarget) Util.getAdapter(targetPart,
106: IShowInTarget.class);
107: }
108:
109: /**
110: * Returns the <code>ShowInContext</code> to show in the selected target,
111: * or <code>null</code> if there is no valid context to show.
112: * <p>
113: * This implementation obtains the context from the <code>IShowInSource</code>
114: * of the source part (if provided), or, if the source part is an editor,
115: * it creates the context from the editor's input and selection.
116: * <p>
117: * Subclasses may extend or reimplement.
118: *
119: * @return the <code>ShowInContext</code> to show or <code>null</code>
120: */
121: private ShowInContext getContext(IWorkbenchPart sourcePart) {
122: IShowInSource source = getShowInSource(sourcePart);
123: if (source != null) {
124: ShowInContext context = source.getShowInContext();
125: if (context != null) {
126: return context;
127: }
128: } else if (sourcePart instanceof IEditorPart) {
129: Object input = ((IEditorPart) sourcePart).getEditorInput();
130: ISelectionProvider sp = sourcePart.getSite()
131: .getSelectionProvider();
132: ISelection sel = sp == null ? null : sp.getSelection();
133: return new ShowInContext(input, sel);
134: }
135: return null;
136: }
137:
138: /**
139: * Generates a system beep.
140: */
141: private void beep() {
142: window.getShell().getDisplay().beep();
143: }
144:
145: }
|