Search in sources :

Example 1 with StringParameter

use of com.biglybt.ui.swt.config.StringParameter in project BiglyBT by BiglySoftware.

the class BasicPluginConfigImpl method configSectionCreate.

@Override
public Composite configSectionCreate(final Composite parent) {
    int userMode = COConfigurationManager.getIntParameter("User Mode");
    // main tab set up
    Composite main_tab = new Composite(parent, SWT.NULL);
    GridData main_gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
    Utils.setLayoutData(main_tab, main_gridData);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.marginHeight = 0;
    main_tab.setLayout(layout);
    final Map comp_map = new HashMap();
    Composite current_composite = main_tab;
    Map<ParameterGroupImpl, Composite> group_map = new HashMap<>();
    Map<ParameterTabFolderImpl, CTabFolder> tab_folder_map = new HashMap<>();
    Map<ParameterGroupImpl, Composite> tab_map = new HashMap<>();
    BasicPluginConfigModel model = model_ref.get();
    if (model == null) {
        return (main_tab);
    }
    com.biglybt.pif.ui.config.Parameter[] parameters = model.getParameters();
    List<Button> buttons = new ArrayList<>();
    for (int i = 0; i < parameters.length; i++) {
        final ParameterImpl param = (ParameterImpl) parameters[i];
        if (param.getMinimumRequiredUserMode() > userMode) {
            continue;
        }
        ParameterGroupImpl pg = param.getGroup();
        if (pg == null) {
            current_composite = main_tab;
        } else {
            ParameterTabFolderImpl tab_folder = pg.getTabFolder();
            if (tab_folder != null) {
                ParameterGroupImpl tab_group = tab_folder.getGroup();
                CTabFolder tf = tab_folder_map.get(tab_folder);
                if (tf == null) {
                    Composite tab_parent = current_composite;
                    if (tab_group != null) {
                        String tg_resource = tab_group.getResourceName();
                        if (tg_resource != null) {
                            tab_parent = group_map.get(tab_group);
                            if (tab_parent == null) {
                                tab_parent = new Group(current_composite, SWT.NULL);
                                Messages.setLanguageText(tab_parent, tg_resource);
                                GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
                                gridData.horizontalSpan = 2;
                                if (tab_group.getMinimumRequiredUserMode() > userMode) {
                                    tab_parent.setVisible(false);
                                    gridData.widthHint = 0;
                                    gridData.heightHint = 0;
                                }
                                Utils.setLayoutData(tab_parent, gridData);
                                layout = new GridLayout();
                                layout.numColumns = tab_group.getNumberColumns() * 2;
                                tab_parent.setLayout(layout);
                                group_map.put(tab_group, tab_parent);
                            }
                        }
                    }
                    tf = new CTabFolder(tab_parent, SWT.LEFT);
                    tf.setBorderVisible(tab_group == null);
                    tf.setTabHeight(20);
                    GridData grid_data = new GridData(GridData.FILL_HORIZONTAL);
                    grid_data.horizontalSpan = 2;
                    if (tab_folder.getMinimumRequiredUserMode() > userMode) {
                        tf.setVisible(false);
                        grid_data.widthHint = 0;
                        grid_data.heightHint = 0;
                    }
                    Utils.setLayoutData(tf, grid_data);
                    tab_folder_map.put(tab_folder, tf);
                }
                Composite tab_composite = tab_map.get(pg);
                if (tab_composite == null) {
                    CTabItem tab_item = new CTabItem(tf, SWT.NULL);
                    String tab_name = pg.getResourceName();
                    if (tab_name != null) {
                        Messages.setLanguageText(tab_item, tab_name);
                    }
                    tab_composite = new Composite(tf, SWT.NONE);
                    tab_item.setControl(tab_composite);
                    layout = new GridLayout();
                    layout.numColumns = 2;
                    tab_composite.setLayout(layout);
                    GridData grid_data = new GridData(GridData.FILL_BOTH);
                    if (pg.getMinimumRequiredUserMode() > userMode) {
                        tab_composite.setVisible(false);
                        grid_data.widthHint = 0;
                        grid_data.heightHint = 0;
                    }
                    Utils.setLayoutData(tab_composite, grid_data);
                    if (tf.getItemCount() == 1) {
                        tf.setSelection(tab_item);
                    }
                    tab_map.put(pg, tab_composite);
                }
                current_composite = tab_composite;
            }
            Composite comp = group_map.get(pg);
            if (comp == null) {
                boolean nested = pg.getGroup() != null || tab_folder != null;
                Composite group_parent = nested ? current_composite : main_tab;
                String resource_name = pg.getResourceName();
                boolean use_composite = resource_name == null || tab_folder != null;
                current_composite = use_composite ? new Composite(group_parent, SWT.NONE) : new Group(group_parent, SWT.NULL);
                if (!use_composite) {
                    Messages.setLanguageText(current_composite, resource_name);
                }
                GridData grid_data = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
                grid_data.grabExcessHorizontalSpace = true;
                grid_data.horizontalSpan = 2;
                if (pg.getMinimumRequiredUserMode() > userMode) {
                    current_composite.setVisible(false);
                    grid_data.widthHint = 0;
                    grid_data.heightHint = 0;
                }
                Utils.setLayoutData(current_composite, grid_data);
                layout = new GridLayout();
                layout.numColumns = pg.getNumberColumns() * 2;
                current_composite.setLayout(layout);
                group_map.put(pg, current_composite);
            } else {
                current_composite = comp;
            }
        }
        Label label = null;
        String label_key = param.getLabelKey();
        String label_text = label_key == null ? param.getLabelText() : MessageText.getString(label_key);
        if (label_text.indexOf('\n') != -1 || label_text.indexOf('\t') != -1 || !(param instanceof BooleanParameterImpl)) {
            String hyperlink = null;
            if (param instanceof HyperlinkParameterImpl) {
                hyperlink = ((HyperlinkParameterImpl) param).getHyperlink();
            }
            label = new Label(current_composite, (param instanceof LabelParameterImpl) ? SWT.WRAP : SWT.NULL);
            boolean add_copy;
            if (label_key == null) {
                label.setText(param.getLabelText());
                add_copy = true;
            } else {
                Messages.setLanguageText(label, label_key);
                add_copy = label_key.startsWith("!");
            }
            if (add_copy) {
                final Label f_label = label;
                ClipboardCopy.addCopyToClipMenu(label, new ClipboardCopy.copyToClipProvider() {

                    @Override
                    public String getText() {
                        return (f_label.getText().trim());
                    }
                });
            }
            if (hyperlink != null) {
                LinkLabel.makeLinkedLabel(label, hyperlink);
            }
            if (param instanceof HyperlinkParameterImpl) {
                final Label f_label = label;
                param.addListener(new ParameterListener() {

                    @Override
                    public void parameterChanged(com.biglybt.pif.ui.config.Parameter p) {
                        if (f_label.isDisposed()) {
                            param.removeListener(this);
                        } else {
                            final String hyperlink = ((HyperlinkParameterImpl) param).getHyperlink();
                            if (hyperlink != null) {
                                Utils.execSWTThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        LinkLabel.updateLinkedLabel(f_label, hyperlink);
                                    }
                                });
                            }
                        }
                    }
                });
            }
        }
        String key = param.getKey();
        // System.out.println( "key = " + key );
        final Parameter swt_param;
        if (param instanceof BooleanParameterImpl) {
            if (label == null) {
                swt_param = new BooleanParameter(current_composite, key, ((BooleanParameterImpl) param).getDefaultValue(), param.getLabelKey());
            } else {
                swt_param = new BooleanParameter(current_composite, key, ((BooleanParameterImpl) param).getDefaultValue());
            }
            GridData data = new GridData();
            data.horizontalSpan = label == null ? 2 : 1;
            swt_param.setLayoutData(data);
            param.addListener(new ParameterListener() {

                @Override
                public void parameterChanged(com.biglybt.pif.ui.config.Parameter p) {
                    if (swt_param.getControls()[0].isDisposed()) {
                        param.removeListener(this);
                    } else {
                        ((BooleanParameter) swt_param).setSelected(((BooleanParameterImpl) param).getValue());
                    }
                }
            });
        } else if (param instanceof IntParameterImpl) {
            IntParameterImpl int_param = (IntParameterImpl) param;
            swt_param = new IntParameter(current_composite, key, int_param.getDefaultValue());
            if (int_param.isLimited()) {
                ((IntParameter) swt_param).setMinimumValue(int_param.getMinValue());
                ((IntParameter) swt_param).setMaximumValue(int_param.getMaxValue());
            }
            param.addListener(new ParameterListener() {

                @Override
                public void parameterChanged(com.biglybt.pif.ui.config.Parameter p) {
                    if (swt_param.getControls()[0].isDisposed()) {
                        param.removeListener(this);
                    } else {
                        ((IntParameter) swt_param).setValue(((IntParameterImpl) param).getValue());
                    }
                }
            });
            GridData gridData = new GridData();
            if (!Constants.isUnix) {
                gridData.widthHint = 100;
            }
            swt_param.setLayoutData(gridData);
        } else if (param instanceof ColorParameterImpl) {
            final Composite area = new Composite(current_composite, SWT.NULL);
            // GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_HORIZONTAL );
            GridData gridData = new GridData();
            Utils.setLayoutData(area, gridData);
            layout = new GridLayout();
            layout.numColumns = 2;
            layout.marginHeight = 0;
            layout.marginWidth = 0;
            area.setLayout(layout);
            final ButtonParameter[] reset_button_holder = new ButtonParameter[1];
            final ColorParameterImpl color_param = (ColorParameterImpl) param;
            swt_param = new com.biglybt.ui.swt.config.ColorParameter(area, key, color_param.getRedValue(), color_param.getGreenValue(), color_param.getBlueValue()) {

                @Override
                public void newColorSet(RGB newColor) {
                    color_param.reloadParamDataFromConfig(true);
                    if (reset_button_holder[0] == null) {
                        return;
                    }
                    reset_button_holder[0].getControl().setEnabled(true);
                }
            };
            // Reuse the same label as defined for UI reset buttons.
            reset_button_holder[0] = new ButtonParameter(area, "ConfigView.section.style.colorOverrides.reset");
            reset_button_holder[0].getControl().setEnabled(color_param.isOverridden());
            reset_button_holder[0].getControl().addListener(SWT.Selection, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    reset_button_holder[0].getControl().setEnabled(false);
                    color_param.resetToDefault();
                    color_param.reloadParamDataFromConfig(false);
                }
            });
            gridData = new GridData();
            gridData.widthHint = 50;
            swt_param.setLayoutData(gridData);
        } else if (param instanceof StringParameterImpl) {
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
            gridData.widthHint = 150;
            StringParameterImpl s_param = (StringParameterImpl) param;
            int num_lines = s_param.getMultiLine();
            if (num_lines <= 1) {
                swt_param = new StringParameter(current_composite, key, s_param.getDefaultValue(), s_param.getGenerateIntermediateEvents());
                swt_param.setLayoutData(gridData);
            } else {
                StringAreaParameter sa_param = new StringAreaParameter(current_composite, key, s_param.getDefaultValue());
                swt_param = sa_param;
                gridData.heightHint = sa_param.getPreferredHeight(num_lines);
                swt_param.setLayoutData(gridData);
            }
        } else if (param instanceof InfoParameterImpl) {
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
            gridData.widthHint = 150;
            swt_param = new InfoParameter(current_composite, key, "");
            swt_param.setLayoutData(gridData);
        } else if (param instanceof StringListParameterImpl) {
            StringListParameterImpl sl_param = (StringListParameterImpl) param;
            GridData gridData = new GridData();
            gridData.widthHint = 150;
            swt_param = new StringListParameter(current_composite, key, sl_param.getDefaultValue(), sl_param.getLabels(), sl_param.getValues());
            swt_param.setLayoutData(gridData);
        } else if (param instanceof PasswordParameterImpl) {
            GridData gridData = new GridData();
            gridData.widthHint = 150;
            swt_param = new PasswordParameter(current_composite, key, ((PasswordParameterImpl) param).getEncodingType());
            swt_param.setLayoutData(gridData);
        } else if (param instanceof DirectoryParameterImpl || param instanceof com.biglybt.pifimpl.local.ui.config.FileParameter) {
            Composite area = new Composite(current_composite, SWT.NULL);
            GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_HORIZONTAL);
            Utils.setLayoutData(area, gridData);
            layout = new GridLayout();
            layout.numColumns = 2;
            layout.marginHeight = 0;
            layout.marginWidth = 0;
            area.setLayout(layout);
            if (param instanceof DirectoryParameterImpl) {
                swt_param = new DirectoryParameter(area, key, ((DirectoryParameterImpl) param).getDefaultValue());
            } else {
                com.biglybt.pifimpl.local.ui.config.FileParameter fp = (com.biglybt.pifimpl.local.ui.config.FileParameter) param;
                swt_param = new com.biglybt.ui.swt.config.FileParameter(area, key, fp.getDefaultValue(), fp.getFileExtensions());
            }
        } else if (param instanceof ActionParameterImpl) {
            ActionParameterImpl _param = (ActionParameterImpl) param;
            if (_param.getStyle() == ActionParameter.STYLE_BUTTON) {
                ButtonParameter bp = new ButtonParameter(current_composite, _param.getActionResource());
                swt_param = bp;
                buttons.add(bp.getButton());
            } else {
                swt_param = new LinkParameter(current_composite, _param.getActionResource());
            }
            swt_param.addChangeListener(new ParameterChangeAdapter() {

                @Override
                public void parameterChanged(Parameter p, boolean caused_internally) {
                    try {
                        param.parameterChanged("");
                    } catch (Throwable t) {
                        Debug.out(t);
                    }
                }
            });
        } else if (param instanceof UIParameterImpl) {
            if (((UIParameterImpl) param).getContext() instanceof UISWTParameterContext) {
                UISWTParameterContext context = (UISWTParameterContext) ((UIParameterImpl) param).getContext();
                Composite internal_composite = new Composite(current_composite, SWT.NULL);
                GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
                Utils.setLayoutData(internal_composite, gridData);
                boolean initialised_component = true;
                try {
                    context.create(internal_composite);
                } catch (Exception e) {
                    Debug.printStackTrace(e);
                    initialised_component = false;
                }
                if (initialised_component) {
                    swt_param = new UISWTParameter(internal_composite, param.getKey());
                } else {
                    swt_param = null;
                    // so we don't care that this text is not localised.
                    if (label != null) {
                        label.setText("Error while generating UI component.");
                    }
                }
            } else {
                swt_param = null;
            }
        } else if (param instanceof UITextAreaImpl) {
            swt_param = new TextAreaParameter(current_composite, ((UITextAreaImpl) param));
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
            gridData.horizontalSpan = 2;
            gridData.heightHint = 100;
            swt_param.setLayoutData(gridData);
        } else {
            // label
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
            gridData.horizontalSpan = 2;
            // for wrap to work
            gridData.widthHint = 300;
            Utils.setLayoutData(label, gridData);
            swt_param = null;
        }
        if (swt_param == null) {
            if (label == null) {
                comp_map.put(param, new Object[] { null });
            } else {
                comp_map.put(param, new Object[] { null, label });
            }
        } else {
            Control[] c = swt_param.getControls();
            Object[] moo = new Object[c.length + (label == null ? 1 : 2)];
            int pos = 1;
            moo[0] = swt_param;
            if (label != null) {
                moo[pos++] = label;
            }
            System.arraycopy(c, 0, moo, 0 + pos, c.length);
            comp_map.put(param, moo);
        }
    }
    if (buttons.size() > 1) {
        Utils.makeButtonsEqualWidth(buttons);
    }
    // Only need one instance
    ParameterImplListener parameterImplListener = new ParameterImplListener() {

        @Override
        public void enabledChanged(final ParameterImpl p) {
            final Object[] stuff = (Object[]) comp_map.get(p);
            if (stuff != null) {
                if (stuff[1] != null && ((Control) stuff[1]).isDisposed()) {
                    // lazy tidyup
                    p.removeImplListener(this);
                } else {
                    Utils.execSWTThread(new AERunnable() {

                        @Override
                        public void runSupport() {
                            {
                                for (int k = 1; k < stuff.length; k++) {
                                    if (stuff[k] instanceof Control)
                                        ((Control) stuff[k]).setEnabled(p.isEnabled());
                                }
                            }
                        }
                    });
                }
            }
        }

        @Override
        public void labelChanged(final ParameterImpl p, final String text, final boolean bIsKey) {
            final Object[] stuff = (Object[]) comp_map.get(p);
            if (stuff == null)
                return;
            Label lbl;
            if (stuff[1] instanceof Label)
                lbl = (Label) stuff[1];
            else if (stuff[0] instanceof Label)
                lbl = (Label) stuff[0];
            else
                return;
            if (lbl.isDisposed()) {
                // lazy tidyup
                p.removeImplListener(this);
            } else {
                final Label finalLabel = lbl;
                Utils.execSWTThread(new AERunnable() {

                    @Override
                    public void runSupport() {
                        if (bIsKey)
                            Messages.setLanguageText(finalLabel, text);
                        else {
                            finalLabel.setData("");
                            finalLabel.setText(text);
                        }
                        finalLabel.getParent().layout(true);
                    }
                });
            }
        }
    };
    for (int i = 0; i < parameters.length; i++) {
        final ParameterImpl param = (ParameterImpl) parameters[i];
        param.addImplListener(parameterImplListener);
        if (!param.isEnabled()) {
            Object[] stuff = (Object[]) comp_map.get(param);
            if (stuff != null) {
                for (int k = 1; k < stuff.length; k++) {
                    ((Control) stuff[k]).setEnabled(false);
                }
            }
        }
        if (!param.isVisible()) {
            Object[] stuff = (Object[]) comp_map.get(param);
            if (stuff != null) {
                for (int k = 1; k < stuff.length; k++) {
                    Control con = (Control) stuff[k];
                    con.setVisible(false);
                    con.setSize(0, 0);
                    GridData gridData = new GridData();
                    gridData.heightHint = 0;
                    gridData.verticalSpan = 0;
                    gridData.grabExcessVerticalSpace = false;
                    con.setLayoutData(gridData);
                }
            }
        }
        if (param instanceof EnablerParameter) {
            List controlsToEnable = new ArrayList();
            Iterator iter = param.getEnabledOnSelectionParameters().iterator();
            while (iter.hasNext()) {
                ParameterImpl enable_param = (ParameterImpl) iter.next();
                Object[] stuff = (Object[]) comp_map.get(enable_param);
                if (stuff != null) {
                    controlsToEnable.addAll(Arrays.asList(stuff).subList(1, stuff.length));
                }
            }
            List controlsToDisable = new ArrayList();
            iter = param.getDisabledOnSelectionParameters().iterator();
            while (iter.hasNext()) {
                ParameterImpl disable_param = (ParameterImpl) iter.next();
                Object[] stuff = (Object[]) comp_map.get(disable_param);
                if (stuff != null) {
                    controlsToDisable.addAll(Arrays.asList(stuff).subList(1, stuff.length));
                }
            }
            Control[] ce = new Control[controlsToEnable.size()];
            Control[] cd = new Control[controlsToDisable.size()];
            if (ce.length + cd.length > 0) {
                IAdditionalActionPerformer ap = new DualChangeSelectionActionPerformer((Control[]) controlsToEnable.toArray(ce), (Control[]) controlsToDisable.toArray(cd));
                Object[] data = (Object[]) comp_map.get(param);
                if (data != null) {
                    BooleanParameter target = (BooleanParameter) data[0];
                    target.setAdditionalActionPerformer(ap);
                }
            }
        }
    }
    return (main_tab);
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) CTabFolder(org.eclipse.swt.custom.CTabFolder) LinkLabel(com.biglybt.ui.swt.components.LinkLabel) CTabItem(org.eclipse.swt.custom.CTabItem) List(java.util.List) RGB(org.eclipse.swt.graphics.RGB) UISWTParameterContext(com.biglybt.ui.swt.pif.UISWTParameterContext) IntParameter(com.biglybt.ui.swt.config.IntParameter) DirectoryParameter(com.biglybt.ui.swt.config.DirectoryParameter) GridLayout(org.eclipse.swt.layout.GridLayout) PasswordParameter(com.biglybt.ui.swt.config.PasswordParameter) InfoParameter(com.biglybt.ui.swt.config.InfoParameter) StringParameter(com.biglybt.ui.swt.config.StringParameter) ClipboardCopy(com.biglybt.ui.swt.mainwindow.ClipboardCopy) BooleanParameter(com.biglybt.ui.swt.config.BooleanParameter) StringListParameter(com.biglybt.ui.swt.config.StringListParameter) AERunnable(com.biglybt.core.util.AERunnable) GridData(org.eclipse.swt.layout.GridData) BooleanParameter(com.biglybt.ui.swt.config.BooleanParameter) PasswordParameter(com.biglybt.ui.swt.config.PasswordParameter) StringParameter(com.biglybt.ui.swt.config.StringParameter) InfoParameter(com.biglybt.ui.swt.config.InfoParameter) StringListParameter(com.biglybt.ui.swt.config.StringListParameter) DirectoryParameter(com.biglybt.ui.swt.config.DirectoryParameter) IntParameter(com.biglybt.ui.swt.config.IntParameter) Parameter(com.biglybt.ui.swt.config.Parameter) com.biglybt.ui.swt.config(com.biglybt.ui.swt.config) com.biglybt.pifimpl.local.ui.config(com.biglybt.pifimpl.local.ui.config) com.biglybt.pif.ui.config(com.biglybt.pif.ui.config) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Example 2 with StringParameter

