Search in sources :

Example 16 with DiskManagerPiece

use of com.biglybt.core.disk.DiskManagerPiece in project BiglyBT by BiglySoftware.

the class FileInfoView method createFileInfoPanel.

private Composite createFileInfoPanel(Composite parent) {
    GridLayout layout;
    GridData gridData;
    // Peer Info section contains
    // - Peer's Block display
    // - Peer's Datarate
    fileInfoComposite = new Composite(parent, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 2;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    fileInfoComposite.setLayout(layout);
    gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
    fileInfoComposite.setLayoutData(gridData);
    new Label(fileInfoComposite, SWT.NULL).setLayoutData(new GridData());
    topLabel = new Label(fileInfoComposite, SWT.NULL);
    gridData = new GridData(SWT.FILL, SWT.DEFAULT, false, false);
    topLabel.setLayoutData(gridData);
    sc = new ScrolledComposite(fileInfoComposite, 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);
    fileInfoCanvas = new Canvas(sc, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL, SWT.DEFAULT, true, false);
    fileInfoCanvas.setLayoutData(gridData);
    fileInfoCanvas.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) {
                    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) {
            }
        }
    });
    fileInfoCanvas.addMouseTrackListener(new MouseTrackAdapter() {

        @Override
        public void mouseHover(MouseEvent event) {
            showPieceDetails(event.x, event.y);
        }
    });
    Listener doNothingListener = new Listener() {

        @Override
        public void handleEvent(Event event) {
        }
    };
    fileInfoCanvas.addListener(SWT.KeyDown, doNothingListener);
    final Menu menu = new Menu(fileInfoCanvas.getShell(), SWT.POP_UP);
    fileInfoCanvas.setMenu(menu);
    fileInfoCanvas.addListener(SWT.MenuDetect, new Listener() {

        @Override
        public void handleEvent(Event event) {
            Point pt = fileInfoCanvas.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 = file.getDownloadManager();
                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 pe_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 SelectionAdapter() {

                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            picker.setForcePiece(piece_number, force_piece.getSelection());
                        }
                    });
                }
                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 SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        dm_piece.reset();
                        if (pe_piece != null) {
                            pe_piece.reset();
                        }
                    }
                });
            }
        }
    });
    fileInfoCanvas.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            if (refreshInfoCanvasQueued) {
                return;
            }
            refreshInfoCanvasQueued = 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 = fileInfoCanvas.getClientArea().width / BLOCK_SIZE;
                        if (iOldColCount != iNewColCount)
                            refreshInfoCanvas();
                    }
                }
            });
        }
    });
    sc.setContent(fileInfoCanvas);
    Legend.createLegendComposite(fileInfoComposite, blockColors, new String[] { "FileView.BlockView.Done", "FileView.BlockView.Skipped", "FileView.BlockView.Active", "FileView.BlockView.Outstanding" }, new GridData(SWT.FILL, SWT.DEFAULT, true, false, 2, 1));
    int iFontPixelsHeight = 10;
    int iFontPointHeight = (iFontPixelsHeight * 72) / Utils.getDPIRaw(fileInfoCanvas.getDisplay()).y;
    Font f = fileInfoCanvas.getFont();
    FontData[] fontData = f.getFontData();
    fontData[0].setHeight(iFontPointHeight);
    font = new Font(fileInfoCanvas.getDisplay(), fontData);
    return fileInfoComposite;
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) UISWTViewCoreEventListener(com.biglybt.ui.swt.pifimpl.UISWTViewCoreEventListener) 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) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) PEPiece(com.biglybt.core.peer.PEPiece) GridData(org.eclipse.swt.layout.GridData) UISWTViewEvent(com.biglybt.ui.swt.pif.UISWTViewEvent) LogEvent(com.biglybt.core.logging.LogEvent) PEPeerManager(com.biglybt.core.peer.PEPeerManager) MenuBuildUtils(com.biglybt.ui.swt.MenuBuildUtils)

Example 17 with DiskManagerPiece

use of com.biglybt.core.disk.DiskManagerPiece in project BiglyBT by BiglySoftware.

the class FileInfoView method refreshInfoCanvas.

