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 @ 3039
History | View | Annotate | Download (5.22 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 |
@Deprecated
|
78 |
public void setCurrent(IconTheme iconTheme) { |
79 |
this.currentTheme = iconTheme;
|
80 |
} |
81 |
|
82 |
public void setActive(IconTheme iconTheme) { |
83 |
this.currentTheme = iconTheme;
|
84 |
} |
85 |
|
86 |
@Deprecated
|
87 |
@Override
|
88 |
public IconTheme getCurrent() {
|
89 |
return this.currentTheme; |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public IconTheme getActive() {
|
94 |
return this.currentTheme; |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public IconTheme get(String themeID) { |
99 |
IconTheme t = this.getThemes().get(themeID);
|
100 |
if(t != null){ |
101 |
return t;
|
102 |
} |
103 |
for (IconTheme theme : this.getThemes().values()) { |
104 |
if(theme.matchID(themeID)){
|
105 |
return theme;
|
106 |
} |
107 |
} |
108 |
return null; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public IconTheme get(int index) { |
113 |
return this.getListOfThemes().get(index); |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public Iterator<IconTheme> iterator() { |
118 |
return this.getThemes().values().iterator(); |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public boolean contains(IconTheme theme) { |
123 |
return this.getThemes().containsValue(theme); |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public boolean add(IconTheme theme) { |
128 |
this.getThemes().put(theme.getID(), theme);
|
129 |
return true; |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public boolean remove(IconTheme theme) { |
134 |
return this.getThemes().remove(theme)==null ? false: true; |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public void clear() { |
139 |
this.themesByName = null; |
140 |
this.themes = null; |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public FolderSet getRepository() {
|
145 |
return this.repository; |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public void update(Observable observable, Object notification) { |
150 |
if( observable != this.repository ) { |
151 |
return;
|
152 |
} |
153 |
this.clear();
|
154 |
} |
155 |
|
156 |
private void buildThemes() { |
157 |
Map<String, IconTheme> themesByName = new HashMap<>(); |
158 |
File[] folders = this.repository.listFiles(); |
159 |
for( File folder : folders ) { |
160 |
if( folder.isDirectory() ) {
|
161 |
FolderIconTheme theme = new FolderIconTheme(this.getDefault()); |
162 |
try {
|
163 |
theme.load(folder); |
164 |
themesByName.put(theme.getID(), theme); |
165 |
}catch( IllegalArgumentException ex) { |
166 |
// Do nothing
|
167 |
} |
168 |
} |
169 |
} |
170 |
List<IconTheme> themes = new ArrayList<>(); |
171 |
themes.addAll(themesByName.values()); |
172 |
this.themes = themes;
|
173 |
this.themesByName = themesByName;
|
174 |
} |
175 |
|
176 |
private List<IconTheme> getListOfThemes() { |
177 |
if( this.themes == null ) { |
178 |
this.buildThemes();
|
179 |
} |
180 |
return this.themes; |
181 |
} |
182 |
|
183 |
private Map<String, IconTheme> getThemes() { |
184 |
if( this.themesByName == null ) { |
185 |
this.buildThemes();
|
186 |
} |
187 |
return this.themesByName; |
188 |
} |
189 |
|
190 |
public double getScaleFactor() { |
191 |
return this.scaleFactor; |
192 |
} |
193 |
|
194 |
public void setScaleFactor(double scaleFactor) { |
195 |
this.scaleFactor = scaleFactor;
|
196 |
} |
197 |
|
198 |
|
199 |
} |