use of com.biglybt.ui.swt.config.StringParameter in project BiglyBT by BiglySoftware.

the class ConfigSectionBackupRestore method configSectionCreate.

@Override
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(Constants.URL_WIKI + "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() {

        @Override
        public void mouseDoubleClick(MouseEvent arg0) {
            Utils.launch((String) ((Label) arg0.widget).getData());
        }

        @Override
        public void mouseDown(MouseEvent arg0) {
            Utils.launch((String) ((Label) arg0.widget).getData());
        }
    });
    ClipboardCopy.addCopyToClipMenu(linkLabel);
    final BackupManager backup_manager = BackupManagerFactory.getManager(CoreFactory.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() {

        @Override
        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() {

        @Override
        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)
			 */
        @Override
        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);
            }
        }
    });
    Composite gPeriod = new Composite(gDefaultDir, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 6;
    layout.marginLeft = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.marginTop = 0;
    layout.marginBottom = 0;
    gPeriod.setLayout(layout);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 3;
    gPeriod.setLayoutData(gridData);
    Label lbl_backup_days = new Label(gPeriod, SWT.NULL);
    Messages.setLanguageText(lbl_backup_days, "br.backup.auto.everydays");
    IntParameter backup_everydays = new IntParameter(gPeriod, "br.backup.auto.everydays", 0, Integer.MAX_VALUE);
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    backup_everydays.setLayoutData(gridData);
    Label lbl_backup_hours = new Label(gPeriod, SWT.NULL);
    Messages.setLanguageText(lbl_backup_hours, "br.backup.auto.everyhours");
    IntParameter backup_everyhours = new IntParameter(gPeriod, "br.backup.auto.everyhours", 1, Integer.MAX_VALUE);
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    backup_everyhours.setLayoutData(gridData);
    // for whatever reason if you set enable synchronously it gets reset :(
    Utils.execSWTThreadLater(1, new Runnable() {

        public void run() {
            backup_everyhours.setEnabled(backup_everydays.getValue() == 0);
        }
    });
    backup_everydays.addChangeListener(new ParameterChangeAdapter() {

        @Override
        public void parameterChanged(Parameter p, boolean caused_internally) {
            backup_everyhours.setEnabled(backup_everydays.getValue() == 0);
        }
    });
    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() {

        @Override
        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() {

        @Override
        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() {

                    @Override
                    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() {

                            @Override
                            public boolean reportProgress(String str) {
                                return (append(str, false));
                            }

                            @Override
                            public void reportComplete() {
                                append("Restore Complete!", true);
                                Utils.execSWTThread(new AERunnable() {

                                    @Override
                                    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() {

                                            @Override
                                            public void prompterClosed(int returnVal) {
                                                UIFunctionsSWT uiFunctions = UIFunctionsManagerSWT.getUIFunctionsSWT();
                                                if (uiFunctions != null) {
                                                    uiFunctions.dispose(true, false);
                                                }
                                            }
                                        });
                                    }
                                });
                            }

                            @Override
                            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() {

                                    @Override
                                    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);
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) ParameterChangeListener(com.biglybt.ui.swt.config.ParameterChangeListener) UserPrompterResultListener(com.biglybt.ui.UserPrompterResultListener) Image(org.eclipse.swt.graphics.Image) GridLayout(org.eclipse.swt.layout.GridLayout) TextViewerWindow(com.biglybt.ui.swt.TextViewerWindow) StringParameter(com.biglybt.ui.swt.config.StringParameter) MouseEvent(org.eclipse.swt.events.MouseEvent) MouseAdapter(org.eclipse.swt.events.MouseAdapter) ParameterChangeAdapter(com.biglybt.ui.swt.config.ParameterChangeAdapter) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) UIFunctionsSWT(com.biglybt.ui.swt.UIFunctionsSWT) BackupManager(com.biglybt.core.backup.BackupManager) BooleanParameter(com.biglybt.ui.swt.config.BooleanParameter) Date(java.util.Date) UserPrompterResultListener(com.biglybt.ui.UserPrompterResultListener) AERunnable(com.biglybt.core.util.AERunnable) GridData(org.eclipse.swt.layout.GridData) MouseEvent(org.eclipse.swt.events.MouseEvent) BooleanParameter(com.biglybt.ui.swt.config.BooleanParameter) StringParameter(com.biglybt.ui.swt.config.StringParameter) IntParameter(com.biglybt.ui.swt.config.IntParameter) Parameter(com.biglybt.ui.swt.config.Parameter) ChangeSelectionActionPerformer(com.biglybt.ui.swt.config.ChangeSelectionActionPerformer) ImageLoader(com.biglybt.ui.swt.imageloader.ImageLoader) File(java.io.File) IntParameter(com.biglybt.ui.swt.config.IntParameter)

Aggregations

AERunnable (com.biglybt.core.util.AERunnable)2 BooleanParameter (com.biglybt.ui.swt.config.BooleanParameter)2 IntParameter (com.biglybt.ui.swt.config.IntParameter)2 Parameter (com.biglybt.ui.swt.config.Parameter)2 StringParameter (com.biglybt.ui.swt.config.StringParameter)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 BackupManager (com.biglybt.core.backup.BackupManager)1 com.biglybt.pif.ui.config (com.biglybt.pif.ui.config)1 BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)1 com.biglybt.pifimpl.local.ui.config (com.biglybt.pifimpl.local.ui.config)1 UserPrompterResultListener (com.biglybt.ui.UserPrompterResultListener)1 TextViewerWindow (com.biglybt.ui.swt.TextViewerWindow)1 UIFunctionsSWT (com.biglybt.ui.swt.UIFunctionsSWT)1 LinkLabel (com.biglybt.ui.swt.components.LinkLabel)1 com.biglybt.ui.swt.config (com.biglybt.ui.swt.config)1 ChangeSelectionActionPerformer (com.biglybt.ui.swt.config.ChangeSelectionActionPerformer)1 DirectoryParameter (com.biglybt.ui.swt.config.DirectoryParameter)1 InfoParameter (com.biglybt.ui.swt.config.InfoParameter)1 ParameterChangeAdapter (com.biglybt.ui.swt.config.ParameterChangeAdapter)1