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.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.vectomatic.client.rep.RepApplication;
28  import org.vectomatic.client.rep.command.SetAttributeCommand;
29  import org.vectomatic.client.rep.events.IShapeSelectionListener;
30  import org.vectomatic.client.rep.view.IStyleMenu;
31  import org.vectomatic.client.rep.view.PlainColorMenu;
32  import org.vectomatic.client.rep.view.StyleWell;
33  import org.vectomatic.common.model.Attribute;
34  import org.vectomatic.common.model.FloatAttributeValue;
35  import org.vectomatic.common.model.Shape;
36  import org.vectomatic.common.model.geometry.ShapeGroup;
37  import org.vectomatic.common.model.style.Color;
38  import org.vectomatic.common.model.style.IStyle;
39  import org.vectomatic.common.model.style.NoneStyle;
40  
41  import com.google.gwt.user.client.ui.ChangeListener;
42  import com.google.gwt.user.client.ui.HorizontalPanel;
43  import com.google.gwt.user.client.ui.Label;
44  import com.google.gwt.user.client.ui.ListBox;
45  import com.google.gwt.user.client.ui.Widget;
46  
47  /**
48   * Class to manage changes of the color or transparency of a shape
49   * @author Lukas Laag
50   */
51  public class StyleController extends ControllerBase implements IShapeSelectionListener, ChangeListener {
52  	private Attribute _styleAttribute;
53  	private Attribute _opacityAttribute;
54  	private ListBox _styleBox;
55  	private StyleWell _styleWell;
56  	private Label _styleLabel;
57  	private HorizontalPanel _row;
58  	private IStyle _defaultStyle;
59  	private FloatAttributeValue _defaultOpacity;
60  
61  	private Set<Integer> _styleKinds;
62  	private Set<IStyle> _styleValues;
63  	private Set<FloatAttributeValue> _opacities;
64  	private Map<Integer, IStyleMenu> _styleKindToMenu;
65  
66  	public StyleController(RepApplication app, String label, Attribute styleAttribute, Attribute opacityAttribute, IStyle defaultStyle, FloatAttributeValue defaultOpacity) {
67  		super(app);
68  		_styleKinds = new HashSet<Integer>();
69  		_styleValues = new HashSet<IStyle>();
70  		_opacities = new HashSet<FloatAttributeValue>();
71  		_styleKindToMenu = new HashMap<Integer, IStyleMenu>();
72  		
73  		_app.getSelection().addShapeSelectionListener(this);
74  		_styleAttribute = styleAttribute;
75  		_opacityAttribute = opacityAttribute;
76  		_defaultStyle = defaultStyle;
77  		_defaultOpacity = defaultOpacity;
78  		
79  	    PlainColorMenu plainColorMenu = new PlainColorMenu(app, this);
80  	    _styleKindToMenu.put(Color.BLACK.getKind(), plainColorMenu);
81  		_styleLabel = new Label(label);
82  		_styleWell = new StyleWell();
83  		_styleWell.setWidth("30px");
84  		_styleWell.setHeight("8px");
85  		_styleBox = new ListBox();
86  		_styleBox.addItem(_app.getConstants().plainItem());
87  		//_styleBox.addItem(_constants.gradientItem());
88  		//_styleBox.addItem(_constants.patternItem());
89  		_styleBox.addItem(_app.getConstants().noneItem());
90  		_styleBox.addChangeListener(this);
91  		_row = new HorizontalPanel();
92  		_row.add(_styleLabel);
93  		_row.add(_styleBox);
94  		_row.add(_styleWell);
95  		
96  		IStyleMenu menu = _styleKindToMenu.get(_defaultStyle.getKind());
97  		_styleWell.setStyle(_defaultStyle, menu);
98  		if (menu != null) {
99  			menu.setOpacity(_defaultOpacity);
100 		}
101 	}
102 	
103 	public Widget getWidget() {
104 		return _row;
105 	}
106 	
107 	public void selectionChanged(ShapeSelection selection) {
108 		List<Shape> shapes = new ArrayList<Shape>(selection.getSelectedShapes());
109 		_styleKinds.clear();
110 		_styleValues.clear();
111 		_opacities.clear();
112 		
113 		IStyle styleValue = null;
114 		Integer styleKind = null;
115 		FloatAttributeValue opacity = null;
116 		for (int i = 0; i < shapes.size(); i++) {
117 			Shape shape = shapes.get(i);
118 			styleValue = (IStyle)shape.getAttribute(_styleAttribute);
119 			if ((styleValue == null) && (shape instanceof ShapeGroup)) {
120 				shapes.addAll(((ShapeGroup)shape).getShapes());
121 				continue;
122 			}
123 			styleKind = styleValue.getKind();
124 			opacity = (FloatAttributeValue)shape.getAttribute(_opacityAttribute);
125 			_styleKinds.add(styleKind);
126 			_styleValues.add(styleValue);
127 			_opacities.add(opacity);
128 		}
129 		
130 		if (_styleValues.size() == 0) {
131 			_styleBox.setSelectedIndex(_defaultStyle.getKind().intValue());
132 			IStyleMenu menu = _styleKindToMenu.get(_defaultStyle.getKind());
133 			_styleWell.setStyle(_defaultStyle, menu);
134 			if (menu != null) {
135 				menu.setOpacity(_defaultOpacity);
136 			}
137 		} else if (_styleValues.size() == 1) {
138 			_styleBox.setSelectedIndex(styleKind.intValue());
139 			IStyleMenu menu = _styleKindToMenu.get(styleKind);
140 			_styleWell.setStyle(styleValue, menu);
141 			if (menu != null) {
142 				menu.setOpacity(opacity);
143 			}
144 		} else {
145 			IStyleMenu menu = null;
146 			if (_styleKinds.size() == 1) {
147 				_styleBox.setSelectedIndex(styleKind.intValue());
148 				menu = _styleKindToMenu.get(styleKind);
149 			} else {
150 				_styleBox.setSelectedIndex(-1);
151 			}
152 			_styleWell.setStyle(NoneStyle.NONE, menu);
153 		}
154 	}
155 
156 	/**
157 	 * Invoked when the user changes the list box.
158 	 */
159 	public void onChange(Widget sender) {
160 		RepApplication.app.debugPrint("style kind change");
161 		IStyleMenu menu = _styleKindToMenu.get(new Integer(_styleBox.getSelectedIndex()));
162 		IStyle style = (menu != null) ? menu.getSelectedStyle() : NoneStyle.NONE;
163 		_styleWell.setStyle(style, menu);
164 		//Determine if the change is real and should generate a corresponding command
165 		if (_app.getSelection().hasAttributeChanged(_styleAttribute, style)) {
166 			SetAttributeCommand setAttributeCommand = new SetAttributeCommand(_app, _styleAttribute, style);
167 			setAttributeCommand.execute();
168 			_app.getHistory().addCommand(setAttributeCommand);
169 		}
170 		if (_app.getSelection().getSelectedShapes().size() == 0) {
171 			_defaultStyle = style;
172 		}
173 	}
174 	
175 	/**
176 	 * Invoked when the user changes the opacity in the color menu
177 	 * @param opacity 
178 	 */
179 	public void setOpacity(FloatAttributeValue opacity) {
180 		RepApplication.app.debugPrint("opacity change");
181 		//Determine if the change is real and should generate a corresponding command
182 		if (_app.getSelection().hasAttributeChanged(_opacityAttribute, opacity)) {
183 			SetAttributeCommand setAttributeCommand = new SetAttributeCommand(_app, _opacityAttribute, opacity);
184 			setAttributeCommand.execute();
185 			_app.getHistory().addCommand(setAttributeCommand);
186 		}
187 		if (_app.getSelection().getSelectedShapes().size() == 0) {
188 			_defaultOpacity = opacity;
189 		}
190 	}
191 	
192 	/**
193 	 * Invoked when the user changes the color in the color menu
194 	 */
195 	public void setStyle(IStyle style) {
196 		RepApplication.app.debugPrint("style value change");
197 		IStyleMenu menu = _styleKindToMenu.get(style.getClass().getName());
198 		_styleWell.setStyle(style, menu);
199 		//Determine if the change is real and should generate a corresponding command
200 		if (_app.getSelection().hasAttributeChanged(_styleAttribute, style)) {
201 			SetAttributeCommand setAttributeCommand = new SetAttributeCommand(_app, _styleAttribute, style);
202 			setAttributeCommand.execute();
203 			_app.getHistory().addCommand(setAttributeCommand);
204 		}
205 		if (_app.getSelection().getSelectedShapes().size() == 0) {
206 			_defaultStyle = style;
207 		}
208 	}
209 
210 
211 	/**
212 	 * Returns the currently selected style.
213 	 * This method is called by controllers which instantiate new shapes
214 	 * @return
215 	 */
216 	public IStyle getStyle() {
217 		return _defaultStyle;
218 	}
219 	
220 	/**
221 	 * Returns the currently selected opacity
222 	 * This method is called by controllers which instantiate new shapes
223 	 * @return
224 	 */
225 	public FloatAttributeValue getOpacity() {
226 		return _defaultOpacity;
227 	}
228 	
229 	public Attribute getStyleAttribute() {
230 		return _styleAttribute;
231 	}
232 	
233 }