package net.mzalewski.goblin.gui.tabs;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TransfersTab {
final private CTabFolder tabFolder;
private CTabItem tab;
private Table downloadsTable;
private static TransfersTab ref;
private TransfersTab(CTabFolder tabFolder) {
this.tabFolder = tabFolder;
newTransfersTab();
}
/**
* Get instance of TransfersTab
* @param tabFolder CTabFolder which have a transfers tab
* @return reference reference for TransfersTab
*/
public static synchronized TransfersTab getInstance(CTabFolder tabFolder) {
if (ref == null)
ref = new TransfersTab(tabFolder);
return ref;
}
/** Create new TransfersTab */
private void newTransfersTab() {
tab = new CTabItem(tabFolder, SWT.CLOSE, 0);
tab.setText("Transfery");
downloadsTable = new Table(tabFolder, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
downloadsTable.setHeaderVisible(true);
String[] titles = {"Nr", "Nazwa", "Rozmiar", "Pozosta�o", "Zako�czone", "Pr�dko��"};
for (int i = 0; i < titles.length; ++i) {
TableColumn column = new TableColumn(downloadsTable, SWT.NONE);
column.setText(titles[i]);
}
for (int i = 0; i < titles.length; ++i)
downloadsTable.getColumn(i).pack();
tab.setControl(downloadsTable);
}
/**
* Add new transfer
* @param name name of file
* @param size size of file
* @param remaining remaining to complete download
* @param percent percent of downloaded
* @param speed current download speed
*/
public void addTransfer(String name, String size, String remaining, String percent, String speed) {
TableItem item = new TableItem(downloadsTable, SWT.NONE);
item.setText(0, Integer.toString(downloadsTable.getItemCount()));
item.setText(1, name);
item.setText(2, size);
item.setText(3, remaining);
item.setText(4, percent);
item.setText(5, speed);
}
/**
* Update exists transfer
* @param index index of item to update
* @param remaining remaining to complete download
* @param percent percent of downloaded
* @param speed current download speed
*/
public void updateTransfer(int index, String remaining, String percent, String speed) {
TableItem item = downloadsTable.getItem(index);
item.setText(3, remaining);
item.setText(4, percent);
item.setText(5, speed);
}
/**
* Remove exists transfer
* @param index index of transfer to remove
*/
public void removeTransfer(int index) {
downloadsTable.remove(index);
for (int i = index; i < downloadsTable.getItemCount(); ++i)
downloadsTable.getItem(i).setText(0, Integer.toString(i + 1));
}
/**
* Remove exist transfers
* @param start begin index of transfers to remove
* @param end end index of transfers to remove
*/
public void removeTransfers(int start, int end) {
downloadsTable.remove(start, end);
for (int i = start; i < downloadsTable.getItemCount(); ++i)
downloadsTable.getItem(i).setText(0, Integer.toString(i + 1));
}
/** Show TransfersTab */
public void show() {
if (tab.isDisposed())
newTransfersTab();
tabFolder.setSelection(tab);
}
}