Search in sources :

Example 1 with DualChangeSelectionActionPerformer

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

the class ConfigView method buildScreen.

private static void buildScreen(final Composite main_tab, Parameter[] parameters, Map<ParameterImpl, BaseSwtParameter> mapParamToSwtParam, ParameterImplListener parameterImplListener) {
    int userMode = Utils.getUserMode();
    // main tab set up
    Composite curComposite = main_tab;
    Map<ParameterGroupImpl, Composite> group_map = new HashMap<>();
    Map<ParameterTabFolderImpl, CTabFolder> tab_folder_map = new HashMap<>();
    Map<ParameterGroupImpl, Composite> tab_map = new HashMap<>();
    for (int i = 0; i < parameters.length; i++) {
        ParameterImpl param = (ParameterImpl) parameters[i];
        if (param.getMinimumRequiredUserMode() > userMode) {
            continue;
        }
        ParameterGroupImpl pg = param.getGroup();
        if (pg != null && pg.getGroup() != null && group_map.get(pg.getGroup()) == null && tab_folder_map.get(pg.getGroup()) == null) {
            // Parent group hasn't been created yet.
            // Hack in a label to parent group, so it gets created
            // doesn't solve deeper nesting
            i--;
            param = new ParameterImpl(null, null) {
            };
            pg = pg.getGroup();
            param.setGroup(pg);
        }
        if (pg == null) {
            curComposite = main_tab;
        } else {
            ParameterTabFolderImpl tab_folder = pg.getTabFolder();
            if (tab_folder != null) {
                curComposite = handleTabFolder(userMode, curComposite, group_map, tab_folder_map, tab_map, pg, tab_folder);
            }
            Composite comp = group_map.get(pg);
            if (comp == null) {
                ParameterGroupImpl pgParent = pg.getGroup();
                boolean nested = pgParent != null || tab_folder != null;
                if (tab_folder == null) {
                    Composite composite = group_map.get(pgParent);
                    if (composite != null) {
                        curComposite = composite;
                    }
                }
                Composite group_parent = nested ? curComposite : main_tab;
                String resource_name = pg.getGroupTitleKey();
                boolean use_composite = resource_name == null || tab_folder != null;
                curComposite = use_composite ? new Composite(group_parent, SWT.NONE) : new Group(group_parent, SWT.NULL);
                Control relatedControl = null;
                if (!use_composite) {
                    Canvas gap = new Canvas(group_parent, SWT.NULL);
                    relatedControl = gap;
                    gap.setData("gap", true);
                    GridData gridData = new GridData();
                    gridData.widthHint = 1;
                    gridData.heightHint = 2;
                    gridData.horizontalSpan = ((GridLayout) group_parent.getLayout()).numColumns;
                    gap.setLayoutData(gridData);
                    Messages.setLanguageText(curComposite, resource_name);
                    if (groupFont == null) {
                        groupFont = FontUtils.getFontPercentOf(curComposite.getFont(), 1.25f);
                    }
                    curComposite.setFont(groupFont);
                }
                if (group_parent.getLayout() instanceof GridLayout) {
                    GridData grid_data = new GridData(GridData.FILL_HORIZONTAL);
                    grid_data.horizontalSpan = 2;
                    if (pg.getMinimumRequiredUserMode() > userMode) {
                        curComposite.setVisible(false);
                        grid_data.widthHint = 0;
                        grid_data.heightHint = 0;
                    }
                    grid_data.horizontalIndent = pg.getIndent() * 20;
                    if (!use_composite) {
                        Control[] children = group_parent.getChildren();
                        // last - 2 = gap widget or previous control
                        if (children.length > 3 && children[children.length - 3].getData("gap") == null) {
                            grid_data.verticalIndent = 5;
                        }
                    }
                    curComposite.setLayoutData(grid_data);
                }
                int numColumns = pg.getNumberColumns();
                if (numColumns > 0) {
                    GridLayout layout = new GridLayout();
                    layout.numColumns = numColumns * 2;
                    if (use_composite) {
                        layout.marginWidth = layout.marginHeight = 0;
                    } else {
                        layout.marginTop = 5;
                    }
                    curComposite.setLayout(layout);
                } else {
                    RowLayout layout = new RowLayout();
                    layout.marginLeft = pg.getIndent() * 20;
                    layout.marginRight = 0;
                    layout.spacing = 5;
                    layout.center = true;
                    curComposite.setLayout(layout);
                }
                group_map.put(pg, curComposite);
                UISWTParameter swt_param = new GroupSWTParameter(curComposite, pg, relatedControl);
                int indent = pg.getIndent();
                if (indent > 0) {
                    swt_param.setIndent(indent, param.isIndentFancy());
                }
                String refID = pg.getReferenceID();
                if (refID != null) {
                    // TODO: Migrate to a field
                    swt_param.getMainControl().setData(SELECT_KEY, refID);
                }
                mapParamToSwtParam.put(pg, swt_param);
            } else {
                curComposite = comp;
            }
        }
        String label_key = param.getLabelKey();
        if (label_key == null) {
            String labelText = param.getLabelText();
            if (labelText != null) {
                label_key = "!" + labelText + "!";
            }
        }
        String key = param.getConfigKeyName();
        // System.out.println( "key = " + key );
        final BaseSwtParameter swt_param;
        if (param instanceof HyperlinkParameterImpl) {
            // check must be before LabelParameterImpl
            swt_param = new LinkSwtParameter(curComposite, (HyperlinkParameterImpl) param);
        } else if (param instanceof LabelParameterImpl) {
            // check must be after HyperlinkParameterImpl
            swt_param = new InfoSwtParameter(curComposite, (LabelParameterImpl) param);
        } else if (param instanceof BooleanParameterImpl) {
            swt_param = new BooleanSwtParameter(curComposite, (BooleanParameterImpl) param);
        } else if (param instanceof IntParameterImpl) {
            swt_param = new IntSwtParameter(curComposite, (IntParameterImpl) param);
        } else if (param instanceof FloatParameterImpl) {
            swt_param = new FloatSwtParameter(curComposite, (FloatParameterImpl) param);
        } else if (param instanceof ColorParameterImpl) {
            swt_param = new ColorSwtParameter(curComposite, (ColorParameterImpl) param);
        } else if (param instanceof StringParameterImpl) {
            StringParameterImpl s_param = (StringParameterImpl) param;
            int num_lines = s_param.getMultiLine();
            if (num_lines <= 1) {
                swt_param = new StringSwtParameter(curComposite, s_param);
            } else {
                swt_param = new StringAreaSwtParameter(curComposite, s_param);
            }
        } else if (param instanceof InfoParameterImpl) {
            swt_param = new InfoSwtParameter(curComposite, (InfoParameterImpl) param);
        } else if (param instanceof StringListParameterImpl) {
            swt_param = new StringListSwtParameter(curComposite, (StringListParameterImpl) param);
        } else if (param instanceof IntListParameterImpl) {
            IntListParameterImpl il_param = (IntListParameterImpl) param;
            int listType = il_param.getListType();
            if (listType == IntListParameter.TYPE_RADIO_LIST || listType == IntListParameter.TYPE_RADIO_COMPACT) {
                swt_param = new IntRadioListSwtParameter(curComposite, il_param);
            } else {
                swt_param = new IntListSwtParameter(curComposite, il_param);
            }
        } else if (param instanceof PasswordParameterImpl) {
            swt_param = new PasswordSwtParameter(curComposite, (PasswordParameterImpl) param);
        } else if (param instanceof FileParameterImpl) {
            swt_param = new FileSwtParameter(curComposite, (FileParameterImpl) param);
        } else if (param instanceof DirectoryParameterImpl) {
            swt_param = new DirectorySwtParameter(curComposite, (DirectoryParameterImpl) param);
        } else if (param instanceof ActionParameterImpl) {
            ActionParameterImpl _param = (ActionParameterImpl) param;
            if (_param.getStyle() == ActionParameter.STYLE_BUTTON) {
                swt_param = new ButtonSwtParameter(curComposite, (ActionParameterImpl) param);
            } else {
                swt_param = new LinkSwtParameter(curComposite, (ActionParameterImpl) param);
            }
        } else if (param instanceof UIParameterImpl) {
            if (((UIParameterImpl) param).getContext() instanceof UISWTParameterContext) {
                UISWTParameterContext context = (UISWTParameterContext) ((UIParameterImpl) param).getContext();
                Composite internal_composite = new Composite(curComposite, SWT.NULL);
                GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
                gridData.horizontalSpan = param.getLabelKey() == null || param.getLabelText() == null ? 2 : 1;
                internal_composite.setLayoutData(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.getConfigKeyName());
                } else {
                    swt_param = null;
                }
            } else {
                swt_param = null;
            }
        } else if (param instanceof UITextAreaImpl) {
            swt_param = new TextAreaSwtParameter(curComposite, ((UITextAreaImpl) param));
        } else {
            swt_param = null;
        }
        if (swt_param != null) {
            int indent = param.getIndent();
            if (indent > 0) {
                swt_param.setIndent(indent, param.isIndentFancy());
            }
            String refID = param.getReferenceID();
            if (refID != null) {
                // TODO: Migrate to a field
                swt_param.getMainControl().setData(SELECT_KEY, refID);
            }
            mapParamToSwtParam.put(param, swt_param);
        }
    }
    for (Parameter parameter : parameters) {
        final ParameterImpl param = (ParameterImpl) parameter;
        param.addImplListener(parameterImplListener);
        if (!param.isEnabled()) {
            BaseSwtParameter swtParam = mapParamToSwtParam.get(param);
            if (swtParam != null) {
                swtParam.setEnabled(false);
            }
        }
        if (!param.isVisible()) {
            BaseSwtParameter swtParam = mapParamToSwtParam.get(param);
            if (swtParam != null) {
                swtParam.setVisible(false);
            }
        }
        List<BaseSwtParameter> swtParamsToEnable = new ArrayList<>();
        List<Parameter> listEnableOnSelection = param.getEnabledOnSelectionParameters();
        for (Parameter enable_param : listEnableOnSelection) {
            BaseSwtParameter stuff = mapParamToSwtParam.get(enable_param);
            if (stuff != null) {
                swtParamsToEnable.add(stuff);
            }
        }
        List<BaseSwtParameter> swtParamsToDisable = new ArrayList<>();
        List<Parameter> listDisableOnSelection = param.getDisabledOnSelectionParameters();
        for (Parameter disable_param : listDisableOnSelection) {
            BaseSwtParameter swtParameter = mapParamToSwtParam.get(disable_param);
            if (swtParameter != null) {
                swtParamsToDisable.add(swtParameter);
            }
        }
        if (swtParamsToEnable.size() + swtParamsToDisable.size() > 0) {
            BaseSwtParameter swtParameter = mapParamToSwtParam.get(param);
            if (swtParameter instanceof BooleanSwtParameter) {
                IAdditionalActionPerformer<Boolean> ap = new DualChangeSelectionActionPerformer(swtParamsToEnable.toArray(new BaseSwtParameter[0]), swtParamsToDisable.toArray(new BaseSwtParameter[0]));
                ((BooleanSwtParameter) swtParameter).setAdditionalActionPerformer(ap);
            }
        }
    }
}
Also used : DualChangeSelectionActionPerformer(com.biglybt.ui.swt.config.actionperformer.DualChangeSelectionActionPerformer) UISWTParameterContext(com.biglybt.ui.swt.pif.UISWTParameterContext) ActionParameter(com.biglybt.pif.ui.config.ActionParameter) IntListParameter(com.biglybt.pif.ui.config.IntListParameter) Parameter(com.biglybt.pif.ui.config.Parameter)

Aggregations

ActionParameter (com.biglybt.pif.ui.config.ActionParameter)1 IntListParameter (com.biglybt.pif.ui.config.IntListParameter)1 Parameter (com.biglybt.pif.ui.config.Parameter)1 DualChangeSelectionActionPerformer (com.biglybt.ui.swt.config.actionperformer.DualChangeSelectionActionPerformer)1 UISWTParameterContext (com.biglybt.ui.swt.pif.UISWTParameterContext)1