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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ExpandEvent;
import org.eclipse.swt.events.ExpandListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Shell;
public class Test5 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
new Test5(shell);
shell.setMaximized(true);
shell.setLayout(new FillLayout());
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private ScrolledComposite scrolledComp = null;
// Ces variables permettent de gérer les niveaux de navigation
private ArrayList<ExpandBar> expandBarreTab = new ArrayList<ExpandBar>();
private ArrayList<Composite> compositeTab = new ArrayList<Composite>();
private ArrayList<ExpandItem> expandItemTab = new ArrayList<ExpandItem>();
private int position = 0;
public Test5(final Composite parent) {
// initialisation
scrolledComp = new ScrolledComposite(parent, SWT.V_SCROLL);
scrolledComp.setData(0);
createBar(0, 0, "Test1");
createBar(1, 0, "Test11");
createBar(1, 0, "Test12");
createBar(2, 2, "Test121");
createBar(2, 2, "Test122");
createBar(2, 2, "Test123");
createBar(1, 0, "Test13");
createBar(0, 0, "Test2");
createBar(0, 0, "Test3");
createBar(0, 0, "Test4");
createBar(0, 0, "Test5");
createBar(0, 0, "Test6");
createBar(1, 11, "Test61");
createBar(1, 11, "Test62");
createBar(2, 13, "Test621");
createBar(2, 13, "Test622");
createBar(2, 13, "Test623");
createBar(3, 16, "Test6231");
createBar(3, 16, "Test6231");
createBar(0, 11, "Test63");
// Mise en place des Listeners liés à la barre de défilement
for (int i = 0; i < expandBarreTab.size(); i++) {
position = i;
final int Tprincipal = compositeTab.get(position).computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
expandItemTab.get(position).setHeight(Tprincipal);
expandBarreTab.get(position).addExpandListener(
new ExpandListener() {
public void itemCollapsed(ExpandEvent event) {
// expandItemTab.get(position).setHeight(Tprincipal);
}
public void itemExpanded(ExpandEvent event) {
String eventString = event.item.toString();
eventString = eventString.substring(12, eventString.length() - 1);
int positionEvent = itemPosition(eventString);
int positionParent = 0;
int newSize = compositeTab.get(positionEvent).computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
Composite parcours = expandBarreTab.get(positionEvent).getParent();
while (!(parcours.getParent() instanceof org.eclipse.swt.widgets.Shell)) {
positionParent = (Integer) expandBarreTab.get(positionEvent).getParent().getData();
System.out.println(expandItemTab.get(positionParent).getText() + ": " + (compositeTab.get(positionEvent) .computeSize(SWT.DEFAULT, SWT.DEFAULT).y) + " - " + newSize);
expandItemTab.get(positionEvent).setHeight(compositeTab.get(positionParent).computeSize(SWT.DEFAULT,SWT.DEFAULT).y + newSize);
compositeTab.get(positionEvent).layout(true, true);
// Incrémentation - on remonte l'arbre
positionEvent = (Integer) expandBarreTab
.get(positionEvent).getParent()
.getData();
parcours = parcours.getParent();
}
System.out.println();
}
});
}
scrolledComp.setContent(expandBarreTab.get(0));
scrolledComp.setExpandHorizontal(true);
scrolledComp.setExpandVertical(true);
scrolledComp.setMinSize(expandBarreTab.get(0).computeSize(SWT.DEFAULT,
SWT.DEFAULT));
/* Jump to the end */
scrolledComp.setOrigin(0, scrolledComp.getSize().y);
}
/**
* Permet de créer une nouvelle Bar
* @param level niveau de positionnement de la Bar
* @param parentPosition emplacement dans le tableau des Bar du parent
* @param tabName nom de la Bar
*/
public void createBar(int level, int parentPosition, String tabName) {
// Création d'une nouvelle expandBarre et mise en forme
if (level == 0 && parentPosition == 0) {
expandBarreTab.add(new ExpandBar(scrolledComp, SWT.NONE));
expandBarreTab.get(level).setSpacing(4);
} else {
expandBarreTab.add(new ExpandBar(compositeTab.get(parentPosition),SWT.NONE));
expandBarreTab.get(expandBarreTab.size() - 1).setLayoutData( new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1));
expandBarreTab.get(expandBarreTab.size() - 1).setSpacing(4);
}
position = expandBarreTab.size() - 1;
// Mise en place des composants
if (level == 0 && parentPosition == 0) {
compositeTab
.add(new Composite(expandBarreTab.get(level), SWT.NONE));
compositeTab.get(position).setLayout(new GridLayout(2, true));
compositeTab.get(position).setData(position);
expandItemTab.add(new ExpandItem(expandBarreTab.get(level),
SWT.NONE, 0));
expandItemTab.get(position).setText(tabName);
expandItemTab.get(position).setControl(compositeTab.get(position));
} else {
compositeTab.add(new Composite(expandBarreTab.get(position),
SWT.NONE));
compositeTab.get(position).setLayout(new GridLayout(2, true));
compositeTab.get(position).setData(position);
expandItemTab.add(new ExpandItem(expandBarreTab.get(position),
SWT.NONE, 0));
expandItemTab.get(position).setText(tabName);
expandItemTab.get(position).setControl(compositeTab.get(position));
}
}
/**
* Permet de savoir le nombre d'enfants que possède un composite
*
* @param composite
* composite parent
* @return int nombre d'enfants
*/
public int numberOfChildrens(Composite composite) {
int result = 0;
Control[] children = composite.getChildren();
for (int i = 0; i < children.length; i++) {
result++;
}
return result;
}
public int itemPosition(String name) {
int result = -1;
for (int i = 0; i < expandItemTab.size(); i++) {
if (name.equals(expandItemTab.get(i).getText())) {
result = i;
}
}
return result;
}
} |
Partager