1 19 20 package org.openide.util; 21 22 import java.beans.BeanDescriptor ; 23 import java.beans.IntrospectionException ; 24 import java.beans.Introspector ; 25 import java.net.URL ; 26 import java.util.logging.Logger ; 27 import javax.swing.JComponent ; 28 29 35 public final class HelpCtx extends Object { 36 private static final Logger err = Logger.getLogger("org.openide.util.HelpCtx"); 38 42 public final static HelpCtx DEFAULT_HELP = new HelpCtx(HelpCtx.class.getName() + ".DEFAULT_HELP"); 44 45 private final URL helpCtx; 46 47 48 private final String helpID; 49 50 54 @Deprecated 55 public HelpCtx(URL helpCtx) { 56 this.helpCtx = helpCtx; 57 this.helpID = null; 58 } 59 60 68 public HelpCtx(String helpID) { 69 this.helpID = helpID; 70 this.helpCtx = null; 71 } 72 73 79 public HelpCtx(Class clazz) { 80 this(clazz.getName()); 81 } 82 83 86 public URL getHelp() { 87 return helpCtx; 88 } 89 90 93 public String getHelpID() { 94 return helpID; 95 } 96 97 public int hashCode() { 99 int base = HelpCtx.class.hashCode(); 100 101 if (helpCtx != null) { 102 base ^= helpCtx.hashCode(); 103 } 104 105 if (helpID != null) { 106 base ^= helpID.hashCode(); 107 } 108 109 return base; 110 } 111 112 public boolean equals(Object o) { 113 if ((o == null) || !(o instanceof HelpCtx)) { 114 return false; 115 } 116 117 HelpCtx oo = (HelpCtx) o; 118 119 return ((helpCtx == oo.helpCtx) || ((helpCtx != null) && helpCtx.equals(oo.helpCtx))) && 120 ((helpID == oo.helpID) || ((helpID != null) && helpID.equals(oo.helpID))); 121 } 122 123 public String toString() { 124 if (helpID != null) { 125 return "HelpCtx[" + helpID + "]"; } else { 127 return "HelpCtx[" + helpCtx + "]"; } 129 } 130 131 135 public static void setHelpIDString(JComponent comp, String helpID) { 136 err.fine("setHelpIDString: " + helpID + " on " + comp); 137 138 comp.putClientProperty("HelpID", helpID); } 140 141 150 public static HelpCtx findHelp(java.awt.Component comp) { 151 err.fine("findHelp on " + comp); 152 153 while (comp != null) { 154 if (comp instanceof HelpCtx.Provider) { 155 HelpCtx h = ((HelpCtx.Provider) comp).getHelpCtx(); 156 157 err.fine("found help " + h + " through HelpCtx.Provider interface"); 158 159 return h; 160 } 161 162 if (comp instanceof JComponent ) { 163 JComponent jc = (JComponent ) comp; 164 String hid = (String ) jc.getClientProperty("HelpID"); 166 if (hid != null) { 167 err.fine("found help " + hid + " by client property"); 168 169 return new HelpCtx(hid); 170 } 171 } 172 173 comp = comp.getParent(); 174 175 err.fine("no luck, trying parent " + comp); 176 } 177 178 err.fine("nothing found"); 179 180 return DEFAULT_HELP; 181 } 182 183 195 public static HelpCtx findHelp(Object instance) { 196 if (instance instanceof java.awt.Component ) { 197 return findHelp((java.awt.Component ) instance); 198 } 199 200 if (instance instanceof HelpCtx.Provider) { 201 return ((HelpCtx.Provider) instance).getHelpCtx(); 202 } 203 204 try { 205 BeanDescriptor d = Introspector.getBeanInfo(instance.getClass()).getBeanDescriptor(); 206 String v = (String ) d.getValue("helpID"); 208 if (v != null) { 209 return new HelpCtx(v); 210 } 211 } catch (IntrospectionException e) { 212 err.fine("findHelp on " + instance + ": " + e); 213 } 214 215 return HelpCtx.DEFAULT_HELP; 216 } 217 218 224 public static interface Provider { 225 230 public HelpCtx getHelpCtx(); 231 } 232 } 233 | Popular Tags |