Search in sources :

Example 1 with PluginConfigModel

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

the class ConfigSectionPlugins method configSectionCreate.

@Override
public Composite configSectionCreate(final Composite parent) {
    if (!CoreFactory.isCoreRunning()) {
        Composite cSection = new Composite(parent, SWT.NULL);
        cSection.setLayout(new FillLayout());
        Label lblNotAvail = new Label(cSection, SWT.WRAP);
        Messages.setLanguageText(lblNotAvail, "core.not.available");
        return cSection;
    }
    GridLayout layout;
    GridData gridData;
    Label label;
    ImageLoader imageLoader = ImageLoader.getInstance();
    imgRedLed = imageLoader.getImage("redled");
    imgGreenLed = imageLoader.getImage("greenled");
    Composite infoGroup = new Composite(parent, SWT.NULL);
    gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
    Utils.setLayoutData(infoGroup, gridData);
    layout = new GridLayout();
    layout.numColumns = 1;
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    infoGroup.setLayout(layout);
    infoGroup.setLayout(new GridLayout());
    String sep = System.getProperty("file.separator");
    File fUserPluginDir = FileUtil.getUserFile("plugins");
    String sUserPluginDir;
    try {
        sUserPluginDir = fUserPluginDir.getCanonicalPath();
    } catch (Throwable e) {
        sUserPluginDir = fUserPluginDir.toString();
    }
    if (!sUserPluginDir.endsWith(sep)) {
        sUserPluginDir += sep;
    }
    File fAppPluginDir = FileUtil.getApplicationFile("plugins");
    String sAppPluginDir;
    try {
        sAppPluginDir = fAppPluginDir.getCanonicalPath();
    } catch (Throwable e) {
        sAppPluginDir = fAppPluginDir.toString();
    }
    if (!sAppPluginDir.endsWith(sep)) {
        sAppPluginDir += sep;
    }
    label = new Label(infoGroup, SWT.WRAP);
    Utils.setLayoutData(label, new GridData(GridData.FILL_HORIZONTAL));
    Messages.setLanguageText(label, "ConfigView.pluginlist.whereToPut");
    label = new Label(infoGroup, SWT.WRAP);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalIndent = 10;
    Utils.setLayoutData(label, gridData);
    label.setText(sUserPluginDir.replaceAll("&", "&&"));
    label.setForeground(Colors.blue);
    label.setCursor(label.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
    final String _sUserPluginDir = sUserPluginDir;
    label.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent arg0) {
            if (_sUserPluginDir.endsWith("/plugins/") || _sUserPluginDir.endsWith("\\plugins\\")) {
                File f = new File(_sUserPluginDir);
                String dir = _sUserPluginDir;
                if (!(f.exists() && f.isDirectory())) {
                    dir = _sUserPluginDir.substring(0, _sUserPluginDir.length() - 9);
                }
                Utils.launch(dir);
            }
        }
    });
    label = new Label(infoGroup, SWT.WRAP);
    Utils.setLayoutData(label, new GridData(GridData.FILL_HORIZONTAL));
    Messages.setLanguageText(label, "ConfigView.pluginlist.whereToPutOr");
    label = new Label(infoGroup, SWT.WRAP);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalIndent = 10;
    Utils.setLayoutData(label, gridData);
    label.setText(sAppPluginDir.replaceAll("&", "&&"));
    label.setForeground(Colors.blue);
    label.setCursor(label.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
    final String _sAppPluginDir = sAppPluginDir;
    // TODO : Fix it for windows
    label.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent arg0) {
            if (_sAppPluginDir.endsWith("/plugins/") || _sAppPluginDir.endsWith("\\plugins\\")) {
                File f = new File(_sAppPluginDir);
                if (f.exists() && f.isDirectory()) {
                    Utils.launch(_sAppPluginDir);
                } else {
                    String appDir = _sAppPluginDir.substring(0, _sAppPluginDir.length() - 9);
                    System.out.println(appDir);
                    Utils.launch(appDir);
                }
            }
        }
    });
    pluginIFs = rebuildPluginIFs();
    Collections.sort(pluginIFs, new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            return (((PluginInterface) o1).getPluginName().compareToIgnoreCase(((PluginInterface) o2).getPluginName()));
        }
    });
    Label labelInfo = new Label(infoGroup, SWT.WRAP);
    Utils.setLayoutData(labelInfo, new GridData(GridData.FILL_HORIZONTAL));
    Messages.setLanguageText(labelInfo, "ConfigView.pluginlist.info");
    table = new Table(infoGroup, SWT.BORDER | SWT.SINGLE | SWT.CHECK | SWT.VIRTUAL | SWT.FULL_SELECTION);
    gridData = new GridData(GridData.FILL_BOTH);
    gridData.heightHint = 200;
    gridData.widthHint = 200;
    Utils.setLayoutData(table, gridData);
    for (int i = 0; i < COLUMN_HEADERS.length; i++) {
        final TableColumn tc = new TableColumn(table, COLUMN_ALIGNS[i]);
        tc.setWidth(Utils.adjustPXForDPI(COLUMN_SIZES[i]));
        tc.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                boolean ascending = comparator.setField(table.indexOf(tc));
                try {
                    table.setSortColumn(tc);
                    table.setSortDirection(ascending ? SWT.UP : SWT.DOWN);
                } catch (NoSuchMethodError ignore) {
                // Ignore Pre 3.0
                }
                Collections.sort(pluginIFs, comparator);
                table.clearAll();
            }
        });
        Messages.setLanguageText(tc, HEADER_PREFIX + COLUMN_HEADERS[i]);
    }
    table.setHeaderVisible(true);
    Composite cButtons = new Composite(infoGroup, SWT.NONE);
    layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.numColumns = 5;
    cButtons.setLayout(layout);
    Utils.setLayoutData(cButtons, new GridData());
    final Button btnUnload = new Button(cButtons, SWT.PUSH);
    Utils.setLayoutData(btnUnload, new GridData());
    Messages.setLanguageText(btnUnload, "ConfigView.pluginlist.unloadSelected");
    btnUnload.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            final int[] items = table.getSelectionIndices();
            new AEThread2("unload") {

                @Override
                public void run() {
                    for (int i = 0; i < items.length; i++) {
                        int index = items[i];
                        if (index >= 0 && index < pluginIFs.size()) {
                            PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
                            if (pluginIF.getPluginState().isOperational()) {
                                if (pluginIF.getPluginState().isUnloadable()) {
                                    try {
                                        pluginIF.getPluginState().unload();
                                    } catch (PluginException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                    }
                                }
                            }
                            Utils.execSWTThread(new Runnable() {

                                @Override
                                public void run() {
                                    pluginIFs = rebuildPluginIFs();
                                    table.setItemCount(pluginIFs.size());
                                    Collections.sort(pluginIFs, comparator);
                                    table.clearAll();
                                }
                            });
                        }
                    }
                }
            }.start();
        }
    });
    btnUnload.setEnabled(false);
    final Button btnLoad = new Button(cButtons, SWT.PUSH);
    Utils.setLayoutData(btnUnload, new GridData());
    Messages.setLanguageText(btnLoad, "ConfigView.pluginlist.loadSelected");
    btnLoad.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            final int[] items = table.getSelectionIndices();
            new AEThread2("load") {

                @Override
                public void run() {
                    for (int i = 0; i < items.length; i++) {
                        int index = items[i];
                        if (index >= 0 && index < pluginIFs.size()) {
                            PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
                            // Already loaded.
                            if (pluginIF.getPluginState().isOperational()) {
                                continue;
                            }
                            // initialise.
                            if (pluginIF.getPluginState().isDisabled()) {
                                if (pluginIF.getPluginState().hasFailed()) {
                                    continue;
                                }
                                pluginIF.getPluginState().setDisabled(false);
                            }
                            try {
                                pluginIF.getPluginState().reload();
                            } catch (PluginException e1) {
                                // TODO Auto-generated catch block
                                Debug.printStackTrace(e1);
                            }
                            Utils.execSWTThread(new Runnable() {

                                @Override
                                public void run() {
                                    if (table == null || table.isDisposed()) {
                                        return;
                                    }
                                    pluginIFs = rebuildPluginIFs();
                                    table.setItemCount(pluginIFs.size());
                                    Collections.sort(pluginIFs, comparator);
                                    table.clearAll();
                                }
                            });
                        }
                    }
                }
            }.start();
        }
    });
    btnLoad.setEnabled(false);
    // scan
    final Button btnScan = new Button(cButtons, SWT.PUSH);
    Utils.setLayoutData(btnScan, new GridData());
    Messages.setLanguageText(btnScan, "ConfigView.pluginlist.scan");
    btnScan.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            CoreFactory.getSingleton().getPluginManager().refreshPluginList(false);
            pluginIFs = rebuildPluginIFs();
            table.setItemCount(pluginIFs.size());
            Collections.sort(pluginIFs, comparator);
            table.clearAll();
        }
    });
    // uninstall
    final Button btnUninstall = new Button(cButtons, SWT.PUSH);
    Utils.setLayoutData(btnUninstall, new GridData());
    Messages.setLanguageText(btnUninstall, "ConfigView.pluginlist.uninstallSelected");
    btnUninstall.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            btnUninstall.setEnabled(false);
            final int[] items = table.getSelectionIndices();
            new AEThread2("uninstall") {

                @Override
                public void run() {
                    try {
                        List<PluginInterface> pis = new ArrayList<>();
                        for (int i = 0; i < items.length; i++) {
                            int index = items[i];
                            if (index >= 0 && index < pluginIFs.size()) {
                                PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
                                pis.add(pluginIF);
                            }
                        }
                        if (pis.size() > 0) {
                            PluginInterface[] ps = new PluginInterface[pis.size()];
                            pis.toArray(ps);
                            try {
                                final AESemaphore wait_sem = new AESemaphore("unist:wait");
                                ps[0].getPluginManager().getPluginInstaller().uninstall(ps, new PluginInstallationListener() {

                                    @Override
                                    public void completed() {
                                        wait_sem.release();
                                    }

                                    @Override
                                    public void cancelled() {
                                        wait_sem.release();
                                    }

                                    @Override
                                    public void failed(PluginException e) {
                                        wait_sem.release();
                                    }
                                });
                                wait_sem.reserve();
                            } catch (Exception e) {
                                Debug.printStackTrace(e);
                            }
                        }
                    } finally {
                        Utils.execSWTThread(new Runnable() {

                            @Override
                            public void run() {
                                pluginIFs = rebuildPluginIFs();
                                table.setItemCount(pluginIFs.size());
                                Collections.sort(pluginIFs, comparator);
                                table.clearAll();
                                table.setSelection(new int[0]);
                            }
                        });
                    }
                }
            }.start();
        }
    });
    btnUninstall.setEnabled(false);
    table.addListener(SWT.SetData, new Listener() {

        @Override
        public void handleEvent(Event event) {
            TableItem item = (TableItem) event.item;
            int index = table.indexOf(item);
            PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
            for (int i = 0; i < COLUMN_HEADERS.length; i++) {
                if (i == FilterComparator.FIELD_NAME)
                    item.setImage(i, pluginIF.getPluginState().isOperational() ? imgGreenLed : imgRedLed);
                String sText = comparator.getFieldValue(i, pluginIF);
                if (sText == null)
                    sText = "";
                item.setText(i, sText);
            }
            item.setGrayed(pluginIF.getPluginState().isMandatory());
            boolean bEnabled = pluginIF.getPluginState().isLoadedAtStartup();
            Utils.setCheckedInSetData(item, bEnabled);
            item.setData("PluginID", pluginIF.getPluginID());
            Utils.alternateRowBackground(item);
        }
    });
    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            TableItem[] items = table.getSelection();
            if (items.length == 1) {
                int index = table.indexOf(items[0]);
                PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
                PluginConfigModel[] models = pluginIF.getUIManager().getPluginConfigModels();
                for (PluginConfigModel model : models) {
                    if (model.getPluginInterface() == pluginIF) {
                        if (model instanceof BasicPluginConfigModel) {
                            String id = ((BasicPluginConfigModel) model).getSection();
                            UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
                            if (uiFunctions != null) {
                                uiFunctions.getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_CONFIG, id);
                            }
                        }
                    }
                }
            }
        }
    });
    table.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            TableItem item = (TableItem) e.item;
            int index = table.indexOf(item);
            PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
            if (e.detail == SWT.CHECK) {
                if (item.getGrayed()) {
                    if (!item.getChecked())
                        item.setChecked(true);
                    return;
                }
                pluginIF.getPluginState().setDisabled(!item.getChecked());
                pluginIF.getPluginState().setLoadedAtStartup(item.getChecked());
            }
            btnUnload.setEnabled(pluginIF.getPluginState().isOperational() && pluginIF.getPluginState().isUnloadable());
            btnLoad.setEnabled(!pluginIF.getPluginState().isOperational() && !pluginIF.getPluginState().hasFailed());
            btnUninstall.setEnabled(!(pluginIF.getPluginState().isBuiltIn() || pluginIF.getPluginState().isMandatory()));
        }
    });
    table.setItemCount(pluginIFs.size());
    return infoGroup;
}
Also used : ParameterListener(com.biglybt.core.config.ParameterListener) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener) GridLayout(org.eclipse.swt.layout.GridLayout) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener) UIFunctions(com.biglybt.ui.UIFunctions) SelectionEvent(org.eclipse.swt.events.SelectionEvent) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel) PluginConfigModel(com.biglybt.pif.ui.model.PluginConfigModel) MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) PluginInterface(com.biglybt.pif.PluginInterface) PluginException(com.biglybt.pif.PluginException) MouseAdapter(org.eclipse.swt.events.MouseAdapter) FillLayout(org.eclipse.swt.layout.FillLayout) PluginException(com.biglybt.pif.PluginException) GridData(org.eclipse.swt.layout.GridData) MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ImageLoader(com.biglybt.ui.swt.imageloader.ImageLoader) File(java.io.File) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Aggregations

ParameterListener (com.biglybt.core.config.ParameterListener)1 PluginException (com.biglybt.pif.PluginException)1 PluginInterface (com.biglybt.pif.PluginInterface)1 PluginInstallationListener (com.biglybt.pif.installer.PluginInstallationListener)1 BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)1 PluginConfigModel (com.biglybt.pif.ui.model.PluginConfigModel)1 UIFunctions (com.biglybt.ui.UIFunctions)1 ImageLoader (com.biglybt.ui.swt.imageloader.ImageLoader)1 File (java.io.File)1 MouseAdapter (org.eclipse.swt.events.MouseAdapter)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 FillLayout (org.eclipse.swt.layout.FillLayout)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1