Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / icontheme / DefaultIconThemeManager.java @ 1847

History | View | Annotate | Download (4.68 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.swing.impl.icontheme;
25

    
26
import java.io.File;
27
import java.util.ArrayList;
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32

    
33
import org.gvsig.tools.observer.ComplexObserver;
34
import org.gvsig.tools.observer.Observable;
35
import org.gvsig.tools.swing.icontheme.IconTheme;
36
import org.gvsig.tools.swing.icontheme.IconThemeManager;
37
import org.gvsig.tools.util.FolderSet;
38
import org.gvsig.tools.util.impl.DefaultFolderSet;
39

    
40
public class DefaultIconThemeManager implements IconThemeManager , ComplexObserver{
41
        private static IconThemeManager iconThemeManager = null;
42

    
43
        private IconTheme defaultTheme = null;
44
        private IconTheme currentTheme = null;
45
        private Map<String, IconTheme> themesByName = null;
46
        private List<IconTheme> themes = null; 
47
        private FolderSet repository = null;
48
        private double scaleFactor = 1.0;
49
        
50
        public static IconThemeManager getIconThemeManager(){
51
                if (iconThemeManager == null){
52
                        iconThemeManager = new DefaultIconThemeManager();
53
                }
54
                return iconThemeManager;
55
        }
56

    
57
        public DefaultIconThemeManager() {
58
                this.scaleFactor = 1.0;
59
                this.defaultTheme = new BaseIconTheme();
60
                this.currentTheme = this.defaultTheme;
61
                this.themesByName = null;
62
                this.repository = new DefaultFolderSet();
63
                this.repository.addObserver(this);
64
        }
65
        
66
    @Override
67
        public IconTheme getDefault() {
68
                return this.defaultTheme;
69
        }
70

    
71
    @Override
72
        public int getCount() {
73
                return this.getThemes().size();
74
        }
75
        
76
    @Override
77
        public void setCurrent(IconTheme iconTheme) {
78
                this.currentTheme = iconTheme;
79
        }
80

    
81
    @Override
82
        public IconTheme getCurrent() {
83
                return this.currentTheme;
84
        }
85

    
86
    @Override
87
        public IconTheme get(String themeID) {
88
                return this.getThemes().get(themeID);
89
        }
90

    
91
    @Override
92
        public IconTheme get(int index) {
93
                return this.getListOfThemes().get(index);
94
        }
95

    
96
    @Override
97
        public Iterator<IconTheme> iterator() {
98
                return this.getThemes().values().iterator();
99
        }
100

    
101
    @Override
102
        public boolean contains(IconTheme theme) {
103
                return this.getThemes().containsValue(theme);
104
        }
105

    
106
    @Override
107
        public boolean add(IconTheme theme) {
108
                this.getThemes().put(theme.getID(), theme);
109
                return true; 
110
        }
111

    
112
    @Override
113
        public boolean remove(IconTheme theme) {
114
                return this.getThemes().remove(theme)==null ? false: true;
115
        }
116

    
117
    @Override
118
        public void clear() {
119
                this.themesByName = null;
120
                this.themes = null;
121
        }
122

    
123
    @Override
124
        public FolderSet getRepository() {
125
                return this.repository;
126
        }
127

    
128
    @Override
129
        public void update(Observable observable, Object notification) {
130
                if( observable != this.repository ) {
131
                        return;
132
                }
133
                this.clear();
134
        }
135

    
136
        private void buildThemes() {
137
                Map<String, IconTheme> themesByName = new HashMap<>();
138
                File[] folders = this.repository.listFiles();
139
        for( File folder : folders ) {
140
            if( folder.isDirectory() ) {
141
                FolderIconTheme theme = new FolderIconTheme(this.getDefault());
142
                try {
143
                    theme.load(folder);
144
                    themesByName.put(theme.getID(), theme);
145
                }catch( IllegalArgumentException ex) {
146
                    // Do nothing
147
                }
148
            }
149
        }
150
                List<IconTheme> themes = new ArrayList<>();
151
                themes.addAll(themesByName.values());
152
                this.themes = themes;
153
                this.themesByName = themesByName;
154
        }
155

    
156
        private List<IconTheme> getListOfThemes() {
157
                if( this.themes == null ) {
158
                        this.buildThemes();
159
                }
160
                return this.themes;
161
        }
162
        
163
        private Map<String, IconTheme> getThemes() {
164
                if( this.themesByName == null ) {
165
                        this.buildThemes();
166
                }
167
                return this.themesByName;
168
        }
169
        
170
        public double getScaleFactor() {
171
            return this.scaleFactor;
172
        }
173

    
174
    public void setScaleFactor(double scaleFactor) {
175
        this.scaleFactor = scaleFactor;
176
    }
177
        
178
        
179
}