public static boolean prepareToInstall(
      List<PluginNode> pluginsToInstall, List<IdeaPluginDescriptor> allPlugins) {
    ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();

    final List<PluginId> pluginIds = new ArrayList<PluginId>();
    for (PluginNode pluginNode : pluginsToInstall) {
      pluginIds.add(pluginNode.getPluginId());
    }

    boolean result = false;

    for (final PluginNode pluginNode : pluginsToInstall) {
      if (pi != null) pi.setText(pluginNode.getName());

      try {
        result |= prepareToInstall(pluginNode, pluginIds, allPlugins);
      } catch (final IOException e) {
        SwingUtilities.invokeLater(
            new Runnable() {
              public void run() {
                Messages.showErrorDialog(
                    pluginNode.getName() + ": " + e.getMessage(),
                    CommonBundle.message("title.error"));
              }
            });
      }
    }
    return result;
  }
  public void install(@Nullable final Runnable onSuccess, boolean confirmed) {
    IdeaPluginDescriptor[] selection = getPluginTable().getSelectedObjects();

    if (confirmed || userConfirm(selection)) {
      final List<PluginNode> list = new ArrayList<PluginNode>();
      for (IdeaPluginDescriptor descr : selection) {
        PluginNode pluginNode = null;
        if (descr instanceof PluginNode) {
          pluginNode = (PluginNode) descr;
        } else if (descr instanceof IdeaPluginDescriptorImpl) {
          PluginId pluginId = descr.getPluginId();
          pluginNode = new PluginNode(pluginId);
          pluginNode.setName(descr.getName());
          pluginNode.setDepends(
              Arrays.asList(descr.getDependentPluginIds()), descr.getOptionalDependentPluginIds());
          pluginNode.setSize("-1");
          pluginNode.setRepositoryName(PluginInstaller.UNKNOWN_HOST_MARKER);
        }

        if (pluginNode != null) {
          list.add(pluginNode);
          ourInstallingNodes.add(pluginNode);
        }
      }

      final InstalledPluginsTableModel installedModel =
          (InstalledPluginsTableModel) myInstalled.getPluginsModel();
      final Set<IdeaPluginDescriptor> disabled = new HashSet<IdeaPluginDescriptor>();
      final Set<IdeaPluginDescriptor> disabledDependants = new HashSet<IdeaPluginDescriptor>();
      for (PluginNode node : list) {
        final PluginId pluginId = node.getPluginId();
        if (installedModel.isDisabled(pluginId)) {
          disabled.add(node);
        }
        final List<PluginId> depends = node.getDepends();
        if (depends != null) {
          final Set<PluginId> optionalDeps =
              new HashSet<PluginId>(Arrays.asList(node.getOptionalDependentPluginIds()));
          for (PluginId dependantId : depends) {
            if (optionalDeps.contains(dependantId)) continue;
            final IdeaPluginDescriptor pluginDescriptor = PluginManager.getPlugin(dependantId);
            if (pluginDescriptor != null && installedModel.isDisabled(dependantId)) {
              disabledDependants.add(pluginDescriptor);
            }
          }
        }
      }

      if (suggestToEnableInstalledPlugins(installedModel, disabled, disabledDependants, list)) {
        myInstalled.setRequireShutdown(true);
      }

      try {
        Runnable onInstallRunnable =
            new Runnable() {
              @Override
              public void run() {
                for (PluginNode node : list) {
                  installedModel.appendOrUpdateDescriptor(node);
                }
                if (!myInstalled.isDisposed()) {
                  getPluginTable().updateUI();
                  myInstalled.setRequireShutdown(true);
                } else {
                  boolean needToRestart = false;
                  for (PluginNode node : list) {
                    final IdeaPluginDescriptor pluginDescriptor =
                        PluginManager.getPlugin(node.getPluginId());
                    if (pluginDescriptor == null || pluginDescriptor.isEnabled()) {
                      needToRestart = true;
                      break;
                    }
                  }

                  if (needToRestart) {
                    PluginManagerMain.notifyPluginsUpdated(null);
                  }
                }
                if (onSuccess != null) {
                  onSuccess.run();
                }
              }
            };
        Runnable cleanupRunnable =
            new Runnable() {
              @Override
              public void run() {
                ourInstallingNodes.removeAll(list);
              }
            };
        PluginManagerMain.downloadPlugins(
            list, myHost.getPluginsModel().getAllPlugins(), onInstallRunnable, cleanupRunnable);
      } catch (final IOException e1) {
        ourInstallingNodes.removeAll(list);
        PluginManagerMain.LOG.error(e1);
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(
            new Runnable() {
              @Override
              public void run() {
                IOExceptionDialog.showErrorDialog(
                    IdeBundle.message("action.download.and.install.plugin"),
                    IdeBundle.message("error.plugin.download.failed"));
              }
            });
      }
    }
  }
  private static boolean prepareToInstall(
      final PluginNode pluginNode,
      final List<PluginId> pluginIds,
      List<IdeaPluginDescriptor> allPlugins)
      throws IOException {
    // check for dependent plugins at first.
    if (pluginNode.getDepends() != null && pluginNode.getDepends().size() > 0) {
      // prepare plugins list for install

      final PluginId[] optionalDependentPluginIds = pluginNode.getOptionalDependentPluginIds();
      final List<PluginNode> depends = new ArrayList<PluginNode>();
      final List<PluginNode> optionalDeps = new ArrayList<PluginNode>();
      for (int i = 0; i < pluginNode.getDepends().size(); i++) {
        PluginId depPluginId = pluginNode.getDepends().get(i);

        if (PluginManager.isPluginInstalled(depPluginId)
            || PluginManager.isModuleDependency(depPluginId)
            || (pluginIds != null && pluginIds.contains(depPluginId))) {
          //  ignore installed or installing plugins
          continue;
        }

        PluginNode depPlugin = new PluginNode(depPluginId);
        depPlugin.setSize("-1");
        depPlugin.setName(depPluginId.getIdString()); // prevent from exceptions

        if (optionalDependentPluginIds != null
            && ArrayUtil.indexOf(optionalDependentPluginIds, depPluginId) != -1) {
          if (isPluginInRepo(depPluginId, allPlugins)) {
            optionalDeps.add(depPlugin);
          }
        } else {
          depends.add(depPlugin);
        }
      }

      if (depends.size() > 0) { // has something to install prior installing the plugin
        final boolean[] proceed = new boolean[1];
        final StringBuffer buf = new StringBuffer();
        for (PluginNode depend : depends) {
          buf.append(depend.getName()).append(",");
        }
        try {
          GuiUtils.runOrInvokeAndWait(
              new Runnable() {
                public void run() {
                  proceed[0] =
                      Messages.showYesNoDialog(
                              IdeBundle.message(
                                  "plugin.manager.dependencies.detected.message",
                                  depends.size(),
                                  buf.substring(0, buf.length() - 1)),
                              IdeBundle.message("plugin.manager.dependencies.detected.title"),
                              Messages.getWarningIcon())
                          == DialogWrapper.OK_EXIT_CODE;
                }
              });
        } catch (Exception e) {
          return false;
        }
        if (proceed[0]) {
          if (!prepareToInstall(depends, allPlugins)) {
            return false;
          }
        } else {
          return false;
        }
      }

      if (optionalDeps.size() > 0) {
        final StringBuffer buf = new StringBuffer();
        for (PluginNode depend : optionalDeps) {
          buf.append(depend.getName()).append(",");
        }
        final boolean[] proceed = new boolean[1];
        try {
          GuiUtils.runOrInvokeAndWait(
              new Runnable() {
                public void run() {
                  proceed[0] =
                      Messages.showYesNoDialog(
                              IdeBundle.message(
                                  "plugin.manager.optional.dependencies.detected.message",
                                  optionalDeps.size(),
                                  buf.substring(0, buf.length() - 1)),
                              IdeBundle.message("plugin.manager.dependencies.detected.title"),
                              Messages.getWarningIcon())
                          == DialogWrapper.OK_EXIT_CODE;
                }
              });
        } catch (Exception e) {
          return false;
        }
        if (proceed[0]) {
          if (!prepareToInstall(optionalDeps, allPlugins)) {
            return false;
          }
        }
      }
    }

    synchronized (PluginManager.lock) {
      final PluginDownloader downloader = PluginDownloader.createDownloader(pluginNode);
      if (downloader.prepareToInstall(ProgressManager.getInstance().getProgressIndicator())) {
        downloader.install();
        pluginNode.setStatus(PluginNode.STATUS_DOWNLOADED);
      } else {
        return false;
      }
    }

    return true;
  }
示例#4
0
  public static void serialize(PaletteScene scene, Element rootElement) {
    Element pluginsElement = new Element("plugins");
    Element thisPluginElement;
    Point loc;
    AbstractPlugin plugin;
    for (PluginNode node : scene.getNodes()) {
      Widget widget = scene.findWidget(node);
      loc = widget.getPreferredLocation();

      plugin = node.getPlugin();
      thisPluginElement = new Element("plugin");
      thisPluginElement.setAttribute(ATT_PLUGIN_ID, plugin.getPluginKey().getUniqueID());
      thisPluginElement.setAttribute(ATT_PLUGIN_NAME, node.getName());
      thisPluginElement.setAttribute(ATT_PLUGIN_X, Integer.toString(loc.x));
      thisPluginElement.setAttribute(ATT_PLUGIN_Y, Integer.toString(loc.y));
      thisPluginElement.setAttribute(
          ATT_PLUGIN_HIDDEN, String.valueOf(plugin.isParameterPanelHidden()));
      boolean isHudOpen = plugin.getHudContainer() != null && plugin.getHudContainer().isOpened();
      thisPluginElement.setAttribute(ATT_HUD_VISIBLE, String.valueOf(isHudOpen));

      if (plugin instanceof IParameterPanel) {
        Element parameterPanelElement = ((IParameterPanel) plugin).createWorkspaceParameters();
        if (parameterPanelElement != null) {
          thisPluginElement.addContent(new Element("parameters").addContent(parameterPanelElement));
        }
      }
      pluginsElement.addContent(thisPluginElement);
    }
    rootElement.addContent(pluginsElement);

    Element connectionsElement = new Element("connections");
    Element thisConnectionElement;
    for (ConnectorEdge edge : scene.getEdges()) {
      thisConnectionElement = new Element("connection");
      thisConnectionElement.setAttribute(ATT_EDGE_ID, edge.getName());

      PluginNode sourceNode = scene.getEdgeSource(edge);
      if (sourceNode != null) {
        thisConnectionElement.setAttribute(ATT_EDGE_SOURCE, sourceNode.getName());
      }

      PluginNode targetNode = scene.getEdgeTarget(edge);
      if (targetNode != null) {
        thisConnectionElement.setAttribute(ATT_EDGE_TARGET, targetNode.getName());
      }

      ConnectionWidget cw = (ConnectionWidget) scene.findWidget(edge);
      thisConnectionElement.setAttribute(
          ATT_EDGE_ROUTER, cw.getRouter().getClass().getSimpleName());
      List<Point> pts = cw.getControlPoints();
      if (pts != null && !pts.isEmpty()) {
        Element vertsElement = new Element("verts");
        Element thisVertexElement;
        for (Point pt : cw.getControlPoints()) {
          thisVertexElement = new Element("vert");
          thisVertexElement.setAttribute(ATT_VERT_X, Integer.toString(pt.x));
          thisVertexElement.setAttribute(ATT_VERT_Y, Integer.toString(pt.y));
          vertsElement.addContent(thisVertexElement);
        }
        thisConnectionElement.addContent(vertsElement);
      }
      connectionsElement.addContent(thisConnectionElement);
    }
    rootElement.addContent(connectionsElement);

    // Save I/O connections
    Element ioConnectionsElement = new Element("ioConnections");
    for (String ioc : scene.getDefaultPaletteModel().getConnections()) {
      ioConnectionsElement.addContent(new Element("io").setAttribute("name", ioc));
    }
    rootElement.addContent(ioConnectionsElement);

    Element annotationsElement = new Element("annotations");
    Element thisAnnotation;
    AnnotationWidget aw;
    Font font;
    for (Widget w : scene.getChildren()) {
      if (!(w instanceof AnnotationWidget)) continue;
      aw = (AnnotationWidget) w;

      thisAnnotation = new Element("entry");
      thisAnnotation.setAttribute("text", aw.getLabel());

      font = aw.getFont();
      thisAnnotation.setAttribute("font", String.valueOf(font.getName()));
      thisAnnotation.setAttribute("fontSize", String.valueOf(font.getSize()));
      thisAnnotation.setAttribute("fontStyle", String.valueOf(font.getStyle()));

      loc = aw.getPreferredLocation();
      thisAnnotation.setAttribute("xLoc", String.valueOf(loc.x));
      thisAnnotation.setAttribute("yLoc", String.valueOf(loc.y));

      thisAnnotation.setAttribute(
          "fore", "#" + Integer.toHexString(aw.getForeground().getRGB()).substring(1));
      thisAnnotation.setAttribute(
          "back", "#" + Integer.toHexString(((Color) aw.getBackground()).getRGB()).substring(1));
      thisAnnotation.setAttribute("orient", aw.getOrientation().name());
      annotationsElement.addContent(thisAnnotation);
    }
    rootElement.addContent(annotationsElement);
  }
