Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.legend / org.gvsig.raster.lib.legend.impl / src / main / java / org / gvsig / raster / lib / legend / impl / DefaultRasterLegendManager.java @ 6321

History | View | Annotate | Download (6.15 KB)

1
package org.gvsig.raster.lib.legend.impl;
2

    
3
import java.awt.Color;
4
import java.io.File;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.HashMap;
8
import java.util.List;
9
import java.util.Map;
10

    
11
import org.gvsig.raster.lib.buffer.api.FilterList;
12
import org.gvsig.raster.lib.legend.api.ColorInterpretation;
13
import org.gvsig.raster.lib.legend.api.ColorTable;
14
import org.gvsig.raster.lib.legend.api.ColorTableClass;
15
import org.gvsig.raster.lib.legend.api.ColorTableIO;
16
import org.gvsig.raster.lib.legend.api.ColorTableIOFactory;
17
import org.gvsig.raster.lib.legend.api.RasterLegend;
18
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
19
import org.gvsig.raster.lib.legend.api.Transparency;
20
import org.gvsig.raster.lib.legend.api.TransparencyRange;
21
import org.gvsig.raster.lib.legend.impl.io.gvSIGColorTableIO;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

    
25
/**
26
 * Default implementation of {@link RasterLegendManager}.
27
 *
28
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
29
 *
30
 */
31
public class DefaultRasterLegendManager implements RasterLegendManager {
32

    
33
    private static final Logger LOG = LoggerFactory.getLogger(DefaultRasterLegendManager.class);
34

    
35
    private Map<String, ColorTableIOFactory> colorTableIOFactories =
36
        new HashMap<String, ColorTableIOFactory>();
37

    
38
    @Override
39
    public RasterLegend createLegend() {
40
        return new DefaultRasterLegend();
41
    }
42

    
43
    @Override
44
    public RasterLegend createLegend(ColorInterpretation colorInterpretation) {
45
        return new DefaultRasterLegend(colorInterpretation);
46
    }
47

    
48
    @Override
49
    public RasterLegend createLegend(ColorInterpretation colorInterpretation,
50
        ColorTable colorTable, Transparency transparency, FilterList filters) {
51
        return new DefaultRasterLegend(colorInterpretation, colorTable, transparency, filters);
52
    }
53

    
54
    @Override
55
    public List<ColorTable> getColorTables(File folder) {
56

    
57
        if (folder.isFile()) {
58
            throw new IllegalArgumentException("File object has to be a directory");
59
        }
60

    
61
        if (!folder.canRead()) {
62
            throw new IllegalArgumentException(
63
                "It is necessary read permissions to get files and load color tables");
64
        }
65

    
66
        List<ColorTable> colorTables = new ArrayList<ColorTable>();
67
        for (File file : listFilesForFolder(folder)) {
68

    
69
            Collection<ColorTableIOFactory> factories = this.colorTableIOFactories.values();
70
            for (ColorTableIOFactory colorTableIOFactory : factories) {
71

    
72
                if(colorTableIOFactory.accept(file)){
73
                    ColorTableIO colorTableIO = colorTableIOFactory.create();
74
                    try{
75
                        ColorTable colorTable = colorTableIO.read(file);
76
                        colorTables.add(colorTable);
77
                        break;
78
                    } catch (Exception e){
79
                        // Log warn but continue reading files.
80
                        LOG.warn(String.format(
81
                            "%1s factory accepts file but it can not get ColorTable from file",
82
                            colorTableIOFactory.getName()), e);
83
                    }
84
                }
85
            }
86
        }
87

    
88
        return colorTables;
89
    }
90

    
91
    private List<File> listFilesForFolder(File folder) {
92
        List<File> files = new ArrayList<File>();
93
        for (File fileEntry : folder.listFiles()) {
94
            if (fileEntry.isDirectory()) {
95
                listFilesForFolder(fileEntry);
96
            } else {
97
                files.add(fileEntry);
98
            }
99
        }
100
        return files;
101
    }
102

    
103
    @Override
104
    public ColorTable createColorTable() {
105
        return new DefaultColorTable();
106
    }
107

    
108
    @Override
109
    public ColorTable createColorTable(String name, List<ColorTableClass> colorTableClasses,
110
        boolean interpolated) {
111
        return new DefaultColorTable(name, colorTableClasses, interpolated);
112
    }
113

    
114
    @Override
115
    public ColorTableIO getColorTableIO() {
116
        ColorTableIOFactory defaultFactory = this.colorTableIOFactories.get(gvSIGColorTableIO.NAME);
117
        if (defaultFactory == null) {
118
            if (this.colorTableIOFactories.isEmpty()) {
119
                LOG.warn("Any default Color Table IO factory registered");
120
                return null;
121
            }
122
        }
123
        return defaultFactory.create();
124
    }
125

    
126
    @Override
127
    public List<ColorTableIOFactory> getColorTableIOFactories() {
128
        return new ArrayList<ColorTableIOFactory>(this.colorTableIOFactories.values());
129
    }
130

    
131
    @Override
132
    public ColorTableIOFactory getColorTableIOFactory(String name) {
133
        return this.colorTableIOFactories.get(name);
134
    }
135

    
136
    @Override
137
    public void registerColorTableIOFactory(String name, ColorTableIOFactory colorTableIOFactory) {
138
        colorTableIOFactories.put(name, colorTableIOFactory);
139
    }
140

    
141
    @Override
142
    public ColorTableClass createColorTableClass() {
143
        return new DefaultColorTableClass();
144
    }
145

    
146
    @Override
147
    public ColorTableClass createColorTableClass(String className, double value,
148
        double interpolated, Color color) {
149
        return new DefaultColorTableClass(className, value, color, interpolated);
150
    }
151

    
152
    @Override
153
    public ColorInterpretation createColorInterpretation(String[] coloInterpretations) {
154
        return new DefaultColorInterpretation(coloInterpretations);
155
    }
156

    
157
    @Override
158
    public ColorInterpretation createColorInterpretation(String definedColorInterpretation) {
159
        return new DefaultColorInterpretation(definedColorInterpretation);
160
    }
161

    
162
    @Override
163
    public Transparency createTransparency() {
164
        return new DefaultTransparency();
165
    }
166

    
167
    @Override
168
    public Transparency createTransparency(int transparency,
169
        List<TransparencyRange> transparencyRanges) {
170
        return new DefaultTransparency(transparency, transparencyRanges);
171
    }
172

    
173
    @Override
174
    public TransparencyRange createTransparencyRange() {
175
        return new DefaultTransparencyRange();
176
    }
177

    
178
    @Override
179
    public TransparencyRange createTransparencyRange(int[] redRange, int[] greenRange, int[] blueRange, int alpha,
180
        boolean isAnd) {
181
        return new DefaultTransparencyRange(redRange, greenRange, blueRange, alpha, isAnd);
182
    }
183

    
184
}