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 @ 1845
History | View | Annotate | Download (4.39 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 | |||
49 | public static IconThemeManager getIconThemeManager(){ |
||
50 | if (iconThemeManager == null){ |
||
51 | iconThemeManager = new DefaultIconThemeManager();
|
||
52 | } |
||
53 | return iconThemeManager;
|
||
54 | } |
||
55 | |||
56 | public DefaultIconThemeManager() {
|
||
57 | this.defaultTheme = new BaseIconTheme(); |
||
58 | this.currentTheme = this.defaultTheme; |
||
59 | 710 | jjdelcerro | this.themesByName = null; |
60 | 699 | jjdelcerro | this.repository = new DefaultFolderSet(); |
61 | this.repository.addObserver(this); |
||
62 | } |
||
63 | |||
64 | 1579 | jjdelcerro | @Override
|
65 | 699 | jjdelcerro | public IconTheme getDefault() {
|
66 | return this.defaultTheme; |
||
67 | } |
||
68 | |||
69 | 1579 | jjdelcerro | @Override
|
70 | 710 | jjdelcerro | public int getCount() { |
71 | return this.getThemes().size(); |
||
72 | } |
||
73 | |||
74 | 1579 | jjdelcerro | @Override
|
75 | 699 | jjdelcerro | public void setCurrent(IconTheme iconTheme) { |
76 | 1579 | jjdelcerro | this.currentTheme = iconTheme;
|
77 | 699 | jjdelcerro | } |
78 | |||
79 | 1579 | jjdelcerro | @Override
|
80 | 699 | jjdelcerro | public IconTheme getCurrent() {
|
81 | return this.currentTheme; |
||
82 | } |
||
83 | |||
84 | 1579 | jjdelcerro | @Override
|
85 | 699 | jjdelcerro | public IconTheme get(String themeID) { |
86 | 710 | jjdelcerro | return this.getThemes().get(themeID); |
87 | 699 | jjdelcerro | } |
88 | |||
89 | 1579 | jjdelcerro | @Override
|
90 | 710 | jjdelcerro | public IconTheme get(int index) { |
91 | return this.getListOfThemes().get(index); |
||
92 | } |
||
93 | |||
94 | 1579 | jjdelcerro | @Override
|
95 | 699 | jjdelcerro | public Iterator<IconTheme> iterator() { |
96 | return this.getThemes().values().iterator(); |
||
97 | } |
||
98 | |||
99 | 1579 | jjdelcerro | @Override
|
100 | 699 | jjdelcerro | public boolean contains(IconTheme theme) { |
101 | return this.getThemes().containsValue(theme); |
||
102 | } |
||
103 | |||
104 | 1579 | jjdelcerro | @Override
|
105 | 699 | jjdelcerro | public boolean add(IconTheme theme) { |
106 | this.getThemes().put(theme.getID(), theme);
|
||
107 | return true; |
||
108 | } |
||
109 | |||
110 | 1579 | jjdelcerro | @Override
|
111 | 699 | jjdelcerro | public boolean remove(IconTheme theme) { |
112 | return this.getThemes().remove(theme)==null ? false: true; |
||
113 | } |
||
114 | |||
115 | 1579 | jjdelcerro | @Override
|
116 | 699 | jjdelcerro | public void clear() { |
117 | 710 | jjdelcerro | this.themesByName = null; |
118 | 699 | jjdelcerro | this.themes = null; |
119 | } |
||
120 | |||
121 | 1579 | jjdelcerro | @Override
|
122 | 699 | jjdelcerro | public FolderSet getRepository() {
|
123 | return this.repository; |
||
124 | } |
||
125 | |||
126 | 1579 | jjdelcerro | @Override
|
127 | 699 | jjdelcerro | public void update(Observable observable, Object notification) { |
128 | if( observable != this.repository ) { |
||
129 | return;
|
||
130 | } |
||
131 | 710 | jjdelcerro | this.clear();
|
132 | 699 | jjdelcerro | } |
133 | |||
134 | 710 | jjdelcerro | private void buildThemes() { |
135 | 1579 | jjdelcerro | Map<String, IconTheme> themesByName = new HashMap<>(); |
136 | 699 | jjdelcerro | File[] folders = this.repository.listFiles(); |
137 | 1579 | jjdelcerro | for( File folder : folders ) { |
138 | if( folder.isDirectory() ) {
|
||
139 | FolderIconTheme theme = new FolderIconTheme(this.getDefault()); |
||
140 | try {
|
||
141 | theme.load(folder); |
||
142 | themesByName.put(theme.getID(), theme); |
||
143 | }catch( IllegalArgumentException ex) { |
||
144 | // Do nothing
|
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | List<IconTheme> themes = new ArrayList<>(); |
||
149 | 710 | jjdelcerro | themes.addAll(themesByName.values()); |
150 | 699 | jjdelcerro | this.themes = themes;
|
151 | 710 | jjdelcerro | this.themesByName = themesByName;
|
152 | 699 | jjdelcerro | } |
153 | 710 | jjdelcerro | |
154 | private List<IconTheme> getListOfThemes() { |
||
155 | if( this.themes == null ) { |
||
156 | this.buildThemes();
|
||
157 | } |
||
158 | return this.themes; |
||
159 | } |
||
160 | |||
161 | private Map<String, IconTheme> getThemes() { |
||
162 | if( this.themesByName == null ) { |
||
163 | this.buildThemes();
|
||
164 | } |
||
165 | return this.themesByName; |
||
166 | } |
||
167 | 699 | jjdelcerro | } |