Search in sources :

Example 11 with BasicPluginConfigModel

use of com.biglybt.pif.ui.model.BasicPluginConfigModel 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 12 with BasicPluginConfigModel

use of com.biglybt.pif.ui.model.BasicPluginConfigModel in project BiglyBT by BiglySoftware.

the class PluginUpdatePlugin method initialize.

@Override
public void initialize(PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;
    plugin_interface.getPluginProperties().setProperty("plugin.version", "1.0");
    plugin_interface.getPluginProperties().setProperty("plugin.name", "Plugin Updater");
    log = plugin_interface.getLogger().getChannel("Plugin Update");
    log.setDiagnostic();
    log.setForce(true);
    UIManager ui_manager = plugin_interface.getUIManager();
    final BasicPluginViewModel model = ui_manager.createBasicPluginViewModel(PLUGIN_RESOURCE_ID);
    final PluginConfig plugin_config = plugin_interface.getPluginconfig();
    boolean enabled = plugin_config.getPluginBooleanParameter("enable.update", true);
    model.setConfigSectionID(PLUGIN_CONFIGSECTION_ID);
    model.getStatus().setText(enabled ? "Running" : "Optional checks disabled");
    model.getActivity().setVisible(false);
    model.getProgress().setVisible(false);
    log.addListener(new LoggerChannelListener() {

        @Override
        public void messageLogged(int type, String message) {
            model.getLogArea().appendText(message + "\n");
        }

        @Override
        public void messageLogged(String str, Throwable error) {
            model.getLogArea().appendText(error.toString() + "\n");
        }
    });
    BasicPluginConfigModel config = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_PLUGINS, PLUGIN_CONFIGSECTION_ID);
    config.addBooleanParameter2("enable.update", "Plugin.pluginupdate.enablecheck", true);
    plugin_interface.addEventListener(new PluginEventListener() {

        @Override
        public void handleEvent(PluginEvent ev) {
            if (ev.getType() == PluginEvent.PEV_ALL_PLUGINS_INITIALISED) {
                plugin_interface.removeEventListener(this);
                initComplete(plugin_config);
            }
        }
    });
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) UIManager(com.biglybt.pif.ui.UIManager) BasicPluginViewModel(com.biglybt.pif.ui.model.BasicPluginViewModel) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Example 13 with BasicPluginConfigModel

use of com.biglybt.pif.ui.model.BasicPluginConfigModel in project BiglyBT by BiglySoftware.

the class BuddyPlugin method initialize.

