1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.TreeItem;
import internal.model.Resource;
import views.recomposition.model.RecompositionUE;
public class RecompositionSizeCellLabelProvider extends CellLabelProvider {
/**
* Constructeur de la classe <code>RecompositionSizeCellLabelProvider</code>.
*/
public RecompositionSizeCellLabelProvider() {
super();
}
@Override
public void update(final ViewerCell cell) {
final TreeItem item = (TreeItem) cell.getItem();
Resource res = (Resource) cell.getElement();
DisposeListener listener = new DisposeListener() {
public void widgetDisposed(final DisposeEvent e) {
if (item.getData("EDITOR") != null) {
TreeEditor editor = (TreeEditor) item.getData("EDITOR");
editor.getEditor().dispose();
editor.dispose();
}
}
};
if (item.getData("EDITOR") != null) {
TreeEditor editor = (TreeEditor) item.getData("EDITOR");
editor.getEditor().dispose();
editor.dispose();
}
if (item.getData("DISPOSELISTNER") != null) {
item.removeDisposeListener((DisposeListener) item.getData("DISPOSELISTNER"));
}
TreeEditor editor = new TreeEditor(item.getParent());
item.setData("EDITOR", editor);
Composite comp = new Composite(item.getParent(), SWT.NONE);
comp.setBackground(item.getParent().getBackground());
comp.setBackgroundMode(SWT.INHERIT_DEFAULT);
comp.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true, 1, 1));
// Layout
GridLayout gl = new GridLayout(1, true);
gl.marginHeight = 0;
gl.marginWidth = 0;
gl.marginTop = 0;
gl.marginBottom = 0;
comp.setLayout(gl);
createEditor(comp, res);
editor.grabHorizontal = true;
editor.setEditor(comp, item, 1);
item.addDisposeListener(listener);
item.setData("DISPOSELISTNER", listener);
}
/**
* La méthode <code>createEditor</code> crée le contenu pour la resource spécifiée.
* @param parent : Composite Parent
* @param res : resource (fichier ou répertoire ou media)
*/
private void createEditor(final Composite parent, final Resource res) {
if (res != null) {
if (res instanceof RecompositionUE) {
createUeEditor(parent, (RecompositionUE) res);
} else {
createOtherEditor(parent, res);
}
}
}
/**
* La méthode <code>createUeEditor</code> crée le contenu pour l'UE (progressBar).
* @param parent : Composite Parent
* @param ue : Media a recomposition
*/
private void createUeEditor(final Composite parent, final RecompositionUE ue) {
final ProgressBar progressBar = new ProgressBar(parent, SWT.HORIZONTAL);
// Affichage du text sur la barre de progression
progressBar.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent e) {
float sel = progressBar.getSelection();
float max = progressBar.getMaximum();
String string = String.format("%.1f%s", sel / max * 100., "%");
Point point = progressBar.getSize();
FontMetrics fontMetrics = e.gc.getFontMetrics();
int width = fontMetrics.getAverageCharWidth() * string.length();
int height = fontMetrics.getHeight();
e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
e.gc.drawString(string, (point.x - width) / 2, (point.y - height) / 2, true);
}
});
progressBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
int max = (int) (ue.getType().getCapacite() / 1048576.0);
int sel = (int) (ue.getSize());
progressBar.setMaximum(max);
progressBar.setSelection(sel);
}
/**
* La méthode <code>createOtherEditor</code> crée le contenu pour tous les autres cas.
* @param parent : Composite Parent
* @param res : resource (fichier ou repertoire)
*/
private void createOtherEditor(final Composite parent, final Resource res) {
Label label = new Label(parent, SWT.NONE);
label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
label.setText(String.format("%.2f", res.getSize()));
}
} |
Partager