示例#5
0
  public static Element deserialize(PaletteScene scene, Element rootElement) {
    HashMap<String, PluginNode> registeredNodeMap = new HashMap<String, PluginNode>();
    Element pluginsElement = rootElement.getChild("plugins");
    List plugins = pluginsElement.getChildren("plugin");

    ArrayList<AbstractPlugin> addedPlugins = new ArrayList<AbstractPlugin>();
    PluginUtilities pm = PluginUtilities.getDefault();
    String pluginID, pluginName, isHudVisible, isParameterPanelHidden;
    Element pluginElement;
    for (Object plugin : plugins) {
      pluginElement = (Element) plugin;
      pluginID = pluginElement.getAttributeValue(ATT_PLUGIN_ID);
      pluginName = pluginElement.getAttributeValue(ATT_PLUGIN_NAME);

      isHudVisible = pluginElement.getAttributeValue(ATT_HUD_VISIBLE);
      isHudVisible = isHudVisible == null ? "false" : isHudVisible;

      isParameterPanelHidden = pluginElement.getAttributeValue(ATT_PLUGIN_HIDDEN);
      isParameterPanelHidden = isParameterPanelHidden == null ? "false" : isParameterPanelHidden;

      AbstractPlugin p = pm.instantiate(pluginID, pluginName, scene.getDefaultPaletteModel());
      p.setParameterPanelHidden(Boolean.parseBoolean(isParameterPanelHidden));

      if (p instanceof IParameterPanel) {
        ((IParameterPanel) p).loadSavedWorkspaceParameters(pluginElement.getChild("parameters"));
      }

      if (p instanceof HudInterface && Boolean.parseBoolean(isHudVisible)) {
        actionShowHUD(p);
      }
      addedPlugins.add(p);

      PluginNode thisNode = new PluginNode(p);
      registeredNodeMap.put(thisNode.getName(), thisNode);
      Widget nodeWidget = scene.addNode(thisNode);
      int x = Integer.parseInt(pluginElement.getAttributeValue(ATT_PLUGIN_X));
      int y = Integer.parseInt(pluginElement.getAttributeValue(ATT_PLUGIN_Y));
      // nodeWidget.setPreferredLocation(new Point(x, y));
      nodeWidget.setPreferredLocation(new Point(x, y));
    }
    scene.revalidate();

    Lookup.Result<IPluginsAdded> pluginsAdded =
        LatizLookup.getDefault().lookupResult(IPluginsAdded.class);
    for (IPluginsAdded ipa : pluginsAdded.allInstances()) {
      ipa.pluginsAdded(scene, addedPlugins);
    }

    List connections = rootElement.getChild("connections").getChildren("connection");
    Element connectionElement;
    for (Object connection : connections) {
      connectionElement = (Element) connection;
      ConnectorEdge edge = new ConnectorEdge(connectionElement.getAttributeValue(ATT_EDGE_ID));
      PluginNode sourceNode =
          registeredNodeMap.get(connectionElement.getAttributeValue(ATT_EDGE_SOURCE));
      PluginNode targetNode =
          registeredNodeMap.get(connectionElement.getAttributeValue(ATT_EDGE_TARGET));

      // Create connection panels.
      Lookup.Result<IPluginConnection> pcis =
          LatizLookup.getDefault().lookupResult(IPluginConnection.class);
      for (IPluginConnection pci : pcis.allInstances()) {
        pci.connectionMade(scene, sourceNode.getPlugin(), targetNode.getPlugin());
      }

      scene.addEdge(edge);
      scene.setEdgeSource(edge, sourceNode);
      scene.setEdgeTarget(edge, targetNode);

      // Apply vertices
      ArrayList<Point> controlPoints = new ArrayList<Point>();
      List verts = connectionElement.getChild("verts").getChildren();
      Element vertElement;
      for (Object vert : verts) {
        vertElement = (Element) vert;
        int x = Integer.parseInt(vertElement.getAttributeValue(ATT_VERT_X));
        int y = Integer.parseInt(vertElement.getAttributeValue(ATT_VERT_Y));
        controlPoints.add(new Point(x, y));
      }

      ConnectionWidget cw = (ConnectionWidget) scene.findWidget(edge);
      String router = connectionElement.getAttributeValue(ATT_EDGE_ROUTER);
      if (router.equals("FreeRouter")) {
        cw.setRouter(RouterFactory.createFreeRouter());
      } else if (router.equals("DirectRouter")) {
        cw.setRouter(RouterFactory.createDirectRouter());
      } else if (router.equals("OrthogonalSearchRouter")) {
        cw.setRouter(RouterFactory.createOrthogonalSearchRouter(scene.getMainLayer()));
      }
      cw.setControlPoints(controlPoints, true);
    }

    // Deserialize I/O connections
    Element ioConnectionsElement = rootElement.getChild("ioConnections");
    if (ioConnectionsElement == null) {
      return rootElement;
    }
    List<String> ioConnections = scene.getDefaultPaletteModel().getConnections();
    ioConnections.clear();
    for (Object o : ioConnectionsElement.getChildren()) {
      ioConnections.add(((Element) o).getAttributeValue("name"));
    }

    // Annotations
    Element annotationsElement = rootElement.getChild("annotations");
    Element annotationElement;
    AnnotationWidget aw;
    if (annotationsElement != null) {
      for (Object o : annotationsElement.getChildren()) {
        annotationElement = (Element) o;
        aw = new AnnotationWidget(scene, annotationElement.getAttributeValue("text"));

        String fontName = annotationElement.getAttributeValue("font");
        int fontSize = Integer.parseInt(annotationElement.getAttributeValue("fontSize"));
        int fontStyle = Integer.parseInt(annotationElement.getAttributeValue("fontStyle"));
        Font font = new Font(fontName, fontStyle, fontSize);
        aw.setFont(font);

        int xLoc = Integer.parseInt(annotationElement.getAttributeValue("xLoc"));
        int yLoc = Integer.parseInt(annotationElement.getAttributeValue("yLoc"));
        aw.setPreferredLocation(new Point(xLoc, yLoc));
        aw.setForeground(Color.decode(annotationElement.getAttributeValue("fore")));
        aw.setBackground(Color.decode(annotationElement.getAttributeValue("back")));
        aw.setOrientation(Orientation.valueOf(annotationElement.getAttributeValue("orient")));
        scene.addChild(aw);
      }
    }
    scene.validate();

    // Removed all Plugin Selection Cookies.
    LatizLookup.getDefault().removeAllFromLookup(PluginSelectionCookie.class);

    return rootElement;
  }