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.command.CutCommand;
25  import org.vectomatic.client.rep.events.IShapeSelectionListener;
26  import org.vectomatic.client.rep.view.DrawingView;
27  import org.vectomatic.common.model.CloneShapeVisitor;
28  import org.vectomatic.common.model.Shape;
29  
30  /**
31   * Controller to respond to cut request and turn
32   * them into CutCommand
33   */
34  public class CutController extends ControllerBase implements IShapeSelectionListener {
35  	private ControllerMenuItem _cutMenuItem;
36  	private CloneShapeVisitor _cloner;
37  	
38  	public CutController(RepApplication app) {
39  		super(app);
40  		_app.getSelection().addShapeSelectionListener(this);
41  		_cutMenuItem = new ControllerMenuItem(_app.getView(), app.getConstants().cutCommand(), this);
42  		_cloner = new CloneShapeVisitor();
43  		selectionChanged(_app.getSelection());
44  	}
45  	
46  	@Override
47  	public void activate(DrawingView view) {
48  		// Clone the selected shapes and put the clones in the clipboard
49  		List<Shape> selectedShapes = _app.getSelection().getSelectedShapes();
50  		List<Shape> clonedShapes = new ArrayList<Shape>();
51  		for (int i = 0, size = selectedShapes.size(); i < size; i++) {
52  			Shape shape = selectedShapes.get(i);
53  			shape.acceptVisitor(_cloner);
54  			clonedShapes.add(_cloner.getClone());
55  		}
56  		_app.getClipboard().setContent(clonedShapes);
57  		
58  		// Create a new cut command
59  		CutCommand cutCommand = new CutCommand(_app);
60  		
61  		// Execute the command to remove the shapes from the model
62  		cutCommand.execute();
63  		_app.getHistory().addCommand(cutCommand);
64  	}
65  	
66  	public ControllerMenuItem getCutMenuItem() {
67  		return _cutMenuItem;
68  	}
69  
70  	public void selectionChanged(ShapeSelection selection) {
71  		_cutMenuItem.setEnabled(_app.getSelection().getSelectedShapes().size() > 0);	
72  	}
73  }