Search in sources :

Example 1 with TimerEvent

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

the class SB_Dashboard method build.

protected void build(Composite dashboard_composite) {
    try {
        building++;
        if (dashboard_composite == null) {
            return;
        }
        Utils.disposeComposite(dashboard_composite, false);
        int[][] layout = getDashboardLayout();
        layout = compactLayout(layout, 0, 0);
        Map<Integer, DashboardItem> item_map = new HashMap<>();
        for (DashboardItem item : items) {
            item_map.put(item.getUID(), item);
        }
        for (int[] row : layout) {
            for (int i = 0; i < row.length; i++) {
                int c = row[i];
                if (!item_map.containsKey(c)) {
                    row[i] = -1;
                }
            }
        }
        boolean use_tabs = getUseTabs();
        final List<SashForm> sashes = new ArrayList<>();
        List<Control> controls = new ArrayList<>();
        build(item_map, dashboard_composite, use_tabs, sashes, controls, layout, 0, 0, layout.length, layout.length);
        int[][] sash_weights = getSashWeights();
        if (sash_weights.length == sashes.size()) {
            for (int i = 0; i < sash_weights.length; i++) {
                int[] weights = sash_weights[i];
                SashForm sf = sashes.get(i);
                int[] sf_weights = sf.getWeights();
                if (sf_weights.length == weights.length) {
                    try {
                        sf.setWeights(weights);
                    } catch (Throwable e) {
                        // in case something borks
                        Debug.out(e);
                    }
                }
                sf.setData(sf_weights.length);
            }
        } else {
            // something's changed
            setSashWeights(new int[0][0]);
            for (SashForm sf : sashes) {
                int[] weights = sf.getWeights();
                sf.setData(weights.length);
            }
        }
        for (Control c : controls) {
            c.addControlListener(new ControlListener() {

                @Override
                public void controlResized(ControlEvent arg0) {
                    if (building > 0) {
                        return;
                    }
                    int[][] weights = new int[sashes.size()][];
                    for (int i = 0; i < sashes.size(); i++) {
                        SashForm sf = sashes.get(i);
                        if (sf.isDisposed()) {
                            return;
                        }
                        weights[i] = sf.getWeights();
                        Object d = sf.getData();
                        if (d == null || ((Integer) d) != weights[i].length) {
                            return;
                        }
                    }
                    setSashWeights(weights);
                }

                @Override
                public void controlMoved(ControlEvent arg0) {
                // TODO Auto-generated method stub
                }
            });
        }
        dashboard_composite.getParent().layout(true, true);
    } finally {
        SimpleTimer.addEvent("delayer", SystemTime.getOffsetTime(2500), new TimerEventPerformer() {

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

                    @Override
                    public void run() {
                        building--;
                    }
                });
            }
        });
    }
}
Also used : Point(org.eclipse.swt.graphics.Point) SashForm(org.eclipse.swt.custom.SashForm) Control(org.eclipse.swt.widgets.Control) TimerEventPerformer(com.biglybt.core.util.TimerEventPerformer) ControlListener(org.eclipse.swt.events.ControlListener) TimerEvent(com.biglybt.core.util.TimerEvent) ControlEvent(org.eclipse.swt.events.ControlEvent)

Example 2 with TimerEvent

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

the class PieceInfoView method createPeerInfoPanel.

