Search in sources :

Example 1 with DiskManagerPiece

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

the class FileInfoView method showPieceDetails.

protected void showPieceDetails(int x, int y) {
    int piece_number = getPieceNumber(x, y);
    if (piece_number >= 0) {
        DownloadManager download_manager = file.getDownloadManager();
        if (download_manager == null) {
            topLabel.setText("");
            return;
        }
        DiskManager disk_manager = download_manager.getDiskManager();
        PEPeerManager peer_manager = download_manager.getPeerManager();
        if (disk_manager == null || peer_manager == null) {
            topLabel.setText("");
            return;
        }
        DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
        PEPiece[] pe_pieces = peer_manager.getPieces();
        DiskManagerPiece dm_piece = dm_pieces[piece_number];
        PEPiece pe_piece = pe_pieces[piece_number];
        String text = "Piece " + piece_number + ": " + dm_piece.getString();
        if (pe_piece != null) {
            text += ", active: " + pe_piece.getString();
        } else {
            if (dm_piece.isNeeded() && !dm_piece.isDone()) {
                text += ", inactive: " + peer_manager.getPiecePicker().getPieceString(piece_number);
            }
        }
        topLabel.setText(text);
    } else {
        topLabel.setText("");
    }
}
Also used : PEPiece(com.biglybt.core.peer.PEPiece) PEPeerManager(com.biglybt.core.peer.PEPeerManager) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager)

Example 2 with DiskManagerPiece

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

the class PeerInfoView method refreshInfoCanvas.

/**
 * Constructs and image representing the download state of _all_
 * the pieces in the torrent.  Particularily slow when there's lots of pieces,
 * and also wasteful since only a fraction of them ever get painted at
 * any given time.
 *
 * TODO: Construct image for visible area only or something
 */