protected void refreshInfoCanvas() {
    if (fileInfoCanvas == null || fileInfoCanvas.isDisposed()) {
        return;
    }
    refreshInfoCanvasQueued = false;
    Rectangle bounds = fileInfoCanvas.getClientArea();
    if (bounds.width <= 0 || bounds.height <= 0)
        return;
    if (img != null && !img.isDisposed()) {
        img.dispose();
        img = null;
    }
    DownloadManager download_manager = file == null ? null : file.getDownloadManager();
    DiskManager disk_manager = download_manager == null ? null : download_manager.getDiskManager();
    PEPeerManager peer_manager = download_manager == null ? null : download_manager.getPeerManager();
    if (file == null || disk_manager == null || peer_manager == null) {
        GC gc = new GC(fileInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        return;
    }
    int first_piece = file.getFirstPieceNumber();
    int num_pieces = file.getNbPieces();
    int iNumCols = bounds.width / BLOCK_SIZE;
    int iNeededHeight = (((num_pieces - 1) / iNumCols) + 1) * BLOCK_SIZE;
    if (sc.getMinHeight() != iNeededHeight) {
        sc.setMinHeight(iNeededHeight);
        sc.layout(true, true);
        bounds = fileInfoCanvas.getClientArea();
    }
    img = new Image(fileInfoCanvas.getDisplay(), bounds.width, iNeededHeight);
    GC gcImg = new GC(img);
    try {
        gcImg.setBackground(fileInfoCanvas.getBackground());
        gcImg.fillRectangle(0, 0, bounds.width, bounds.height);
        DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
        PEPiece[] pe_pieces = peer_manager.getPieces();
        int iRow = 0;
        int iCol = 0;
        for (int i = first_piece; i < first_piece + num_pieces; i++) {
            DiskManagerPiece dm_piece = dm_pieces[i];
            PEPiece pe_piece = pe_pieces[i];
            int colorIndex;
            int iXPos = iCol * BLOCK_SIZE;
            int iYPos = iRow * BLOCK_SIZE;
            if (dm_piece.isDone()) {
                colorIndex = BLOCKCOLOR_DONE;
            } else if (!dm_piece.isNeeded()) {
                colorIndex = BLOCKCOLOR_SKIPPED;
            } else if (pe_piece != null) {
                colorIndex = BLOCKCOLOR_ACTIVE;
            } else {
                colorIndex = BLOCKCOLOR_NEEDED;
            }
            gcImg.setBackground(blockColors[colorIndex]);
            gcImg.fillRectangle(iXPos, iYPos, BLOCK_FILLSIZE, BLOCK_FILLSIZE);
            iCol++;
            if (iCol >= iNumCols) {
                iCol = 0;
                iRow++;
            }
        }
    } catch (Exception e) {
        Logger.log(new LogEvent(LogIDs.GUI, "drawing piece map", e));
    } finally {
        gcImg.dispose();
    }
    fileInfoCanvas.redraw();
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager) PEPiece(com.biglybt.core.peer.PEPiece) PEPeerManager(com.biglybt.core.peer.PEPeerManager) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece)

Example 18 with DiskManagerPiece

use of com.biglybt.core.disk.DiskManagerPiece in project BiglyBT by BiglySoftware.

the class PieceInfoView method refreshInfoCanvas.