private void createPeerInfoPanel(Composite parent) {
    GridLayout layout;
    GridData gridData;
    // Peer Info section contains
    // - Peer's Block display
    // - Peer's Datarate
    pieceInfoComposite = new Composite(parent, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 2;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    pieceInfoComposite.setLayout(layout);
    gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
    pieceInfoComposite.setLayoutData(gridData);
    imageLabel = new Label(pieceInfoComposite, SWT.NULL);
    gridData = new GridData();
    imageLabel.setLayoutData(gridData);
    topLabel = new Label(pieceInfoComposite, SWT.NULL);
    topLabel.setBackground(Colors.white);
    gridData = new GridData(SWT.FILL, SWT.DEFAULT, false, false);
    topLabel.setLayoutData(gridData);
    sc = new ScrolledComposite(pieceInfoComposite, SWT.V_SCROLL);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    layout = new GridLayout();
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    sc.setLayout(layout);
    gridData = new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1);
    sc.setLayoutData(gridData);
    sc.getVerticalBar().setIncrement(BLOCK_SIZE);
    pieceInfoCanvas = new Canvas(sc, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL, SWT.DEFAULT, true, false);
    pieceInfoCanvas.setLayoutData(gridData);
    pieceInfoCanvas.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            if (e.width <= 0 || e.height <= 0)
                return;
            try {
                Rectangle bounds = (img == null) ? null : img.getBounds();
                if (bounds == null || dlm == null || dlm.getPeerManager() == null) {
                    e.gc.fillRectangle(e.x, e.y, e.width, e.height);
                } else {
                    if (e.x + e.width > bounds.width)
                        e.gc.fillRectangle(bounds.width, e.y, e.x + e.width - bounds.width + 1, e.height);
                    if (e.y + e.height > bounds.height)
                        e.gc.fillRectangle(e.x, bounds.height, e.width, e.y + e.height - bounds.height + 1);
                    int width = Math.min(e.width, bounds.width - e.x);
                    int height = Math.min(e.height, bounds.height - e.y);
                    e.gc.drawImage(img, e.x, e.y, width, height, e.x, e.y, width, height);
                }
            } catch (Exception ex) {
            }
        }
    });
    pieceInfoCanvas.addListener(SWT.KeyDown, new DoNothingListener());
    pieceInfoCanvas.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            synchronized (PieceInfoView.this) {
                if (alreadyFilling) {
                    return;
                }
                alreadyFilling = true;
            }
            // wrap in asyncexec because sc.setMinWidth (called later) doesn't work
            // too well inside a resize (the canvas won't size isn't always updated)
            Utils.execSWTThreadLater(0, new AERunnable() {

                @Override
                public void runSupport() {
                    if (img != null) {
                        int iOldColCount = img.getBounds().width / BLOCK_SIZE;
                        int iNewColCount = pieceInfoCanvas.getClientArea().width / BLOCK_SIZE;
                        if (iOldColCount != iNewColCount)
                            refreshInfoCanvas();
                    }
                    synchronized (PieceInfoView.this) {
                        alreadyFilling = false;
                    }
                }
            });
        }
    });
    sc.setContent(pieceInfoCanvas);
    pieceInfoCanvas.addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent event) {
            int piece_number = getPieceNumber(event.x, event.y);
            if (piece_number != selectedPiece) {
                selectedPieceShowFilePending = -1;
            }
        }
    });
    pieceInfoCanvas.addMouseTrackListener(new MouseTrackAdapter() {

        @Override
        public void mouseHover(MouseEvent event) {
            int piece_number = getPieceNumber(event.x, event.y);
            if (piece_number >= 0) {
                selectedPiece = piece_number;
                selectedPieceShowFilePending = piece_number;
                SimpleTimer.addEvent("ShowFile", SystemTime.getOffsetTime(1000), new TimerEventPerformer() {

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

                            @Override
                            public void run() {
                                if (selectedPieceShowFilePending == piece_number) {
                                    selectedPieceShowFile = true;
                                    refreshInfoCanvas();
                                }
                            }
                        });
                    }
                });
                refreshInfoCanvas();
                DiskManager disk_manager = dlm.getDiskManager();
                PEPeerManager pm = dlm.getPeerManager();
                DiskManagerPiece dm_piece = disk_manager.getPiece(piece_number);
                PEPiece pm_piece = pm.getPiece(piece_number);
                String text = "Piece " + piece_number + ": " + dm_piece.getString();
                if (pm_piece != null) {
                    text += ", active: " + pm_piece.getString();
                } else {
                    if (dm_piece.isNeeded() && !dm_piece.isDone()) {
                        text += ", inactive: " + pm.getPiecePicker().getPieceString(piece_number);
                    }
                }
                text += " - ";
                DMPieceList l = disk_manager.getPieceList(piece_number);
                for (int i = 0; i < l.size(); i++) {
                    DMPieceMapEntry entry = l.get(i);
                    DiskManagerFileInfo info = entry.getFile();
                    text += (i == 0 ? "" : "; ") + info.getFile(true).getName();
                }
                topLabelRHS = text;
            } else {
                topLabelRHS = "";
            }
            updateTopLabel();
        }

        @Override
        public void mouseExit(MouseEvent e) {
            selectedPiece = -1;
            selectedPieceShowFile = false;
            refreshInfoCanvas();
        }
    });
    final Menu menu = new Menu(pieceInfoCanvas.getShell(), SWT.POP_UP);
    pieceInfoCanvas.setMenu(menu);
    pieceInfoCanvas.addListener(SWT.MenuDetect, new Listener() {

        @Override
        public void handleEvent(Event event) {
            Point pt = pieceInfoCanvas.toControl(event.x, event.y);
            int piece_number = getPieceNumber(pt.x, pt.y);
            menu.setData("pieceNumber", piece_number);
        }
    });
    MenuBuildUtils.addMaintenanceListenerForMenu(menu, new MenuBuildUtils.MenuBuilder() {

        @Override
        public void buildMenu(Menu menu, MenuEvent event) {
            Integer pn = (Integer) menu.getData("pieceNumber");
            if (pn != null && pn != -1) {
                DownloadManager download_manager = dlm;
                if (download_manager == null) {
                    return;
                }
                DiskManager disk_manager = download_manager.getDiskManager();
                PEPeerManager peer_manager = download_manager.getPeerManager();
                if (disk_manager == null || peer_manager == null) {
                    return;
                }
                final PiecePicker picker = peer_manager.getPiecePicker();
                DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
                PEPiece[] pe_pieces = peer_manager.getPieces();
                final int piece_number = pn;
                final DiskManagerPiece dm_piece = dm_pieces[piece_number];
                final PEPiece pm_piece = pe_pieces[piece_number];
                final MenuItem force_piece = new MenuItem(menu, SWT.CHECK);
                Messages.setLanguageText(force_piece, "label.force.piece");
                boolean done = dm_piece.isDone();
                force_piece.setEnabled(!done);
                if (!done) {
                    force_piece.setSelection(picker.isForcePiece(piece_number));
                    force_piece.addSelectionListener(new SelectionListenerForcePiece(picker, piece_number, force_piece));
                }
                final MenuItem reset_piece = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(reset_piece, "label.reset.piece");
                boolean can_reset = dm_piece.isDone() || dm_piece.getNbWritten() > 0;
                reset_piece.setEnabled(can_reset);
                reset_piece.addSelectionListener(new SelectionListenerResetPiece(dm_piece, pm_piece));
                new MenuItem(menu, SWT.SEPARATOR);
                final MenuItem seq_asc = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(seq_asc, "label.seq.asc.from", new String[] { String.valueOf(piece_number) });
                seq_asc.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
                        picker.setReverseBlockOrder(false);
                        picker.setSequentialAscendingFrom(piece_number);
                    }
                });
                final MenuItem seq_desc = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(seq_desc, "label.seq.desc.from", new String[] { String.valueOf(piece_number) });
                seq_desc.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
                        picker.setReverseBlockOrder(true);
                        picker.setSequentialDescendingFrom(piece_number);
                    }
                });
                final MenuItem seq_clear = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(seq_clear, "label.seq.clear", new String[] { String.valueOf(piece_number) });
                seq_clear.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
                        picker.setReverseBlockOrder(false);
                        picker.clearSequential();
                    }
                });
            }
        }
    });
    Legend.createLegendComposite(pieceInfoComposite, blockColors, new String[] { "PiecesView.BlockView.Have", "PiecesView.BlockView.NoHave", "PeersView.BlockView.Transfer", "PeersView.BlockView.NextRequest", "PeersView.BlockView.AvailCount", "PeersView.BlockView.ShowFile" }, new GridData(SWT.FILL, SWT.DEFAULT, true, false, 2, 1));
    int iFontPixelsHeight = 10;
    int iFontPointHeight = (iFontPixelsHeight * 72) / Utils.getDPIRaw(pieceInfoCanvas.getDisplay()).y;
    Font f = pieceInfoCanvas.getFont();
    FontData[] fontData = f.getFontData();
    fontData[0].setHeight(iFontPointHeight);
    font = new Font(pieceInfoCanvas.getDisplay(), fontData);
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) DownloadManagerPieceListener(com.biglybt.core.download.DownloadManagerPieceListener) PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager) GridLayout(org.eclipse.swt.layout.GridLayout) TimerEventPerformer(com.biglybt.core.util.TimerEventPerformer) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) TimerEvent(com.biglybt.core.util.TimerEvent) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList) PEPiece(com.biglybt.core.peer.PEPiece) AERunnable(com.biglybt.core.util.AERunnable) GridData(org.eclipse.swt.layout.GridData) DMPieceMapEntry(com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry) UISWTViewEvent(com.biglybt.ui.swt.pif.UISWTViewEvent) LogEvent(com.biglybt.core.logging.LogEvent) TimerEvent(com.biglybt.core.util.TimerEvent) PEPeerManager(com.biglybt.core.peer.PEPeerManager) MenuBuildUtils(com.biglybt.ui.swt.MenuBuildUtils)

Example 3 with TimerEvent

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

the class NatPMPUPnPImpl method start.

protected void start() {
    SimpleTimer.addPeriodicEvent("NATPMP:search", 60 * 1000, new TimerEventPerformer() {

        @Override
        public void perform(TimerEvent event) {
            search();
        }
    });
    search();
}
Also used : TimerEventPerformer(com.biglybt.core.util.TimerEventPerformer) TimerEvent(com.biglybt.core.util.TimerEvent)

Example 4 with TimerEvent

use of com.biglybt.core.util.TimerEvent 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 5 with TimerEvent

use of com.biglybt.core.util.TimerEvent 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)

Aggregations

TimerEvent (com.biglybt.core.util.TimerEvent)7 TimerEventPerformer (com.biglybt.core.util.TimerEventPerformer)7 TimerEventPeriodic (com.biglybt.core.util.TimerEventPeriodic)3 Point (org.eclipse.swt.graphics.Point)3 GridData (org.eclipse.swt.layout.GridData)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 Core (com.biglybt.core.Core)2 CoreLifecycleAdapter (com.biglybt.core.CoreLifecycleAdapter)2 DownloadManager (com.biglybt.core.download.DownloadManager)2 ViewTitleInfo (com.biglybt.ui.common.viewtitleinfo.ViewTitleInfo)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 ParameterListener (com.biglybt.core.config.ParameterListener)1 DiskManager (com.biglybt.core.disk.DiskManager)1 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)1 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)1