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
|
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
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.Label;
import org.eclipse.swt.widgets.Widget;
public class PlanningComposite extends Composite 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();
//Rectangle clientRect = getClientArea();
//this.setSize(clientRect.width, clientRect.height);
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);
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 mouseDoubleClick(MouseEvent e) {
}
@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);
}
}
}
@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.keyCode) {
case SWT.ARROW_LEFT:
selectCell(selectedCellIndex - 1);
break;
case SWT.ARROW_RIGHT:
selectCell(selectedCellIndex + 1);
break;
case SWT.ARROW_UP:
selectCell(selectedCellIndex - 6);
break;
case SWT.ARROW_DOWN:
selectCell(selectedCellIndex + 6);
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
} |
Partager