[Buix-commits] r1280 - in jaxx/trunk: . jaxx-compiler-api jaxx-compiler-api/src/main/java/jaxx/compiler jaxx-compiler-api/src/main/java/jaxx/tags jaxx-compiler-swing jaxx-compiler-swing/src/main/java/jaxx jaxx-compiler-swing/src/main/java/jaxx/compiler jaxx-runtime-swing jaxx-runtime-swing/src/main/java/jaxx/runtime jaxx-runtime-swing/src/main/java/jaxx/runtime/swing
Author: tchemit Date: 2009-03-29 13:36:21 +0000 (Sun, 29 Mar 2009) New Revision: 1280 Added: jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/HelpRootCompiledObjectDecorator.java jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/JaxxHelpUI.java jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/JaxxHelpBroker.java Modified: jaxx/trunk/changelog.txt jaxx/trunk/jaxx-compiler-api/changelog.txt jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompiledObject.java jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompilerOptions.java jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/DefaultCompiledObjectDecorator.java jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/tags/DefaultComponentHandler.java jaxx/trunk/jaxx-compiler-swing/changelog.txt jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/SwingInitializer.java jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/BoxedCompiledObjectDecorator.java jaxx/trunk/jaxx-runtime-swing/changelog.txt Log: add generate-help goal Modified: jaxx/trunk/changelog.txt =================================================================== --- jaxx/trunk/changelog.txt 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/changelog.txt 2009-03-29 13:36:21 UTC (rev 1280) @@ -1,4 +1,5 @@ 1.3 chemit 20090320 + * 20090329 [chemit] - add java help mojo * 20090313 [chemit] - use i18n 0.10 1.2 ??? 2009???? Modified: jaxx/trunk/jaxx-compiler-api/changelog.txt =================================================================== --- jaxx/trunk/jaxx-compiler-api/changelog.txt 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-api/changelog.txt 2009-03-29 13:36:21 UTC (rev 1280) @@ -1,4 +1,7 @@ 1.3 chemit 20090321 + * 20090327 [chemit] - refactor clientProperties (no more generation in handler but in decorator) + - add javax help mecanism + * 20090321 [chemit] - add compilerCount in launchro to known how mush files where generated * 20090313 [chemit] - can now use geneticType on javaBean object - add an extra method $afterCompleteSetup method to be included if find in script at last statement of $completeSetup method Modified: jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompiledObject.java =================================================================== --- jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompiledObject.java 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompiledObject.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -72,6 +72,10 @@ * the decorator (if null will use {@link DefaultCompiledObjectDecorator}). */ private CompiledObjectDecorator decorator; + /** + * client properties + */ + private Map<String,String> clientProperties; public class ChildRef { @@ -433,6 +437,29 @@ properties.put(property, value); } + public boolean hasClientProperties() { + return clientProperties != null && !clientProperties.isEmpty(); + } + + public void addClientProperty(String property, String value) { + getClientProperties().put(property, value); + } + + public String getClientProperty(String key) { + if (!hasClientProperties()) { + return null; + } + return clientProperties.get(key); + } + + public Map<String, String> getClientProperties() { + if (clientProperties == null) { + clientProperties = new HashMap<String, String>(); + } + return clientProperties; + } + + /** * Returns all properties which have been set for this object. * Modified: jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompilerOptions.java =================================================================== --- jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompilerOptions.java 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/CompilerOptions.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -44,6 +44,40 @@ /** a flag to use {@link javax.swing.UIManager} to retreave icons. */ private boolean useUIManagerForIcon; + /** a flag to generate javax help for any */ + private boolean generateHelp; + + private String helpBrokerFQN; + /** + * The prefix to add to i18n key for any help i18n key. + * + * @since 1.3 + */ + protected String helpsetI18nPrefix; + /** + * The suffix to add to i18n key for an help Id. + * + * @since 1.3 + */ + protected String helpsetTitleI18nSuffix; + /** + * The suffix to add to i18n key for an toc Id. + * + * @since 1.3 + */ + protected String helpsetTocI18nSuffix; + /** + * The suffix to add to i18n key for an toc Id. + * + * @since 1.3 + */ + protected String helpsetIndexI18nSuffix; + /** + * The helpset name + * + * @since 1.3 + */ + protected String helpSetName; /** the default compiled object decorator to use if none specifed via decorator attribute */ private Class<? extends CompiledObjectDecorator> defaultDecoratorClass; @@ -291,5 +325,61 @@ this.profile = profile; } + public boolean isGenerateHelp() { + return generateHelp; + } + + public void setGenerateHelp(boolean generateHelp) { + this.generateHelp = generateHelp; + } + + public String getHelpBrokerFQN() { + return helpBrokerFQN; + } + + public void setHelpBrokerFQN(String helpBrokerFQN) { + this.helpBrokerFQN = helpBrokerFQN; + } + + public String getHelpsetIndexI18nSuffix() { + return helpsetIndexI18nSuffix; + } + + public void setHelpsetIndexI18nSuffix(String helpsetIndexI18nSuffix) { + this.helpsetIndexI18nSuffix = helpsetIndexI18nSuffix; + } + + public String getHelpsetTitleI18nSuffix() { + return helpsetTitleI18nSuffix; + } + + public void setHelpsetTitleI18nSuffix(String helpsetTitleI18nSuffix) { + this.helpsetTitleI18nSuffix = helpsetTitleI18nSuffix; + } + + public String getHelpsetTocI18nSuffix() { + return helpsetTocI18nSuffix; + } + + public void setHelpsetTocI18nSuffix(String helpsetTocI18nSuffix) { + this.helpsetTocI18nSuffix = helpsetTocI18nSuffix; + } + + public String getHelpSetName() { + return helpSetName; + } + + public void setHelpSetName(String helpSetName) { + this.helpSetName = helpSetName; + } + + public String getHelpsetI18nPrefix() { + return helpsetI18nPrefix; + } + + public void setHelpsetI18nPrefix(String helpsetI18nPrefix) { + this.helpsetI18nPrefix = helpsetI18nPrefix; + } + } Modified: jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/DefaultCompiledObjectDecorator.java =================================================================== --- jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/DefaultCompiledObjectDecorator.java 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/compiler/DefaultCompiledObjectDecorator.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -5,6 +5,7 @@ package jaxx.compiler; import java.lang.reflect.Modifier; +import java.util.Map.Entry; import jaxx.CompilerException; import jaxx.types.TypeManager; @@ -75,10 +76,18 @@ @Override public String createCompleteSetupMethod(JAXXCompiler compiler, CompiledObject object, JavaFile javaFile, StringBuffer initDataBindings) { StringBuffer code = new StringBuffer(); + //TC-20090327 generate client properties + if (object.hasClientProperties()) { + // generate putClientProperty invocations + for (Entry<String,String> entry : object.getClientProperties().entrySet()) { + object.appendAdditionCode(object.getJavaCode() + ".putClientProperty(\"" + entry.getKey() + "\", " + entry.getValue() + ");"); + } + + } //TC - 20081017 only generate the method if not empty ? if (object.getId().startsWith("$")) { code.append(object.getAdditionCode()).append(JAXXCompiler.getLineSeparator()); - } else { + } else { String additionCode = object.getAdditionCode(); if (additionCode.length() > 0) { code.append(object.getAdditionMethodName()).append("();").append(JAXXCompiler.getLineSeparator()); Modified: jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/tags/DefaultComponentHandler.java =================================================================== --- jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/tags/DefaultComponentHandler.java 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-api/src/main/java/jaxx/tags/DefaultComponentHandler.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -204,7 +204,9 @@ if (stringValue.startsWith("{")) { stringValue = stringValue.substring(1, stringValue.length() - 1); } - object.appendAdditionCode(object.getJavaCode() + ".putClientProperty(\"" + propertyName.substring(1) + "\", " + stringValue + ");"); + object.addClientProperty(propertyName.substring(1), stringValue); + //TC-20090327 rather not generating code here + //object.appendAdditionCode(object.getJavaCode() + ".putClientProperty(\"" + propertyName.substring(1) + "\", " + stringValue + ");"); return; } if ("icon".equals(propertyName)) { Modified: jaxx/trunk/jaxx-compiler-swing/changelog.txt =================================================================== --- jaxx/trunk/jaxx-compiler-swing/changelog.txt 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-swing/changelog.txt 2009-03-29 13:36:21 UTC (rev 1280) @@ -1,6 +1,7 @@ 1.3 chemit 20090320 + * 20090327 [chemit] - introduce javax help decorator -1.1 chemit 20090220 +1.1 chemit 20090220 * 20090122 [chemit] - refactor poms (sibling dependencies, pluginsManagment,...) 1.0 chemit 20090111 Modified: jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/SwingInitializer.java =================================================================== --- jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/SwingInitializer.java 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/SwingInitializer.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -31,6 +31,7 @@ import java.awt.Insets; import jaxx.compiler.BoxedCompiledObjectDecorator; import jaxx.compiler.CompiledObjectDecorator; +import jaxx.compiler.HelpRootCompiledObjectDecorator; public class SwingInitializer implements Initializer { @@ -89,5 +90,6 @@ TypeManager.registerTypeConverter(KeyStroke.class, new KeyStrokeConverter()); CompiledObjectDecorator.registerDecorator("boxed", BoxedCompiledObjectDecorator.class); + CompiledObjectDecorator.registerDecorator("help", HelpRootCompiledObjectDecorator.class); } } \ No newline at end of file Modified: jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/BoxedCompiledObjectDecorator.java =================================================================== --- jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/BoxedCompiledObjectDecorator.java 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/BoxedCompiledObjectDecorator.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -1,6 +1,7 @@ package jaxx.compiler; import jaxx.compiler.CompiledObject.ChildRef; +import jaxx.runtime.SwingUtil; /** * A decorator to surround a compiled object (should be a component at least) @@ -17,7 +18,7 @@ for (ChildRef child : parent.getChilds()) { if (child.getChild() == object) { String javaCode = child.getChildJavaCode(); - child.setChildJavaCode("jaxx.runtime.SwingUtil.boxComponentWithJxLayer(" + javaCode + ")"); + child.setChildJavaCode(SwingUtil.class.getName()+".boxComponentWithJxLayer(" + javaCode + ")"); break; } } Added: jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/HelpRootCompiledObjectDecorator.java =================================================================== --- jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/HelpRootCompiledObjectDecorator.java (rev 0) +++ jaxx/trunk/jaxx-compiler-swing/src/main/java/jaxx/compiler/HelpRootCompiledObjectDecorator.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -0,0 +1,93 @@ +package jaxx.compiler; + +import java.lang.reflect.Modifier; +import java.util.Iterator; +import java.util.Set; + +/** + * A decorator to place on a root compiled object to process javaHelp on the file. + * + * @author tony + * @since 1.2 + */ +public class HelpRootCompiledObjectDecorator extends DefaultCompiledObjectDecorator { + + /** + * the list of discovered helpId + */ + protected static Set<String> helpIds = new java.util.HashSet<String>(); + + protected String getBrokerFQN(JAXXCompiler compiler) { + String helpBrokerFQN = compiler.getOptions().getHelpBrokerFQN(); + return helpBrokerFQN; + } + + protected String getHelpId(CompiledObject o) { + String helpID = null; + if (o.hasClientProperties()) { + helpID = o.getClientProperty("help"); + } + return helpID; + } + + @Override + public void finalizeCompiler(JAXXCompiler compiler, CompiledObject root, CompiledObject object, JavaFile javaFile, String packageName, String className, String fullClassName) { + super.finalizeCompiler(compiler, root, object, javaFile, packageName, className, fullClassName); + CompilerOptions options = compiler.getOptions(); + + if (options.isGenerateHelp()) { + + // add JaxxHelpUI interface + Class<?> validatorInterface = jaxx.runtime.JaxxHelpUI.class; + String helpBrokerFQN = getBrokerFQN(compiler); + javaFile.addInterface(JAXXCompiler.getCanonicalName(validatorInterface) + "<" + helpBrokerFQN + ">"); + + javaFile.addMethod(JavaMethod.newMethod(Modifier.PUBLIC, "void", "registerHelpId", + "broker.installUI(component, helpId);", + new JavaArgument(helpBrokerFQN, "broker"), + new JavaArgument("Component", "component"), + new JavaArgument("String", "helpId"))); + + javaFile.addMethod(JavaMethod.newMethod(Modifier.PUBLIC, "void", "showHelp", + "getBroker().showHelp(this, helpId);", + new JavaArgument("String", "helpId"))); + + StringBuilder buffer = new StringBuilder(); + + String lineSeparator = JAXXCompiler.getLineSeparator(); + + if (options.isGenerateHelp()) { + + // add code to init javax help system + Iterator<CompiledObject> itr = compiler.getObjectCreationOrder(); + + for (; itr.hasNext();) { + CompiledObject o = itr.next(); + String helpID = getHelpId(o); + if (helpID != null) { + buffer.append(lineSeparator); + // detects a helpId to register + buffer.append("registerHelpId(_broker, " + o.getJavaCode() + ", " + helpID + ");"); + //keep the helpID for helpSet generation + helpIds.add(helpID); + } + } + } + if (buffer.length() > 0) { + + StringBuilder extraCode = new StringBuilder(helpBrokerFQN).append(" _broker = getBroker();"); + + buffer.append(lineSeparator).append("_broker.prepareUI(this);"); + buffer.append(lineSeparator); + + // add the calls + compiler.appendLateInitializer(extraCode.toString()); + compiler.appendLateInitializer(buffer.toString()); + } + } + } + + public static Set<String> getHelpIds() { + return helpIds; + } +} Modified: jaxx/trunk/jaxx-runtime-swing/changelog.txt =================================================================== --- jaxx/trunk/jaxx-runtime-swing/changelog.txt 2009-03-24 14:37:47 UTC (rev 1279) +++ jaxx/trunk/jaxx-runtime-swing/changelog.txt 2009-03-29 13:36:21 UTC (rev 1280) @@ -1,4 +1,5 @@ 1.3 chemit 20090321 + * 20090327 [chemit] - add javax help mecanism * 20090318 [chemit] - introduce the BlockingLayerUI2 class (should be merge with BlockingLayerUI) * 20090318 [chemit] - introduce the CardLayout2Ext class * 20090312 [chemit] - add some usefull code from ObServe (load Nimbus L&F, load ui configuration) Added: jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/JaxxHelpUI.java =================================================================== --- jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/JaxxHelpUI.java (rev 0) +++ jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/JaxxHelpUI.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -0,0 +1,23 @@ +package jaxx.runtime; + +import java.awt.Component; +import jaxx.runtime.swing.JaxxHelpBroker; + +/** + * + * Contract to be added on JAXXObject wihch wants to use javax help. + * + * @param <B> type of broker. + * + * @author tony + * @since 1.3 + * @see JaxxHelpBroker + */ +public interface JaxxHelpUI<B extends JaxxHelpBroker> { + + B getBroker(); + + void registerHelpId(B broker, Component component, String helpId); + + void showHelp(String helpId); +} Added: jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/JaxxHelpBroker.java =================================================================== --- jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/JaxxHelpBroker.java (rev 0) +++ jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/JaxxHelpBroker.java 2009-03-29 13:36:21 UTC (rev 1280) @@ -0,0 +1,510 @@ +package jaxx.runtime.swing; + +import java.applet.Applet; +import java.awt.*; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.net.URL; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; +import java.util.Vector; +import javax.help.CSH; +import javax.help.CSH.DisplayHelpFromSource; +import javax.help.HelpBroker; +import javax.help.HelpSet; +import javax.swing.AbstractButton; +import javax.swing.JComponent; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import jaxx.runtime.JAXXContext; +import jaxx.runtime.JAXXObject; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * La classe pour encapsuler l'aide de l'application. + * + * @param <B> le type de broker + * @author tony + * @since 1.4 + */ +public abstract class JaxxHelpBroker<B extends JaxxHelpBroker> { + + public static final String JAXX_CONTEXT_ENTRY = "jaxxcontext"; + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(JaxxHelpBroker.class); + protected final String helpsetName; + protected final String defaultID; + protected final String helpKey; + // Main HelpSet & Broker + protected final HelpSet helpset; + protected final HelpBroker helpBroker; + protected ActionListener showHelpAction; + protected Hashtable<Component, Cursor> cursors; + protected Cursor onItemCursor; + + protected JaxxHelpBroker(String helpsetName, String helpKey, String defaultID) { + if (helpsetName == null) { + throw new NullPointerException("parameter helpsetName can not be null!"); + } + this.helpsetName = helpsetName; + this.helpKey = helpKey; + this.defaultID = defaultID; + try { + ClassLoader cl = getClass().getClassLoader(); + URL url = HelpSet.findHelpSet(cl, helpsetName); + helpset = new HelpSet(cl, url); + helpBroker = helpset.createHelpBroker(); + } catch (Exception ee) { + throw new IllegalStateException("could not find help set " + helpsetName + " for reason " + ee.getMessage(), ee); + } + } + + public AbstractButton getShowHelperButton(JAXXObject c) { + return (AbstractButton) c.getObjectById("showHelp"); + } + + public void prepareUI(JAXXObject c) { + if (c == null) { + throw new NullPointerException("parameter c can not be null!"); + } + + // l'ui doit avoir un boutton showHelp + AbstractButton help = getShowHelperButton(c); + + if (help == null) { + log.warn("no showButton detected for " + c.getClass()); + } else { + boolean needListener = true; + for (ActionListener a : help.getActionListeners()) { + if (a instanceof DisplayHelpFromSource) { + needListener = false; + break; + } + } + if (needListener) { + // attach context to button + help.putClientProperty(JAXX_CONTEXT_ENTRY, c.getDelegateContext()); + help.addActionListener(getShowHelpAction(c)); + } + + } + if (log.isDebugEnabled()) { + log.debug("for " + c); + } + + //installUI(c, new java.util.ArrayList<String>()); + } + + public HelpBroker getHelpBroker() { + return helpBroker; + } + + public String getHelpKey() { + return helpKey; + } + + public HelpSet getHelpset() { + return helpset; + } + + public String getHelpsetName() { + return helpsetName; + } + + public String getDefaultID() { + return defaultID; + } + + public void showHelpSet() { + log.info(this); + new CSH.DisplayHelpFromSource(helpBroker); + } + + public void showHelp(JAXXContext context, String helpId) { + } + + public void installUI(Component comp, String helpId) { + CSH.setHelpIDString(comp, helpId); + } + + protected void installUI(Object comp, List<String> scanned) { + //log.info(comp); + if (comp instanceof JComponent) { + JComponent c = (JComponent) comp; + if (scanned.contains(c.getName())) { + return; + } else { + scanned.add(c.getName()); + } + Object id = c.getClientProperty(helpKey); + if (id != null && id instanceof String) { + String helpId = (String) id; + + CSH.setHelpIDString(c, helpId); + + log.info(c.getName() + " : " + helpId); + + if (log.isDebugEnabled()) { + log.debug(c.getName() + " : " + helpId); + + } + } + } + + if (comp instanceof JAXXObject) { + JAXXObject jo = (JAXXObject) comp; + + + Map<String, Object> $objectMap = jo.get$objectMap(); + for (String key : $objectMap.keySet()) { + + if (scanned.contains(key)) { + continue; + } + + Object o = $objectMap.get(key); + + if (o == comp) { + continue; + } + + if (o instanceof JAXXObject) { + installUI(o, scanned); + scanned.add(key); + continue; + } + if (o instanceof JComponent) { + installUI(o, scanned); + continue; + } + } + } + } + + protected ActionListener getShowHelpAction(JAXXContext context) { + if (showHelpAction == null) { + showHelpAction = addShowHelpAction(context); + } + return showHelpAction; + } + + protected ActionListener addShowHelpAction(JAXXContext context) { + return new CSH.DisplayHelpAfterTracking(helpBroker); + } + + protected String getHelpID(Component source) { + String helpID = null; + + // It is necessery for UIManager.get("HelpOnItemCursor"); + + // Get the onItemCursor + onItemCursor = (Cursor) UIManager.get("HelpOnItemCursor"); + if (onItemCursor == null) { + return null; + } + + // change all the cursors on all windows + Vector topComponents = null; + cursors = null; + + if (onItemCursor != null) { + cursors = new Hashtable<Component, Cursor>(); + topComponents = getTopContainers(source); + Enumeration enums = topComponents.elements(); + while (enums.hasMoreElements()) { + setAndStoreCursors((Container) enums.nextElement(), onItemCursor); + } + } + /*MouseEvent event = getMouseEvent(); + + if (event != null) { + Component comp = (Component)event.getSource(); + log.info("component traking!!!!!!!! " + comp.getName()+" : "+comp.getClass().getName()); + }*/ + + + Object o = CSH.trackCSEvents(); + + if (o instanceof Component) { + + helpID = CSH.getHelpIDString((Component) o); + + log.info("component traking " + ((Component) o).getName() + " : " + helpID); + + if (log.isDebugEnabled()) { + log.debug("component traking " + ((Component) o).getName() + " : " + helpID); + } + } + + /*HelpSet objHS = getHelpset(); + try { + ID id = ID.create(helpID, objHS); + if (id == null) { + id = objHS.getHomeID(); + } + + } catch (Exception e2) { + e2.printStackTrace(); + } + if (helpID == null) { + helpID = getDefaultID(); + }*/ + + // restore the old cursors + if (topComponents != null) { + Enumeration containers = topComponents.elements(); + while (containers.hasMoreElements()) { + resetAndRestoreCursors((Container) containers.nextElement()); + } + } + cursors = null; + + return helpID; + } + + /* + * Get all top level containers to change it's cursors + */ + protected Vector getTopContainers(Object source) { + // This method is used to obtain all top level components of application + // for which the changing of cursor to question mark is wanted. + // Method Frame.getFrames() is used to get list of Frames and + // Frame.getOwnedWindows() method on elements of the list + // returns all Windows, Dialogs etc. It works correctly in application. + // Problem is in applets. There is no way how to get reference to applets + // from elsewhere than applet itself. So, if request for CSH (this means + // pressing help button or select help menu item) does't come from component + // in a Applet, cursor for applets is not changed to question mark. Only for + // Frames, Windows and Dialogs is cursor changed properly. + + Vector<Component> containers = new Vector<Component>(); + Component topComponent = null; + topComponent = getRoot(source); + if (topComponent instanceof Applet) { + try { + Enumeration<Applet> applets = ((Applet) topComponent).getAppletContext().getApplets(); + while (applets.hasMoreElements()) { + containers.add(applets.nextElement()); + } + } catch (NullPointerException npe) { + containers.add(topComponent); + } + } + Frame frames[] = Frame.getFrames(); + for (int i = 0; i < frames.length; i++) { + Window[] windows = frames[i].getOwnedWindows(); + for (int j = 0; j < windows.length; j++) { + containers.add(windows[j]); + } + if (!containers.contains(frames[i])) { + containers.add(frames[i]); + } + } + return containers; + } + + protected Component getRoot(Object comp) { + Object parent = comp; + while (parent != null) { + comp = parent; + if (comp instanceof MenuComponent) { + parent = ((MenuComponent) comp).getParent(); + } else if (comp instanceof Component) { + if (comp instanceof Window) { + break; + } + if (comp instanceof Applet) { + break; + } + parent = ((Component) comp).getParent(); + } else { + break; + } + } + if (comp instanceof Component) { + return ((Component) comp); + } + return null; + } + + + /* + * Set the cursor for a component and its children. + * Store the old cursors for future resetting + */ + protected void setAndStoreCursors(Component comp, Cursor cursor) { + if (comp == null) { + return; + } + Cursor compCursor = comp.getCursor(); + if (compCursor != cursor) { + cursors.put(comp, compCursor); + log.debug("set cursor on " + comp); + comp.setCursor(cursor); + } + if (comp instanceof Container) { + Component component[] = ((Container) comp).getComponents(); + for (int i = 0; i < component.length; i++) { + setAndStoreCursors(component[i], cursor); + } + } + } + + /* + * Actually restore the cursor for a component and its children + */ + protected void resetAndRestoreCursors(Component comp) { + if (comp == null) { + return; + } + Cursor oldCursor = cursors.get(comp); + if (oldCursor != null) { + log.debug("restored cursor " + oldCursor + " on " + comp); + comp.setCursor(oldCursor); + } + if (comp instanceof Container) { + Component component[] = ((Container) comp).getComponents(); + for (int i = 0; i < component.length; i++) { + resetAndRestoreCursors(component[i]); + } + } + } + + /** + * Context Sensitive Event Tracking + * + * Creates a new EventDispatchThread from which to dispatch events. This + * method returns when stopModal is invoked. + * + * @return MouseEvent The mouse event occurred. Null if + * cancelled on an undetermined object. + */ + public static MouseEvent getMouseEvent() { + // Should the cursor change to a quesiton mark here or + // require the user to change the cursor externally to this method? + // The problem is that each component can have it's own cursor. + // For that reason it might be better to have the user change the + // cusor rather than us. + + // To track context-sensitive events get the event queue and process + // the events the same way EventDispatchThread does. Filter out + // ContextSensitiveEvents SelectObject & Cancel (MouseDown & ???). + // Note: This code only handles mouse events. Accessiblity might + // require additional functionality or event trapping + + // If the eventQueue can't be retrieved, the thread gets interrupted, + // or the thread isn't a instanceof EventDispatchThread then return + // a null as we won't be able to trap events. + try { + if (EventQueue.isDispatchThread()) { + EventQueue eq = null; + + // Find the eventQueue. If we can't get to it then just return + // null since we won't be able to trap any events. + + try { + eq = Toolkit.getDefaultToolkit().getSystemEventQueue(); + } catch (Exception ee) { + log.debug(ee); + } + + // Safe guard + if (eq == null) { + return null; + } + + int eventNumber = -1; + + // Process the events until an object has been selected or + // the context-sensitive search has been canceled. + while (true) { + // This is essentially the body of EventDispatchThread + // modified to trap context-senstive events and act + // appropriately + eventNumber++; + AWTEvent event = eq.getNextEvent(); + Object src = event.getSource(); + // can't call eq.dispatchEvent + // so I pasted it's body here + + // debug(event); + + // Not sure if I should suppress ActiveEvents or not + // Modal dialogs do. For now we will not suppress the + // ActiveEvent events + + if (event instanceof ActiveEvent) { + ((ActiveEvent) event).dispatch(); + continue; + } + + if (src instanceof Component) { + // Trap the context-sensitive events here + if (event instanceof KeyEvent) { + KeyEvent e = (KeyEvent) event; + // if this is the cancel key then exit + // otherwise pass all other keys up + if (e.getKeyCode() == KeyEvent.VK_CANCEL || + e.getKeyCode() == KeyEvent.VK_ESCAPE) { + e.consume(); + return null; + } else { + e.consume(); + // dispatchEvent(event); + } + } else if (event instanceof MouseEvent) { + MouseEvent e = (MouseEvent) event; + int eID = e.getID(); + if ((eID == MouseEvent.MOUSE_CLICKED || + eID == MouseEvent.MOUSE_PRESSED || + eID == MouseEvent.MOUSE_RELEASED) && + SwingUtilities.isLeftMouseButton(e)) { + if (eID == MouseEvent.MOUSE_CLICKED) { + if (eventNumber == 0) { + dispatchEvent(event); + continue; + } + } + e.consume(); + return e; + } else { + e.consume(); + } + } else { + dispatchEvent(event); + } + } else if (src instanceof MenuComponent) { + if (event instanceof InputEvent) { + ((InputEvent) event).consume(); + } + } else { + System.err.println("unable to dispatch event: " + event); + } + } + } + } catch (InterruptedException e) { + log.debug(e); + } + log.debug("Fall Through code"); + return null; + } + + private static void dispatchEvent(AWTEvent event) { + Object src = event.getSource(); + if (event instanceof ActiveEvent) { + // This could become the sole method of dispatching in time. + ((ActiveEvent) event).dispatch(); + } else if (src instanceof Component) { + ((Component) src).dispatchEvent(event); + } else if (src instanceof MenuComponent) { + ((MenuComponent) src).dispatchEvent(event); + } else { + System.err.println("unable to dispatch event: " + event); + } + } +}
participants (1)
-
tchemit@users.labs.libre-entreprise.org