View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal 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   * svgreal 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 svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.gxt.form;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import com.extjs.gxt.ui.client.Style.IconAlign;
24  import com.extjs.gxt.ui.client.Style.Orientation;
25  import com.extjs.gxt.ui.client.event.ButtonEvent;
26  import com.extjs.gxt.ui.client.event.SelectionListener;
27  import com.extjs.gxt.ui.client.widget.LayoutContainer;
28  import com.extjs.gxt.ui.client.widget.button.ToggleButton;
29  import com.extjs.gxt.ui.client.widget.form.AdapterField;
30  import com.extjs.gxt.ui.client.widget.layout.FillData;
31  import com.extjs.gxt.ui.client.widget.layout.FillLayout;
32  import com.google.gwt.user.client.ui.AbstractImagePrototype;
33  
34  /**
35   * Field subclass with a discrete set of possible values,
36   * each value being represented by a toggle button with
37   * an icon
38   * @author laaglu
39   */
40  public class ToggleGroupField extends AdapterField {
41  	static int toggleGroupId;
42  	private class ToggleGroupFieldPanel extends LayoutContainer {
43  		private Map<ToggleButton, String> toggleToValue;
44  		private Map<String, ToggleButton> valueToToggle;
45  		private SelectionListener<ButtonEvent> listener = new SelectionListener<ButtonEvent>() {
46  			@Override
47  			public void componentSelected(ButtonEvent ce) {
48  				ToggleGroupField.this.setValue(toggleToValue.get(ce.getButton()));
49  			}
50  		};
51  		
52  		public ToggleGroupFieldPanel(
53  				String[] values,
54  				String[] tooltips,
55  				AbstractImagePrototype icons[]) { 
56  			setLayout(new FillLayout(Orientation.HORIZONTAL));
57  			toggleToValue = new HashMap<ToggleButton, String>();
58  			valueToToggle = new HashMap<String, ToggleButton>();
59  			String groupName = "ToggleGroupField" + toggleGroupId++;
60  			for (int i = 0; i < values.length;i++) {
61  				ToggleButton button = new ToggleButton();
62  				button.setToggleGroup(groupName);
63  				button.setToolTip(tooltips[i]);
64  				button.setIconAlign(IconAlign.TOP);
65  				button.setIcon(icons[i]);
66  				button.addSelectionListener(listener);
67  				add(button, i < values.length -1 ? new FillData(0, 2, 0, 0) : new FillData(0));
68  				toggleToValue.put(button, values[i]);
69  				valueToToggle.put(values[i], button);
70  			}
71  		}
72  		
73  		public void update(String value) {
74  			if (value != null) {
75  				ToggleButton button = valueToToggle.get(value);
76  				if (button != null) {
77  					button.toggle(true);
78  				}
79  			}
80  		}
81  	}
82  	
83  	public ToggleGroupField(
84  			String[] values,
85  			String[] tooltips,
86  			AbstractImagePrototype icons[]) {
87  		super(null);
88  		widget = new ToggleGroupFieldPanel(values, tooltips, icons);
89  		setResizeWidget(true);
90  		setFireChangeEventOnSetValue(true);
91  	}
92  
93  	@Override
94  	public void setValue(Object value) {
95  		((ToggleGroupFieldPanel)widget).update((String)value);
96  		super.setValue(value);
97  	}
98  	
99  	@Override
100 	public Object getValue() {
101 		return value;
102 	}
103 }