Search in sources :

Example 1 with TimerEventPeriodic

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

the class ViewQuickConfig method addTemporaryRates.

private void addTemporaryRates(Composite composite) {
    Group temp_rates = new Group(composite, SWT.NULL);
    Messages.setLanguageText(temp_rates, "label.temporary.rates");
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 4;
    Utils.setLayoutData(temp_rates, gridData);
    GridLayout layout = new GridLayout(10, false);
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    temp_rates.setLayout(layout);
    // label = new Label(temp_rates, SWT.NULL);
    // Messages.setLanguageText( label, "label.temporary.rates" );
    Label label = new Label(temp_rates, SWT.NULL);
    gridData = new GridData();
    gridData.horizontalIndent = 4;
    Utils.setLayoutData(label, gridData);
    Messages.setLanguageText(label, "label.upload.kbps", new String[] { DisplayFormatters.getRateUnit(DisplayFormatters.UNIT_KB) });
    final IntParameter tempULRate = new IntParameter(temp_rates, "global.download.rate.temp.kbps", 0, Integer.MAX_VALUE);
    label = new Label(temp_rates, SWT.NULL);
    Messages.setLanguageText(label, "label.download.kbps", new String[] { DisplayFormatters.getRateUnit(DisplayFormatters.UNIT_KB) });
    final IntParameter tempDLRate = new IntParameter(temp_rates, "global.upload.rate.temp.kbps", 0, Integer.MAX_VALUE);
    label = new Label(temp_rates, SWT.NULL);
    Messages.setLanguageText(label, "label.duration.mins");
    final IntParameter tempMins = new IntParameter(temp_rates, "global.rate.temp.min", 0, Integer.MAX_VALUE);
    final Button activate = new Button(temp_rates, SWT.TOGGLE);
    Messages.setLanguageText(activate, "label.activate");
    final BufferedLabel remLabel = new BufferedLabel(temp_rates, SWT.DOUBLE_BUFFERED);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.widthHint = 150;
    Utils.setLayoutData(remLabel, gridData);
    activate.addSelectionListener(new SelectionAdapter() {

        private CoreLifecycleAdapter listener;

        private TimerEventPeriodic event;

        private boolean auto_up_enabled;

        private boolean auto_up_seeding_enabled;

        private boolean seeding_limits_enabled;

        private int up_limit;

        private int down_limit;

        private long end_time;

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (activate.getSelection()) {
                listener = new CoreLifecycleAdapter() {

                    @Override
                    public void stopping(Core core) {
                        deactivate(true);
                    }
                };
                CoreFactory.getSingleton().addLifecycleListener(listener);
                Messages.setLanguageText(activate, "FileView.BlockView.Active");
                tempDLRate.setEnabled(false);
                tempULRate.setEnabled(false);
                tempMins.setEnabled(false);
                auto_up_enabled = COConfigurationManager.getBooleanParameter(TransferSpeedValidator.AUTO_UPLOAD_ENABLED_CONFIGKEY);
                auto_up_seeding_enabled = COConfigurationManager.getBooleanParameter(TransferSpeedValidator.AUTO_UPLOAD_SEEDING_ENABLED_CONFIGKEY);
                seeding_limits_enabled = COConfigurationManager.getBooleanParameter(TransferSpeedValidator.UPLOAD_SEEDING_ENABLED_CONFIGKEY);
                up_limit = COConfigurationManager.getIntParameter(TransferSpeedValidator.UPLOAD_CONFIGKEY);
                down_limit = COConfigurationManager.getIntParameter(TransferSpeedValidator.DOWNLOAD_CONFIGKEY);
                COConfigurationManager.setParameter(TransferSpeedValidator.AUTO_UPLOAD_ENABLED_CONFIGKEY, false);
                COConfigurationManager.setParameter(TransferSpeedValidator.AUTO_UPLOAD_SEEDING_ENABLED_CONFIGKEY, false);
                COConfigurationManager.setParameter(TransferSpeedValidator.UPLOAD_SEEDING_ENABLED_CONFIGKEY, false);
                COConfigurationManager.setParameter(TransferSpeedValidator.UPLOAD_CONFIGKEY, tempULRate.getValue());
                COConfigurationManager.setParameter(TransferSpeedValidator.DOWNLOAD_CONFIGKEY, tempDLRate.getValue());
                end_time = SystemTime.getCurrentTime() + tempMins.getValue() * 60 * 1000;
                event = SimpleTimer.addPeriodicEvent("TempRates", 1000, new TimerEventPerformer() {

                    @Override
                    public void perform(TimerEvent e) {
                        Utils.execSWTThread(new Runnable() {

                            @Override
                            public void run() {
                                if (event == null) {
                                    return;
                                }
                                long now = SystemTime.getCurrentTime();
                                long rem = end_time - now;
                                if (rem < 1000 || composite.isDisposed()) {
                                    deactivate(false);
                                } else {
                                    remLabel.setText(MessageText.getString("TableColumn.header.remaining") + ": " + DisplayFormatters.formatTime(rem));
                                }
                            }
                        });
                    }
                });
            } else {
                deactivate(false);
            }
        }

        private void deactivate(boolean closing) {
            COConfigurationManager.setParameter(TransferSpeedValidator.AUTO_UPLOAD_ENABLED_CONFIGKEY, auto_up_enabled);
            COConfigurationManager.setParameter(TransferSpeedValidator.AUTO_UPLOAD_SEEDING_ENABLED_CONFIGKEY, auto_up_seeding_enabled);
            COConfigurationManager.setParameter(TransferSpeedValidator.UPLOAD_SEEDING_ENABLED_CONFIGKEY, seeding_limits_enabled);
            COConfigurationManager.setParameter(TransferSpeedValidator.UPLOAD_CONFIGKEY, up_limit);
            COConfigurationManager.setParameter(TransferSpeedValidator.DOWNLOAD_CONFIGKEY, down_limit);
            if (!closing) {
                if (listener != null) {
                    CoreFactory.getSingleton().removeLifecycleListener(listener);
                    listener = null;
                }
                if (!composite.isDisposed()) {
                    Messages.setLanguageText(activate, "label.activate");
                    activate.setSelection(false);
                    tempDLRate.setEnabled(true);
                    tempULRate.setEnabled(true);
                    tempMins.setEnabled(true);
                    remLabel.setText("");
                }
            }
            if (event != null) {
                event.cancel();
                event = null;
            }
        }
    });
    activate.setEnabled(tempMins.getValue() > 0);
    tempMins.addChangeListener(new ParameterChangeAdapter() {

        @Override
        public void parameterChanged(Parameter p, boolean caused_internally) {
            activate.setEnabled(tempMins.getValue() > 0);
        }
    });
}
Also used : TimerEventPeriodic(com.biglybt.core.util.TimerEventPeriodic) BufferedLabel(com.biglybt.ui.swt.components.BufferedLabel) CoreLifecycleAdapter(com.biglybt.core.CoreLifecycleAdapter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) BufferedLabel(com.biglybt.ui.swt.components.BufferedLabel) ParameterChangeAdapter(com.biglybt.ui.swt.config.ParameterChangeAdapter) Point(org.eclipse.swt.graphics.Point) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TimerEventPerformer(com.biglybt.core.util.TimerEventPerformer) TimerEvent(com.biglybt.core.util.TimerEvent) IntParameter(com.biglybt.ui.swt.config.IntParameter) Parameter(com.biglybt.ui.swt.config.Parameter) IntParameter(com.biglybt.ui.swt.config.IntParameter) Core(com.biglybt.core.Core)

