Package net.jmesnil.jmx.ui.internal.actions

Source Code of net.jmesnil.jmx.ui.internal.actions.DoubleClickAction

/*******************************************************************************
* Copyright (c) 2008 Jeff Mesnil
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    "Rob Stryker" <rob.stryker@redhat.com> - Initial implementation
*******************************************************************************/

package net.jmesnil.jmx.ui.internal.actions;

import net.jmesnil.jmx.core.IConnectionWrapper;
import net.jmesnil.jmx.ui.internal.EditorUtils;
import net.jmesnil.jmx.ui.internal.editors.EditorConnectionMapping;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.navigator.CommonViewer;

/**
* The double click action
*/
public class DoubleClickAction extends Action implements
    ISelectionChangedListener {
  private ISelection selection;
  private CommonViewer viewer;
  protected EditorConnectionMapping mapping;
  public DoubleClickAction() {
    mapping = new EditorConnectionMapping();
  }
  public EditorConnectionMapping getMapping() {
    return mapping;
  }
  public void selectionChanged(SelectionChangedEvent event) {
    this.selection = event.getSelection();
    viewer = (CommonViewer)event.getSource();
  }

  public void run() {
    if( selection == null )
      return;

    StructuredSelection structured = (StructuredSelection) selection;
    Object element = structured.getFirstElement();
    viewer.expandToLevel(element, 1);
    IEditorInput editorInput = EditorUtils.getEditorInput(element);
    if (editorInput != null) {
      IEditorPart editor = EditorUtils
          .openMBeanEditor(editorInput);
      if (editor != null) {
        EditorUtils.revealInEditor(editor, element);
        editor.setFocus();
        mapping.open(findParent(structured), editor);
      }
    }
  }
 
  protected IConnectionWrapper findParent(IStructuredSelection sel) {
    if( sel instanceof TreeSelection ) {
      TreeSelection sel2 = ((TreeSelection)sel);
      TreePath[] paths = sel2.getPathsFor(sel.getFirstElement());
      if( paths != null && paths.length == 1 ) {
        if( paths[0].getFirstSegment() instanceof IConnectionWrapper )
          return (IConnectionWrapper) paths[0].getFirstSegment();
      }
    }
    return null;
  }
}
TOP

Related Classes of net.jmesnil.jmx.ui.internal.actions.DoubleClickAction

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.