1 /** 2 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 15 * 16 */ 17 18 package com.aelitis.azureus.ui.swt.views.skin; 19 20 import java.util.*; 21 22 import org.gudy.azureus2.core3.util.AEMonitor2; 23 import org.gudy.azureus2.core3.util.Debug; 24 25 import com.aelitis.azureus.ui.swt.skin.SWTSkinObject; 26 27 /** 28 * Manages a list of SkinViews currently in use by the app 29 * 30 * @author TuxPaper 31 * @created Oct 6, 2006 32 * 33 */ 34 public class SkinViewManager 35 { 36 37 private static Map<Class<?>, List<SkinView>> mapSkinViews = new HashMap<Class<?>, List<SkinView>>(); 38 39 private static AEMonitor2 mon_skinViews = new AEMonitor2("skinViews"); 40 41 /** 42 * Map SkinObjectID to skin view 43 */ 44 private static Map<String, SkinView> skinIDs = new HashMap<String, SkinView>(); 45 46 private static Map<String, SkinView> skinViewIDs = new HashMap<String, SkinView>(); 47 48 private static List listeners = new ArrayList(); 49 50 /** 51 * @param key 52 * @param skinView 53 */ add(final SkinView skinView)54 public static void add(final SkinView skinView) { 55 mon_skinViews.enter(); 56 try { 57 List<SkinView> list = mapSkinViews.get(skinView.getClass()); 58 if (list == null) { 59 list = new ArrayList<SkinView>(1); 60 mapSkinViews.put(skinView.getClass(), list); 61 } 62 list.add(skinView); 63 } finally { 64 mon_skinViews.exit(); 65 } 66 67 SWTSkinObject mainSkinObject = skinView.getMainSkinObject(); 68 if (mainSkinObject != null) { 69 skinIDs.put(mainSkinObject.getSkinObjectID(), skinView); 70 String viewID = mainSkinObject.getViewID(); 71 if (viewID != null && viewID.length() > 0) { 72 skinViewIDs.put(viewID, skinView); 73 } 74 } 75 76 triggerViewAddedListeners(skinView); 77 } 78 remove(SkinView skinView)79 public static void remove(SkinView skinView) { 80 if (skinView == null) { 81 return; 82 } 83 84 mon_skinViews.enter(); 85 try { 86 List<SkinView> list = mapSkinViews.get(skinView.getClass()); 87 if (list != null) { 88 list.remove(skinView); 89 if (list.isEmpty()) { 90 mapSkinViews.remove(skinView.getClass()); 91 } 92 } 93 } finally { 94 mon_skinViews.exit(); 95 } 96 97 SWTSkinObject mainSkinObject = skinView.getMainSkinObject(); 98 if (mainSkinObject != null) { 99 skinIDs.remove(mainSkinObject.getSkinObjectID()); 100 skinViewIDs.remove(mainSkinObject.getViewID()); 101 } 102 } 103 104 /** 105 * Gets the first SkinView created of the specified class 106 * 107 * @param cla 108 * @return 109 */ getByClass(Class<?> cla)110 public static SkinView getByClass(Class<?> cla) { 111 List<SkinView> list = mapSkinViews.get(cla); 112 if (list == null) { 113 return null; 114 } 115 116 Object[] skinViews = list.toArray(); 117 for (int i = 0; i < skinViews.length; i++) { 118 SkinView sv = (SkinView) skinViews[i]; 119 120 SWTSkinObject so = sv.getMainSkinObject(); 121 if (so != null) { 122 if (!so.isDisposed()) { 123 return sv; 124 } 125 remove(sv); 126 } 127 } 128 129 return null; 130 } 131 132 /** 133 * Return all added SkinViews of a certain class 134 * 135 * @param cla 136 * @return 137 */ getMultiByClass(Class<?> cla)138 public static SkinView[] getMultiByClass(Class<?> cla) { 139 List<SkinView> list = mapSkinViews.get(cla); 140 if (list == null) { 141 return new SkinView[0]; 142 } 143 return list.toArray(new SkinView[0]); 144 } 145 146 /** 147 * Get the SkinView related to a SkinObjectID 148 * 149 * @param id 150 * @return 151 */ getBySkinObjectID(String id)152 public static SkinView getBySkinObjectID(String id) { 153 SkinView sv = skinIDs.get(id); 154 if (sv != null) { 155 SWTSkinObject so = sv.getMainSkinObject(); 156 if (so != null && so.isDisposed()) { 157 remove(sv); 158 return null; 159 } 160 } 161 return sv; 162 } 163 164 /** 165 * Get the SkinView related to a View ID 166 * 167 * @param viewID 168 * @return 169 */ getByViewID(String viewID)170 public static SkinView getByViewID(String viewID) { 171 SkinView sv = skinViewIDs.get(viewID); 172 if (sv != null) { 173 SWTSkinObject so = sv.getMainSkinObject(); 174 if (so != null && so.isDisposed()) { 175 remove(sv); 176 return null; 177 } 178 } 179 return sv; 180 } 181 182 /** 183 * Listen in on SkinView adds 184 * 185 * @param l 186 */ addListener(SkinViewManagerListener l)187 public static void addListener(SkinViewManagerListener l) { 188 synchronized (SkinViewManager.class) { 189 if (!listeners.contains(l)) { 190 listeners.add(l); 191 } 192 } 193 } 194 addListener(Class cla, SkinViewManagerListener l)195 public static void addListener(Class cla, SkinViewManagerListener l) { 196 synchronized (SkinViewManager.class) { 197 if (!listeners.contains(l)) { 198 listeners.add(l); 199 } 200 } 201 202 SkinView[] svs = SkinViewManager.getMultiByClass(cla); 203 if (svs != null) { 204 for (SkinView skinView : svs) { 205 l.skinViewAdded(skinView); 206 } 207 } 208 } 209 RemoveListener(SkinViewManagerListener l)210 public static void RemoveListener(SkinViewManagerListener l) { 211 synchronized (SkinViewManager.class) { 212 listeners.remove(l); 213 } 214 } 215 triggerViewAddedListeners(SkinView skinView)216 public static void triggerViewAddedListeners(SkinView skinView) { 217 Object[] array; 218 synchronized (SkinViewManager.class) { 219 array = listeners.toArray(); 220 } 221 for (int i = 0; i < array.length; i++) { 222 SkinViewManagerListener l = (SkinViewManagerListener) array[i]; 223 try { 224 l.skinViewAdded(skinView); 225 } catch (Exception e) { 226 Debug.out(e); 227 } 228 } 229 } 230 231 public static interface SkinViewManagerListener { skinViewAdded(SkinView skinview)232 public void skinViewAdded(SkinView skinview); 233 } 234 } 235