private void refreshInfoCanvas() {
    refreshInfoCanvasQueued = false;
    if (peerInfoComposite == null || peerInfoComposite.isDisposed() || !peerInfoComposite.isVisible()) {
        return;
    }
    peerInfoCanvas.layout(true);
    Rectangle bounds = peerInfoCanvas.getClientArea();
    if (bounds.width <= 0 || bounds.height <= 0)
        return;
    if (img != null && !img.isDisposed()) {
        img.dispose();
        img = null;
    }
    if (peer == null || peer.getPeerState() != PEPeer.TRANSFERING) {
        GC gc = new GC(peerInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        return;
    }
    BitFlags peerHavePieces = peer.getAvailable();
    if (peerHavePieces == null) {
        GC gc = new GC(peerInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        return;
    }
    DiskManagerPiece[] dm_pieces = null;
    PEPeerManager pm = peer.getManager();
    DiskManager dm = pm.getDiskManager();
    dm_pieces = dm.getPieces();
    int iNumCols = bounds.width / BLOCK_SIZE;
    int iNeededHeight = (((dm.getNbPieces() - 1) / iNumCols) + 1) * BLOCK_SIZE;
    if (sc.getMinHeight() != iNeededHeight) {
        sc.setMinHeight(iNeededHeight);
        sc.layout(true, true);
        bounds = peerInfoCanvas.getClientArea();
    }
    img = new Image(peerInfoCanvas.getDisplay(), bounds.width, iNeededHeight);
    GC gcImg = new GC(img);
    try {
        // use advanced capabilities for faster drawText
        gcImg.setAdvanced(true);
        gcImg.setBackground(peerInfoCanvas.getBackground());
        gcImg.fillRectangle(0, 0, bounds.width, iNeededHeight);
        int[] availability = pm.getAvailability();
        int iNextDLPieceID = -1;
        int iDLPieceID = -1;
        int[] ourRequestedPieces = peer.getOutgoingRequestedPieceNumbers();
        if (ourRequestedPieces != null) {
            if (!peer.isChokingMe()) {
                if (ourRequestedPieces.length > 0) {
                    iDLPieceID = ourRequestedPieces[0];
                    if (ourRequestedPieces.length > 1)
                        iNextDLPieceID = ourRequestedPieces[1];
                }
            } else {
                if (ourRequestedPieces.length > 0)
                    iNextDLPieceID = ourRequestedPieces[0];
            }
        // if (iNextDLPieceID == -1) {
        // iNextDLPieceID = peer.getNextPieceNumberGuess();
        // }
        }
        int[] peerRequestedPieces = peer.getIncomingRequestedPieceNumbers();
        if (peerRequestedPieces == null)
            peerRequestedPieces = new int[0];
        int peerNextRequestedPiece = -1;
        if (peerRequestedPieces.length > 0)
            peerNextRequestedPiece = peerRequestedPieces[0];
        Arrays.sort(peerRequestedPieces);
        int iRow = 0;
        int iCol = 0;
        for (int i = 0; i < peerHavePieces.flags.length; i++) {
            int colorIndex;
            boolean done = (dm_pieces == null) ? false : dm_pieces[i].isDone();
            int iXPos = iCol * BLOCK_SIZE;
            int iYPos = iRow * BLOCK_SIZE;
            if (done) {
                if (peerHavePieces.flags[i])
                    colorIndex = BLOCKCOLOR_AVAIL_HAVE;
                else
                    colorIndex = BLOCKCOLOR_NOAVAIL_HAVE;
                gcImg.setBackground(blockColors[colorIndex]);
                gcImg.fillRectangle(iXPos, iYPos, BLOCK_FILLSIZE, BLOCK_FILLSIZE);
            } else {
                // !done
                boolean partiallyDone = (dm_pieces == null) ? false : dm_pieces[i].getNbWritten() > 0;
                int x = iXPos;
                int width = BLOCK_FILLSIZE;
                if (partiallyDone) {
                    if (peerHavePieces.flags[i])
                        colorIndex = BLOCKCOLOR_AVAIL_HAVE;
                    else
                        colorIndex = BLOCKCOLOR_NOAVAIL_HAVE;
                    gcImg.setBackground(blockColors[colorIndex]);
                    // partiallyDone false when dm_pieces null
                    @SuppressWarnings("null") 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;
                    gcImg.fillRectangle(x, iYPos, iNewWidth, BLOCK_FILLSIZE);
                    width -= iNewWidth;
                    x += iNewWidth;
                }
                if (peerHavePieces.flags[i])
                    colorIndex = BLOCKCOLOR_AVAIL_NOHAVE;
                else
                    colorIndex = BLOCKCOLOR_NOAVAIL_NOHAVE;
                gcImg.setBackground(blockColors[colorIndex]);
                gcImg.fillRectangle(x, iYPos, width, BLOCK_FILLSIZE);
            }
            // Down Arrow inside box for "dowloading" piece
            if (i == iDLPieceID) {
                gcImg.setBackground(blockColors[BLOCKCOLOR_TRANSFER]);
                gcImg.fillPolygon(new int[] { iXPos, iYPos, iXPos + BLOCK_FILLSIZE, iYPos, iXPos + (BLOCK_FILLSIZE / 2), iYPos + BLOCK_FILLSIZE });
            }
            // Small Down Arrow inside box for next download piece
            if (i == iNextDLPieceID) {
                gcImg.setBackground(blockColors[BLOCKCOLOR_NEXT]);
                gcImg.fillPolygon(new int[] { iXPos + 2, iYPos + 2, iXPos + BLOCK_FILLSIZE - 1, iYPos + 2, iXPos + (BLOCK_FILLSIZE / 2), iYPos + BLOCK_FILLSIZE - 1 });
            }
            // Up Arrow in uploading piece
            if (i == peerNextRequestedPiece) {
                gcImg.setBackground(blockColors[BLOCKCOLOR_TRANSFER]);
                gcImg.fillPolygon(new int[] { iXPos, iYPos + BLOCK_FILLSIZE, iXPos + BLOCK_FILLSIZE, iYPos + BLOCK_FILLSIZE, iXPos + (BLOCK_FILLSIZE / 2), iYPos });
            } else if (Arrays.binarySearch(peerRequestedPieces, i) >= 0) {
                // Small Up Arrow each upload request
                gcImg.setBackground(blockColors[BLOCKCOLOR_NEXT]);
                gcImg.fillPolygon(new int[] { iXPos + 1, iYPos + BLOCK_FILLSIZE - 2, iXPos + BLOCK_FILLSIZE - 2, iYPos + BLOCK_FILLSIZE - 2, iXPos + (BLOCK_FILLSIZE / 2), iYPos + 2 });
            }
            if (availability != null && availability[i] < 10) {
                gcImg.setFont(font);
                String sNumber = String.valueOf(availability[i]);
                Point size = gcImg.stringExtent(sNumber);
                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++;
            if (iCol >= iNumCols) {
                iCol = 0;
                iRow++;
            }
        }
    } catch (Exception e) {
        Logger.log(new LogEvent(LogIDs.GUI, "drawing piece map", e));
    } finally {
        gcImg.dispose();
    }
    peerInfoCanvas.redraw();
}
Also used : BitFlags(com.biglybt.core.peermanager.piecepicker.util.BitFlags) LogEvent(com.biglybt.core.logging.LogEvent) DiskManager(com.biglybt.core.disk.DiskManager) PEPeerManager(com.biglybt.core.peer.PEPeerManager) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece)

Example 3 with DiskManagerPiece

use of com.biglybt.core.disk.DiskManagerPiece 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 4 with DiskManagerPiece

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

the class RemainingPiecesItem method refresh.

@Override
public void refresh(TableCell cell) {
    DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) cell.getDataSource();
    // dm may be null if this is a skeleton file view
    DiskManager dm = fileInfo == null ? null : fileInfo.getDiskManager();
    int remaining = 0;
    if (fileInfo != null && dm != null) {
        if (fileInfo instanceof FilesView.FilesViewTreeNode && !((FilesView.FilesViewTreeNode) fileInfo).isLeaf()) {
            remaining = -1;
        } else {
            int start = fileInfo.getFirstPieceNumber();
            int end = start + fileInfo.getNbPieces();
            DiskManagerPiece[] pieces = dm.getPieces();
            for (int i = start; i < end; i++) {
                if (!pieces[i].isDone())
                    remaining++;
            }
        }
    } else {
        // unknown
        remaining = -1;
    }
    if (!cell.setSortValue(remaining) && cell.isValid()) {
        return;
    }
    cell.setText("" + (remaining < 0 ? "" : ("" + remaining)));
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) FilesView(com.biglybt.ui.swt.views.FilesView) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManager(com.biglybt.core.disk.DiskManager)

Example 5 with DiskManagerPiece

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

the class PiecesItem method refresh.

@Override
public void refresh(TableCell cell) {
    /* Notes:
		 * We store our image and imageBufer in DownloadManager using
		 * setData & getData.
		 */
    // Named infoObj so code can be copied easily to the other PiecesItem
    DownloadManager infoObj = (DownloadManager) cell.getDataSource();
    long lCompleted = (infoObj == null) ? 0 : infoObj.getStats().getCompleted();
    boolean bForce = infoObj != null && infoObj.getUserData("PiecesImage") == null;
    if (!cell.setSortValue(lCompleted) && cell.isValid() && !bForce) {
        return;
    }
    if (infoObj == null)
        return;
    // Compute bounds ...
    int newWidth = cell.getWidth();
    if (newWidth <= 0)
        return;
    int newHeight = cell.getHeight();
    int x0 = borderVerticalSize;
    int x1 = newWidth - 1 - borderVerticalSize;
    int y0 = completionHeight + borderHorizontalSize + borderSplit;
    int y1 = newHeight - 1 - borderHorizontalSize;
    int drawWidth = x1 - x0 + 1;
    if (drawWidth < 10 || y1 < 3)
        return;
    boolean bImageBufferValid = true;
    int[] imageBuffer = (int[]) infoObj.getUserData("PiecesImageBuffer");
    if (imageBuffer == null || imageBuffer.length != drawWidth) {
        imageBuffer = new int[drawWidth];
        bImageBufferValid = false;
    }
    Image image = (Image) infoObj.getUserData("PiecesImage");
    GC gcImage;
    boolean bImageChanged;
    Rectangle imageBounds;
    if (image == null || image.isDisposed()) {
        bImageChanged = true;
    } else {
        imageBounds = image.getBounds();
        bImageChanged = imageBounds.width != newWidth || imageBounds.height != newHeight;
    }
    if (bImageChanged) {
        if (image != null && !image.isDisposed()) {
            image.dispose();
        }
        image = new Image(Utils.getDisplay(), newWidth, newHeight);
        imageBounds = image.getBounds();
        bImageBufferValid = false;
        // draw border
        gcImage = new GC(image);
        gcImage.setForeground(Colors.grey);
        if (borderHorizontalSize > 0) {
            if (borderVerticalSize > 0) {
                gcImage.drawRectangle(0, 0, newWidth - 1, newHeight - 1);
            } else {
                gcImage.drawLine(0, 0, newWidth - 1, 0);
                gcImage.drawLine(0, newHeight - 1, newWidth - 1, newHeight - 1);
            }
        } else if (borderVerticalSize > 0) {
            gcImage.drawLine(0, 0, 0, newHeight - 1);
            gcImage.drawLine(newWidth - 1, 0, newWidth - 1, newHeight - 1);
        }
        if (borderSplit > 0) {
            gcImage.setForeground(Colors.white);
            gcImage.drawLine(x0, completionHeight + borderHorizontalSize, x1, completionHeight + borderHorizontalSize);
        }
    } else {
        gcImage = new GC(image);
    }
    DiskManager disk_manager = infoObj.getDiskManager();
    DiskManagerPiece[] pieces = disk_manager == null ? null : disk_manager.getPieces();
    int nbPieces = infoObj.getNbPieces();
    try {
        int nbComplete = 0;
        int a0;
        int a1 = 0;
        for (int i = 0; i < drawWidth; i++) {
            if (i == 0) {
                // always start out with one piece
                a0 = 0;
                a1 = nbPieces / drawWidth;
                if (a1 == 0)
                    a1 = 1;
            } else {
                // the last iteration, a1 will be nbPieces
                a0 = a1;
                a1 = ((i + 1) * nbPieces) / (drawWidth);
            }
            int index;
            if (a1 <= a0) {
                index = imageBuffer[i - 1];
            } else {
                int nbAvailable = 0;
                for (int j = a0; j < a1; j++) if (pieces != null && pieces[j].isDone())
                    nbAvailable++;
                nbComplete += nbAvailable;
                index = (nbAvailable * Colors.BLUES_DARKEST) / (a1 - a0);
            // System.out.println("i="+i+";nbAvailable="+nbAvailable+";nbComplete="+nbComplete+";nbPieces="+nbPieces+";a0="+a0+";a1="+a1);
            }
            if (!bImageBufferValid || imageBuffer[i] != index) {
                imageBuffer[i] = index;
                bImageChanged = true;
                gcImage.setForeground(index == INDEX_COLOR_NONEAVAIL ? Colors.red : Colors.blues[index]);
                gcImage.drawLine(i + x0, y0, i + x0, y1);
            }
        }
        // pieces can sometimes be 0 due to timing or bad torrent (well, there's a bug with a /0 error
        // so it can happen somehow :)
        int limit = nbPieces == 0 ? 0 : ((drawWidth * nbComplete) / nbPieces);
        if (limit < drawWidth) {
            gcImage.setBackground(Colors.blues[Colors.BLUES_LIGHTEST]);
            gcImage.fillRectangle(limit + x0, borderHorizontalSize, x1 - limit, completionHeight);
        }
        gcImage.setBackground(Colors.colorProgressBar);
        gcImage.fillRectangle(x0, borderHorizontalSize, limit, completionHeight);
    } catch (Exception e) {
        System.out.println("Error Drawing PiecesItem");
        Debug.printStackTrace(e);
    }
    gcImage.dispose();
    Image oldImage = null;
    Graphic graphic = cell.getGraphic();
    if (graphic instanceof UISWTGraphic) {
        oldImage = ((UISWTGraphic) graphic).getImage();
    }
    if (bImageChanged || image != oldImage || !cell.isValid()) {
        if (cell instanceof TableCellSWT) {
            ((TableCellSWT) cell).setGraphic(image);
        } else {
            cell.setGraphic(new UISWTGraphicImpl(image));
        }
        if (bImageChanged) {
            cell.invalidate();
        }
        infoObj.setUserData("PiecesImage", image);
        infoObj.setUserData("PiecesImageBuffer", imageBuffer);
    }
}
Also used : TableCellSWT(com.biglybt.ui.swt.views.table.TableCellSWT) Graphic(com.biglybt.pif.ui.Graphic) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager) UISWTGraphicImpl(com.biglybt.ui.swt.pifimpl.UISWTGraphicImpl) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) GC(org.eclipse.swt.graphics.GC)

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