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
   |  
public class DrawingCanvas extends Canvas {
 
	/*
	 * Le contrôleur du canvas
	 */
	private DrawingController 	controller;
	/*
	 * La fiche de manoeuvre
	 */
	private SheetOfManoeuvre 	sheetOfManoeuvre;
	/*
	 * La marge autour du schéma
	 */
	private static final float 	MARGIN = 40;
	/*
	 * L'échelle utilisé pour ce schéma
	 */
	private float              	scale = 1f;
	/*
	 * Les curseurs
	 */
	private Cursor 				arrowCursor;
	private Cursor 				crossCursor;
	/*
	 * Le rectangle de selection
	 */
	private Path               rectangleFeedback;
 
	private Rectangle2D        planBoundsCache;
 
 
 
	public DrawingCanvas(Composite parent, int style, DrawingController controller) {
		super(parent, style);		
		this.controller = controller;
		this.sheetOfManoeuvre = controller.getSheetOfManoeuvre();
		//initialisation des curseurs
		arrowCursor = new Cursor(this.getDisplay(), SWT.CURSOR_ARROW);
	    crossCursor = new Cursor(this.getDisplay(), SWT.CURSOR_CROSS);
		//ajoute des listeners sur la souris
		addMouseListeners();
		//ajoute des listeners sur les touches clavier
		addKeyListener();
		//ajoute un listener sur le focus
		addFocusListener();
		//ajoute des listeners sur le modèle
		addModelListener();
		//ajoute des listeners sur le dessin
		addCanvasListener();
	}
 
	public DrawingCanvas getDrawingCanvas(){
		return this;
	}
 
	/**
         * Ajoute un listener sur la souris
         */
	private void addMouseListeners() {
	}
 
	  /**
           * Ajoute un listener sur les touches du clavier  
           */
	  private void addKeyListener() {
	}
 
	/**
         * Ajoute un listener sur le focus
         */
	private void addFocusListener() {
	}
 
	/**
         * Ajoute un listener pour recevoir les notifications
         * de la fiche de manoeuvre 
         */
	private void addModelListener() {
		//ajoute un listener qui écoute les modifications sur les traits
		sheetOfManoeuvre.getDrawing().addLineListener(new LineListener() {
			public void lineChanged(LineEvent ev) {
				if (getParent() instanceof ScrolledComposite) {
			          ((ScrolledComposite)getParent()).setMinSize(
			              computeSize(SWT.DEFAULT, SWT.DEFAULT));
			        }
				redraw();
			}
		});
		//ajoute un listener qui écoute les selections
		sheetOfManoeuvre.getDrawing().addSelectionListener(new SelectionListener() {
			public void selectionChanged(SelectionEvent ev) {
				redraw();
			}
		});		
	}
 
	/**
         * Ajoute un listener pour recevoir les notifications
         * de la fiche de manoeuvre 
         */
	private void addCanvasListener() {
		//ajoute un listener qui écoute les modifications sur le dessin
		this.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {					
				DrawingCanvas.this.paintControl(event.gc);
			}
		});
	    this.addDisposeListener(new DisposeListener () {
	        public void widgetDisposed(DisposeEvent e) {
	          if (rectangleFeedback != null) {
	            rectangleFeedback.dispose();
	          }
	          arrowCursor.dispose();
	          crossCursor.dispose();
	        }
	      });
	}
 
 
 
	/**
         * Dessine le canvas
         */
	private void paintControl(GC gc) {
		paintBackground(gc);
		gc.setAntialias(SWT.ON);
		// dessine les composants
		paintLines(gc);
		paintRectangleFeedback(gc);
 
	}
 
	/**
         * Remplit le fond avec la couleur du systeme
         */
	private void paintBackground(GC gc) {
		gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		gc.fillRectangle(0, 0, this.getSize().x,
				this.getSize().y);
	}
 
	/**
         * Renvoie le rectangle englobant le schéma affiché par ce composant
         */
	private Rectangle2D getPlanBounds() {
		if (this.planBoundsCache == null) {
			this.planBoundsCache = new Rectangle2D.Float(0, 0, 1000, 1000);
			for (Line line : sheetOfManoeuvre.getDrawing().getLines()) {
				this.planBoundsCache.add(line.getXStart(), line.getYStart());
				this.planBoundsCache.add(line.getXEnd(), line.getYEnd());
			}
		}
		return this.planBoundsCache;
	}
 
	/**
         * Dessine le rectangle de selection
         */
	private void paintRectangleFeedback(GC gc) {
 
	}
 
	/**
         * Dessine les traits
         */
	private void paintLines(GC gc) {
		//Dessine les traits selectionnés en les surlignants
		List<Object> selectedItems = this.sheetOfManoeuvre.getDrawing().getSelectedItems();
		if (!selectedItems.isEmpty()) {
			gc.setForeground(this.getDisplay().getSystemColor(
					SWT.COLOR_LIST_SELECTION));
			gc.setAlpha(128);
			gc.setLineWidth((int) Math.round(6 / this.scale));
			gc.setLineJoin(SWT.JOIN_ROUND);
			for (Object item : selectedItems) {
				if (item instanceof Line) {
					Line line = (Line)item;
						gc.drawLine(Math.round(line.getXStart()), Math.round(line
							.getYStart()), Math.round(line.getXEnd()), Math
							.round(line.getYEnd()));
				}
			}
		}
		// Dessine les traits
		gc.setForeground(this.getDisplay().getSystemColor(
				SWT.COLOR_LIST_FOREGROUND));
		gc.setAlpha(255);
		gc.setLineWidth((int) Math.round(2 / this.scale));
		gc.setLineJoin(SWT.JOIN_MITER);		
		for (Line line : this.sheetOfManoeuvre.getDrawing().getLines()) {
			gc.drawLine(Math.round(line.getXStart()), Math.round(line
					.getYStart()), Math.round(line.getXEnd()), Math.round(line
					.getYEnd()));
		}
	}
 
	/**
         * Returns the scale used to display the plan.
         */
	public float getScale() {
		return this.scale;
	}
} | 
Partager