Package org.osmorc.run.ui

Source Code of org.osmorc.run.ui.BundleSelector$BundleTreeRenderer

/*
* Copyright (c) 2007-2009, Osmorc Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright notice, this list
*       of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright notice, this
*       list of conditions and the following disclaimer in the documentation and/or other
*       materials provided with the distribution.
*     * Neither the name of 'Osmorc Development Team' nor the names of its contributors may be
*       used to endorse or promote products derived from this software without specific
*       prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.osmorc.run.ui;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderEnumerator;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.TreeUIHelper;
import com.intellij.ui.treeStructure.SimpleTree;
import com.intellij.util.containers.ContainerUtil;
import icons.OsmorcIdeaIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.osgi.jps.build.CachingBundleInfoProvider;
import org.osmorc.facet.OsmorcFacet;
import org.osmorc.frameworkintegration.FrameworkInstanceDefinition;
import org.osmorc.frameworkintegration.FrameworkIntegrator;
import org.osmorc.frameworkintegration.FrameworkIntegratorRegistry;
import org.osmorc.i18n.OsmorcBundle;
import org.osmorc.make.BundleCompiler;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.util.List;

import static org.osmorc.frameworkintegration.FrameworkInstanceManager.FrameworkBundleType;

/**
* Dialog for selecting a bundle to be deployed.
*
* @author <a href="mailto:janthomae@janthomae.de">Jan Thom&auml;</a>
*/
public class BundleSelector extends DialogWrapper {
  private JPanel myContentPane;
  private SimpleTree myBundleTree;

  public BundleSelector(@NotNull Project project, @Nullable FrameworkInstanceDefinition instance, @NotNull List<SelectedBundle> toHide) {
    super(project);

    setTitle(OsmorcBundle.message("bundle.selector.title"));
    setModal(true);

    myContentPane.setPreferredSize(new Dimension(600, 400));
    myBundleTree.setModel(createModel(project, instance, toHide));
    myBundleTree.setCellRenderer(new BundleTreeRenderer());
    myBundleTree.addTreeSelectionListener(new TreeSelectionListener() {
      @Override
      public void valueChanged(TreeSelectionEvent e) {
        setOKActionEnabled(myBundleTree.getSelectionCount() > 0);
      }
    });
    for (int i = 0; i < myBundleTree.getRowCount(); i++) myBundleTree.expandRow(i);
    TreeUIHelper.getInstance().installTreeSpeedSearch(myBundleTree);

    init();
  }

  private static TreeModel createModel(Project project, FrameworkInstanceDefinition instance, List<SelectedBundle> toHide) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();

    // all the modules
    DefaultMutableTreeNode moduleNode = new DefaultMutableTreeNode(OsmorcBundle.message("bundle.selector.group.modules"));
    Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
      if (OsmorcFacet.hasOsmorcFacet(module)) {
        SelectedBundle bundle = new SelectedBundle(SelectedBundle.BundleType.Module, module.getName(), null);
        if (!toHide.contains(bundle)) {
          moduleNode.add(new DefaultMutableTreeNode(bundle));
        }
      }
    }
    if (moduleNode.getChildCount() > 0) root.add(moduleNode);

    // all the framework bundles (if there are any)
    if (instance != null) {
      FrameworkIntegrator integrator = FrameworkIntegratorRegistry.getInstance().findIntegratorByInstanceDefinition(instance);
      if (integrator != null) {
        DefaultMutableTreeNode frameworkNode = new DefaultMutableTreeNode(OsmorcBundle.message("bundle.selector.group.framework"));
        for (SelectedBundle bundle : integrator.getFrameworkInstanceManager().getFrameworkBundles(instance, FrameworkBundleType.OTHER)) {
          if (!toHide.contains(bundle)) {
            frameworkNode.add(new DefaultMutableTreeNode(bundle));
          }
        }
        if (frameworkNode.getChildCount() > 0) root.add(frameworkNode);
      }
    }

    // all the libraries that are bundles already (doesn't make much sense to start bundlified libs as they have no activator).
    DefaultMutableTreeNode libraryNode = new DefaultMutableTreeNode(OsmorcBundle.message("bundle.selector.group.libraries"));
    List<String> paths = OrderEnumerator.orderEntries(project)
      .withoutSdk()
      .withoutModuleSourceEntries()
      .withoutDepModules()
      .productionOnly()
      .runtimeOnly()
      .satisfying(BundleCompiler.NOT_FRAMEWORK_LIBRARY_CONDITION)
      .classes()
      .getPathsList().getPathList();

    for (String path : paths) {
      String displayName = CachingBundleInfoProvider.getBundleSymbolicName(path);
      if (displayName != null) {
        SelectedBundle bundle = new SelectedBundle(SelectedBundle.BundleType.StartLibrary, displayName, path);
        if (!toHide.contains(bundle)) {
          libraryNode.add(new DefaultMutableTreeNode(bundle));
        }
      }
    }
    if (libraryNode.getChildCount() > 0) root.add(libraryNode);

    return new DefaultTreeModel(root);
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    return myContentPane;
  }

  @Nullable
  @Override
  public JComponent getPreferredFocusedComponent() {
    return myBundleTree;
  }

  @NotNull
  public List<SelectedBundle> getSelectedBundles() {
    TreePath[] paths = myBundleTree.getSelectionPaths();
    if (paths == null) return ContainerUtil.emptyList();

    List<SelectedBundle> bundles = ContainerUtil.newArrayListWithCapacity(paths.length);
    for (TreePath path : paths) {
      Object last = path.getLastPathComponent();
      if (last instanceof DefaultMutableTreeNode) {
        Object object = ((DefaultMutableTreeNode)last).getUserObject();
        if (object instanceof SelectedBundle) {
          bundles.add((SelectedBundle)object);
        }
      }
    }
    return bundles;
  }


  private static class BundleTreeRenderer extends ColoredTreeCellRenderer {
    @Override
    public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
      if (value instanceof DefaultMutableTreeNode) {
        Object object = ((DefaultMutableTreeNode)value).getUserObject();
        if (object instanceof SelectedBundle) {
          SelectedBundle bundle = (SelectedBundle)object;
          if (bundle.isModule()) {
            setIcon(AllIcons.Nodes.Module);
          }
          else if (bundle.getBundleType() == SelectedBundle.BundleType.FrameworkBundle) {
            setIcon(OsmorcIdeaIcons.Osgi);
          }
          else {
            setIcon(AllIcons.Nodes.PpJar);
          }
        }
      }

      append(value.toString());
    }
  }
}
TOP

Related Classes of org.osmorc.run.ui.BundleSelector$BundleTreeRenderer

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.