diff options
| author | Simon Rettberg | 2015-07-29 18:25:03 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-07-29 18:25:03 +0200 |
| commit | 0242cebe6cfaa7d3392e58ec7ecd2290e897e0f2 (patch) | |
| tree | 7b9a4f2e18993ff638495553f3c11dd9cd09465d /dozentenmodul/src/main/java/org | |
| parent | [client] first steps for the swingified imagedetailswindow (diff) | |
| download | tutor-module-0242cebe6cfaa7d3392e58ec7ecd2290e897e0f2.tar.gz tutor-module-0242cebe6cfaa7d3392e58ec7ecd2290e897e0f2.tar.xz tutor-module-0242cebe6cfaa7d3392e58ec7ecd2290e897e0f2.zip | |
[client] Ported BlockProgressBar to Swing
Diffstat (limited to 'dozentenmodul/src/main/java/org')
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/BlockProgressBar.java | 133 |
1 files changed, 61 insertions, 72 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/BlockProgressBar.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/BlockProgressBar.java index 762fcd20..4d5c0add 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/BlockProgressBar.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/BlockProgressBar.java @@ -1,20 +1,18 @@ package org.openslx.dozmod.gui.control; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.MouseAdapter; -import org.eclipse.swt.events.MouseEvent; -import org.eclipse.swt.events.PaintEvent; -import org.eclipse.swt.events.PaintListener; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.graphics.Rectangle; -import org.eclipse.swt.widgets.Canvas; -import org.eclipse.swt.widgets.Composite; -import org.openslx.thrifthelper.TransferStatusWrapper; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Rectangle; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.geom.Rectangle2D; + +import javax.swing.JPanel; -public class BlockProgressBar extends Canvas { +import org.openslx.thrifthelper.TransferStatusWrapper; - private final Color white, blue, black; +@SuppressWarnings("serial") +public class BlockProgressBar extends JPanel { private final Color[] blockColors = new Color[5]; @@ -22,119 +20,110 @@ public class BlockProgressBar extends Canvas { private boolean simpleMode = true; - public BlockProgressBar(Composite parent, byte[] blocks) { - super(parent, SWT.NO_BACKGROUND); + public BlockProgressBar(byte[] blocks) { + super(); setupListeners(); - white = getDisplay().getSystemColor(SWT.COLOR_WHITE); - blue = getDisplay().getSystemColor(SWT.COLOR_BLUE); - black = getDisplay().getSystemColor(SWT.COLOR_BLACK); // 0 = complete, 1 = missing, 2 = uploading, 3 = queued for copying, 4 = copying - blockColors[0] = black; - blockColors[1] = getDisplay().getSystemColor(SWT.COLOR_RED); - blockColors[2] = getDisplay().getSystemColor(SWT.COLOR_YELLOW); - blockColors[3] = blue; - blockColors[4] = getDisplay().getSystemColor(SWT.COLOR_GREEN); + blockColors[0] = Color.BLACK; + blockColors[1] = Color.RED; + blockColors[2] = Color.YELLOW; + blockColors[3] = Color.BLUE; + blockColors[4] = Color.GREEN; this.blocks.setBlocks(blocks); } public void setStatus(byte[] blocks) { this.blocks.setBlocks(blocks); - this.redraw(); + this.repaint(100); } private void setupListeners() { final BlockProgressBar me = this; - // Paint listener - this.addPaintListener(new PaintListener() { - @Override - public void paintControl(PaintEvent e) { - me.draw(e); - } - }); - this.addMouseListener(new MouseAdapter() { @Override - public void mouseDoubleClick(MouseEvent e) { - me.toggleMode(); + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + me.toggleMode(); + } } }); } private void toggleMode() { simpleMode = !simpleMode; - this.redraw(); + this.repaint(100); } - private void draw(PaintEvent e) { + @Override + public void paintComponent(Graphics g) { if (blocks.isEmpty()) { // No valid block data, draw white window - e.gc.setBackground(white); - e.gc.fillRectangle(e.x, e.y, e.width, e.height); + g.setColor(Color.WHITE); + g.fillRect(0, 0, getWidth(), getHeight()); } else if (simpleMode) { - drawSimple(e); + drawSimple(g); } else { - drawDetailed(e); + drawDetailed(g); } } - private void drawSimple(PaintEvent e) { - final Rectangle a = this.getClientArea(); - final float width = a.width - 2; + private void drawSimple(Graphics g) { + final float width = getWidth(); + final int height = getHeight(); final float complete = blocks.getComplete(); final float doneWidth = width * complete; - final Rectangle doneRect = new Rectangle(a.x, a.y, (int) doneWidth, a.height); - final Rectangle remainingRect = new Rectangle(a.x + (int) doneWidth, a.y, (int) (width - doneWidth), a.height); - e.gc.setBackground(blue); - e.gc.fillRectangle(doneRect); - e.gc.setBackground(white); - e.gc.fillRectangle(remainingRect); + final Rectangle doneRect = new Rectangle(0, 0, (int) doneWidth, height); + final Rectangle remainingRect = new Rectangle((int) doneWidth, 0, (int) (width - doneWidth), height); + g.setColor(Color.BLUE); + g.fillRect(doneRect.x, doneRect.y, doneRect.width, doneRect.height); + g.setColor(Color.WHITE); + g.fillRect(remainingRect.x, remainingRect.y, remainingRect.width, remainingRect.height); final String progress = (int) (complete * 100) + "%"; - Point textExtent = e.gc.textExtent(progress, SWT.DRAW_TRANSPARENT); - final int textX = a.x + (a.width - textExtent.x) / 2; - final int textY = a.y + (a.height - textExtent.y) / 2; - e.gc.setForeground(white); - e.gc.setClipping(doneRect); - e.gc.drawText(progress, textX, textY, true); - e.gc.setForeground(black); - e.gc.setClipping(remainingRect); - e.gc.drawText(progress, textX, textY, true); + Rectangle2D textExtent = g.getFontMetrics().getStringBounds(progress, g); + final int textX = (int) ((width - textExtent.getWidth()) / 2); + final int textY = (int) ((height - textExtent.getHeight()) / 2); + g.setColor(Color.WHITE); + g.setClip(doneRect.x, doneRect.y, doneRect.width, doneRect.height); + g.drawString(progress, textX, textY); + g.setColor(Color.BLACK); + g.setClip(remainingRect.x, remainingRect.y, remainingRect.width, remainingRect.height); + g.drawString(progress, textX, textY); } - private void drawDetailed(PaintEvent e) { + private void drawDetailed(Graphics g) { final int blockCount = blocks.getBlockCount(); // Calculate display mode - final Rectangle a = this.getClientArea(); - final float width = a.width - 2; + final int height = getHeight(); + final float width = getWidth() - 2; float blockWidth = width / blockCount; int rows = 1; - while (blockWidth * rows < 6 && (float) a.height / rows > 7) { + while (blockWidth * rows < 6 && (float) height / rows > 7) { rows++; } blockWidth *= rows; final float blockHeight; final float blockHeightVisible; if (rows == 1) { - blockHeightVisible = blockHeight = a.height; + blockHeightVisible = blockHeight = height; } else { - blockHeight = (a.height + 2) / rows; + blockHeight = (height + 2) / rows; blockHeightVisible = blockHeight - 2; } // Draw - e.gc.setBackground(white); - e.gc.fillRectangle(a); - float x = a.x, y = a.y; + g.setColor(Color.WHITE); + g.fillRect(0, 0, getWidth(), getHeight()); + float x = 0, y = 0; for (byte block : blocks.getBlocks()) { float toX = x + blockWidth; if (block >= 0 && block < blockColors.length) { - e.gc.setBackground(blockColors[block]); + g.setColor(blockColors[block]); final int boxWidth = ((int) toX) - ((int) x) - 1; final int boxHeight = (int) (y + blockHeightVisible) - (int) y; - e.gc.fillRectangle((int) x, (int) y, boxWidth > 1 ? boxWidth : 1, boxHeight > 1 ? boxHeight - : 1); + g.fillRect((int) x, (int) y, boxWidth > 1 ? boxWidth : 1, boxHeight > 1 ? boxHeight : 1); } x = toX; - if (x + 0.5 > a.width) { - x = a.x; + if (x + 0.5 > width) { + x = 0; y += blockHeight; } } |
