@Override
 public void actionPerformed(ActionEvent e) {
     if (getTable() != null) {
         final SelectionInfo<?, ?> selection;
         if (isSelectionOnly()) {
             selection = getTable().getSelectionInfo(true, true);
         } else {
             selection = getTable().getSelectionInfo(false, true);
         }
         boolean allexpaned = true;
         final ArrayList<AbstractPackageNode> list = new ArrayList<AbstractPackageNode>();
         for (PackageView<?, ?> p : selection.getPackageViews()) {
             if (!p.isExpanded()) {
                 allexpaned = false;
             }
             list.add(p.getPackage());
         }
         getTable().getModel().setFilePackageExpand(!allexpaned, list.toArray(new AbstractPackageNode[] {}));
     }
 }
    public AggregatedNumbers(SelectionInfo<FilePackage, DownloadLink> selection) {
        totalBytes = 0l;
        disabledTotalBytes = 0l;
        disabledLoadedBytes = 0l;

        loadedBytes = 0l;
        downloadSpeed = 0l;
        running = 0l;
        connections = 0l;
        packageCount = selection.getPackageViews().size();
        linkCount = selection.getChildren().size();
        downloadsFinished = 0l;
        downloadsFailed = 0l;
        downloadsSkipped = 0l;
        disabledDownloadsFinished = 0l;
        disabledDownloadsFailed = 0l;
        disabledDownloadsSkipped = 0l;
        for (DownloadLink dl : selection.getChildren()) {
            if (dl == null) continue;

            if (dl.isEnabled()) {
                FinalLinkState state = dl.getFinalLinkState();
                if (state == null) {
                    if (dl.isSkipped()) {
                        downloadsSkipped++;
                    }
                } else {
                    if (state.isFailed()) {
                        downloadsFailed++;
                    } else if (state.isFinished()) {
                        downloadsFinished++;
                    }
                }
                totalBytes += dl.getView().getBytesTotalEstimated();
                loadedBytes += dl.getView().getBytesLoaded();

            } else {
                FinalLinkState state = dl.getFinalLinkState();
                if (state == null) {
                    if (dl.isSkipped()) {
                        disabledDownloadsSkipped++;
                    }
                } else {
                    if (state.isFailed()) {
                        disabledDownloadsFailed++;
                    } else if (state.isFinished()) {
                        disabledDownloadsFinished++;
                    }
                }
                disabledTotalBytes += dl.getView().getBytesTotalEstimated();
                disabledLoadedBytes += dl.getView().getBytesLoaded();

            }

            downloadSpeed += dl.getView().getSpeedBps();
            SingleDownloadController sdc = dl.getDownloadLinkController();
            if (sdc != null) {
                running++;
                DownloadInterface conInst = sdc.getDownloadInstance();
                if (conInst != null) {
                    ManagedThrottledConnectionHandler handlerP = conInst.getManagedConnetionHandler();
                    if (handlerP != null) {
                        connections += handlerP.size();

                    }

                }
            }

        }

        eta = downloadSpeed == 0 ? 0 : (totalBytes - loadedBytes) / downloadSpeed;

    }
  public void actionPerformed(ActionEvent e) {
    final SelectionInfo<CrawledPackage, CrawledLink> finalSelecction = getSelection();
    final String newName;
    final String newDownloadFolder;
    if (isMergePackages() && finalSelecction.getPackageViews().size() > 1) {
      if (isAskForNewDownloadFolderAndPackageName()) {
        try {
          final NewPackageDialog d =
              new NewPackageDialog(finalSelecction) {
                @Override
                public String getDontShowAgainKey() {
                  return "ABSTRACTDIALOG_DONT_SHOW_AGAIN_"
                      + SplitPackagesByHost.this.getClass().getSimpleName();
                }
              };

          Dialog.getInstance().showDialog(d);

          newName = d.getName();
          newDownloadFolder = d.getDownloadFolder();
          if (StringUtils.isEmpty(newName)) {
            return;
          }
        } catch (Throwable ignore) {
          return;
        }
      } else {
        newName = "";
        newDownloadFolder = finalSelecction.getFirstPackage().getRawDownloadFolder();
      }
    } else {
      newName = null;
      newDownloadFolder = null;
    }
    LinkCollector.getInstance()
        .getQueue()
        .add(
            new QueueAction<Void, RuntimeException>() {

              @Override
              protected Void run() throws RuntimeException {
                final HashMap<CrawledPackage, HashMap<String, ArrayList<CrawledLink>>> splitMap =
                    new HashMap<CrawledPackage, HashMap<String, ArrayList<CrawledLink>>>();
                int insertAt = -1;
                switch (getLocation()) {
                  case BEFORE_SELECTION:
                    insertAt = Integer.MAX_VALUE;
                }
                for (AbstractNode child : finalSelecction.getChildren()) {
                  if (child instanceof CrawledLink) {
                    final CrawledLink cL = (CrawledLink) child;
                    final CrawledPackage parent = isMergePackages() ? null : cL.getParentNode();

                    HashMap<String, ArrayList<CrawledLink>> parentMap = splitMap.get(parent);
                    if (parentMap == null) {
                      parentMap = new HashMap<String, ArrayList<CrawledLink>>();
                      splitMap.put(parent, parentMap);
                    }
                    final String host = cL.getDomainInfo().getTld();
                    ArrayList<CrawledLink> hostList = parentMap.get(host);
                    if (hostList == null) {
                      hostList = new ArrayList<CrawledLink>();
                      parentMap.put(host, hostList);
                    }
                    hostList.add(cL);
                    switch (getLocation()) {
                      case AFTER_SELECTION:
                        insertAt =
                            Math.max(
                                insertAt,
                                LinkCollector.getInstance()
                                        .indexOf(((CrawledLink) child).getParentNode())
                                    + 1);
                        break;
                      case BEFORE_SELECTION:
                        insertAt =
                            Math.min(
                                insertAt,
                                LinkCollector.getInstance()
                                    .indexOf(((CrawledLink) child).getParentNode()));
                        break;
                      case END_OF_LIST:
                        insertAt = -1;
                        break;
                      case TOP_OF_LIST:
                        insertAt = 0;
                        break;
                    }
                  }
                }
                if (insertAt == Integer.MAX_VALUE) {
                  insertAt = 0;
                }
                final String nameFactory =
                    JsonConfig.create(LinkgrabberSettings.class)
                        .getSplitPackageNameFactoryPattern();
                final boolean merge =
                    JsonConfig.create(LinkgrabberSettings.class).isSplitPackageMergeEnabled();
                final HashMap<String, CrawledPackage> mergedPackages =
                    new HashMap<String, CrawledPackage>();
                final Iterator<Entry<CrawledPackage, HashMap<String, ArrayList<CrawledLink>>>> it =
                    splitMap.entrySet().iterator();
                while (it.hasNext()) {
                  final Entry<CrawledPackage, HashMap<String, ArrayList<CrawledLink>>> next =
                      it.next();
                  // sourcePackage may be null
                  final CrawledPackage sourcePackage = next.getKey();
                  final HashMap<String, ArrayList<CrawledLink>> items = next.getValue();
                  final Iterator<Entry<String, ArrayList<CrawledLink>>> it2 =
                      items.entrySet().iterator();
                  while (it2.hasNext()) {
                    final Entry<String, ArrayList<CrawledLink>> next2 = it2.next();
                    final String host = next2.getKey();
                    final String newPackageName =
                        getNewPackageName(
                            nameFactory,
                            sourcePackage == null ? newName : sourcePackage.getName(),
                            host);
                    final CrawledPackage newPkg;
                    if (merge) {
                      CrawledPackage destPackage = mergedPackages.get(newPackageName);
                      if (destPackage == null) {
                        destPackage = new CrawledPackage();
                        destPackage.setExpanded(CFG_LINKCOLLECTOR.CFG.isPackageAutoExpanded());
                        if (sourcePackage != null) {
                          sourcePackage.copyPropertiesTo(destPackage);
                        } else {
                          destPackage.setDownloadFolder(newDownloadFolder);
                        }
                        destPackage.setName(newPackageName);
                        mergedPackages.put(newPackageName, destPackage);
                      }
                      newPkg = destPackage;
                    } else {
                      newPkg = new CrawledPackage();
                      newPkg.setExpanded(CFG_LINKCOLLECTOR.CFG.isPackageAutoExpanded());
                      if (sourcePackage != null) {
                        sourcePackage.copyPropertiesTo(newPkg);
                      } else {
                        newPkg.setDownloadFolder(newDownloadFolder);
                      }
                      newPkg.setName(newPackageName);
                    }
                    LinkCollector.getInstance().moveOrAddAt(newPkg, next2.getValue(), 0, insertAt);
                    insertAt++;
                  }
                }
                return null;
              }
            });
  }