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