Package com.subhajit.eclipse.util

Source Code of com.subhajit.eclipse.util.ViewUtils

package com.subhajit.eclipse.util;

import java.util.NoSuchElementException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.IViewDescriptor;

public class ViewUtils {
  private ViewUtils() {
    super();
  }

  /**
   * Shows the view identified by <code>viewId</code>.
   *
   * <p>
   * Since some views created in this and other projects use Swing components,
   * and since those components are created asynchronously, a view my not be
   * ready to use immediately after it is shown if it was not shown earlier.
   * </p>
   *
   * @param viewId
   * @throws PartInitException
   */
  public static void showView(final String viewId) throws PartInitException {
    if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() == null) {
      Display display = PlatformUI.getWorkbench().getDisplay();
      display.asyncExec(new Runnable() {
        public void run() {
          try {
            PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                .getActivePage().showView(viewId);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      });
    } else {
      PlatformUI.getWorkbench().getActiveWorkbenchWindow()
          .getActivePage().showView(viewId);
    }
  }

  /**
   * Returns the {@link IViewPart} for the given <code>viewId</code>.
   *
   * @param viewId
   * @return
   * @throws CoreException
   *             Rethrown from {@link IViewDescriptor#createView()}.
   * @throws NoSuchElementException
   *             If no view with id <tt>viewId</tt> is found in the view
   *             registry.
   */
  public static IViewPart getView(String viewId) throws CoreException,
      NoSuchElementException {
    IViewDescriptor descriptor = PlatformUI.getWorkbench()
        .getViewRegistry().find(viewId);
    if (descriptor == null) {
      throw new NoSuchElementException("No view with id - " + viewId);
    }
    return descriptor.createView();
  }

}
TOP

Related Classes of com.subhajit.eclipse.util.ViewUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.