Ejemplo n.º 1
0
  public Composite configSectionCreate(final Composite parent) {
    ImageLoader imageLoader = ImageLoader.getInstance();
    Image imgOpenFolder = imageLoader.getImage("openFolderButton");

    GridData gridData;
    GridLayout layout;

    final Composite cBR = new Composite(parent, SWT.NULL);

    gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
    cBR.setLayoutData(gridData);
    layout = new GridLayout();
    layout.numColumns = 1;
    cBR.setLayout(layout);

    Label info_label = new Label(cBR, SWT.WRAP);
    Messages.setLanguageText(info_label, "ConfigView.section.br.overview");
    gridData = Utils.getWrappableLabelGridData(1, GridData.HORIZONTAL_ALIGN_FILL);

    info_label.setLayoutData(gridData);

    // wiki link

    final Label linkLabel = new Label(cBR, SWT.NULL);
    linkLabel.setText(MessageText.getString("ConfigView.label.please.visit.here"));
    linkLabel.setData("http://wiki.vuze.com/w/Backup_And_Restore");
    linkLabel.setCursor(linkLabel.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
    linkLabel.setForeground(Colors.blue);
    gridData = Utils.getWrappableLabelGridData(1, 0);
    linkLabel.setLayoutData(gridData);
    linkLabel.addMouseListener(
        new MouseAdapter() {
          public void mouseDoubleClick(MouseEvent arg0) {
            Utils.launch((String) ((Label) arg0.widget).getData());
          }

          public void mouseDown(MouseEvent arg0) {
            Utils.launch((String) ((Label) arg0.widget).getData());
          }
        });
    ClipboardCopy.addCopyToClipMenu(linkLabel);

    final BackupManager backup_manager =
        BackupManagerFactory.getManager(AzureusCoreFactory.getSingleton());

    // backup

    Group gBackup = new Group(cBR, SWT.NULL);
    Messages.setLanguageText(gBackup, "br.backup");
    layout = new GridLayout(2, false);
    gBackup.setLayout(layout);
    gBackup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // info

    Label last_backup_label = new Label(gBackup, SWT.NULL);
    Messages.setLanguageText(last_backup_label, "br.backup.last.time");

    final Label last_backup_time = new Label(gBackup, SWT.NULL);

    Label last_backup_error_label = new Label(gBackup, SWT.NULL);
    Messages.setLanguageText(last_backup_error_label, "br.backup.last.error");

    final Label last_backup_error = new Label(gBackup, SWT.NULL);

    final Runnable stats_updater =
        new Runnable() {
          public void run() {
            long backup_time = backup_manager.getLastBackupTime();

            last_backup_time.setText(backup_time == 0 ? "" : String.valueOf(new Date(backup_time)));

            last_backup_error.setText(backup_manager.getLastBackupError());
          };
        };

    stats_updater.run();

    // manual button

    Label backup_manual_label = new Label(gBackup, SWT.NULL);
    Messages.setLanguageText(backup_manual_label, "br.backup.manual.info");

    Button backup_button = new Button(gBackup, SWT.PUSH);
    Messages.setLanguageText(backup_button, "br.backup");

    backup_button.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            String def_dir = COConfigurationManager.getStringParameter("br.backup.folder.default");

            DirectoryDialog dialog = new DirectoryDialog(parent.getShell(), SWT.APPLICATION_MODAL);

            if (def_dir != null) {
              dialog.setFilterPath(def_dir);
            }

            dialog.setMessage(MessageText.getString("br.backup.folder.info"));
            dialog.setText(MessageText.getString("br.backup.folder.title"));

            String path = dialog.open();

            if (path != null) {

              COConfigurationManager.setParameter("br.backup.folder.default", path);

              runBackup(backup_manager, path, stats_updater);
            }
          }
        });

    final BooleanParameter auto_backup_enable =
        new BooleanParameter(gBackup, "br.backup.auto.enable", "br.backup.auto.enable");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    auto_backup_enable.setLayoutData(gridData);

    Composite gDefaultDir = new Composite(gBackup, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 3;
    layout.marginHeight = 2;
    gDefaultDir.setLayout(layout);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 2;
    gDefaultDir.setLayoutData(gridData);

    Label lblDefaultDir = new Label(gDefaultDir, SWT.NONE);
    Messages.setLanguageText(lblDefaultDir, "ConfigView.section.file.defaultdir.ask");
    lblDefaultDir.setLayoutData(new GridData());

    gridData = new GridData(GridData.FILL_HORIZONTAL);
    final StringParameter pathParameter =
        new StringParameter(gDefaultDir, "br.backup.auto.dir", "");
    pathParameter.setLayoutData(gridData);

    if (pathParameter.getValue().length() == 0) {
      String def_dir = COConfigurationManager.getStringParameter("br.backup.folder.default");

      pathParameter.setValue(def_dir);
    }

    Button browse = new Button(gDefaultDir, SWT.PUSH);
    browse.setImage(imgOpenFolder);
    imgOpenFolder.setBackground(browse.getBackground());
    browse.setToolTipText(MessageText.getString("ConfigView.button.browse"));

    browse.addListener(
        SWT.Selection,
        new Listener() {
          /* (non-Javadoc)
           * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
           */
          public void handleEvent(Event event) {
            DirectoryDialog dialog = new DirectoryDialog(parent.getShell(), SWT.APPLICATION_MODAL);
            dialog.setFilterPath(pathParameter.getValue());
            dialog.setMessage(MessageText.getString("br.backup.auto.dir.select"));
            dialog.setText(MessageText.getString("ConfigView.section.file.defaultdir.ask"));
            String path = dialog.open();
            if (path != null) {
              pathParameter.setValue(path);

              COConfigurationManager.setParameter("br.backup.folder.default", path);
            }
          }
        });

    Label lbl_backup_days = new Label(gDefaultDir, SWT.NULL);
    Messages.setLanguageText(lbl_backup_days, "br.backup.auto.everydays");

    IntParameter backup_everydays =
        new IntParameter(gDefaultDir, "br.backup.auto.everydays", 1, Integer.MAX_VALUE);
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    backup_everydays.setLayoutData(gridData);

    Label lbl_backup_retain = new Label(gDefaultDir, SWT.NULL);
    Messages.setLanguageText(lbl_backup_retain, "br.backup.auto.retain");

    IntParameter backup_retain =
        new IntParameter(gDefaultDir, "br.backup.auto.retain", 1, Integer.MAX_VALUE);
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    backup_retain.setLayoutData(gridData);

    BooleanParameter chkNotify =
        new BooleanParameter(gDefaultDir, "br.backup.notify", "br.backup.notify");
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 3;
    chkNotify.setLayoutData(gridData);

    Label backup_auto_label = new Label(gDefaultDir, SWT.NULL);
    Messages.setLanguageText(backup_auto_label, "br.backup.auto.now");

    Button backup_auto_button = new Button(gDefaultDir, SWT.PUSH);
    Messages.setLanguageText(backup_auto_button, "br.test");

    backup_auto_button.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            runBackup(backup_manager, null, stats_updater);
          }
        });

    auto_backup_enable.setAdditionalActionPerformer(
        new ChangeSelectionActionPerformer(gDefaultDir));

    // restore

    Group gRestore = new Group(cBR, SWT.NULL);
    Messages.setLanguageText(gRestore, "br.restore");
    layout = new GridLayout(2, false);
    gRestore.setLayout(layout);
    gRestore.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Label restore_label = new Label(gRestore, SWT.NULL);
    Messages.setLanguageText(restore_label, "br.restore.info");

    Button restore_button = new Button(gRestore, SWT.PUSH);
    Messages.setLanguageText(restore_button, "br.restore");

    restore_button.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            String def_dir = COConfigurationManager.getStringParameter("br.backup.folder.default");

            DirectoryDialog dialog = new DirectoryDialog(parent.getShell(), SWT.APPLICATION_MODAL);

            if (def_dir != null) {
              dialog.setFilterPath(def_dir);
            }

            dialog.setMessage(MessageText.getString("br.restore.folder.info"));

            dialog.setText(MessageText.getString("br.restore.folder.title"));

            final String path = dialog.open();

            if (path != null) {

              MessageBoxShell mb =
                  new MessageBoxShell(
                      SWT.ICON_WARNING | SWT.OK | SWT.CANCEL,
                      MessageText.getString("br.restore.warning.title"),
                      MessageText.getString("br.restore.warning.info"));

              mb.setDefaultButtonUsingStyle(SWT.CANCEL);
              mb.setParent(parent.getShell());

              mb.open(
                  new UserPrompterResultListener() {
                    public void prompterClosed(int returnVal) {
                      if (returnVal != SWT.OK) {
                        return;
                      }

                      final TextViewerWindow viewer =
                          new TextViewerWindow(
                              MessageText.getString("br.backup.progress"), null, "", true, true);

                      viewer.setEditable(false);

                      viewer.setOKEnabled(false);

                      backup_manager.restore(
                          new File(path),
                          new BackupManager.BackupListener() {
                            public boolean reportProgress(String str) {
                              return (append(str, false));
                            }

                            public void reportComplete() {
                              append("Restore Complete!", true);

                              Utils.execSWTThread(
                                  new AERunnable() {
                                    public void runSupport() {
                                      MessageBoxShell mb =
                                          new MessageBoxShell(
                                              SWT.ICON_INFORMATION | SWT.OK,
                                              MessageText.getString(
                                                  "ConfigView.section.security.restart.title"),
                                              MessageText.getString(
                                                  "ConfigView.section.security.restart.msg"));
                                      mb.setParent(parent.getShell());
                                      mb.open(
                                          new UserPrompterResultListener() {
                                            public void prompterClosed(int returnVal) {
                                              UIFunctionsSWT uiFunctions =
                                                  UIFunctionsManagerSWT.getUIFunctionsSWT();

                                              if (uiFunctions != null) {

                                                uiFunctions.dispose(true, false);
                                              }
                                            }
                                          });
                                    }
                                  });
                            }

                            public void reportError(Throwable error) {
                              append(
                                  "Restore Failed: " + Debug.getNestedExceptionMessage(error),
                                  true);
                            }

                            private boolean append(final String str, final boolean complete) {
                              if (viewer.isDisposed()) {

                                return (false);
                              }

                              Utils.execSWTThread(
                                  new AERunnable() {
                                    public void runSupport() {
                                      if (str.endsWith("...")) {

                                        viewer.append(str);

                                      } else {

                                        viewer.append(str + "\r\n");
                                      }

                                      if (complete) {

                                        viewer.setOKEnabled(true);
                                      }
                                    }
                                  });

                              return (true);
                            }
                          });

                      viewer.goModal();
                    }
                  });
            }
          }
        });

    return (cBR);
  }
  private void buildOptions(
      final Composite parent, final PlatformManager platform, final Composite area, boolean rebuild)
      throws PlatformManagerException {
    if (rebuild) {

      Control[] kids = area.getChildren();

      for (Control k : kids) {
        k.dispose();
      }
    }

    String[] options = platform.getExplicitVMOptions();

    {
      // max mem

      long max_mem = AEMemoryMonitor.getJVMLongOption(options, "-Xmx");

      final int MIN_MAX_JVM = 32 * 1024 * 1024;

      GridData gridData = new GridData();
      Label label = new Label(area, SWT.NULL);
      label.setLayoutData(gridData);
      Messages.setLanguageText(label, "jvm.max.mem", new String[] {encodeDisplayLong(MIN_MAX_JVM)});

      gridData = new GridData();
      gridData.widthHint = 125;
      final StringParameter max_vm = new StringParameter(area, "jvm.max.mem", "", false);
      max_vm.setLayoutData(gridData);

      max_vm.setValue(max_mem == -1 ? "" : encodeDisplayLong(max_mem));

      max_vm.addChangeListener(
          new ParameterChangeAdapter() {
            private String last_value;

            public void parameterChanged(Parameter p, boolean caused_internally) {
              if (max_vm.isDisposed()) {

                max_vm.removeChangeListener(this);

                return;
              }

              String val = max_vm.getValue();

              if (last_value != null && last_value.equals(val)) {

                return;
              }

              last_value = val;

              try {
                long max_mem = decodeDisplayLong(val);

                if (max_mem < MIN_MAX_JVM) {

                  throw (new Exception("Min=" + encodeDisplayLong(MIN_MAX_JVM)));
                }

                String[] options = platform.getExplicitVMOptions();

                options = AEMemoryMonitor.setJVMLongOption(options, "-Xmx", max_mem);

                long min_mem = AEMemoryMonitor.getJVMLongOption(options, "-Xms");

                if (min_mem == -1 || min_mem > max_mem) {

                  options = AEMemoryMonitor.setJVMLongOption(options, "-Xms", max_mem);
                }

                platform.setExplicitVMOptions(options);

                buildOptions(parent, platform, area, true);

              } catch (Throwable e) {

                String param_name = MessageText.getString("jvm.max.mem");

                int pos = param_name.indexOf('[');

                if (pos != -1) {

                  param_name = param_name.substring(0, pos).trim();
                }

                MessageBoxShell mb =
                    new MessageBoxShell(
                        SWT.ICON_ERROR | SWT.OK,
                        MessageText.getString("ConfigView.section.invalid.value.title"),
                        MessageText.getString(
                            "ConfigView.section.invalid.value",
                            new String[] {val, param_name, Debug.getNestedExceptionMessage(e)}));

                mb.setParent(parent.getShell());
                mb.open(null);
              }
            }
          });

      label = new Label(area, SWT.NULL);
      gridData = new GridData(GridData.FILL_HORIZONTAL);
      label.setLayoutData(gridData);

      Long max_heap_mb = AEMemoryMonitor.getMaxHeapMB();

      if (max_heap_mb > 0) {

        Messages.setLanguageText(
            label,
            "jvm.max.mem.current",
            new String[] {
              DisplayFormatters.formatByteCountToKiBEtc(max_heap_mb * 1024 * 1024, true)
            });
      }
    }

    {
      // min mem

      final int MIN_MIN_JVM = 8 * 1024 * 1024;

      long min_mem = AEMemoryMonitor.getJVMLongOption(options, "-Xms");

      GridData gridData = new GridData();
      Label label = new Label(area, SWT.NULL);
      label.setLayoutData(gridData);
      Messages.setLanguageText(label, "jvm.min.mem", new String[] {encodeDisplayLong(MIN_MIN_JVM)});

      gridData = new GridData();
      gridData.widthHint = 125;
      final StringParameter min_vm = new StringParameter(area, "jvm.min.mem", "", false);
      min_vm.setLayoutData(gridData);

      min_vm.setValue(min_mem == -1 ? "" : encodeDisplayLong(min_mem));

      min_vm.addChangeListener(
          new ParameterChangeAdapter() {
            private String last_value;

            public void parameterChanged(Parameter p, boolean caused_internally) {
              if (min_vm.isDisposed()) {

                min_vm.removeChangeListener(this);

                return;
              }

              String val = min_vm.getValue();

              if (last_value != null && last_value.equals(val)) {

                return;
              }

              last_value = val;

              try {
                long min_mem = decodeDisplayLong(val);

                if (min_mem < MIN_MIN_JVM) {

                  throw (new Exception("Min=" + encodeDisplayLong(MIN_MIN_JVM)));
                }

                String[] options = platform.getExplicitVMOptions();

                options = AEMemoryMonitor.setJVMLongOption(options, "-Xms", min_mem);

                long max_mem = AEMemoryMonitor.getJVMLongOption(options, "-Xmx");

                if (max_mem == -1 || max_mem < min_mem) {

                  options = AEMemoryMonitor.setJVMLongOption(options, "-Xmx", min_mem);
                }

                platform.setExplicitVMOptions(options);

                buildOptions(parent, platform, area, true);

              } catch (Throwable e) {

                String param_name = MessageText.getString("jvm.min.mem");

                int pos = param_name.indexOf('[');

                if (pos != -1) {

                  param_name = param_name.substring(0, pos).trim();
                }

                MessageBoxShell mb =
                    new MessageBoxShell(
                        SWT.ICON_ERROR | SWT.OK,
                        MessageText.getString("ConfigView.section.invalid.value.title"),
                        MessageText.getString(
                            "ConfigView.section.invalid.value",
                            new String[] {val, param_name, Debug.getNestedExceptionMessage(e)}));

                mb.setParent(parent.getShell());
                mb.open(null);
              }
            }
          });

      label = new Label(area, SWT.NULL);
      gridData = new GridData(GridData.FILL_HORIZONTAL);
      label.setLayoutData(gridData);
    }

    {
      // max DIRECT mem

      final int MIN_DIRECT_JVM = 32 * 1024 * 1024;

      final String OPTION_KEY = "-XX:MaxDirectMemorySize=";

      long max_direct = AEMemoryMonitor.getJVMLongOption(options, OPTION_KEY);

      GridData gridData = new GridData();
      Label label = new Label(area, SWT.NULL);
      label.setLayoutData(gridData);
      Messages.setLanguageText(
          label, "jvm.max.direct.mem", new String[] {encodeDisplayLong(MIN_DIRECT_JVM)});

      gridData = new GridData();
      gridData.widthHint = 125;
      final StringParameter max_direct_vm =
          new StringParameter(area, "jvm.max.direct.mem", "", false);
      max_direct_vm.setLayoutData(gridData);

      max_direct_vm.setValue(max_direct == -1 ? "" : encodeDisplayLong(max_direct));

      max_direct_vm.addChangeListener(
          new ParameterChangeAdapter() {
            private String last_value;

            public void parameterChanged(Parameter p, boolean caused_internally) {
              if (max_direct_vm.isDisposed()) {

                max_direct_vm.removeChangeListener(this);

                return;
              }

              String val = max_direct_vm.getValue();

              if (last_value != null && last_value.equals(val)) {

                return;
              }

              last_value = val;

              try {
                long max_direct = decodeDisplayLong(val);

                if (max_direct < MIN_DIRECT_JVM) {

                  throw (new Exception("Min=" + encodeDisplayLong(MIN_DIRECT_JVM)));
                }

                String[] options = platform.getExplicitVMOptions();

                options = AEMemoryMonitor.setJVMLongOption(options, OPTION_KEY, max_direct);

                platform.setExplicitVMOptions(options);

                buildOptions(parent, platform, area, true);

              } catch (Throwable e) {

                String param_name = MessageText.getString("jvm.max.direct.mem");

                int pos = param_name.indexOf('[');

                if (pos != -1) {

                  param_name = param_name.substring(0, pos).trim();
                }

                MessageBoxShell mb =
                    new MessageBoxShell(
                        SWT.ICON_ERROR | SWT.OK,
                        MessageText.getString("ConfigView.section.invalid.value.title"),
                        MessageText.getString(
                            "ConfigView.section.invalid.value",
                            new String[] {val, param_name, Debug.getNestedExceptionMessage(e)}));

                mb.setParent(parent.getShell());
                mb.open(null);
              }
            }
          });

      label = new Label(area, SWT.NULL);
      gridData = new GridData(GridData.FILL_HORIZONTAL);
      label.setLayoutData(gridData);
      Messages.setLanguageText(label, "jvm.max.direct.mem.info");
    }

    // all options

    Label label = new Label(area, SWT.NULL);
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 3;
    label.setLayoutData(gridData);
    Messages.setLanguageText(label, "jvm.options.summary");

    for (String option : options) {

      label = new Label(area, SWT.NULL);
      label.setText(option);
      gridData = new GridData();
      gridData.horizontalSpan = 3;
      gridData.horizontalIndent = 20;
      label.setLayoutData(gridData);
    }

    if (rebuild) {

      parent.layout(true, true);
    }
  }