use of com.biglybt.core.disk.DiskManager 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();
}
Aggregations