001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017
018package org.opends.quicksetup.ui;
019
020import java.awt.Component;
021import java.awt.event.ActionEvent;
022import java.awt.event.ActionListener;
023import java.util.HashSet;
024
025import javax.swing.JButton;
026import javax.swing.text.Element;
027import javax.swing.text.View;
028import javax.swing.text.ViewFactory;
029import javax.swing.text.html.FormView;
030import javax.swing.text.html.HTMLEditorKit;
031
032/**
033 * Class used to be able to detect events in the button inside an HTML pane.
034 */
035public class CustomHTMLEditorKit extends HTMLEditorKit
036{
037  private HashSet<ActionListener> listeners = new HashSet<>();
038  private static final long serialVersionUID = 298103926252426388L;
039
040  /**
041   * Default constructor.
042   */
043  public CustomHTMLEditorKit()
044  {
045    super();
046  }
047
048  /** {@inheritDoc} */
049  public ViewFactory getViewFactory()
050  {
051    return new MyHTMLFactory();
052  }
053
054  /**
055   * Adds an action listener.
056   * @param l the action listener to add.
057   */
058  public void addActionListener(ActionListener l)
059  {
060    listeners.add(l);
061  }
062
063  /**
064   * Removes an action listener.
065   * @param l the action listener to remove.
066   */
067  public void removeActionListener(ActionListener l)
068  {
069    listeners.remove(l);
070  }
071
072  /**
073   * Class used to be able to detect events in the button inside an HTML pane.
074   */
075  class MyHTMLFactory extends HTMLFactory
076  {
077    /** {@inheritDoc} */
078    public View create(Element elem)
079    {
080      View v = super.create(elem);
081      if (v instanceof FormView)
082      {
083        v = new MyFormView(elem);
084      }
085      return v;
086    }
087  }
088
089  /**
090   * Class used to be able to detect events in the button inside an HTML pane.
091   */
092  class MyFormView extends FormView
093  {
094    /**
095     * Creates a new FormView object.
096     *
097     * @param elem the element to decorate
098     */
099    MyFormView(Element elem)
100    {
101      super(elem);
102    }
103
104    /** {@inheritDoc} */
105    public void actionPerformed(ActionEvent ev)
106    {
107      if (ev != null && ev.getWhen() != lastActionWhen) {
108        lastActionWhen = ev.getWhen();
109        for (ActionListener l: listeners)
110        {
111          l.actionPerformed(ev);
112        }
113      }
114    }
115
116    /** {@inheritDoc} */
117    protected Component createComponent()
118    {
119      Component comp = super.createComponent();
120      if (comp instanceof JButton)
121      {
122        ((JButton)comp).setOpaque(false);
123      }
124      return comp;
125    }
126  }
127
128  private static long lastActionWhen;
129
130}