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
| public class PlanningComposite extends ScrollableComposite implements MouseListener, FocusListener, KeyListener{
private Color activeSelectionBackground;
private Color inactiveSelectionBackground;
private Color activeSelectionForeground;
private Color inactiveSelectionForeground;
private static final int NUMBER_DAYS_PER_WEEK = 5;
private static final int NUMBER_AGENT = 6;
private static final String[] dayTitles= {"","lundi","mardi","mercredi","jeudi","vendredi"};
private PlanningCell[] pCells;
private PlanningCell selectedCell;
private int selectedCellIndex;
public PlanningComposite(Composite parent, int style) {
super(parent, style);
this.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
activeSelectionBackground = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
inactiveSelectionBackground = getBackground();
activeSelectionForeground = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
inactiveSelectionForeground = getForeground();
GridLayout gridLayout = new GridLayout();
gridLayout.makeColumnsEqualWidth = true;
gridLayout.numColumns = NUMBER_DAYS_PER_WEEK + 1;
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
setLayout(gridLayout);
drawHeader();
drawCells();
addMouseListener(this);
addFocusListener(this);
addKeyListener(this);
}
private void drawHeader() {
for (int i=0;i < NUMBER_DAYS_PER_WEEK + 1;i++){
Label label = new Label(this, SWT.CENTER);
label.setText(dayTitles[i]);
label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
}
}
private void drawCells() {
int nbOfCells = (NUMBER_DAYS_PER_WEEK +1) * NUMBER_AGENT;
pCells = new PlanningCell[nbOfCells];
for (int i=0;i < nbOfCells;i++){
//PlanningCell cell = new PlanningCell(this,SWT.BORDER);
FormColors formColor = new FormColors( Display.getCurrent());
FormToolkit formToolkit = new FormToolkit( formColor);
Composite composite = formToolkit.createComposite(this,SWT.BORDER);
PlanningCell cell = (PlanningCell) composite;
cell.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
pCells[i] = cell;
cell.addMouseListener(this);
cell.addKeyListener(this);
}
}
private int findCell(Widget cellComposite) {
for (int i = 0; i < pCells.length; i++) {
if (pCells[i] == cellComposite) {
selectedCellIndex = i;
return i;
}
}
return -1;
}
private void selectCell(int index) {
//On sort de la méthode si la cellule n'est pas selectionnable
if (index <=0 || index >= (NUMBER_DAYS_PER_WEEK +1) * NUMBER_AGENT
|| index != 0 && ( index % 6 == 0 ) ){
return;
};
//Déselection de la cellule selectionné précédemment
if (selectedCell != null){
selectedCell.setBackground(getBackground());
selectedCell.setForeground(getBackground());
}
//Sélection de la cellule
selectedCell = pCells[index];
selectedCellIndex = index;
selectedCell.setBackground(getSelectionBackgroundColor());
selectedCell.setForeground(getSelectionForegroundColor());
}
private Color getSelectionBackgroundColor() {
return isFocusControl() ? activeSelectionBackground : inactiveSelectionBackground;
}
private Color getSelectionForegroundColor() {
return isFocusControl() ? activeSelectionForeground : inactiveSelectionForeground;
}
@Override
public boolean isFocusControl() {
for (Control control = getDisplay().getFocusControl(); control != null; control = control.getParent()) {
if (control == this) {
return true;
}
}
return false;
}
@Override
public void focusGained(FocusEvent e) {
selectedCell.setBackground(getSelectionBackgroundColor());
selectedCell.setForeground(getSelectionForegroundColor());
}
@Override
public void focusLost(FocusEvent e) {
selectedCell.setBackground(getSelectionBackgroundColor());
selectedCell.setForeground(getSelectionForegroundColor());
}
@Override
public void mouseDown(MouseEvent e) {
if (e.button == 1) { // Left click
setFocus();
if (e.widget instanceof PlanningCell) {
int index = findCell(e.widget);
selectCell(index);
}
}
}
// encore du code ici
} |
Partager