Example 2 with TimerEventPeriodic

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

the class ViewQuickConfig method addTemporaryData.

private void addTemporaryData(Composite composite) {
    Group temp_rates = new Group(composite, SWT.NULL);
    Messages.setLanguageText(temp_rates, "label.temporary.data");
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 4;
    Utils.setLayoutData(temp_rates, gridData);
    GridLayout layout = new GridLayout(10, false);
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    temp_rates.setLayout(layout);
    // label = new Label(temp_rates, SWT.NULL);
    // Messages.setLanguageText( label, "label.temporary.rates" );
    Label label = new Label(temp_rates, SWT.NULL);
    gridData = new GridData();
    gridData.horizontalIndent = 4;
    Utils.setLayoutData(label, gridData);
    Messages.setLanguageText(label, "label.upload.mb", new String[] { DisplayFormatters.getUnit(DisplayFormatters.UNIT_MB) });
    final IntParameter tempULLimit = new IntParameter(temp_rates, "global.upload.limit.temp.mb", 0, Integer.MAX_VALUE);
    label = new Label(temp_rates, SWT.NULL);
    Messages.setLanguageText(label, "label.download.mb", new String[] { DisplayFormatters.getUnit(DisplayFormatters.UNIT_MB) });
    final IntParameter tempDLLimit = new IntParameter(temp_rates, "global.download.limit.temp.mb", 0, Integer.MAX_VALUE);
    final Button activate = new Button(temp_rates, SWT.TOGGLE);
    Messages.setLanguageText(activate, "label.activate");
    final BufferedLabel remLabel = new BufferedLabel(temp_rates, SWT.DOUBLE_BUFFERED);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.widthHint = 200;
    Utils.setLayoutData(remLabel, gridData);
    activate.addSelectionListener(new SelectionAdapter() {

        private CoreLifecycleAdapter listener;

        private TimerEventPeriodic event;

        private long end_upload;

        private long end_download;

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (activate.getSelection()) {
                listener = new CoreLifecycleAdapter() {

                    @Override
                    public void stopping(Core core) {
                        deactivate(true);
                    }
                };
                Core core = CoreFactory.getSingleton();
                core.addLifecycleListener(listener);
                Messages.setLanguageText(activate, "FileView.BlockView.Active");
                final GlobalManagerStats stats = core.getGlobalManager().getStats();
                tempULLimit.setEnabled(false);
                tempDLLimit.setEnabled(false);
                long u_limit = tempULLimit.getValue();
                if (u_limit > 0) {
                    end_upload = stats.getTotalDataProtocolBytesSent() + u_limit * DisplayFormatters.getMinB();
                } else {
                    end_upload = 0;
                }
                long d_limit = tempDLLimit.getValue();
                if (d_limit > 0) {
                    end_download = stats.getTotalDataProtocolBytesReceived() + d_limit * DisplayFormatters.getMinB();
                } else {
                    end_download = 0;
                }
                event = SimpleTimer.addPeriodicEvent("TempData", 5000, new TimerEventPerformer() {

                    @Override
                    public void perform(TimerEvent e) {
                        Utils.execSWTThread(new Runnable() {

                            @Override
                            public void run() {
                                if (event == null) {
                                    return;
                                }
                                long rem_up = 0;
                                long rem_down = 0;
                                if (end_upload > 0) {
                                    rem_up = end_upload - stats.getTotalDataProtocolBytesSent();
                                }
                                if (end_download > 0) {
                                    rem_down = end_download - stats.getTotalDataProtocolBytesReceived();
                                }
                                if (end_upload > 0 && rem_up <= 0) {
                                    java.util.List<DownloadManager> dms = core.getGlobalManager().getDownloadManagers();
                                    for (DownloadManager dm : dms) {
                                        if (!dm.isForceStart()) {
                                            int state = dm.getState();
                                            if (state != DownloadManager.STATE_STOPPED && state != DownloadManager.STATE_ERROR && !dm.isPaused()) {
                                                ManagerUtils.stop(dm, null);
                                                dm.setStopReason(MessageText.getString("label.temporary.data"));
                                            }
                                        }
                                    }
                                }
                                if (end_download > 0 && rem_down <= 0) {
                                    java.util.List<DownloadManager> dms = core.getGlobalManager().getDownloadManagers();
                                    for (DownloadManager dm : dms) {
                                        if (!dm.isForceStart()) {
                                            int state = dm.getState();
                                            if (state != DownloadManager.STATE_STOPPED && state != DownloadManager.STATE_ERROR && !dm.isPaused()) {
                                                if (!dm.isDownloadComplete(false)) {
                                                    ManagerUtils.stop(dm, null);
                                                    dm.setStopReason(MessageText.getString("label.temporary.data"));
                                                }
                                            }
                                        }
                                    }
                                }
                                if ((rem_up <= 0 && rem_down <= 0) || composite.isDisposed()) {
                                    deactivate(false);
                                } else {
                                    remLabel.setText(MessageText.getString("TableColumn.header.remaining") + ": " + DisplayFormatters.formatByteCountToKiBEtc(rem_up < 0 ? 0 : rem_up) + "/" + DisplayFormatters.formatByteCountToKiBEtc(rem_down < 0 ? 0 : rem_down));
                                    temp_rates.layout(new Control[] { remLabel.getControl() });
                                }
                            }
                        });
                    }
                });
            } else {
                deactivate(false);
            }
        }

        private void deactivate(boolean closing) {
            if (!closing) {
                if (listener != null) {
                    CoreFactory.getSingleton().removeLifecycleListener(listener);
                    listener = null;
                }
                if (!composite.isDisposed()) {
                    Messages.setLanguageText(activate, "label.activate");
                    activate.setSelection(false);
                    tempULLimit.setEnabled(true);
                    tempDLLimit.setEnabled(true);
                    remLabel.setText("");
                    temp_rates.layout(true);
                }
            }
            if (event != null) {
                event.cancel();
                event = null;
            }
        }
    });
    activate.setEnabled(tempULLimit.getValue() > 0 || tempDLLimit.getValue() > 0);
    ParameterChangeAdapter adapter = new ParameterChangeAdapter() {

        @Override
        public void parameterChanged(Parameter p, boolean caused_internally) {
            activate.setEnabled(tempULLimit.getValue() > 0 || tempDLLimit.getValue() > 0);
        }
    };
    tempULLimit.addChangeListener(adapter);
    tempDLLimit.addChangeListener(adapter);
}
Also used : TimerEventPeriodic(com.biglybt.core.util.TimerEventPeriodic) BufferedLabel(com.biglybt.ui.swt.components.BufferedLabel) CoreLifecycleAdapter(com.biglybt.core.CoreLifecycleAdapter) BufferedLabel(com.biglybt.ui.swt.components.BufferedLabel) DownloadManager(com.biglybt.core.download.DownloadManager) GridLayout(org.eclipse.swt.layout.GridLayout) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TimerEventPerformer(com.biglybt.core.util.TimerEventPerformer) TimerEvent(com.biglybt.core.util.TimerEvent) Core(com.biglybt.core.Core) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ParameterChangeAdapter(com.biglybt.ui.swt.config.ParameterChangeAdapter) GlobalManagerStats(com.biglybt.core.global.GlobalManagerStats) Point(org.eclipse.swt.graphics.Point) GridData(org.eclipse.swt.layout.GridData) IntParameter(com.biglybt.ui.swt.config.IntParameter) Parameter(com.biglybt.ui.swt.config.Parameter) IntParameter(com.biglybt.ui.swt.config.IntParameter)

