Revision 40688

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.swing/org.gvsig.symbology.swing.api/src/main/java/org/gvsig/app/gui/styling/SingleStyleSelectorFilter.java
28 28

  
29 29
public class SingleStyleSelectorFilter implements SelectorFilter {
30 30

  
31
	IStyle allowedStyle = null;
31
	private Class clazz = null;
32 32

  
33 33
	public SingleStyleSelectorFilter(IStyle sty) {
34
		this.allowedStyle = sty;
34
		clazz = sty.getClass();
35 35
	}
36 36

  
37
	public SingleStyleSelectorFilter(Class clz) {
38
		if (IStyle.class.isAssignableFrom(clz)) {
39
			clazz = clz;
40
		}
41
	}
42
	
37 43
	public boolean accepts(Object obj) {
38
		return obj.getClass().equals(allowedStyle.getClass());
44
		return clazz != null && obj != null &&
45
				clazz.isAssignableFrom(obj.getClass());
39 46
	}
40 47

  
41
	public IStyle getAllowedObject() {
42
		return allowedStyle;
43
	}
44

  
45
	public void setAllowedObject(IStyle allowedObject) {
46
		this.allowedStyle = allowedObject;
47
	}
48

  
49 48
}
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.swing/org.gvsig.symbology.swing.api/src/main/java/org/gvsig/app/gui/styling/StyleSelectorListModel.java
57 57
package org.gvsig.app.gui.styling;
58 58

  
59 59
import java.io.File;
60
import java.io.FileFilter;
61
import java.io.FileInputStream;
60 62
import java.io.FileNotFoundException;
61 63
import java.io.FileReader;
64
import java.io.IOException;
62 65
import java.util.Comparator;
63 66
import java.util.TreeSet;
64 67
import java.util.Vector;
65 68

  
69
import javax.swing.event.ListDataListener;
70

  
66 71
import org.exolab.castor.xml.MarshalException;
67 72
import org.exolab.castor.xml.ValidationException;
68 73
import org.gvsig.andami.messages.NotificationManager;
74
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
75
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
76
import org.gvsig.fmap.mapcontext.rendering.symbols.impl.LoadSymbolException;
69 77
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
78
import org.gvsig.tools.ToolsLocator;
79
import org.gvsig.tools.persistence.PersistenceManager;
80
import org.gvsig.tools.persistence.PersistentState;
81
import org.gvsig.tools.persistence.exception.PersistenceException;
70 82
import org.gvsig.utils.XMLEntity;
83
import org.gvsig.utils.listManager.ListModel;
71 84
import org.gvsig.utils.xmlEntity.generate.XmlTag;
85
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
72 87

  
73 88

  
74 89
/**
......
77 92
 * elements for it from a directory which is also specified when the StyleSelectorModel
78 93
 * is created.
79 94
 *
80
 * @author jaume dominguez faus - jaume.dominguez@iver.es
81 95
 *
82 96
 */
