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.RepApplication;
24  import org.vectomatic.client.rep.events.IShapeSelectionListener;
25  import org.vectomatic.client.rep.view.DrawingView;
26  import org.vectomatic.common.model.CloneShapeVisitor;
27  import org.vectomatic.common.model.Shape;
28  
29  /**
30   * Controller to respond to copy request
31   */
32  public class CopyController extends ControllerBase implements IShapeSelectionListener {
33  	private ControllerMenuItem _copyMenuItem;
34  	private CloneShapeVisitor _cloner;
35  	
36  	public CopyController(RepApplication app) {
37  		super(app);
38  		_app.getSelection().addShapeSelectionListener(this);
39  		_copyMenuItem = new ControllerMenuItem(_app.getView(), _app.getConstants().copyCommand(), this);
40  		selectionChanged(_app.getSelection());
41  		_cloner = new CloneShapeVisitor();
42  	}
43  	
44  	@Override
45  	public void activate(DrawingView view) {
46  		List<Shape> selectedShapes = _app.getSelection().getSelectedShapes();
47  		List<Shape> clonedShapes = new ArrayList<Shape>();
48  		for (int i = 0, size = selectedShapes.size(); i < size; i++) {
49  			Shape shape = selectedShapes.get(i);
50  			shape.acceptVisitor(_cloner);
51  			clonedShapes.add(_cloner.getClone());
52  		}
53  		_app.getClipboard().setContent(clonedShapes);
54  	}
55  	
56  	public ControllerMenuItem getCopyMenuItem() {
57  		return _copyMenuItem;
58  	}
59  	public void selectionChanged(ShapeSelection selection) {
60  		_copyMenuItem.setEnabled(_app.getSelection().getSelectedShapes().size() > 0);	
61  	}
62  }