@Override
public void initialize(final PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;
    ta_category = plugin_interface.getTorrentManager().getAttribute(TorrentAttribute.TA_CATEGORY);
    az2_handler = new BuddyPluginAZ2(this);
    sec_man = plugin_interface.getUtilities().getSecurityManager();
    logger = plugin_interface.getLogger().getChannel("Friends");
    logger.setDiagnostic();
    final LocaleUtilities lu = plugin_interface.getUtilities().getLocaleUtilities();
    lu.addListener(new LocaleListener() {

        @Override
        public void localeChanged(Locale l) {
            updateLocale(lu);
        }
    });
    updateLocale(lu);
    BasicPluginConfigModel config = plugin_interface.getUIManager().createBasicPluginConfigModel("Views.plugins." + VIEW_ID + ".title");
    // enabled
    classic_enabled_param = config.addBooleanParameter2("azbuddy.enabled", "azbuddy.enabled", false);
    // nickname
    nick_name_param = config.addStringParameter2("azbuddy.nickname", "azbuddy.nickname", "");
    nick_name_param.setGenerateIntermediateEvents(false);
    nick_name_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            updateNickName(nick_name_param.getValue());
        }
    });
    // online status
    String[] os_values = STATUS_VALUES;
    String[] os_labels = STATUS_STRINGS;
    online_status_param = config.addStringListParameter2("azbuddy.online_status", "azbuddy.online_status", os_values, os_labels, os_values[0]);
    online_status_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            int status = Integer.parseInt(online_status_param.getValue());
            updateOnlineStatus(status);
        }
    });
    // If we add this then use proper message texts in the STATUS_STRINGS
    online_status_param.setVisible(SUPPORT_ONLINE_STATUS);
    // protocol speed
    final IntParameter protocol_speed = config.addIntParameter2("azbuddy.protocolspeed", "azbuddy.protocolspeed", 32);
    protocol_speed.setMinimumRequiredUserMode(Parameter.MODE_ADVANCED);
    ConnectionManager cman = plugin_interface.getConnectionManager();
    int inbound_limit = protocol_speed.getValue() * 1024;
    inbound_limiter = cman.createRateLimiter("buddy_up", inbound_limit);
    outbound_limiter = cman.createRateLimiter("buddy_down", 0);
    protocol_speed.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            inbound_limiter.setRateLimitBytesPerSecond(protocol_speed.getValue() * 1024);
        }
    });
    // chat notifications
    enable_chat_notifications = config.addBooleanParameter2("azbuddy.enable_chat_notif", "azbuddy.enable_chat_notif", true);
    // default published tags or cats
    cat_pub = config.addStringParameter2("azbuddy.enable_cat_pub", "azbuddy.enable_cat_pub", "");
    cat_pub.setGenerateIntermediateEvents(false);
    setPublicTagsOrCategories(cat_pub.getValue(), false);
    final BooleanParameter tracker_enable = config.addBooleanParameter2("azbuddy.tracker.enabled", "azbuddy.tracker.enabled", true);
    final BooleanParameter tracker_so_enable = config.addBooleanParameter2("azbuddy.tracker.seeding.only.enabled", "azbuddy.tracker.seeding.only.enabled", false);
    final BooleanParameter buddies_lan_local = config.addBooleanParameter2("azbuddy.tracker.con.lan.local", "azbuddy.tracker.con.lan.local", true);
    buddies_lan_local.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            lan_local_peers = buddies_lan_local.getValue();
        }
    });
    lan_local_peers = buddies_lan_local.getValue();
    cat_pub.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            setPublicTagsOrCategories(cat_pub.getValue(), false);
        }
    });
    config.createGroup("label.friends", new Parameter[] { classic_enabled_param, nick_name_param, online_status_param, protocol_speed, enable_chat_notifications, cat_pub, tracker_enable, tracker_so_enable, buddies_lan_local });
    // decentralised stuff
    beta_enabled_param = config.addBooleanParameter2("azbuddy.dchat.decentralized.enabled", "azbuddy.dchat.decentralized.enabled", true);
    config.createGroup("azbuddy.dchat.decentralized", new Parameter[] { beta_enabled_param });
    // config end
    beta_plugin = new BuddyPluginBeta(plugin_interface, this, beta_enabled_param);
    final TableContextMenuItem menu_item_itorrents = plugin_interface.getUIManager().getTableManager().addContextMenuItem(TableManager.TABLE_MYTORRENTS_INCOMPLETE, "azbuddy.contextmenu");
    final TableContextMenuItem menu_item_ctorrents = plugin_interface.getUIManager().getTableManager().addContextMenuItem(TableManager.TABLE_MYTORRENTS_COMPLETE, "azbuddy.contextmenu");
    menu_item_itorrents.setStyle(TableContextMenuItem.STYLE_MENU);
    menu_item_itorrents.setHeaderCategory(MenuItem.HEADER_SOCIAL);
    menu_item_ctorrents.setStyle(TableContextMenuItem.STYLE_MENU);
    menu_item_ctorrents.setHeaderCategory(MenuItem.HEADER_SOCIAL);
    MenuItemFillListener menu_fill_listener = new MenuItemFillListener() {

        @Override
        public void menuWillBeShown(MenuItem menu, Object _target) {
            menu.removeAllChildItems();
            if (!(isClassicEnabled() && isAvailable())) {
                menu.setEnabled(false);
                return;
            }
            final List<Torrent> torrents = new ArrayList<>();
            if (_target instanceof TableRow) {
                addDownload(torrents, (TableRow) _target);
            } else {
                TableRow[] rows = (TableRow[]) _target;
                for (TableRow row : rows) {
                    addDownload(torrents, row);
                }
            }
            if (torrents.size() == 0) {
                menu.setEnabled(false);
            } else {
                List<BuddyPluginBuddy> buddies = getBuddies();
                boolean incomplete = ((TableContextMenuItem) menu).getTableID() == TableManager.TABLE_MYTORRENTS_INCOMPLETE;
                TableContextMenuItem parent = incomplete ? menu_item_itorrents : menu_item_ctorrents;
                for (int i = 0; i < buddies.size(); i++) {
                    final BuddyPluginBuddy buddy = (BuddyPluginBuddy) buddies.get(i);
                    boolean online = buddy.isOnline(true);
                    TableContextMenuItem item = plugin_interface.getUIManager().getTableManager().addContextMenuItem(parent, "!" + buddy.getName() + (online ? "" : (" - " + MessageText.getString("label.disconnected"))) + "!");
                    item.addMultiListener(new MenuItemListener() {

                        @Override
                        public void selected(MenuItem menu, Object target) {
                            for (Torrent torrent : torrents) {
                                az2_handler.sendAZ2Torrent(torrent, buddy);
                            }
                        }
                    });
                    item.setEnabled(online);
                }
                menu.setEnabled(true);
            }
        }

        protected void addDownload(List<Torrent> torrents, TableRow row) {
            Object obj = row.getDataSource();
            Download download;
            if (obj instanceof Download) {
                download = (Download) obj;
            } else {
                DiskManagerFileInfo file = (DiskManagerFileInfo) obj;
                try {
                    download = file.getDownload();
                } catch (DownloadException e) {
                    Debug.printStackTrace(e);
                    return;
                }
            }
            Torrent torrent = download.getTorrent();
            if (torrent != null && !TorrentUtils.isReallyPrivate(PluginCoreUtils.unwrap(torrent))) {
                torrents.add(torrent);
            }
        }
    };
    menu_item_itorrents.addFillListener(menu_fill_listener);
    menu_item_ctorrents.addFillListener(menu_fill_listener);
    buddy_tracker = new BuddyPluginTracker(this, tracker_enable, tracker_so_enable);
    plugin_interface.getUIManager().addUIListener(new UIManagerListener() {

        @Override
        public void UIAttached(final UIInstance instance) {
            if (instance.getUIType().equals(UIInstance.UIT_SWT)) {
                try {
                    synchronized (swt_ui_waiters) {
                        swt_ui = (BuddyPluginViewInterface) Class.forName("com.biglybt.plugin.net.buddy.swt.BuddyPluginView").getConstructor(new Class[] { BuddyPlugin.class, UIInstance.class, String.class }).newInstance(new Object[] { BuddyPlugin.this, instance, VIEW_ID });
                        for (Runnable r : swt_ui_waiters) {
                            try {
                                r.run();
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                        swt_ui_waiters.clear();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
            setupDisablePrompt(instance);
        }

        @Override
        public void UIDetached(UIInstance instance) {
            if (instance.getUIType().equals(UIInstance.UIT_SWT) && swt_ui != null) {
                swt_ui.destroy();
                swt_ui = null;
            }
        }
    });
    ParameterListener enabled_listener = new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            boolean classic_enabled = classic_enabled_param.getValue();
            nick_name_param.setEnabled(classic_enabled);
            online_status_param.setEnabled(classic_enabled);
            protocol_speed.setEnabled(classic_enabled);
            enable_chat_notifications.setEnabled(classic_enabled);
            cat_pub.setEnabled(classic_enabled);
            tracker_enable.setEnabled(classic_enabled);
            tracker_so_enable.setEnabled(tracker_enable.getValue());
            if (param != null) {
                setClassicEnabledInternal(classic_enabled);
                fireEnabledStateChanged();
            }
        }
    };
    enabled_listener.parameterChanged(null);
    classic_enabled_param.addListener(enabled_listener);
    beta_enabled_param.addListener(enabled_listener);
    tracker_enable.addListener(enabled_listener);
    loadConfig();
    registerMessageHandler();
    plugin_interface.addListener(new PluginListener() {

        @Override
        public void initializationComplete() {
            final DelayedTask dt = plugin_interface.getUtilities().createDelayedTask(new Runnable() {

                @Override
                public void run() {
                    new AEThread2("BuddyPlugin:init", true) {

                        @Override
                        public void run() {
                            startup();
                            beta_plugin.startup();
                            initialization_complete.set(true);
                        }
                    }.start();
                }
            });
            dt.queue();
        }

        @Override
        public void closedownInitiated() {
            saveConfig(true);
            closedown();
            beta_plugin.closedown();
        }

        @Override
        public void closedownComplete() {
        }
    });
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TableContextMenuItem(com.biglybt.pif.ui.tables.TableContextMenuItem) PluginListener(com.biglybt.pif.PluginListener) ConnectionManager(com.biglybt.pif.network.ConnectionManager) DownloadException(com.biglybt.pif.download.DownloadException) MenuItemListener(com.biglybt.pif.ui.menus.MenuItemListener) BuddyPluginTracker(com.biglybt.plugin.net.buddy.tracker.BuddyPluginTracker) Download(com.biglybt.pif.download.Download) UIInstance(com.biglybt.pif.ui.UIInstance) DiskManagerFileInfo(com.biglybt.pif.disk.DiskManagerFileInfo) TableContextMenuItem(com.biglybt.pif.ui.tables.TableContextMenuItem) MenuItem(com.biglybt.pif.ui.menus.MenuItem) MenuItemFillListener(com.biglybt.pif.ui.menus.MenuItemFillListener) TableRow(com.biglybt.pif.ui.tables.TableRow) ParameterListener(com.biglybt.pif.ui.config.ParameterListener) UIManagerListener(com.biglybt.pif.ui.UIManagerListener) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Example 14 with BasicPluginConfigModel

use of com.biglybt.pif.ui.model.BasicPluginConfigModel in project BiglyBT by BiglySoftware.

the class NetStatusPlugin method initialize.

@Override
public void initialize(final PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;
    logger = plugin_interface.getLogger().getChannel("NetStatus");
    logger.setDiagnostic();
    BasicPluginConfigModel config = plugin_interface.getUIManager().createBasicPluginConfigModel("Views.plugins." + VIEW_ID + ".title");
    logging_detailed = config.addBooleanParameter2("plugin.aznetstatus.logfull", "plugin.aznetstatus.logfull", false);
    plugin_interface.getUIManager().addUIListener(new UIManagerListener() {

        @Override
        public void UIAttached(UIInstance instance) {
            if (instance.getUIType().equals(UIInstance.UIT_SWT)) {
                try {
                    swt_ui = (NetStatusPluginViewInterface) Class.forName("com.biglybt.plugin.net.netstatus.swt.NetStatusPluginView").getConstructor(new Class[] { NetStatusPlugin.class, UIInstance.class, String.class }).newInstance(new Object[] { NetStatusPlugin.this, instance, VIEW_ID });
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void UIDetached(UIInstance instance) {
            if (instance.getUIType().equals(UIInstance.UIT_SWT) && swt_ui != null) {
                swt_ui.detach(instance);
                swt_ui = null;
            }
        }
    });
    plugin_interface.addListener(new PluginListener() {

        @Override
        public void initializationComplete() {
            new AEThread2("NetstatusPlugin:init", true) {

                @Override
                public void run() {
                    try {
                        protocol_tester = new NetStatusProtocolTester(NetStatusPlugin.this, plugin_interface);
                        if (test_button != null) {
                            test_button.setEnabled(true);
                        }
                    } finally {
                        protocol_tester_sem.releaseForever();
                    }
                }
            }.start();
        }

        @Override
        public void closedownInitiated() {
        }

        @Override
        public void closedownComplete() {
        }
    });
}
Also used : UIManagerListener(com.biglybt.pif.ui.UIManagerListener) UIInstance(com.biglybt.pif.ui.UIInstance) AEThread2(com.biglybt.core.util.AEThread2) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel) PluginListener(com.biglybt.pif.PluginListener)

Example 15 with BasicPluginConfigModel

use of com.biglybt.pif.ui.model.BasicPluginConfigModel in project BiglyBT by BiglySoftware.

the class SearchUI method delayedInit.

private void delayedInit() {
    final MetaSearchManager manager = MetaSearchManagerFactory.getSingleton();
    if (manager == null) {
        return;
    }
    BasicPluginConfigModel configModel = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_ROOT, CONFIG_SECTION_ID);
    // proxy enable
    final BooleanParameter proxy_enable = configModel.addBooleanParameter2("search.proxy.enable", "search.proxy.enable", manager.getProxyRequestsEnabled());
    proxy_enable.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            manager.setProxyRequestsEnabled(proxy_enable.getValue());
        }
    });
    // open rcm on search
    final BooleanParameter paramRCMWindow = configModel.addBooleanParameter2("search.showRCMView", "search.showRCMView", false);
}
Also used : ParameterListener(com.biglybt.pif.ui.config.ParameterListener) BooleanParameter(com.biglybt.pif.ui.config.BooleanParameter) Parameter(com.biglybt.pif.ui.config.Parameter) MetaSearchManager(com.biglybt.core.metasearch.MetaSearchManager) BooleanParameter(com.biglybt.pif.ui.config.BooleanParameter) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Aggregations

BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)18 PluginListener (com.biglybt.pif.PluginListener)6 UIManager (com.biglybt.pif.ui.UIManager)6 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)5 BasicPluginViewModel (com.biglybt.pif.ui.model.BasicPluginViewModel)5 UIInstance (com.biglybt.pif.ui.UIInstance)4 UIManagerListener (com.biglybt.pif.ui.UIManagerListener)4 AERunnable (com.biglybt.core.util.AERunnable)3 BooleanParameter (com.biglybt.pif.ui.config.BooleanParameter)3 DelayedTask (com.biglybt.pif.utils.DelayedTask)3 PluginInterface (com.biglybt.pif.PluginInterface)2 Torrent (com.biglybt.pif.torrent.Torrent)2 ParameterListener (com.biglybt.pif.ui.config.ParameterListener)2 UTTimerEvent (com.biglybt.pif.utils.UTTimerEvent)2 UTTimerEventPerformer (com.biglybt.pif.utils.UTTimerEventPerformer)2 File (java.io.File)2 InetSocketAddress (java.net.InetSocketAddress)2 URL (java.net.URL)2 ParameterListener (com.biglybt.core.config.ParameterListener)1 DHT (com.biglybt.core.dht.DHT)1