View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * Vectomatic is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.client.rep.controller;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.vectomatic.client.rep.ClipBoard;
24  import org.vectomatic.client.rep.RepApplication;
25  import org.vectomatic.client.rep.command.PasteCommand;
26  import org.vectomatic.client.rep.events.IClipBoardListener;
27  import org.vectomatic.client.rep.view.DrawingView;
28  import org.vectomatic.common.model.CloneShapeVisitor;
29  import org.vectomatic.common.model.Shape;
30  import org.vectomatic.common.model.geometry.Point;
31  
32  /**
33   * Controller to respond to paste requests and turn
34   * them into PasteCommand
35   */
36  public class PasteController extends ControllerBase implements IClipBoardListener {
37  	private ControllerMenuItem _pasteMenuItem;
38  	private CloneShapeVisitor _cloner;
39  	private int _pasteCount;
40  	
41  	public PasteController(RepApplication app) {
42  		super(app);
43  		_app.getClipboard().addShapeSelectionListener(this);
44  		_pasteMenuItem = new ControllerMenuItem(_app.getView(), app.getConstants().pasteCommand(), this);
45  		_cloner = new CloneShapeVisitor();
46  		clipBoardChanged(_app.getClipboard());
47  	}
48  	
49  	@Override
50  	public void activate(DrawingView view) {
51  		_pasteCount++;
52  		// Clone the clipboard shapes
53  		List<Shape> clipBoardShapes = (List<Shape>)_app.getClipboard().getContent();
54  		List<Shape> clonedShapes = new ArrayList<Shape>();
55  		float d = view.convertToReferenceLength(5);
56  		Point p1 = new Point();
57  		Point p2 = new Point(_pasteCount * d, _pasteCount * d);
58  		for (int i = 0, size = clipBoardShapes.size(); i < size; i++) {
59  			Shape shape = clipBoardShapes.get(i);
60  			shape.acceptVisitor(_cloner);
61  			Shape clonedShape = _cloner.getClone();
62  			// Move the clone slightly with respect to the original
63  			// so that the 2 do not exactly overlap
64  			clonedShape.setTranslation(clonedShape.getTranslation(p1).add(p2));
65  			clonedShapes.add(clonedShape);
66  		}
67  		// Create a new paste command
68  		PasteCommand pasteCommand = new PasteCommand(_app, clonedShapes);
69  		
70  		// Execute the command to add the shapes from the model
71  		pasteCommand.execute();
72  		_app.getHistory().addCommand(pasteCommand);
73  	}
74  	
75  	public ControllerMenuItem getPasteMenuItem() {
76  		return _pasteMenuItem;
77  	}
78  
79  	public void clipBoardChanged(ClipBoard clipBoard) {
80  		_pasteMenuItem.setEnabled(_app.getClipboard().getContent() != null);
81  		_pasteCount = 0;
82  	}
83  
84  }