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.view;
19  
20  import org.vectomatic.common.model.IStyleVisitor;
21  import org.vectomatic.common.model.style.Color;
22  import org.vectomatic.common.model.style.IStyle;
23  import org.vectomatic.common.model.style.NoneStyle;
24  
25  import com.google.gwt.user.client.DOM;
26  import com.google.gwt.user.client.Event;
27  import com.google.gwt.user.client.ui.ChangeListener;
28  import com.google.gwt.user.client.ui.PopupPanel;
29  import com.google.gwt.user.client.ui.Widget;
30  
31  /**
32   * Widget to represent the style
33   */
34  public class StyleWell extends Widget implements ChangeListener {
35  	private IStyle _style;
36  	private PopupPanel _menu;
37  	private IStyleVisitor _styleVisitor = new IStyleVisitor() {
38  
39  		public void visitColor(Color color) {
40  			String style = color.toString();
41  			DOM.setStyleAttribute(getElement(), "backgroundColor", style);
42  		}
43  
44  		public void visitNoneStyle(NoneStyle none) {
45  			DOM.setStyleAttribute(getElement(), "backgroundColor", "rgb(255, 255, 255)");
46  		}
47  		
48  	};
49  
50  	public StyleWell() {
51  		setElement(DOM.createDiv());
52  		setStyleName("colorEditor-styleWell");
53  		sinkEvents(Event.ONCLICK);
54  	}
55  
56  	public IStyle getStyle() {
57  		return _style;
58  	}
59  
60  	public void setStyle(IStyle color, IStyleMenu menu) {
61  		_style = color;
62  		_menu = (PopupPanel)menu;
63  		update();
64  	}
65  
66  	public void update() {
67  		if (_style != null) {
68  			_style.acceptVisitor(_styleVisitor);
69  		}
70  	}
71  
72  	@Override
73  	public void onBrowserEvent(Event event) {
74  		switch (DOM.eventGetType(event)) {
75  		case Event.ONCLICK:
76  			if (_menu != null) {
77  			    int left = getAbsoluteLeft();
78  			    int top = getAbsoluteTop() + getOffsetHeight();
79  			    _menu.setPopupPosition(left, top);
80  				_menu.show();
81  			}
82  			break;
83  		}
84  	}
85  
86  	public void onChange(Widget sender) {
87  		update();		
88  	}
89  }