Example 3 with TimerEventPeriodic

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

the class MainMDISetup method setupSideBar.

public static void setupSideBar(final MultipleDocumentInterfaceSWT mdi, final MdiListener l) {
    if (Utils.isAZ2UI()) {
        setupSidebarClassic(mdi);
    } else {
        setupSidebarVuzeUI(mdi);
    }
    SBC_TorrentDetailsView.TorrentDetailMdiEntry.register(mdi);
    PluginInterface pi = PluginInitializer.getDefaultInterface();
    pi.getUIManager().addUIListener(new UIManagerListener2() {

        @Override
        public void UIDetached(UIInstance instance) {
        }

        @Override
        public void UIAttached(UIInstance instance) {
        }

        @Override
        public void UIAttachedComplete(UIInstance instance) {
            PluginInitializer.getDefaultInterface().getUIManager().removeUIListener(this);
            MdiEntry currentEntry = mdi.getCurrentEntry();
            if (currentEntry != null) {
                // User or another plugin selected an entry
                return;
            }
            final String CFG_STARTTAB = "v3.StartTab";
            final String CFG_STARTTAB_DS = "v3.StartTab.ds";
            String startTab;
            String datasource = null;
            boolean showWelcome = false;
            if (showWelcome) {
                startTab = SideBar.SIDEBAR_SECTION_WELCOME;
            } else {
                if (!COConfigurationManager.hasParameter(CFG_STARTTAB, true)) {
                    COConfigurationManager.setParameter(CFG_STARTTAB, SideBar.SIDEBAR_SECTION_LIBRARY);
                }
                startTab = COConfigurationManager.getStringParameter(CFG_STARTTAB);
                datasource = COConfigurationManager.getStringParameter(CFG_STARTTAB_DS, null);
            }
            if (!mdi.loadEntryByID(startTab, true, false, datasource)) {
                mdi.showEntryByID(SideBar.SIDEBAR_SECTION_LIBRARY);
            }
            if (l != null) {
                mdi.addListener(l);
            }
        }
    });
    configBetaEnabledListener = new ParameterListener() {

        @Override
        public void parameterChanged(String parameterName) {
            boolean enabled = COConfigurationManager.getBooleanParameter("Beta Programme Enabled");
            if (enabled) {
                boolean closed = COConfigurationManager.getBooleanParameter("Beta Programme Sidebar Closed");
                if (!closed) {
                    mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_BETAPROGRAM, false);
                }
            }
        }
    };
    COConfigurationManager.addAndFireParameterListener("Beta Programme Enabled", configBetaEnabledListener);
    mdi.registerEntry(StatsView.VIEW_ID, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_PLUGINS, new StatsView(), id, true, null, null);
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_ALLPEERS, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, new PeersSuperView(), id, true, null, null);
            entry.setImageLeftID("image.sidebar.allpeers");
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_LOGGER, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_PLUGINS, new LoggerView(), id, true, null, null);
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_TAGS, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromSkinRef(MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, id, "tagsview", "{tags.view.heading}", null, null, true, null);
            entry.setImageLeftID("image.sidebar.tag-overview");
            entry.setDefaultExpanded(true);
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_TAG_DISCOVERY, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromSkinRef(MultipleDocumentInterface.SIDEBAR_SECTION_TAGS, id, "tagdiscoveryview", "{mdi.entry.tagdiscovery}", null, null, true, null);
            entry.setImageLeftID("image.sidebar.tag-overview");
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_CHAT, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            final ViewTitleInfo title_info = new ViewTitleInfo() {

                @Override
                public Object getTitleInfoProperty(int propertyID) {
                    BuddyPluginBeta bp = BuddyPluginUtils.getBetaPlugin();
                    if (bp == null) {
                        return (null);
                    }
                    if (propertyID == TITLE_INDICATOR_TEXT) {
                        int num = 0;
                        for (ChatInstance chat : bp.getChats()) {
                            if (chat.getMessageOutstanding()) {
                                num++;
                            }
                        }
                        if (num > 0) {
                            return (String.valueOf(num));
                        } else {
                            return (null);
                        }
                    } else if (propertyID == TITLE_INDICATOR_COLOR) {
                        for (ChatInstance chat : bp.getChats()) {
                            if (chat.getMessageOutstanding()) {
                                if (chat.hasUnseenMessageWithNick()) {
                                    return (SBC_ChatOverview.COLOR_MESSAGE_WITH_NICK);
                                }
                            }
                        }
                    }
                    return null;
                }
            };
            MdiEntry mdi_entry = mdi.createEntryFromSkinRef(MultipleDocumentInterface.SIDEBAR_HEADER_DISCOVERY, MultipleDocumentInterface.SIDEBAR_SECTION_CHAT, "chatsview", "{mdi.entry.chatsoverview}", title_info, null, true, null);
            mdi_entry.setImageLeftID("image.sidebar.chat-overview");
            final TimerEventPeriodic timer = SimpleTimer.addPeriodicEvent("sb:chatup", 5 * 1000, new TimerEventPerformer() {

                private String last_text;

                private int[] last_colour;

                @Override
                public void perform(TimerEvent event) {
                    String text = (String) title_info.getTitleInfoProperty(ViewTitleInfo.TITLE_INDICATOR_TEXT);
                    int[] colour = (int[]) title_info.getTitleInfoProperty(ViewTitleInfo.TITLE_INDICATOR_COLOR);
                    boolean changed = text != last_text && (text == null || last_text == null || !text.equals(last_text));
                    if (!changed) {
                        changed = colour != last_colour && (colour == null || last_colour == null || !Arrays.equals(colour, last_colour));
                    }
                    if (changed) {
                        last_text = text;
                        last_colour = colour;
                        mdi_entry.redraw();
                    }
                    ViewTitleInfoManager.refreshTitleInfo(title_info);
                }
            });
            mdi_entry.addListener(new MdiCloseListener() {

                @Override
                public void mdiEntryClosed(MdiEntry entry, boolean userClosed) {
                    timer.cancel();
                }
            });
            return mdi_entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_ARCHIVED_DOWNLOADS, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            final com.biglybt.pif.download.DownloadManager download_manager = PluginInitializer.getDefaultInterface().getDownloadManager();
            final ViewTitleInfo title_info = new ViewTitleInfo() {

                @Override
                public Object getTitleInfoProperty(int propertyID) {
                    if (propertyID == TITLE_INDICATOR_TEXT) {
                        int num = download_manager.getDownloadStubCount();
                        return (String.valueOf(num));
                    }
                    return null;
                }
            };
            MdiEntry entry = mdi.createEntryFromSkinRef(MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, MultipleDocumentInterface.SIDEBAR_SECTION_ARCHIVED_DOWNLOADS, "archivedlsview", "{mdi.entry.archiveddownloadsview}", title_info, null, true, null);
            entry.setImageLeftID("image.sidebar.archive");
            final DownloadStubListener stub_listener = new DownloadStubListener() {

                @Override
                public void downloadStubEventOccurred(DownloadStubEvent event) {
                    ViewTitleInfoManager.refreshTitleInfo(title_info);
                }
            };
            download_manager.addDownloadStubListener(stub_listener, false);
            entry.addListener(new MdiCloseListener() {

                @Override
                public void mdiEntryClosed(MdiEntry entry, boolean userClosed) {
                    download_manager.removeDownloadStubListener(stub_listener);
                }
            });
            entry.addListener(new MdiEntryDropListener() {

                @Override
                public boolean mdiEntryDrop(MdiEntry entry, Object data) {
                    if (data instanceof String) {
                        String str = (String) data;
                        if (str.startsWith("DownloadManager\n")) {
                            String[] bits = str.split("\n");
                            com.biglybt.pif.download.DownloadManager dm = PluginInitializer.getDefaultInterface().getDownloadManager();
                            List<Download> downloads = new ArrayList<>();
                            boolean failed = false;
                            for (int i = 1; i < bits.length; i++) {
                                byte[] hash = Base32.decode(bits[i]);
                                try {
                                    Download download = dm.getDownload(hash);
                                    if (download.canStubbify()) {
                                        downloads.add(download);
                                    } else {
                                        failed = true;
                                    }
                                } catch (Throwable e) {
                                }
                            }
                            final boolean f_failed = failed;
                            ManagerUtils.moveToArchive(downloads, new ManagerUtils.ArchiveCallback() {

                                boolean error = f_failed;

                                @Override
                                public void failed(DownloadStub original, Throwable e) {
                                    error = true;
                                }

                                @Override
                                public void completed() {
                                    if (error) {
                                        String title = MessageText.getString("archive.failed.title");
                                        String text = MessageText.getString("archive.failed.text");
                                        MessageBoxShell prompter = new MessageBoxShell(title, text, new String[] { MessageText.getString("Button.ok") }, 0);
                                        prompter.setAutoCloseInMS(0);
                                        prompter.open(null);
                                    }
                                }
                            });
                        }
                        return (true);
                    }
                    return false;
                }
            });
            return entry;
        }
    });
    // download history
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_DOWNLOAD_HISTORY, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            final DownloadHistoryManager history_manager = (DownloadHistoryManager) CoreFactory.getSingleton().getGlobalManager().getDownloadHistoryManager();
            final ViewTitleInfo title_info = new ViewTitleInfo() {

                @Override
                public Object getTitleInfoProperty(int propertyID) {
                    if (propertyID == TITLE_INDICATOR_TEXT) {
                        if (history_manager == null) {
                            return (null);
                        } else if (history_manager.isEnabled()) {
                            int num = history_manager.getHistoryCount();
                            return (String.valueOf(num));
                        } else {
                            return (MessageText.getString("label.disabled"));
                        }
                    }
                    return null;
                }
            };
            MdiEntry entry = mdi.createEntryFromSkinRef(MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, MultipleDocumentInterface.SIDEBAR_SECTION_DOWNLOAD_HISTORY, "downloadhistoryview", "{mdi.entry.downloadhistoryview}", title_info, null, true, null);
            entry.setImageLeftID("image.sidebar.logview");
            if (history_manager != null) {
                final DownloadHistoryListener history_listener = new DownloadHistoryListener() {

                    @Override
                    public void downloadHistoryEventOccurred(DownloadHistoryEvent event) {
                        ViewTitleInfoManager.refreshTitleInfo(title_info);
                    }
                };
                history_manager.addListener(history_listener, false);
                entry.addListener(new MdiCloseListener() {

                    @Override
                    public void mdiEntryClosed(MdiEntry entry, boolean userClosed) {
                        history_manager.removeListener(history_listener);
                    }
                });
            }
            return entry;
        }
    });
    // torrent options
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_OPTIONS, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, TorrentOptionsView.class, MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_OPTIONS, true, null, null);
            entry.setImageLeftID("image.sidebar.torrentoptions");
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_MY_SHARES, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, MySharesView.class, MultipleDocumentInterface.SIDEBAR_SECTION_MY_SHARES, true, null, null);
            entry.setImageLeftID("image.sidebar.myshares");
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_MY_TRACKER, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, MyTrackerView.class, MultipleDocumentInterface.SIDEBAR_SECTION_MY_TRACKER, true, null, null);
            entry.setImageLeftID("image.sidebar.mytracker");
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_CLIENT_STATS, new MdiEntryCreationListener() {

        @Override
        public MdiEntry createMDiEntry(String id) {
            MdiEntry entry = mdi.createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_PLUGINS, ClientStatsView.class, MultipleDocumentInterface.SIDEBAR_SECTION_CLIENT_STATS, true, null, null);
            entry.setImageLeftID("image.sidebar.clientstats");
            return entry;
        }
    });
    mdi.registerEntry(MultipleDocumentInterface.SIDEBAR_SECTION_CONFIG, new MdiEntryCreationListener2() {

        @Override
        public MdiEntry createMDiEntry(MultipleDocumentInterface mdi, String id, Object datasource, Map<?, ?> params) {
            String section = (datasource instanceof String) ? ((String) datasource) : null;
            boolean uiClassic = COConfigurationManager.getStringParameter("ui").equals("az2");
            if (uiClassic || COConfigurationManager.getBooleanParameter("Show Options In Side Bar")) {
                MdiEntry entry = ((MultipleDocumentInterfaceSWT) mdi).createEntryFromEventListener(MultipleDocumentInterface.SIDEBAR_HEADER_PLUGINS, ConfigView.class, MultipleDocumentInterface.SIDEBAR_SECTION_CONFIG, true, null, null);
                entry.setImageLeftID("image.sidebar.config");
                return entry;
            }
            ConfigShell.getInstance().open(section);
            return null;
        }
    });
    try {
        if (!COConfigurationManager.getBooleanParameter("my.shares.view.auto.open.done", false)) {
            final ShareManager share_manager = pi.getShareManager();
            if (share_manager.getShares().length > 0) {
            // stop showing this by default
            // mdi.showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_MY_SHARES);
            } else {
                shareManagerListener = new ShareManagerListener() {

                    boolean done = false;

                    @Override
                    public void resourceModified(ShareResource old_resource, ShareResource new_resource) {
                    }

                    @Override
                    public void resourceDeleted(ShareResource resource) {
                    }

                    @Override
                    public void resourceAdded(ShareResource resource) {
                        if (done) {
                            return;
                        }
                        done = true;
                        share_manager.removeListener(this);
                        COConfigurationManager.setParameter("my.shares.view.auto.open.done", true);
                        mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_MY_SHARES, false);
                    }

                    @Override
                    public void reportProgress(int percent_complete) {
                    }

                    @Override
                    public void reportCurrentTask(String task_description) {
                    }
                };
                share_manager.addListener(shareManagerListener);
            }
        }
    } catch (Throwable t) {
    }
    try {
        if (!COConfigurationManager.getBooleanParameter("my.tracker.view.auto.open.done", false)) {
            // Load Tracker View on first host of file
            TRHost trackerHost = CoreFactory.getSingleton().getTrackerHost();
            trackerHostListener = new TRHostListener() {

                boolean done = false;

                @Override
                public void torrentRemoved(TRHostTorrent t) {
                }

                @Override
                public void torrentChanged(TRHostTorrent t) {
                }

                @Override
                public void torrentAdded(TRHostTorrent t) {
                    if (done) {
                        return;
                    }
                    done = true;
                    trackerHost.removeListener(this);
                    COConfigurationManager.setParameter("my.tracker.view.auto.open.done", true);
                    mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_MY_TRACKER, false);
                }

                @Override
                public boolean handleExternalRequest(InetSocketAddress client_address, String user, String url, URL absolute_url, String header, InputStream is, OutputStream os, AsyncController async) throws IOException {
                    return false;
                }
            };
            trackerHost.addListener(trackerHostListener);
        }
    } catch (Throwable t) {
    }
    UIManager uim = pi.getUIManager();
    if (uim != null) {
        MenuItem menuItem = uim.getMenuManager().addMenuItem(MenuManager.MENU_MENUBAR, "tags.view.heading");
        menuItem.setDisposeWithUIDetach(UIInstance.UIT_SWT);
        menuItem.addListener(new MenuItemListener() {

            @Override
            public void selected(MenuItem menu, Object target) {
                UIFunctionsManager.getUIFunctions().getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TAGS);
            }
        });
        menuItem = uim.getMenuManager().addMenuItem(MenuManager.MENU_MENUBAR, "tag.discovery.view.heading");
        menuItem.setDisposeWithUIDetach(UIInstance.UIT_SWT);
        menuItem.addListener(new MenuItemListener() {

            @Override
            public void selected(MenuItem menu, Object target) {
                UIFunctionsManager.getUIFunctions().getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TAG_DISCOVERY);
            }
        });
        menuItem = uim.getMenuManager().addMenuItem(MenuManager.MENU_MENUBAR, "chats.view.heading");
        menuItem.setDisposeWithUIDetach(UIInstance.UIT_SWT);
        menuItem.addListener(new MenuItemListener() {

            @Override
            public void selected(MenuItem menu, Object target) {
                UIFunctionsManager.getUIFunctions().getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_CHAT);
            }
        });
        menuItem = uim.getMenuManager().addMenuItem(MenuManager.MENU_MENUBAR, "archivedlsview.view.heading");
        menuItem.setDisposeWithUIDetach(UIInstance.UIT_SWT);
        menuItem.addListener(new MenuItemListener() {

            @Override
            public void selected(MenuItem menu, Object target) {
                UIFunctionsManager.getUIFunctions().getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_ARCHIVED_DOWNLOADS);
            }
        });
        menuItem = uim.getMenuManager().addMenuItem(MenuManager.MENU_MENUBAR, "downloadhistoryview.view.heading");
        menuItem.setDisposeWithUIDetach(UIInstance.UIT_SWT);
        menuItem.addListener(new MenuItemListener() {

            @Override
            public void selected(MenuItem menu, Object target) {
                UIFunctionsManager.getUIFunctions().getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_DOWNLOAD_HISTORY);
            }
        });
    }
