Search in sources :

Example 11 with PiecePicker

use of com.biglybt.core.peermanager.piecepicker.PiecePicker 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

PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)11 PEPeerManager (com.biglybt.core.peer.PEPeerManager)7 PEPiece (com.biglybt.core.peer.PEPiece)7 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)5 DiskManager (com.biglybt.core.disk.DiskManager)4 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)4 DownloadManager (com.biglybt.core.download.DownloadManager)4 LogEvent (com.biglybt.core.logging.LogEvent)3 AERunnable (com.biglybt.core.util.AERunnable)3 MenuBuildUtils (com.biglybt.ui.swt.MenuBuildUtils)3 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)2 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)2 PEPeer (com.biglybt.core.peer.PEPeer)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)2 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)2 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)2 Color (org.eclipse.swt.graphics.Color)2 GC (org.eclipse.swt.graphics.GC)2 Image (org.eclipse.swt.graphics.Image)2 GridData (org.eclipse.swt.layout.GridData)2