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.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.vectomatic.client.rep.RepApplication;
26  import org.vectomatic.client.rep.command.SetAttributeCommand;
27  import org.vectomatic.client.rep.events.IShapeSelectionListener;
28  import org.vectomatic.common.model.Attribute;
29  import org.vectomatic.common.model.FloatAttributeValue;
30  import org.vectomatic.common.model.Shape;
31  import org.vectomatic.common.model.geometry.ShapeGroup;
32  
33  import com.google.gwt.user.client.ui.Widget;
34  
35  /**
36   * Controller to respond to line width modifications and turn
37   * them into SetAttributeCommand
38   */
39  public class LineWidthController extends ControllerBase implements IShapeSelectionListener {
40  	private FloatAttributeValue _defaultLineWidth;
41  	private LineWidthWell _lineWithWell;
42  	private Set<FloatAttributeValue> _lineWidths;
43  
44  	public LineWidthController(RepApplication app) {
45  		super(app);
46  		_app.getSelection().addShapeSelectionListener(this);
47  		_lineWidths = new HashSet<FloatAttributeValue>();
48  	
49  		_defaultLineWidth = new FloatAttributeValue(1f);
50  		_lineWithWell = new LineWidthWell(this);
51  		_lineWithWell.setLineWidth(_defaultLineWidth);
52  	}
53  	
54  	public Widget getWidget() {
55  		return _lineWithWell;
56  	}
57  
58  	
59  	public void selectionChanged(ShapeSelection selection) {
60  		_lineWidths.clear();
61  		List<Shape> shapes = new ArrayList<Shape>(selection.getSelectedShapes());
62  		for (int i = 0, size = shapes.size(); i < size; i++) {
63  			Shape shape = shapes.get(i);
64  			FloatAttributeValue lineWidth = (FloatAttributeValue)shape.getAttribute(Attribute.LINE_WIDTH);
65  			if ((lineWidth == null) && (shape instanceof ShapeGroup)) {
66  				shapes.addAll(((ShapeGroup)shape).getShapes());
67  				continue;
68  			}
69  			_lineWidths.add(lineWidth);
70  		}
71  		if (_lineWidths.size() == 0) {
72  			_lineWithWell.setLineWidth(_defaultLineWidth);
73  		} else if (_lineWidths.size() == 1) {
74  			_lineWithWell.setLineWidth(_lineWidths.iterator().next());
75  		} else {
76  			_lineWithWell.setLineWidth(null);
77  		}
78  	}
79  	
80  	/**
81  	 * Invoked when the user changes the line width in the line width menu
82  	 * or in the line width editor
83  	 */
84  	public void setLineWidth(FloatAttributeValue lineWidth) {
85  		RepApplication.app.debugPrint("lineWidth change");
86  		//Determine if the change is real and should generate a corresponding command
87  		if (_app.getSelection().hasAttributeChanged(Attribute.LINE_WIDTH, lineWidth)) {
88  			SetAttributeCommand setAttributeCommand = new SetAttributeCommand(_app, Attribute.LINE_WIDTH, lineWidth);
89  			setAttributeCommand.execute();
90  			_app.getHistory().addCommand(setAttributeCommand);
91  		}
92  		if (_app.getSelection().getSelectedShapes().size() == 0) {
93  			_defaultLineWidth = lineWidth;
94  		}
95  		_lineWithWell.setLineWidth(lineWidth);
96  	}
97  	
98  	public FloatAttributeValue getLineWidth() {
99  		return _defaultLineWidth;
100 	}
101 
102 }