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-2016 ForgeRock AS. 016 */ 017package org.opends.quicksetup.ui; 018 019import java.awt.Component; 020import java.awt.event.ActionEvent; 021import java.awt.event.ActionListener; 022import java.util.HashSet; 023import java.util.Set; 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/** Class used to be able to detect events in the button inside an HTML pane. */ 033public class CustomHTMLEditorKit extends HTMLEditorKit 034{ 035 private final Set<ActionListener> listeners = new HashSet<>(); 036 private static final long serialVersionUID = 298103926252426388L; 037 038 /** Default constructor. */ 039 public CustomHTMLEditorKit() 040 { 041 super(); 042 } 043 044 @Override 045 public ViewFactory getViewFactory() 046 { 047 return new MyHTMLFactory(); 048 } 049 050 /** 051 * Adds an action listener. 052 * @param l the action listener to add. 053 */ 054 public void addActionListener(ActionListener l) 055 { 056 listeners.add(l); 057 } 058 059 /** 060 * Removes an action listener. 061 * @param l the action listener to remove. 062 */ 063 public void removeActionListener(ActionListener l) 064 { 065 listeners.remove(l); 066 } 067 068 /** Class used to be able to detect events in the button inside an HTML pane. */ 069 private class MyHTMLFactory extends HTMLFactory 070 { 071 @Override 072 public View create(Element elem) 073 { 074 View v = super.create(elem); 075 if (v instanceof FormView) 076 { 077 v = new MyFormView(elem); 078 } 079 return v; 080 } 081 } 082 083 /** Class used to be able to detect events in the button inside an HTML pane. */ 084 private class MyFormView extends FormView 085 { 086 /** 087 * Creates a new FormView object. 088 * 089 * @param elem the element to decorate 090 */ 091 private MyFormView(Element elem) 092 { 093 super(elem); 094 } 095 096 @Override 097 public void actionPerformed(ActionEvent ev) 098 { 099 if (ev != null && ev.getWhen() != lastActionWhen) { 100 lastActionWhen = ev.getWhen(); 101 for (ActionListener l: listeners) 102 { 103 l.actionPerformed(ev); 104 } 105 } 106 } 107 108 @Override 109 protected Component createComponent() 110 { 111 Component comp = super.createComponent(); 112 if (comp instanceof JButton) 113 { 114 ((JButton)comp).setOpaque(false); 115 } 116 return comp; 117 } 118 } 119 120 private static long lastActionWhen; 121 122}