Package net.jmesnil.jmx.ui.internal.views.navigator

Source Code of net.jmesnil.jmx.ui.internal.views.navigator.MBeanExplorerContentProvider$DelayProxy

/*******************************************************************************
* Copyright (c) 2006 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.views.navigator;

import java.util.HashMap;

import net.jmesnil.jmx.core.ExtensionManager;
import net.jmesnil.jmx.core.IConnectionProviderListener;
import net.jmesnil.jmx.core.IConnectionWrapper;
import net.jmesnil.jmx.core.MBeanFeatureInfoWrapper;
import net.jmesnil.jmx.core.tree.DomainNode;
import net.jmesnil.jmx.core.tree.Node;
import net.jmesnil.jmx.core.tree.ObjectNameNode;
import net.jmesnil.jmx.core.tree.Root;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;

/**
* Content provider for the view
*/
public class MBeanExplorerContentProvider implements IConnectionProviderListener,
        IStructuredContentProvider, ITreeContentProvider {
 
  public static class DelayProxy {
    public IConnectionWrapper wrapper;
    public DelayProxy(IConnectionWrapper wrapper) {
      this.wrapper = wrapper;
    }
  }
 
  private Viewer viewer;
  private HashMap<IConnectionWrapper, DelayProxy> loading;
    public MBeanExplorerContentProvider() {
      ExtensionManager.addConnectionProviderListener(this);
      loading = new HashMap<IConnectionWrapper, DelayProxy>();
    }

    public void inputChanged(Viewer v, Object oldInput, Object newInput) {
      this.viewer = v;
    }

    public void dispose() {
    }

    public Object[] getElements(Object parent) {
        return getChildren(parent);
    }

    public Object getParent(Object child) {
        if (child instanceof Node) {
            Node node = (Node) child;
            return node.getParent();
        }
        return null;
    }

    public Object[] getChildren(Object parent) {
      if( parent == null ) return new Object[] {};
    if( parent instanceof IViewPart ) {
      return ExtensionManager.getAllConnections();
    }
    if( parent instanceof IConnectionWrapper && ((IConnectionWrapper)parent).isConnected()) {
      return loadAndGetRootChildren(parent);
    }
        if (parent instanceof Root) {
            Root root = (Root) parent;
            return root.getChildren();
        }
        if (parent instanceof DomainNode) {
            DomainNode node = (DomainNode) parent;
            return node.getChildren();
        }
        if (parent instanceof ObjectNameNode) {
            ObjectNameNode node = (ObjectNameNode) parent;
            return node.getMbeanInfoWrapper().getMBeanFeatureInfos();
        }
        if (parent instanceof Node) {
            Node node = (Node) parent;
            return node.getChildren();
        }
        return new Object[0];
    }

    protected synchronized Object[] loadAndGetRootChildren(final Object parent) {
    final IConnectionWrapper w = (IConnectionWrapper)parent;
   
    if( w.getRoot() != null )
      return getChildren(w.getRoot());
   
    // Must load the model
    Thread t = new Thread() {
      public void run() {
        try {
          w.loadRoot();
        } catch( RuntimeException re ) {
        }
        loading.remove(w);
        Display.getDefault().asyncExec(new Runnable() {
          public void run() {
            if( viewer instanceof StructuredViewer)
              ((StructuredViewer)viewer).refresh(parent);
            else
              viewer.refresh();
          }
        });
      }
    };
   
    if( loading.containsKey(((IConnectionWrapper)parent))) {
      return new Object[] { loading.get((IConnectionWrapper)parent)};
    }
    DelayProxy p = new DelayProxy(w);
    loading.put(w, p);
    t.start();
    return new Object[] { p };
    }

    public boolean hasChildren(Object parent) {
        if (parent instanceof ObjectNameNode) {
            ObjectNameNode node = (ObjectNameNode) parent;
            return (node.getMbeanInfoWrapper().getMBeanFeatureInfos().length > 0);
        }
        if (parent instanceof Node) {
            Node node = (Node) parent;
            return (node.getChildren().length > 0);
        }
        if (parent instanceof MBeanFeatureInfoWrapper) {
            return false;
        }
        if( parent instanceof IConnectionWrapper ) {
          return ((IConnectionWrapper)parent).isConnected();
        }
        return true;
    }

  public void connectionAdded(IConnectionWrapper connection) {
    fireRefresh(connection, true);
  }

  public void connectionChanged(IConnectionWrapper connection) {
    fireRefresh(connection, false);
  }

  public void connectionRemoved(IConnectionWrapper connection) {
    fireRefresh(connection, true);
  }

  private void fireRefresh(final IConnectionWrapper connection, final boolean full) {
    Display.getDefault().asyncExec(new Runnable() {
      public void run() {
        if( viewer != null ) {
          if(full || !(viewer instanceof StructuredViewer))
            viewer.refresh();
          else
            ((StructuredViewer)viewer).refresh(connection);
        }
      }
    });
  }
}
TOP

Related Classes of net.jmesnil.jmx.ui.internal.views.navigator.MBeanExplorerContentProvider$DelayProxy

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.