// System.out.println("Activate sidebar " + startTab + " took "
// + (SystemTime.getCurrentTime() - startTime) + "ms");
// startTime = SystemTime.getCurrentTime();
}
Also used : TimerEventPeriodic(com.biglybt.core.util.TimerEventPeriodic) DownloadHistoryManager(com.biglybt.core.history.DownloadHistoryManager) ChatInstance(com.biglybt.plugin.net.buddy.BuddyPluginBeta.ChatInstance) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) UIManager(com.biglybt.pif.ui.UIManager) UIManagerListener2(com.biglybt.pif.ui.UIManagerListener2) DownloadStub(com.biglybt.pif.download.DownloadStub) TimerEventPerformer(com.biglybt.core.util.TimerEventPerformer) TimerEvent(com.biglybt.core.util.TimerEvent) ShareManagerListener(com.biglybt.pif.sharing.ShareManagerListener) MenuItemListener(com.biglybt.pif.ui.menus.MenuItemListener) UIInstance(com.biglybt.pif.ui.UIInstance) StatsView(com.biglybt.ui.swt.views.stats.StatsView) ClientStatsView(com.biglybt.ui.swt.views.clientstats.ClientStatsView) TRHostTorrent(com.biglybt.core.tracker.host.TRHostTorrent) DownloadStubEvent(com.biglybt.pif.download.DownloadStubEvent) TRHost(com.biglybt.core.tracker.host.TRHost) DownloadHistoryListener(com.biglybt.core.history.DownloadHistoryListener) ViewTitleInfo(com.biglybt.ui.common.viewtitleinfo.ViewTitleInfo) AsyncController(com.biglybt.core.util.AsyncController) ShareManager(com.biglybt.pif.sharing.ShareManager) ManagerUtils(com.biglybt.ui.swt.views.utils.ManagerUtils) URL(java.net.URL) DownloadHistoryEvent(com.biglybt.core.history.DownloadHistoryEvent) BuddyPluginBeta(com.biglybt.plugin.net.buddy.BuddyPluginBeta) Download(com.biglybt.pif.download.Download) ShareResource(com.biglybt.pif.sharing.ShareResource) InputStream(java.io.InputStream) PluginInterface(com.biglybt.pif.PluginInterface) DownloadStubListener(com.biglybt.pif.download.DownloadStubListener) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) MenuItem(com.biglybt.pif.ui.menus.MenuItem) IOException(java.io.IOException) TRHostListener(com.biglybt.core.tracker.host.TRHostListener) ParameterListener(com.biglybt.core.config.ParameterListener) ClientStatsView(com.biglybt.ui.swt.views.clientstats.ClientStatsView)

Aggregations

TimerEvent (com.biglybt.core.util.TimerEvent)3 TimerEventPerformer (com.biglybt.core.util.TimerEventPerformer)3 TimerEventPeriodic (com.biglybt.core.util.TimerEventPeriodic)3 Core (com.biglybt.core.Core)2 CoreLifecycleAdapter (com.biglybt.core.CoreLifecycleAdapter)2 BufferedLabel (com.biglybt.ui.swt.components.BufferedLabel)2 IntParameter (com.biglybt.ui.swt.config.IntParameter)2 Parameter (com.biglybt.ui.swt.config.Parameter)2 ParameterChangeAdapter (com.biglybt.ui.swt.config.ParameterChangeAdapter)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Point (org.eclipse.swt.graphics.Point)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 ParameterListener (com.biglybt.core.config.ParameterListener)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 GlobalManagerStats (com.biglybt.core.global.GlobalManagerStats)1 DownloadHistoryEvent (com.biglybt.core.history.DownloadHistoryEvent)1 DownloadHistoryListener (com.biglybt.core.history.DownloadHistoryListener)1 DownloadHistoryManager (com.biglybt.core.history.DownloadHistoryManager)1