Search in sources :

Example 51 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class MyTorrentsSuperView method initialize.

public void initialize(final Composite parent) {
    if (form != null) {
        return;
    }
    form = new Composite(parent, SWT.NONE);
    FormLayout flayout = new FormLayout();
    flayout.marginHeight = 0;
    flayout.marginWidth = 0;
    form.setLayout(flayout);
    GridData gridData;
    gridData = new GridData(GridData.FILL_BOTH);
    form.setLayoutData(gridData);
    GridLayout layout;
    child1 = new Composite(form, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 1;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    child1.setLayout(layout);
    final Sash sash = Utils.createSash(form, SASH_WIDTH);
    child2 = new Composite(form, SWT.NULL);
    layout = new GridLayout();
    layout.numColumns = 1;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    child2.setLayout(layout);
    FormData formData;
    // More precision, times by 100
    int weight = (int) (COConfigurationManager.getFloatParameter("MyTorrents.SplitAt"));
    if (weight > 10000) {
        weight = 10000;
    } else if (weight < 100) {
        weight *= 100;
    }
    // Min/max of 5%/95%
    if (weight < 500) {
        weight = 500;
    } else if (weight > 9000) {
        weight = 9000;
    }
    double pct = (float) weight / 10000;
    sash.setData("PCT", new Double(pct));
    // FormData for table child1
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.top = new FormAttachment(0, 0);
    formData.bottom = new FormAttachment((int) (pct * 100), 0);
    child1.setLayoutData(formData);
    final FormData child1Data = formData;
    // sash
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.top = new FormAttachment(child1);
    formData.height = SASH_WIDTH;
    sash.setLayoutData(formData);
    // child2
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.bottom = new FormAttachment(100, 0);
    formData.top = new FormAttachment(sash);
    child2.setLayoutData(formData);
    // Listeners to size the folder
    sash.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            final boolean FASTDRAG = true;
            if (FASTDRAG && e.detail == SWT.DRAG)
                return;
            child1Data.height = e.y + e.height - SASH_WIDTH;
            form.layout();
            Double l = new Double((double) child1.getBounds().height / form.getBounds().height);
            sash.setData("PCT", l);
            if (e.detail != SWT.DRAG) {
                int i = (int) (l.doubleValue() * 10000);
                COConfigurationManager.setParameter("MyTorrents.SplitAt", i);
            }
        }
    });
    form.addListener(SWT.Resize, new DelayedListenerMultiCombiner() {

        @Override
        public void handleDelayedEvent(Event e) {
            if (sash.isDisposed()) {
                return;
            }
            Double l = (Double) sash.getData("PCT");
            if (l == null) {
                return;
            }
            int newHeight = (int) (form.getBounds().height * l.doubleValue());
            if (child1Data.height != newHeight || child1Data.bottom != null) {
                child1Data.bottom = null;
                child1Data.height = newHeight;
                form.layout();
            }
        }
    });
    CoreFactory.addCoreRunningListener(new CoreRunningListener() {

        @Override
        public void coreRunning(final Core core) {
            Utils.execSWTThread(new AERunnable() {

                @Override
                public void runSupport() {
                    initializeWithCore(core, parent);
                }
            });
        }
    });
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DelayedListenerMultiCombiner(com.biglybt.ui.swt.DelayedListenerMultiCombiner) SelectionEvent(org.eclipse.swt.events.SelectionEvent) CoreRunningListener(com.biglybt.core.CoreRunningListener) UISWTViewEvent(com.biglybt.ui.swt.pif.UISWTViewEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Core(com.biglybt.core.Core) TableColumnCore(com.biglybt.ui.common.table.TableColumnCore)

Example 52 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class FilesView method updateHeader.

private void updateHeader() {
    if (managers.size() == 0) {
        if (lblHeader != null) {
            Utils.execSWTThread(new AERunnable() {

                @Override
                public void runSupport() {
                    if (lblHeader == null || lblHeader.isDisposed()) {
                        return;
                    }
                    lblHeader.setText("");
                }
            });
        }
        return;
    }
    int total_rows = 0;
    int visible_rows;
    if (tree_view) {
        int[] nums = tv.getRowAndSubRowCount();
        total_rows = nums[0];
        visible_rows = nums[1];
    } else {
        for (DownloadManager manager : managers) {
            total_rows += manager.getNumFileInfos();
        }
        visible_rows = tv.getRowCount();
    }
    String s;
    s = MessageText.getString("library.unopened.header" + (total_rows > 1 ? ".p" : ""), new String[] { String.valueOf(total_rows) });
    if (total_rows != visible_rows) {
        s = MessageText.getString("v3.MainWindow.xofx", new String[] { String.valueOf(visible_rows), s });
    }
    s += getSelectionText();
    final String sHeader = s;
    if (lblHeader != null) {
        Utils.execSWTThread(new AERunnable() {

            @Override
            public void runSupport() {
                if (lblHeader == null || lblHeader.isDisposed()) {
                    return;
                }
                lblHeader.setText(sHeader);
            }
        });
    }
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) DownloadManager(com.biglybt.core.download.DownloadManager)

Example 53 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class ConfigSectionIPFilter 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;
    }
    ImageLoader imageLoader = ImageLoader.getInstance();
    Image imgOpenFolder = imageLoader.getImage("openFolderButton");
    String sCurConfigID;
    GridData gridData;
    int userMode = COConfigurationManager.getIntParameter("User Mode");
    final IpFilterManager ipFilterManager = CoreFactory.getSingleton().getIpFilterManager();
    filter = ipFilterManager.getIPFilter();
    Composite gFilter = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    gFilter.setLayout(layout);
    gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
    Utils.setLayoutData(gFilter, gridData);
    // start controls
    // row: enable filter + allow/deny
    gridData = new GridData();
    BooleanParameter enabled = new BooleanParameter(gFilter, "Ip Filter Enabled");
    enabled.setLayoutData(gridData);
    Messages.setLanguageText(enabled.getControl(), "ConfigView.section.ipfilter.enable");
    gridData = new GridData();
    BooleanParameter deny = new BooleanParameter(gFilter, "Ip Filter Allow");
    deny.setLayoutData(gridData);
    Messages.setLanguageText(deny.getControl(), "ConfigView.section.ipfilter.allow");
    deny.addChangeListener(new ParameterChangeAdapter() {

        @Override
        public void parameterChanged(Parameter p, boolean caused_internally) {
            setPercentageBlocked();
        }
    });
    // row persist banning
    gridData = new GridData();
    BooleanParameter persist_bad_data_banning = new BooleanParameter(gFilter, "Ip Filter Banning Persistent");
    persist_bad_data_banning.setLayoutData(gridData);
    Messages.setLanguageText(persist_bad_data_banning.getControl(), "ConfigView.section.ipfilter.persistblocking");
    BooleanParameter disableForUpdates = new BooleanParameter(gFilter, "Ip Filter Disable For Updates");
    Messages.setLanguageText(disableForUpdates.getControl(), "ConfigView.section.ipfilter.disable.for.updates");
    Group gBlockBanning = new Group(gFilter, SWT.NULL);
    Messages.setLanguageText(gBlockBanning, "ConfigView.section.ipfilter.peerblocking.group");
    layout = new GridLayout();
    layout.numColumns = 2;
    gBlockBanning.setLayout(layout);
    // row block bad + group ban
    BooleanParameter enable_bad_data_banning = new BooleanParameter(gBlockBanning, "Ip Filter Enable Banning", "ConfigView.section.ipfilter.enablebanning");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    enable_bad_data_banning.setLayoutData(gridData);
    Label discard_label = new Label(gBlockBanning, SWT.NULL);
    Messages.setLanguageText(discard_label, "ConfigView.section.ipfilter.discardbanning");
    FloatParameter discard_ratio = new FloatParameter(gBlockBanning, "Ip Filter Ban Discard Ratio");
    gridData = new GridData();
    discard_ratio.setLayoutData(gridData);
    Composite cIndent = new Composite(gBlockBanning, SWT.NONE);
    gridData = new GridData(GridData.FILL_BOTH);
    gridData.horizontalSpan = 2;
    gridData.horizontalIndent = 15;
    Utils.setLayoutData(cIndent, gridData);
    layout = new GridLayout(3, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    cIndent.setLayout(layout);
    Image img = imageLoader.getImage("subitem");
    Label label = new Label(cIndent, SWT.NULL);
    gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
    Utils.setLayoutData(label, gridData);
    label.setImage(img);
    Label discard_min_label = new Label(cIndent, SWT.NULL);
    Messages.setLanguageText(discard_min_label, "ConfigView.section.ipfilter.discardminkb", new String[] { DisplayFormatters.getUnit(DisplayFormatters.UNIT_KB) });
    IntParameter discard_min = new IntParameter(cIndent, "Ip Filter Ban Discard Min KB");
    gridData = new GridData();
    discard_min.setLayoutData(gridData);
    // block banning
    Label block_label = new Label(gBlockBanning, SWT.NULL);
    Messages.setLanguageText(block_label, "ConfigView.section.ipfilter.blockbanning");
    IntParameter block_banning = new IntParameter(gBlockBanning, "Ip Filter Ban Block Limit", 0, 256);
    gridData = new GridData();
    block_banning.setLayoutData(gridData);
    // triggers
    enable_bad_data_banning.setAdditionalActionPerformer(new ChangeSelectionActionPerformer(new Control[] { block_banning.getControl(), block_label, discard_ratio.getControl(), discard_label, discard_min.getControl(), discard_min_label }));
    Group gAutoLoad = new Group(gFilter, SWT.NONE);
    Messages.setLanguageText(gAutoLoad, "ConfigView.section.ipfilter.autoload.group");
    FormLayout flayout = new FormLayout();
    flayout.marginHeight = flayout.marginWidth = 5;
    gAutoLoad.setLayout(flayout);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.widthHint = 500;
    Utils.setLayoutData(gAutoLoad, gridData);
    FormData fd;
    // Load from file
    sCurConfigID = "Ip Filter Autoload File";
    // allConfigIDs.add(sCurConfigID);
    Label lblDefaultDir = new Label(gAutoLoad, SWT.NONE);
    Messages.setLanguageText(lblDefaultDir, "ConfigView.section.ipfilter.autoload.file");
    fd = new FormData();
    Utils.setLayoutData(lblDefaultDir, fd);
    final StringParameter pathParameter = new StringParameter(gAutoLoad, sCurConfigID);
    Button browse = new Button(gAutoLoad, SWT.PUSH);
    browse.setImage(imgOpenFolder);
    imgOpenFolder.setBackground(browse.getBackground());
    browse.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            FileDialog dialog = new FileDialog(parent.getShell(), SWT.APPLICATION_MODAL);
            dialog.setFilterPath(pathParameter.getValue());
            dialog.setText(MessageText.getString("ConfigView.section.ipfilter.autoload.file"));
            dialog.setFilterExtensions(new String[] { "*.dat" + File.pathSeparator + "*.p2p" + File.pathSeparator + "*.p2b" + File.pathSeparator + "*.txt", "*.*" });
            dialog.setFileName("ipfilter.dat");
            String file = dialog.open();
            if (file != null) {
                pathParameter.setValue(file);
            }
        }
    });
    final Button btnLoadNow = new Button(gAutoLoad, SWT.PUSH);
    Messages.setLanguageText(btnLoadNow, "ConfigView.section.ipfilter.autoload.loadnow");
    btnLoadNow.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            btnLoadNow.setEnabled(false);
            COConfigurationManager.setParameter(IpFilterAutoLoaderImpl.CFG_AUTOLOAD_LAST, 0);
            Utils.getOffOfSWTThread(new AERunnable() {

                @Override
                public void runSupport() {
                    try {
                        filter.reloadSync();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Utils.execSWTThread(new AERunnable() {

                        @Override
                        public void runSupport() {
                            if (!btnLoadNow.isDisposed()) {
                                btnLoadNow.setEnabled(true);
                            }
                        }
                    });
                }
            });
        }
    });
    fd = new FormData();
    fd.right = new FormAttachment(100, 0);
    Utils.setLayoutData(btnLoadNow, fd);
    fd = new FormData();
    fd.right = new FormAttachment(btnLoadNow, -5);
    Utils.setLayoutData(browse, fd);
    fd = new FormData();
    fd.left = new FormAttachment(lblDefaultDir, 5);
    fd.right = new FormAttachment(browse, -5);
    pathParameter.setLayoutData(fd);
    // reload period
    Label l_reload_period = new Label(gAutoLoad, SWT.NULL);
    Messages.setLanguageText(l_reload_period, "ConfigView.section.ipfilter.autoload.period");
    int initial_reload_period = COConfigurationManager.getIntParameter(IpFilterAutoLoaderImpl.CFG_AUTOLOAD_DAYS);
    IntParameter reload_period = new IntParameter(gAutoLoad, IpFilterAutoLoaderImpl.CFG_AUTOLOAD_DAYS, 1, 31);
    reload_period.addChangeListener(new ParameterChangeAdapter() {

        private boolean added_listener = false;

        @Override
        public void parameterChanged(Parameter p, boolean caused_internally) {
            if (!added_listener) {
                added_listener = true;
                COConfigurationManager.addListener(new COConfigurationListener() {

                    @Override
                    public void configurationSaved() {
                        COConfigurationManager.removeListener(this);
                        added_listener = false;
                        int period = reload_period.getValue();
                        if (period != initial_reload_period) {
                            Utils.getOffOfSWTThread(new AERunnable() {

                                @Override
                                public void runSupport() {
                                    try {
                                        ipFilterManager.getIPFilter().reload();
                                    } catch (Throwable e) {
                                        Debug.out(e);
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }
    });
    fd = new FormData();
    fd.top = new FormAttachment(btnLoadNow, 3);
    fd.left = new FormAttachment(0, 20);
    Utils.setLayoutData(l_reload_period, fd);
    fd = new FormData();
    fd.top = new FormAttachment(btnLoadNow, 3);
    fd.left = new FormAttachment(l_reload_period, 5);
    Utils.setLayoutData(reload_period.getControl(), fd);
    // reload info
    Label lblAutoLoadInfo = new Label(gAutoLoad, SWT.WRAP);
    Messages.setLanguageText(lblAutoLoadInfo, "ConfigView.section.ipfilter.autoload.info");
    fd = new FormData();
    fd.top = new FormAttachment(reload_period.getControl(), 3);
    fd.left = new FormAttachment(0, 0);
    fd.right = new FormAttachment(100, 0);
    Utils.setLayoutData(lblAutoLoadInfo, fd);
    BooleanParameter clear_on_reload = new BooleanParameter(gAutoLoad, "Ip Filter Clear On Reload");
    fd = new FormData();
    fd.top = new FormAttachment(lblAutoLoadInfo, 3);
    fd.left = new FormAttachment(0, 0);
    fd.right = new FormAttachment(100, 0);
    clear_on_reload.setLayoutData(fd);
    Messages.setLanguageText(clear_on_reload.getControl(), "ConfigView.section.ipfilter.clear.on.reload");
    if (userMode > 0) {
        gridData = new GridData();
        BooleanParameter enableDesc = new BooleanParameter(gFilter, "Ip Filter Enable Description Cache");
        enableDesc.setLayoutData(gridData);
        Messages.setLanguageText(enableDesc.getControl(), "ConfigView.section.ipfilter.enable.descriptionCache");
    }
    // table
    table = new Table(gFilter, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION | SWT.VIRTUAL);
    String[] headers = { "label.description", "ConfigView.section.ipfilter.start", "ConfigView.section.ipfilter.end" };
    int[] sizes = { 110, 110, 110 };
    int[] aligns = { SWT.LEFT, SWT.CENTER, SWT.CENTER };
    for (int i = 0; i < headers.length; i++) {
        TableColumn tc = new TableColumn(table, aligns[i]);
        tc.setText(headers[i]);
        tc.setWidth(Utils.adjustPXForDPI(sizes[i]));
        // $NON-NLS-1$
        Messages.setLanguageText(tc, headers[i]);
    }
    TableColumn[] columns = table.getColumns();
    columns[0].setData(new Integer(FilterComparator.FIELD_NAME));
    columns[1].setData(new Integer(FilterComparator.FIELD_START_IP));
    columns[2].setData(new Integer(FilterComparator.FIELD_END_IP));
    Listener listener = new Listener() {

        @Override
        public void handleEvent(Event e) {
            TableColumn tc = (TableColumn) e.widget;
            int field = ((Integer) tc.getData()).intValue();
            comparator.setField(field);
            if (field == FilterComparator.FIELD_NAME && !bIsCachingDescriptions) {
                ipFilterManager.cacheAllDescriptions();
                bIsCachingDescriptions = true;
            }
            ipRanges = getSortedRanges(filter.getRanges());
            table.setItemCount(ipRanges.length);
            table.clearAll();
            // bug 69398 on Windows
            table.redraw();
        }
    };
    columns[0].addListener(SWT.Selection, listener);
    columns[1].addListener(SWT.Selection, listener);
    columns[2].addListener(SWT.Selection, listener);
    table.setHeaderVisible(true);
    gridData = new GridData(GridData.FILL_BOTH);
    gridData.heightHint = table.getHeaderHeight() * 3;
    gridData.widthHint = 200;
    Utils.setLayoutData(table, gridData);
    Composite cArea = new Composite(gFilter, SWT.NULL);
    layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.numColumns = 4;
    cArea.setLayout(layout);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    Utils.setLayoutData(cArea, gridData);
    Button add = new Button(cArea, SWT.PUSH);
    gridData = new GridData(GridData.CENTER);
    gridData.widthHint = 100;
    Utils.setLayoutData(add, gridData);
    Messages.setLanguageText(add, "Button.add");
    add.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            addRange();
        }
    });
    Button remove = new Button(cArea, SWT.PUSH);
    gridData = new GridData(GridData.CENTER);
    gridData.widthHint = 100;
    Utils.setLayoutData(remove, gridData);
    Messages.setLanguageText(remove, "ConfigView.section.ipfilter.remove");
    remove.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            TableItem[] selection = table.getSelection();
            if (selection.length == 0)
                return;
            removeRange((IpRange) selection[0].getData());
            ipRanges = getSortedRanges(filter.getRanges());
            table.setItemCount(ipRanges.length);
            table.clearAll();
            table.redraw();
        }
    });
    Button edit = new Button(cArea, SWT.PUSH);
    gridData = new GridData(GridData.CENTER);
    gridData.widthHint = 100;
    Utils.setLayoutData(edit, gridData);
    Messages.setLanguageText(edit, "Button.edit");
    edit.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            TableItem[] selection = table.getSelection();
            if (selection.length == 0)
                return;
            editRange((IpRange) selection[0].getData());
        }
    });
    percentage_blocked = new Label(cArea, SWT.WRAP | SWT.RIGHT);
    gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.FILL_HORIZONTAL);
    Utils.setLayoutData(percentage_blocked, gridData);
    Utils.setLayoutData(percentage_blocked, Utils.getWrappableLabelGridData(1, GridData.HORIZONTAL_ALIGN_FILL));
    setPercentageBlocked();
    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDoubleClick(MouseEvent arg0) {
            TableItem[] selection = table.getSelection();
            if (selection.length == 0)
                return;
            editRange((IpRange) selection[0].getData());
        }
    });
    Control[] controls = new Control[3];
    controls[0] = add;
    controls[1] = remove;
    controls[2] = edit;
    IAdditionalActionPerformer enabler = new ChangeSelectionActionPerformer(controls);
    enabled.setAdditionalActionPerformer(enabler);
    ipRanges = getSortedRanges(filter.getRanges());
    table.addListener(SWT.SetData, new Listener() {

        @Override
        public void handleEvent(Event event) {
            TableItem item = (TableItem) event.item;
            int index = table.indexOf(item);
            if (index < 0 || index >= ipRanges.length) {
                return;
            }
            IpRange range = ipRanges[index];
            item.setText(0, range.getDescription());
            item.setText(1, range.getStartIp());
            item.setText(2, range.getEndIp());
            item.setData(range);
        }
    });
    table.setItemCount(ipRanges.length);
    table.clearAll();
    // bug 69398 on Windows
    table.redraw();
    table.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            resizeTable();
        }
    });
    gFilter.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            resizeTable();
        }
    });
    filterListener = new IPFilterListener() {

        @Override
        public void IPFilterEnabledChanged(boolean is_enabled) {
        }

        @Override
        public boolean canIPBeBanned(String ip) {
            return true;
        }

        @Override
        public void IPBanned(BannedIp ip) {
        }

        @Override
        public void IPBlockedListChanged(final IpFilter filter) {
            Utils.execSWTThread(new AERunnable() {

                @Override
                public void runSupport() {
                    if (table.isDisposed()) {
                        filter.removeListener(filterListener);
                        return;
                    }
                    ipRanges = getSortedRanges(filter.getRanges());
                    table.setItemCount(ipRanges.length);
                    table.clearAll();
                    table.redraw();
                }
            });
        }

        @Override
        public boolean canIPBeBlocked(String ip, byte[] torrent_hash) {
            return true;
        }
    };
    filter.addListener(filterListener);
    return gFilter;
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) COConfigurationListener(com.biglybt.core.config.COConfigurationListener) COConfigurationListener(com.biglybt.core.config.COConfigurationListener) Image(org.eclipse.swt.graphics.Image) MouseEvent(org.eclipse.swt.events.MouseEvent) MouseAdapter(org.eclipse.swt.events.MouseAdapter) MouseEvent(org.eclipse.swt.events.MouseEvent) ImageLoader(com.biglybt.ui.swt.imageloader.ImageLoader)