protected void refreshInfoCanvas() {
    synchronized (PieceInfoView.this) {
        alreadyFilling = false;
    }
    if (pieceInfoCanvas == null || pieceInfoCanvas.isDisposed() || !pieceInfoCanvas.isVisible()) {
        return;
    }
    pieceInfoCanvas.layout(true);
    Rectangle bounds = pieceInfoCanvas.getClientArea();
    if (bounds.width <= 0 || bounds.height <= 0) {
        topLabelLHS = "";
        updateTopLabel();
        return;
    }
    if (dlm == null) {
        GC gc = new GC(pieceInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        topLabelLHS = MessageText.getString("view.one.download.only");
        topLabelRHS = "";
        updateTopLabel();
        return;
    }
    PEPeerManager pm = dlm.getPeerManager();
    DiskManager dm = dlm.getDiskManager();
    if (pm == null || dm == null) {
        GC gc = new GC(pieceInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        topLabelLHS = "";
        updateTopLabel();
        return;
    }
    int iNumCols = bounds.width / BLOCK_SIZE;
    int iNeededHeight = (((dm.getNbPieces() - 1) / iNumCols) + 1) * BLOCK_SIZE;
    if (img != null && !img.isDisposed()) {
        Rectangle imgBounds = img.getBounds();
        if (imgBounds.width != bounds.width || imgBounds.height != iNeededHeight) {
            oldBlockInfo = null;
            img.dispose();
            img = null;
        }
    }
    DiskManagerPiece[] dm_pieces = dm.getPieces();
    PEPiece[] currentDLPieces = pm.getPieces();
    byte[] uploadingPieces = new byte[dm_pieces.length];
    // find upload pieces
    for (PEPeer peer : pm.getPeers()) {
        int[] peerRequestedPieces = peer.getIncomingRequestedPieceNumbers();
        if (peerRequestedPieces != null && peerRequestedPieces.length > 0) {
            int pieceNum = peerRequestedPieces[0];
            if (uploadingPieces[pieceNum] < 2)
                uploadingPieces[pieceNum] = 2;
            for (int j = 1; j < peerRequestedPieces.length; j++) {
                pieceNum = peerRequestedPieces[j];
                if (uploadingPieces[pieceNum] < 1)
                    uploadingPieces[pieceNum] = 1;
            }
        }
    }
    if (sc.getMinHeight() != iNeededHeight) {
        sc.setMinHeight(iNeededHeight);
        sc.layout(true, true);
        bounds = pieceInfoCanvas.getClientArea();
    }
    int[] availability = pm.getAvailability();
    int minAvailability = Integer.MAX_VALUE;
    int minAvailability2 = Integer.MAX_VALUE;
    if (availability != null && availability.length > 0) {
        for (int anAvailability : availability) {
            if (anAvailability != 0 && anAvailability < minAvailability) {
                minAvailability2 = minAvailability;
                minAvailability = anAvailability;
                if (minAvailability == 1) {
                    break;
                }
            }
        }
    }
    if (img == null) {
        img = new Image(pieceInfoCanvas.getDisplay(), bounds.width, iNeededHeight);
        oldBlockInfo = null;
    }
    GC gcImg = new GC(img);
    BlockInfo[] newBlockInfo = new BlockInfo[dm_pieces.length];
    int iRow = 0;
    try {
        // use advanced capabilities for faster drawText
        gcImg.setAdvanced(true);
        if (oldBlockInfo == null) {
            gcImg.setBackground(pieceInfoCanvas.getBackground());
            gcImg.fillRectangle(0, 0, bounds.width, iNeededHeight);
        }
        int selectionStart = Integer.MAX_VALUE;
        int selectionEnd = Integer.MIN_VALUE;
        if (selectedPiece != -1) {
            if (selectedPieceShowFile) {
                DMPieceList l = dm.getPieceList(selectedPiece);
                for (int i = 0; i < l.size(); i++) {
                    DMPieceMapEntry entry = l.get(i);
                    DiskManagerFileInfo info = entry.getFile();
                    int first = info.getFirstPieceNumber();
                    int last = info.getLastPieceNumber();
                    if (first < selectionStart) {
                        selectionStart = first;
                    }
                    if (last > selectionEnd) {
                        selectionEnd = last;
                    }
                }
            }
        }
        gcImg.setFont(font);
        int iCol = 0;
        for (int i = 0; i < dm_pieces.length; i++) {
            if (iCol >= iNumCols) {
                iCol = 0;
                iRow++;
            }
            BlockInfo newInfo = newBlockInfo[i] = new BlockInfo();
            if (i >= selectionStart && i <= selectionEnd) {
                newInfo.selected = true;
            }
            boolean done = dm_pieces[i].isDone();
            int iXPos = iCol * BLOCK_SIZE + 1;
            int iYPos = iRow * BLOCK_SIZE + 1;
            if (done) {
                newInfo.haveWidth = BLOCK_FILLSIZE;
            } else {
                // !done
                boolean partiallyDone = dm_pieces[i].getNbWritten() > 0;
                int width = BLOCK_FILLSIZE;
                if (partiallyDone) {
                    int iNewWidth = (int) (((float) dm_pieces[i].getNbWritten() / dm_pieces[i].getNbBlocks()) * width);
                    if (iNewWidth >= width)
                        iNewWidth = width - 1;
                    else if (iNewWidth <= 0)
                        iNewWidth = 1;
                    newInfo.haveWidth = iNewWidth;
                }
            }
            if (currentDLPieces[i] != null && currentDLPieces[i].hasUndownloadedBlock()) {
                newInfo.showDown = currentDLPieces[i].getNbRequests() == 0 ? SHOW_SMALL : SHOW_BIG;
            }
            if (uploadingPieces[i] > 0) {
                newInfo.showUp = uploadingPieces[i] < 2 ? SHOW_SMALL : SHOW_BIG;
            }
            if (availability != null) {
                newInfo.availNum = availability[i];
                if (minAvailability2 == availability[i]) {
                    newInfo.availDotted = true;
                }
            } else {
                newInfo.availNum = -1;
            }
            if (oldBlockInfo != null && i < oldBlockInfo.length && oldBlockInfo[i].sameAs(newInfo)) {
                iCol++;
                continue;
            }
            if (newInfo.selected) {
                Color fc = blockColors[BLOCKCOLOR_SHOWFILE];
                gcImg.setBackground(fc);
                gcImg.fillRectangle(iCol * BLOCK_SIZE, iRow * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
                if (fc != file_color) {
                    file_color = fc;
                    file_color_faded = Colors.getInstance().getLighterColor(fc, 75);
                }
                gcImg.setBackground(file_color_faded);
                gcImg.fillRectangle(iXPos + newInfo.haveWidth, iYPos, BLOCK_FILLSIZE - newInfo.haveWidth, BLOCK_FILLSIZE);
            } else {
                gcImg.setBackground(pieceInfoCanvas.getBackground());
                gcImg.fillRectangle(iCol * BLOCK_SIZE, iRow * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
                gcImg.setBackground(blockColors[BLOCKCOLOR_HAVE]);
                gcImg.fillRectangle(iXPos, iYPos, newInfo.haveWidth, BLOCK_FILLSIZE);
                gcImg.setBackground(blockColors[BLOCKCOLORL_NOHAVE]);
                gcImg.fillRectangle(iXPos + newInfo.haveWidth, iYPos, BLOCK_FILLSIZE - newInfo.haveWidth, BLOCK_FILLSIZE);
            }
            if (newInfo.showDown > 0) {
                drawDownloadIndicator(gcImg, iXPos, iYPos, newInfo.showDown == SHOW_SMALL);
            }
            if (newInfo.showUp > 0) {
                drawUploadIndicator(gcImg, iXPos, iYPos, newInfo.showUp == SHOW_SMALL);
            }
            if (newInfo.availNum != -1) {
                if (minAvailability == newInfo.availNum) {
                    gcImg.setForeground(blockColors[BLOCKCOLOR_AVAILCOUNT]);
                    gcImg.drawRectangle(iXPos - 1, iYPos - 1, BLOCK_FILLSIZE + 1, BLOCK_FILLSIZE + 1);
                }
                if (minAvailability2 == newInfo.availNum) {
                    gcImg.setLineStyle(SWT.LINE_DOT);
                    gcImg.setForeground(blockColors[BLOCKCOLOR_AVAILCOUNT]);
                    gcImg.drawRectangle(iXPos - 1, iYPos - 1, BLOCK_FILLSIZE + 1, BLOCK_FILLSIZE + 1);
                    gcImg.setLineStyle(SWT.LINE_SOLID);
                }
                String sNumber = String.valueOf(newInfo.availNum);
                Point size = gcImg.stringExtent(sNumber);
                if (newInfo.availNum < 100) {
                    int x = iXPos + (BLOCK_FILLSIZE / 2) - (size.x / 2);
                    int y = iYPos + (BLOCK_FILLSIZE / 2) - (size.y / 2);
                    gcImg.setForeground(blockColors[BLOCKCOLOR_AVAILCOUNT]);
                    gcImg.drawText(sNumber, x, y, true);
                }
            }
            iCol++;
        }
        oldBlockInfo = newBlockInfo;
    } catch (Exception e) {
        Logger.log(new LogEvent(LogIDs.GUI, "drawing piece map", e));
    } finally {
        gcImg.dispose();
    }
    topLabelLHS = MessageText.getString("PiecesView.BlockView.Header", new String[] { "" + iNumCols, "" + (iRow + 1), "" + dm_pieces.length });
    PiecePicker picker = pm.getPiecePicker();
    int seq_info = picker.getSequentialInfo();
    if (seq_info != 0) {
        topLabelLHS += "; seq=" + seq_info;
    }
    String egm_info = picker.getEGMInfo();
    if (egm_info != null) {
        topLabelLHS += "; EGM=" + egm_info;
    }
    updateTopLabel();
    pieceInfoCanvas.redraw();
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker) DiskManager(com.biglybt.core.disk.DiskManager) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) LogEvent(com.biglybt.core.logging.LogEvent) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList) PEPiece(com.biglybt.core.peer.PEPiece) DMPieceMapEntry(com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Aggregations

DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)18 DiskManager (com.biglybt.core.disk.DiskManager)13 PEPeerManager (com.biglybt.core.peer.PEPeerManager)8 PEPiece (com.biglybt.core.peer.PEPiece)8 DownloadManager (com.biglybt.core.download.DownloadManager)6 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)5 LogEvent (com.biglybt.core.logging.LogEvent)5 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)5 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)3 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)3 GC (org.eclipse.swt.graphics.GC)3 Image (org.eclipse.swt.graphics.Image)3 Rectangle (org.eclipse.swt.graphics.Rectangle)3 PEPeer (com.biglybt.core.peer.PEPeer)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)2 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)2 AERunnable (com.biglybt.core.util.AERunnable)2 Download (com.biglybt.pif.download.Download)2 MenuBuildUtils (com.biglybt.ui.swt.MenuBuildUtils)2 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)2