1 7 8 package com.ca.commons.cbutil; 9 10 import javax.help.HelpBroker; 11 import javax.help.HelpSet; 12 import javax.swing.*; 13 import java.net.URL ; 14 import java.util.logging.Level ; 15 import java.util.logging.Logger ; 16 17 20 public class CBHelpSystem 21 { 22 private String helpSetName; private HelpSet helpSet = null; private HelpBroker helpBroker; 26 private ClassLoader myClassLoader; 27 28 boolean setup = false; 29 30 private static CBHelpSystem defaultHelpSystem; 32 private static Logger log = Logger.getLogger(CBHelpSystem.class.getName()); 33 34 40 41 public CBHelpSystem(String helpset_Name) 42 { 43 this(helpset_Name, null); 44 if (defaultHelpSystem == null) 45 defaultHelpSystem = this; 46 runDelayedConstructor(); 47 } 48 49 56 public CBHelpSystem(String helpset_Name, ClassLoader cl) 58 { 59 if (cl != null) myClassLoader = cl; 60 if (helpset_Name != null) helpSetName = helpset_Name; 61 runDelayedConstructor(); 62 if (defaultHelpSystem == null) 63 defaultHelpSystem = this; 64 } 65 66 public boolean ready() 67 { 68 return setup; 69 } 70 71 private synchronized void runDelayedConstructor() 72 { 73 79 doDelayedConstructor(); 80 86 } 87 88 92 protected synchronized void doDelayedConstructor() 93 { 94 if (setup == true) return; 96 if (myClassLoader == null) 97 myClassLoader = getClass().getClassLoader(); 98 99 helpSet = getHelpSet(helpSetName, myClassLoader); 100 101 if (helpSet == null) 102 { 103 log.warning("Error creating help set! Can't find: " + helpSetName); 104 return; } 106 else 107 { 108 helpBroker = helpSet.createHelpBroker(); 109 } 110 111 log.info("Initial HelpSet created: " + helpSet.toString()); 112 113 setup = true; } 115 116 117 public synchronized HelpSet getHelpSet() 118 { 119 return helpSet; 120 } 121 122 public synchronized HelpBroker getHelpBroker() 123 { 124 return helpBroker; 125 } 126 127 136 137 public static HelpSet getHelpSet(String newHelpSetName, ClassLoader loader) 138 { 139 HelpSet newHelpSet = null; 140 try 141 { 142 URL url = HelpSet.findHelpSet(loader, newHelpSetName); 143 if (url != null) 144 newHelpSet = new HelpSet(loader, url); 145 } 146 catch (NoClassDefFoundError ec) 147 { 148 log.log(Level.WARNING, "Help Broker initialization error: couldn't find help set ", ec); 149 } 150 catch (ExceptionInInitializerError ex) 151 { 152 log.log(Level.WARNING, "Help Broker initialization error: ", ex); 153 ex.getException().printStackTrace(); } 155 catch (Exception ee) 156 { 157 log.log(Level.WARNING, "Help Set '" + newHelpSetName + "' not found", ee); 158 } 159 160 return newHelpSet; 161 } 162 163 169 170 public void addHelpSet(String pluginHelpSet, ClassLoader loader) 171 { 172 if (helpSet != null) 173 helpSet.add(getHelpSet(pluginHelpSet, loader)); 174 else { 176 helpSet = getHelpSet(pluginHelpSet, loader); 177 if (helpSet != null) 178 helpBroker = helpSet.createHelpBroker(); 179 } 180 } 181 182 public void addHelpSet(HelpSet newSet) 183 { 184 if (newSet == null) return; 185 186 if (helpSet != null) 187 helpSet.add(newSet); 188 else { 190 helpSet = newSet; 191 if (helpSet != null) 192 helpBroker = helpSet.createHelpBroker(); 193 } 194 } 195 196 197 200 201 public void open() 202 { 203 if (setup == false) 204 doDelayedConstructor(); 205 206 if (helpBroker != null) 207 helpBroker.setDisplayed(true); 208 } 209 210 211 217 218 public void openTab(String tab) 219 { 220 if (setup == false) 221 doDelayedConstructor(); 222 223 if (helpBroker != null) 224 { 225 try 226 { 227 helpBroker.setCurrentView(tab); 228 } 229 catch (IllegalArgumentException ia) 230 { 231 log.warning("Caught Illegal Argument Exception - an incorrect Help view was asked for: " 232 + ia + "\n\nOpening help with default view..."); 233 open(); 234 return; 235 } 236 237 helpBroker.setDisplayed(true); 238 } 239 } 240 241 242 247 248 public void open(String contentID) 249 { 250 if (setup == false) 251 doDelayedConstructor(); 252 253 if (helpBroker != null) 254 helpBroker.setDisplayed(true); 255 256 try 257 { 258 helpBroker.setCurrentID(contentID); 259 } 260 catch (Exception e) 261 { 262 log.log(Level.WARNING, "Caught exception: ", e); 263 } 264 } 265 266 267 public static void setdefaultHelpSystem(CBHelpSystem set) 268 { 269 defaultHelpSystem = set; 270 } 271 272 public static CBHelpSystem getDefaultHelpSystem() 273 { 274 return defaultHelpSystem; 275 } 276 277 public static void addToDefaultHelpSystem(String newHelpSet, ClassLoader loader) 278 { 279 log.info("attempting to add help set: '" + newHelpSet + "' using loader: " + loader.toString()); 280 281 if (defaultHelpSystem == null) 282 { 283 CBHelpSystem newDefault = new CBHelpSystem(newHelpSet, loader); 284 log.info("trying to set default help system to: " + ((newDefault == null) ? "null" : newDefault.toString())); 286 if (newDefault != null) 287 setdefaultHelpSystem(newDefault); 288 } 289 else 290 { 291 HelpSet bloop = getHelpSet(newHelpSet, loader); 292 log.info("found help set: " + ((bloop == null) ? " null " : bloop.toString())); 293 defaultHelpSystem.addHelpSet(bloop); 294 } 295 } 296 297 298 305 306 public static void useDefaultHelp(JButton button, String helpString) 307 { 308 310 if (helpString == null) 311 return; 313 if (defaultHelpSystem == null) 314 log.warning("No default HelpSystem."); 315 316 if (defaultHelpSystem.ready() == false) 317 defaultHelpSystem.doDelayedConstructor(); 318 319 try 320 { 321 HelpBroker helpBrok = defaultHelpSystem.getHelpSet().createHelpBroker(); 322 helpBrok.enableHelpOnButton(button, helpString, defaultHelpSystem.getHelpSet()); 323 } 324 catch (Exception e) 325 { 326 log.log(Level.WARNING, "No HelpSet available: ", e); 327 } 328 } 329 330 331 } | Popular Tags |