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.model.svg;
19  
20  import org.vectomatic.svg.edit.client.model.IConverter;
21  
22  import com.extjs.gxt.ui.client.data.BaseModelData;
23  import com.extjs.gxt.ui.client.data.ModelData;
24  import com.extjs.gxt.ui.client.store.ListStore;
25  import com.google.gwt.resources.client.ImageResource;
26  
27  public class ComboStore extends ListStore<ModelData> {
28  	public static final String LABEL = "label";
29  	public static final String ICON = "icon";
30  	public static final String VALUE = "value";
31  	private boolean hasIcons;
32  	
33  	private static class ComboData extends BaseModelData {
34  		@Override
35  		public String toString() {
36  			return this.<String>get(LABEL);
37  		}
38  	}
39  	
40  	public ComboStore(String[] values, String[] labels) {
41  		this(values, labels, null);
42  	}
43  	
44  	public ComboStore(String[] values, String[] labels, ImageResource icons[]) {
45  		super();
46  		assert values.length == labels.length;
47  		assert icons == null || icons.length == labels.length;
48  		hasIcons = icons != null;
49  		for (int i = 0; i < values.length; i++) {
50  			ModelData model = new ComboData(); 
51  			model.set(VALUE, values[i]);
52  			model.set(LABEL, labels[i]);
53  			if (hasIcons()) {
54  				model.set(ICON, icons[i].getSafeUri().asString());
55  			}
56  			add(model);
57  		}
58  	}
59  	
60  	public IConverter<String, ModelData> getConverter() {
61  		return new IConverter<String, ModelData>() {
62  
63  			@Override
64  			public ModelData sourceToDest(String source) {
65  				return findModel(VALUE, source);
66  			}
67  
68  			@Override
69  			public String destToSource(ModelData dest) {
70  				return dest.get(VALUE);
71  			}
72  			
73  		};
74  	}
75  
76  	public boolean hasIcons() {
77  		return hasIcons;
78  	}
79  }