83
public class StyleSelectorListModel extends SymbolSelectorListModel {
97
public class StyleSelectorListModel implements ListModel {
84 98

  
85
	public static final String STYLE_FILE_EXTENSION = ".style";
99
	private static Logger logger = LoggerFactory.getLogger(
100
			StyleSelectorListModel.class);
101
	
102
	public static final String STYLE_FILE_EXTENSION = ".gvssty";
86 103

  
104
	protected Vector<IStyle> elements = null;
105
	protected File folder = null;
106
	protected FileFilter fFilter = null;
107
	protected SelectorFilter sFilter = null;
108
	protected String fileExt = "";
109
	
87 110
	/**
88 111
	 * <p>
89 112
	 * Creates a new instance of the model for the list in the Style Selector window
......
102 125
	 * @param filter, the filter used to show or hide some elements.
103 126
	 * @param fileExtension, file extension used for the files to be parsed.
104 127
	 */
105
	public StyleSelectorListModel(File dir, SelectorFilter filter, String fileExtension) {
106
		super(dir, filter, fileExtension);
107
		// TODO Auto-generated constructor stub
128
	public StyleSelectorListModel(
129
			File dir,
130
			SelectorFilter filter,
131
			String fileExtension) {
132
		
133
		fileExt = fileExtension.toLowerCase();
134
		folder = dir;
135
		sFilter = filter;
136
		fFilter = new FileFilter() {
137
			public boolean accept(File pathname) {
138
				if (!pathname.isFile()) {
139
					return false;
140
				} else {
141
					return pathname.getAbsolutePath().toLowerCase().endsWith(
142
							StyleSelectorListModel.this.fileExt);
143
				}
144
			}
145
		};
108 146
	}
109 147

  
148
	
149
	
150
	
151
	
110 152
	/**
111 153
	 * TODO: use the new Persistence API.
112 154
	 */
113
	public Vector getObjects() {
114
		return null;
115
//		if (elements == null) {
116
//			elements = new Vector();
117
//
118
//			File[] ff = dir.listFiles(ffilter);
119
//			for (int i = 0; i < ff.length; i++) {
120
//
121
//				XMLEntity xml;
122
//				try {
123
//					xml = new XMLEntity((XmlTag) XmlTag.unmarshal(new FileReader(ff[i])));
124
//					IStyle sty =  SymbologyFactory.createStyleFromXML(xml, ff[i].getName());
125
//					if (sfilter.accepts(sty))
126
//						add(sty);
127
//				} catch (MarshalException e) {
128
//					NotificationManager.
129
//					addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
130
//							"File corrupted! Skiping it...", e);
131
//				} catch (ValidationException e) {
132
//					NotificationManager.
133
//					addWarning("Error validating style file ["+ff[i].getAbsolutePath()+"].", e);
134
//				} catch (FileNotFoundException e) {
135
//					// unreachable code, but anyway...
136
//					NotificationManager.
137
//					addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
138
//				}
139
//
140
//			}
141
//		}
142
//		return elements;
155
	public Vector<IStyle> getObjects() {
156

  
157
		if (elements == null) {
158

  
159
			elements = new Vector();
160
			File[] ff = folder.listFiles(fFilter);
161
			for (int i = 0; i < ff.length; i++) {
162

  
163
				try {
164
					IStyle sty = this.loadStyle(ff[i]);
165
					if (sFilter.accepts(sty)) {
166
						add(sty);
167
					}
168
						
169
				} catch (Exception e) {
170
					NotificationManager.
171
					addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. ", e);
172
				}
173

  
174
			}
175
		}
176
		return elements;
143 177
	}
144 178

  
145 179

  
......
167 201
		elements = new Vector(map);
168 202

  
169 203
	}
204

  
205
	public int getSize() {
206
		return getObjects().size();
207
	}
208

  
209
	public Object getElementAt(int index) {
210
		return getObjects().get(index);
211
	}
212
	
213

  
214
	public void addListDataListener(ListDataListener l) {
215
		// TODO Auto-generated method stub
216
		
217
	}
218

  
219
	public void removeListDataListener(ListDataListener l) {
220
		// TODO Auto-generated method stub
221
		
222
	}
223

  
224
	public Object remove(int i) throws ArrayIndexOutOfBoundsException {
225
		return elements.remove(i);
226
	}
227

  
228
	public void insertAt(int i, Object o) {
229
		getObjects().insertElementAt((IStyle) o, i);
230
	}
231
	
232
	// =========================================================
233
	
234
	private IStyle loadStyle(File file) throws Exception {
235
		if (file.exists()) {
236
				FileInputStream fis = new FileInputStream(file);
237

  
238
				PersistenceManager persistenceManager = ToolsLocator
239
						.getPersistenceManager();
240

  
241
				PersistentState state = persistenceManager.loadState(fis);
242
				IStyle sty = (IStyle) persistenceManager.create(state);
243
				fis.close();
244
				
245
				return sty;
246
		} else {
247
			return null;
248
		}
249
		
250
	}	
170 251
}
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.swing/org.gvsig.symbology.swing.api/src/main/java/org/gvsig/app/gui/styling/SymbolSelector.java
50 50
import javax.swing.JTextField;
51 51
import javax.swing.JToggleButton;
52 52
import javax.swing.ListCellRenderer;
53
import javax.swing.ListModel;
53 54
import javax.swing.event.ListSelectionEvent;
54 55
import javax.swing.event.ListSelectionListener;
55 56
import javax.swing.event.TreeSelectionListener;
......
312 313
	 *
313 314
	 * @return listModel SymbolSelectorListModel
314 315
	 */
315
	protected SymbolSelectorListModel newListModel() {
316
	protected ListModel newListModel() {
316 317
		SymbolSelectorListModel listModel = new SymbolSelectorListModel(
317 318
				dir,
318 319
				sFilter,
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.swing/org.gvsig.symbology.swing.api/src/main/java/org/gvsig/app/gui/styling/StyleSelector.java
89 89
import javax.swing.JPanel;
90 90
import javax.swing.JTextField;
91 91
import javax.swing.ListCellRenderer;
92
import javax.swing.ListModel;
92 93
import javax.swing.event.ListSelectionEvent;
93 94
import javax.swing.event.ListSelectionListener;
94 95

  
......
97 98
import org.gvsig.andami.messages.NotificationManager;
98 99
import org.gvsig.app.gui.JComboBoxUnits;
99 100
import org.gvsig.app.gui.panels.ImageSizePanel;
101
import org.gvsig.fmap.mapcontext.MapContextLocator;
100 102
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
101 103
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
102 104
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
......
138 140
	public StyleSelector(IStyle style, int shapeType, SelectorFilter filter) {
139 141
		super(null, shapeType, filter, false);
140 142

  
141
    	Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
142
		rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
143
		rootDir = new File(
144
				MapContextLocator.getSymbolManager().getSymbolPreferences()
145
				.getSymbolLibraryPath());
146
		
147
    	// Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
148
		// rootDir = new File(prefs.get("SymbolStylesFolder",
149
		// System.getProperty("user.home")+"/gvSIG/Styles"));
150
		
143 151
		if (!rootDir.exists())
144 152
			rootDir.mkdir();
145 153

  
......
210 218
		return mySelectedElement;
211 219
	}
212 220

  
213
	protected SymbolSelectorListModel newListModel() {
221
	protected ListModel newListModel() {
214 222
		StyleSelectorListModel listModel = new StyleSelectorListModel(
215 223
				dir,
216
//				selectedElement,
217 224
				sFilter,
218 225
				StyleSelectorListModel.STYLE_FILE_EXTENSION);
219 226
		return listModel;

Also available in: Unified diff