View Javadoc
1   package fr.ifremer.dali.ui.swing;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.core.dao.technical.Assert;
27  import jaxx.runtime.JAXXObject;
28  import jaxx.runtime.SwingUtil;
29  import jaxx.runtime.awt.visitor.BuildTreeVisitor;
30  import jaxx.runtime.awt.visitor.ComponentTreeNode;
31  import jaxx.runtime.awt.visitor.GetCompopentAtPointVisitor;
32  import jaxx.runtime.swing.help.JAXXHelpBroker;
33  import jaxx.runtime.swing.help.JAXXHelpUI;
34  import jaxx.runtime.swing.help.JAXXHelpUIHandler;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import javax.swing.AbstractButton;
39  import java.awt.Component;
40  import java.awt.Point;
41  import java.awt.event.ActionListener;
42  import java.awt.event.MouseEvent;
43  
44  /**
45   * Help broker.
46   *
47   * @since 1.1
48   */
49  public class DaliHelpBroker extends JAXXHelpBroker {
50  
51      /**
52       * Logger
53       */
54      private static final Log LOG = LogFactory.getLog(DaliHelpBroker.class);
55  
56      /**
57       * <p>Constructor for DaliHelpBroker.</p>
58       *
59       * @param defaultID a {@link java.lang.String} object.
60       */
61      public DaliHelpBroker(String defaultID) {
62          super("dali", "help",
63                  defaultID,
64                  (JAXXHelpUIHandler) DaliUIContext.getInstance());
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public void prepareUI(JAXXObject c) {
70  
71          Assert.notNull(c, "parameter c can not be null!");
72  
73          // l'ui doit avoir un boutton showHelp
74          AbstractButton help = getShowHelpButton(c);
75  
76          if (help != null) {
77  
78              // attach context to button
79              if (LOG.isDebugEnabled()) {
80                  LOG.debug("attach context to showhelp button " + c);
81              }
82              help.putClientProperty(JAXX_CONTEXT_ENTRY, c);
83  
84              // add tracking action
85              ActionListener listener = getShowHelpAction();
86              if (LOG.isDebugEnabled()) {
87                  LOG.debug("adding tracking action " + listener);
88              }
89              help.addActionListener(listener);
90  
91              if (LOG.isDebugEnabled()) {
92                  LOG.debug("done for " + c);
93              }
94          }
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public String findHelpId(Component comp) {
100 
101         if (comp == null) {
102             comp = DaliUIContext.getInstance().getMainUI();
103         }
104         JAXXHelpUI<?> parentContainer = SwingUtil.getParent(comp, JAXXHelpUI.class);
105 
106         String result;
107         if (parentContainer != null && this != parentContainer.getBroker()) {
108 
109             JAXXHelpBroker broker = parentContainer.getBroker();
110             result = broker.findHelpId(comp);
111         } else {
112             result = super.findHelpId(comp);
113         }
114 
115         if (result == null) {
116             result = "dali.index.help";
117         }
118 
119         return result;
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public Component getDeppestComponent(Component mouseComponent, MouseEvent event) {
125         ComponentTreeNode tree = BuildTreeVisitor.buildTree(mouseComponent);
126 
127         Point point = event.getPoint();
128 
129         Component component = GetCompopentAtPointVisitor.get(tree, point);
130         if (LOG.isDebugEnabled()) {
131             LOG.debug("Component at (" + point + "): " + component);
132         }
133         return component;
134     }
135 
136 }