Created
May 6, 2011 17:49
-
-
Save turesheim/959423 to your computer and use it in GitHub Desktop.
Animated GIF in JFace TableViewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Data { | |
boolean busy; | |
String firstName; | |
String lastName; | |
public Data(String firstName, String lastName, boolean busy) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.busy = busy; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TableViewerAnimation { | |
/** | |
* The label provider that will animate the GIF. | |
*/ | |
private class MyLabelProvider extends LabelProvider implements | |
ITableLabelProvider { | |
/** | |
* The thread that will do the animation. | |
*/ | |
private class TableImageUpdater extends Thread { | |
/** Items to update */ | |
private final ArrayList<TableItem> list = new ArrayList<TableItem>(); | |
/** The current image number */ | |
private int progress = 0; | |
@Override | |
public void run() { | |
while (true) { | |
long currentTime = System.currentTimeMillis(); | |
while (currentTime + delay[progress] * 10 > System | |
.currentTimeMillis()) { | |
try { | |
Thread.sleep(delay[progress]); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
Display.getDefault().asyncExec(new Runnable() { | |
@Override | |
public void run() { | |
progress++; | |
if (progress >= images.length) { | |
progress = 0; | |
} | |
for (TableItem item : list) { | |
Data d = (Data) item.getData(); | |
if (d.busy) { | |
item.setImage(images[progress]); | |
} | |
} | |
} | |
}); | |
} | |
} | |
} | |
/** The delay between images */ | |
int[] delay; | |
/** The progress images */ | |
Image[] images; | |
/** Thread to update images */ | |
private TableImageUpdater updater; | |
public MyLabelProvider() { | |
initializeImages(); | |
installPainter(); | |
} | |
public Image getColumnImage(Object element, int columnIndex) { | |
if (columnIndex == 0 && ((Data) element).busy) { | |
return images[0]; | |
} | |
return null; | |
} | |
public String getColumnText(Object element, int columnIndex) { | |
Data d = (Data) element; | |
switch (columnIndex) { | |
case 0: | |
return d.firstName; | |
case 1: | |
return d.lastName; | |
} | |
return ""; | |
} | |
private void initializeImages() { | |
ImageLoader loader = new ImageLoader(); | |
loader.load(getClass().getResourceAsStream("PROGRESS.gif")); | |
images = new Image[loader.data.length]; | |
delay = new int[loader.data.length]; | |
for (int x = 0; x < images.length; x++) { | |
ImageData imageData = loader.data[x]; | |
delay[x] = loader.data[x].delayTime; | |
images[x] = new Image(Display.getCurrent(), imageData); | |
} | |
} | |
private void installPainter() { | |
updater = new TableImageUpdater(); | |
updater.start(); | |
// Install the updater on each new item in the table as soon as the | |
// data is set. | |
viewer.getTable().addListener(SWT.SetData, new Listener() { | |
public void handleEvent(Event event) { | |
TableItem item = (TableItem) event.item; | |
updater.list.add(item); | |
} | |
}); | |
} | |
} | |
public static void main(String[] args) { | |
new TableViewerAnimation().run(); | |
} | |
private final Shell shell; | |
private final TableViewer viewer; | |
public TableViewerAnimation() { | |
super(); | |
shell = new Shell(Display.getDefault(), SWT.SHELL_TRIM); | |
shell.setText("Famous generals"); | |
shell.setLayout(new FillLayout()); | |
// We use SWT.VIRTUAL here so that the SWT.SetData event will be called. | |
Table table = new Table(shell, SWT.BORDER | SWT.SINGLE | |
| SWT.FULL_SELECTION | SWT.VIRTUAL); | |
table.setHeaderVisible(true); | |
TableColumn column = new TableColumn(table, SWT.LEFT); | |
column.setText("First Name"); | |
column.setWidth(80); | |
column = new TableColumn(table, SWT.LEFT); | |
column.setText("Last Name"); | |
column.setWidth(180); | |
viewer = new TableViewer(table); | |
viewer.setContentProvider(new ArrayContentProvider()); | |
viewer.setLabelProvider(new MyLabelProvider()); | |
shell.pack(); | |
} | |
public void run() { | |
Display display = Display.getCurrent(); | |
shell.pack(); | |
shell.open(); | |
Data[] data = new Data[] { new Data("Hannibal", "Barca", false), | |
new Data("Alexander", "The Great", true), | |
new Data("Julius", "Caesar", false), | |
new Data("Napoleon", "Bonaparte", true), | |
new Data("Sun", "Tzu", false) }; | |
viewer.setInput(data); | |
while (!shell.isDisposed()) | |
if (!display.readAndDispatch()) | |
display.sleep(); | |
display.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment