Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleSelectorListModel.java @ 10995

History | View | Annotate | Download (2.71 KB)

1
package com.iver.cit.gvsig.gui.styling;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.FileReader;
6
import java.util.Comparator;
7
import java.util.TreeSet;
8
import java.util.Vector;
9

    
10
import org.exolab.castor.xml.MarshalException;
11
import org.exolab.castor.xml.ValidationException;
12

    
13
import com.iver.andami.messages.NotificationManager;
14
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
15
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
16
import com.iver.utiles.XMLEntity;
17
import com.iver.utiles.xmlEntity.generate.XmlTag;
18

    
19
public class StyleSelectorListModel extends SymbolSelectorListModel {
20

    
21
        public static final String STYLE_FILE_EXTENSION = ".style";
22

    
23
        public StyleSelectorListModel(File dir, Object currentElemet, SelectorFilter filter, String fileExtension) {
24
                super(dir, currentElemet, filter, fileExtension);
25
                // TODO Auto-generated constructor stub
26
        }
27

    
28
        public Vector getObjects() {
29
                if (elements == null) {
30
                        elements = new Vector();
31

    
32
                        if (currentElement!= null)
33
                                elements.add(currentElement);
34

    
35
                        File[] ff = dir.listFiles(ffilter);
36
                        for (int i = 0; i < ff.length; i++) {
37

    
38
                                XMLEntity xml;
39
                                try {
40
                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(new FileReader(ff[i])));
41
                                        IStyle sty = SymbologyFactory.createStyleFromXML(xml, ff[i].getName());
42
                                        if (sfilter.accepts(sty))
43
                                                add(sty);
44
                                } catch (MarshalException e) {
45
                                        NotificationManager.
46
                                                addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
47
                                                                "File corrupted! Skiping it...", e);
48
                                } catch (ValidationException e) {
49
                                        NotificationManager.
50
                                                addWarning("Error validating style file ["+ff[i].getAbsolutePath()+"].", e);
51
                                } catch (FileNotFoundException e) {
52
                                        // unreachable code, but anyway...
53
                                        NotificationManager.
54
                                                addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
55
                                }
56

    
57
                        }
58
                }
59
                return elements;
60
        }
61
        
62

    
63
        public void add(Object o) {
64
                TreeSet map = new TreeSet(new Comparator() {
65

    
66
                        public int compare(Object o1, Object o2) {
67
                                // first will always be the current symbol
68
                                if (o1.equals(currentElement)) return -1;
69
                                if (o2.equals(currentElement)) return 1;
70

    
71
                                IStyle sym1 = (IStyle) o1;
72
                                IStyle sym2 = (IStyle) o2;
73
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
74
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
75
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
76

    
77
                                int result = sym1.getDescription().compareTo(sym2.getDescription());
78
                                return (result!=0) ? result: 1; /* this will allow adding symbols with
79
                                                                                                   the same value for description than
80
                                                                                                   a previous one. */
81
                        }
82

    
83
                });
84

    
85
                map.addAll(elements);
86
                map.add(o);
87
                elements = new Vector(map);
88

    
89
        }
90
}