Example 54 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class ConfigSectionBackupRestore method runBackup.

private void runBackup(BackupManager backup_manager, String path, final Runnable stats_updater) {
    final TextViewerWindow viewer = new TextViewerWindow(MessageText.getString("br.backup.progress"), null, "", true, true);
    viewer.setEditable(false);
    viewer.setOKEnabled(false);
    BackupManager.BackupListener listener = new BackupManager.BackupListener() {

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

        @Override
        public void reportComplete() {
            append("Backup Complete!", true);
        }

        @Override
        public void reportError(Throwable error) {
            append("Backup 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);
                        stats_updater.run();
                    }
                }
            });
            return (true);
        }
    };
    if (path == null) {
        backup_manager.runAutoBackup(listener);
    } else {
        backup_manager.backup(new File(path), listener);
    }
    viewer.goModal();
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) TextViewerWindow(com.biglybt.ui.swt.TextViewerWindow) BackupManager(com.biglybt.core.backup.BackupManager) File(java.io.File)

Example 55 with AERunnable

use of com.biglybt.core.util.AERunnable 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)92 GridLayout (org.eclipse.swt.layout.GridLayout)12 DownloadManager (com.biglybt.core.download.DownloadManager)11 Image (org.eclipse.swt.graphics.Image)10 ArrayList (java.util.ArrayList)9 GridData (org.eclipse.swt.layout.GridData)9 CoreRunningListener (com.biglybt.core.CoreRunningListener)7 File (java.io.File)7 PEPeer (com.biglybt.core.peer.PEPeer)6 Shell (org.eclipse.swt.widgets.Shell)6 Core (com.biglybt.core.Core)5 ImageLoader (com.biglybt.ui.swt.imageloader.ImageLoader)5 Point (org.eclipse.swt.graphics.Point)5 PEPeerManager (com.biglybt.core.peer.PEPeerManager)4 Subscription (com.biglybt.core.subs.Subscription)3 TableColumnCore (com.biglybt.ui.common.table.TableColumnCore)3 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)3 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)3 MouseAdapter (org.eclipse.swt.events.MouseAdapter)3 MouseEvent (org.eclipse.swt.events.MouseEvent)3