All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.demo.DemoHelpBroker Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-6
Show newest version
package jaxx.demo;

/*
 * #%L
 * JAXX :: Demo
 * %%
 * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.base.Preconditions;
import jaxx.runtime.JAXXObject;
import jaxx.runtime.SwingUtil;
import jaxx.runtime.awt.visitor.BuildTreeVisitor;
import jaxx.runtime.awt.visitor.ComponentTreeNode;
import jaxx.runtime.awt.visitor.DebugComponentTreeNodeVisitor;
import jaxx.runtime.awt.visitor.GetCompopentAtPointVisitor;
import jaxx.runtime.swing.help.JAXXHelpBroker;
import jaxx.runtime.swing.help.JAXXHelpUI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.AbstractButton;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

/**
 * Help broker.
 *
 * @author Tony Chemit - [email protected]
 * @since 1.1
 */
public class DemoHelpBroker extends JAXXHelpBroker {

    /** Logger */
    private static final Log log = LogFactory.getLog(DemoHelpBroker.class);

    public DemoHelpBroker(String defaultID) {
        super("tutti", "help",
              defaultID,
              DemoApplicationContext.get());
    }

    @Override
    public void prepareUI(JAXXObject c) {

        Preconditions.checkNotNull(c, "parameter c can not be null!");

        // l'ui doit avoir un boutton showHelp
        AbstractButton help = getShowHelpButton(c);

        if (help != null) {

            // attach context to button
            if (log.isDebugEnabled()) {
                log.debug("attach context to showhelp button " + c);
            }
            help.putClientProperty(JAXX_CONTEXT_ENTRY, c);

            // add tracking action
            ActionListener listener = getShowHelpAction();
            if (log.isDebugEnabled()) {
                log.debug("adding tracking action " + listener);
            }
            help.addActionListener(listener);

            if (log.isDebugEnabled()) {
                log.debug("done for " + c);
            }
        }
    }

    @Override
    public String findHelpId(Component comp) {

        if (comp == null) {
            comp = DemoApplicationContext.get().getMainUI();
        }
        JAXXHelpUI parentContainer = SwingUtil.getParent(comp, JAXXHelpUI.class);

        String result;
        if (parentContainer != null && this != parentContainer.getBroker()) {

            JAXXHelpBroker broker = parentContainer.getBroker();
            result = broker.findHelpId(comp);
        } else {
            result = super.findHelpId(comp);
        }

        if (result == null) {
            result = "ui.main.menu";
        }

        return result;
    }

    @Override
    public Component getDeppestComponent(Component mouseComponent, MouseEvent event) {
        ComponentTreeNode tree = BuildTreeVisitor.buildTree(mouseComponent);

        DebugComponentTreeNodeVisitor debugTree = new DebugComponentTreeNodeVisitor() {
            @Override
            public String getMessage(ComponentTreeNode componentTree) {
                String message = super.getMessage(componentTree);
                Component userObject = componentTree.getUserObject();
                if (userObject.isShowing() && userObject.isVisible()) {
                    Rectangle rectangle = new Rectangle(userObject.getLocationOnScreen(), userObject.getSize());
                    message += " visible - " + rectangle;
                } else {
                    message += " invisible";
                }
                return message;
            }
        };

        debugTree.setDebug(log.isDebugEnabled());

        debugTree.parse(tree);

//        Point point = event.getPoint();
        Point point = event.getLocationOnScreen();

        Component component = GetCompopentAtPointVisitor.get(tree, point);
        if (log.isDebugEnabled()) {
            log.debug("Component at (" + point + "): " + component);
        }
        return component;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy