Ejemplo n.º 1
0
  /** Refreshes the list of addons */
  protected void refreshAddons() {
    if (cmbAddonServer_ == null || tableAddons_ == null) {
      return;
    }

    if (loading_) {
      GUIUtils.showInfoMessageBox("Please wait for the previous query to finish.");
      return;
    }

    if (cmbAddonServer_.getSelectionIndex() == -1) {
      return;
    }

    currentPort_ = ports_.get(cmbAddonServer_.getSelectionIndex());
    if (StringUtils.isNullOrEmpty(currentPort_)) {
      return;
    }

    loading_ = true;
    tableAddons_.setItemCount(0);
    tableAddons_.clearAll();

    if (!StringUtils.isNullOrEmpty(currentPort_)) {
      WorkspaceJob loadAddons =
          new WorkspaceJob("Retrieving list...") {

            @Override
            public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
              monitor.beginTask("Retrieving list...", 100);
              monitor.worked(10);

              String installName = Preferences.getDefaultInstallName();

              OutputStream stderr =
                  GUIUtils.createConsole("Wesnoth Addon Manager", null, false).newOutputStream();

              ExternalToolInvoker tool =
                  WMLTools.runWesnothAddonManager(
                      installName,
                      null,
                      currentPort_,
                      Arrays.asList("-w", "-l"), // list addons in raw
                      // mode
                      null,
                      new OutputStream[] {stderr});
              tool.waitForTool();

              /**
               * parse the contents columns = [[" 0 - type", "1 - name", "2 - title", "3 - author",
               * "4 - version", "uploads", "5 - downloads", "size", "timestamp", "translate"]]
               */
              final String[] lines = StringUtils.getLines(tool.getOutputContent());
              final List<String[]> addons = new ArrayList<String[]>();

              String[] tmpColumns = null;
              int index = -1;

              for (String line : lines) {
                index = -1;

                if (line.startsWith("\\ campaign")) {
                  if (tmpColumns != null) {
                    addons.add(tmpColumns);
                  }
                  tmpColumns = new String[6];
                } else if (line.startsWith("  \\ type")) {
                  index = 0;
                } else if (line.startsWith("  \\ name")) {
                  index = 1;
                } else if (line.startsWith("  \\ title")) {
                  index = 2;
                } else if (line.startsWith("  \\ author")) {
                  index = 3;
                } else if (line.startsWith("  \\ version")) {
                  index = 4;
                } else if (line.startsWith("  \\ downloads")) {
                  index = 5;
                }

                // got something interesting? parse it
                if (tmpColumns != null && index != -1) {
                  int firstIndex = line.indexOf('\'') + 1;
                  int lastIndex = line.lastIndexOf('\'');

                  if (lastIndex < firstIndex) {
                    lastIndex = line.length();
                  }

                  tmpColumns[index] = line.substring(firstIndex, lastIndex).trim();
                }
              }

              // need GUI Thread access
              Display.getDefault()
                  .syncExec(
                      new Runnable() {
                        @Override
                        public void run() {
                          // skipp 1st line since it's just the header
                          for (String[] addon : addons) {

                            TableItem tableItem = new TableItem(tableAddons_, SWT.NONE);
                            tableItem.setText(
                                new String[] {
                                  addon[0], addon[1], addon[2], addon[3], addon[4], addon[5]
                                });
                          }

                          tableAddons_.forceFocus();
                        }
                      });
              loading_ = false;

              monitor.worked(100);
              monitor.done();
              return Status.OK_STATUS;
            }
          };
      loadAddons.schedule();
    }
  }
Ejemplo n.º 2
0
  /** Downloads the currently selected addon */
  protected void downloadAddon() {
    if (tableAddons_.getSelectionIndex() == -1) {
      GUIUtils.showErrorMessageBox("No addon selected");
      return;
    }

    TableItem[] selection = tableAddons_.getSelection();
    final String addonName = selection[0].getText(1);

    WorkspaceJob downloadJob =
        new WorkspaceJob("Download") {

          @Override
          public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
            monitor.beginTask("Downloading addon " + addonName, 100);

            String installName = Preferences.getDefaultInstallName();

            RunnableWithResult<String> runnable =
                new RunnableWithResult<String>() {
                  @Override
                  public void run() {
                    // ask the user to select the install for the project
                    SelectWesnothInstallDialog dialog = new SelectWesnothInstallDialog(null);
                    if (dialog.open() == SWT.OK) {
                      setResult(dialog.getSelectedInstallName());
                    }
                  }
                };

            Display.getDefault().syncExec(runnable);
            if (!StringUtils.isNullOrEmpty(runnable.getResult())) {
              installName = runnable.getResult();
            }

            final Paths paths = Preferences.getPaths(installName);

            OutputStream console =
                GUIUtils.createConsole("Wesnoth Addon Manager", null, false).newOutputStream();

            ExternalToolInvoker tool =
                WMLTools.runWesnothAddonManager(
                    installName,
                    null,
                    currentPort_,
                    Arrays.asList("-d", addonName, "-c", paths.getAddonsDir()),
                    new OutputStream[] {console},
                    new OutputStream[] {console});

            tool.waitForTool();

            monitor.worked(50);

            // ask user if he wants to create a project
            if (GUIUtils.showMessageBox(
                    "Do you want to create a new project for the downloaded addon?",
                    SWT.YES | SWT.NO)
                == SWT.YES) {

              ProjectUtils.createWesnothProject(
                  addonName, paths.getAddonsDir() + addonName, installName, monitor);
            }
            monitor.done();

            return Status.OK_STATUS;
          }
        };
    downloadJob.schedule();
  }