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 | 802 | cordinyana | /**
|
---|---|---|---|
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 | 699 | jjdelcerro | package org.gvsig.tools.swing.impl.icontheme; |
25 | |||
26 | import java.io.File; |
||
27 | 710 | jjdelcerro | import java.util.ArrayList; |
28 | 699 | jjdelcerro | import java.util.HashMap; |
29 | import java.util.Iterator; |
||
30 | 710 | jjdelcerro | import java.util.List; |
31 | 699 | jjdelcerro | 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 | 710 | jjdelcerro | private Map<String, IconTheme> themesByName = null; |
46 | private List<IconTheme> themes = null; |
||
47 | 699 | jjdelcerro | private FolderSet repository = null; |
48 | 1847 | jjdelcerro | private double scaleFactor = 1.0; |
49 | |||
50 | 699 | jjdelcerro | public static IconThemeManager getIconThemeManager(){ |
51 | if (iconThemeManager == null){ |
||
52 | iconThemeManager = new DefaultIconThemeManager();
|
||
53 | } |
||
54 | return iconThemeManager;
|
||
55 | } |
||
56 | |||
57 | public DefaultIconThemeManager() {
|
||
58 | 1847 | jjdelcerro | this.scaleFactor = 1.0; |
59 | 699 | jjdelcerro | this.defaultTheme = new BaseIconTheme(); |
60 | this.currentTheme = this.defaultTheme; |
||
61 | 710 | jjdelcerro | this.themesByName = null; |
62 | 699 | jjdelcerro | this.repository = new DefaultFolderSet(); |
63 | this.repository.addObserver(this); |
||
64 | } |
||
65 | |||
66 | 1579 | jjdelcerro | @Override
|
67 | 699 | jjdelcerro | public IconTheme getDefault() {
|
68 | return this.defaultTheme; |
||
69 | } |
||
70 | |||
71 | 1579 | jjdelcerro | @Override
|
72 | 710 | jjdelcerro | public int getCount() { |
73 | return this.getThemes().size(); |
||
74 | } |
||
75 | |||
76 | 1579 | jjdelcerro | @Override
|
77 | 699 | jjdelcerro | public void setCurrent(IconTheme iconTheme) { |
78 | 1579 | jjdelcerro | this.currentTheme = iconTheme;
|
79 | 699 | jjdelcerro | } |
80 | |||
81 | 1579 | jjdelcerro | @Override
|
82 | 699 | jjdelcerro | public IconTheme getCurrent() {
|
83 | return this.currentTheme; |
||
84 | } |
||
85 | |||
86 | 1579 | jjdelcerro | @Override
|
87 | 699 | jjdelcerro | public IconTheme get(String themeID) { |
88 | 710 | jjdelcerro | return this.getThemes().get(themeID); |
89 | 699 | jjdelcerro | } |
90 | |||
91 | 1579 | jjdelcerro | @Override
|
92 | 710 | jjdelcerro | public IconTheme get(int index) { |
93 | return this.getListOfThemes().get(index); |
||
94 | } |
||
95 | |||
96 | 1579 | jjdelcerro | @Override
|
97 | 699 | jjdelcerro | public Iterator<IconTheme> iterator() { |
98 | return this.getThemes().values().iterator(); |
||
99 | } |
||
100 | |||
101 | 1579 | jjdelcerro | @Override
|
102 | 699 | jjdelcerro | public boolean contains(IconTheme theme) { |
103 | return this.getThemes().containsValue(theme); |
||
104 | } |
||
105 | |||
106 | 1579 | jjdelcerro | @Override
|
107 | 699 | jjdelcerro | public boolean add(IconTheme theme) { |
108 | this.getThemes().put(theme.getID(), theme);
|
||
109 | return true; |
||
110 | } |
||
111 | |||
112 | 1579 | jjdelcerro | @Override
|
113 | 699 | jjdelcerro | public boolean remove(IconTheme theme) { |
114 | return this.getThemes().remove(theme)==null ? false: true; |
||
115 | } |
||
116 | |||
117 | 1579 | jjdelcerro | @Override
|
118 | 699 | jjdelcerro | public void clear() { |
119 | 710 | jjdelcerro | this.themesByName = null; |
120 | 699 | jjdelcerro | this.themes = null; |
121 | } |
||
122 | |||
123 | 1579 | jjdelcerro | @Override
|
124 | 699 | jjdelcerro | public FolderSet getRepository() {
|
125 | return this.repository; |
||
126 | } |
||
127 | |||
128 | 1579 | jjdelcerro | @Override
|
129 | 699 | jjdelcerro | public void update(Observable observable, Object notification) { |
130 | if( observable != this.repository ) { |
||
131 | return;
|
||
132 | } |
||
133 | 710 | jjdelcerro | this.clear();
|
134 | 699 | jjdelcerro | } |
135 | |||
136 | 710 | jjdelcerro | private void buildThemes() { |
137 | 1579 | jjdelcerro | Map<String, IconTheme> themesByName = new HashMap<>(); |
138 | 699 | jjdelcerro | File[] folders = this.repository.listFiles(); |
139 | 1579 | jjdelcerro | 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 | 710 | jjdelcerro | themes.addAll(themesByName.values()); |
152 | 699 | jjdelcerro | this.themes = themes;
|
153 | 710 | jjdelcerro | this.themesByName = themesByName;
|
154 | 699 | jjdelcerro | } |
155 | 710 | jjdelcerro | |
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 | 1847 | jjdelcerro | |
170 | public double getScaleFactor() { |
||
171 | return this.scaleFactor; |
||
172 | } |
||
173 | |||
174 | public void setScaleFactor(double scaleFactor) { |
||
175 | this.scaleFactor = scaleFactor;
|
||
176 | } |
||
177 | |||
178 | |||
179 | 699 | jjdelcerro | } |