| 1 | /* |
| 2 | GNU Lesser General Public License |
| 3 | |
| 4 | PropertiesDialog |
| 5 | Copyright (C) 2003 Frits Jalvingh, Jerry Pommer & Howard Kistler |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | package com.hexidec.ekit.component; |
| 23 | |
| 24 | import java.util.Enumeration; |
| 25 | import javax.swing.event.DocumentEvent; |
| 26 | import javax.swing.event.UndoableEditEvent; |
| 27 | import javax.swing.text.AbstractDocument; |
| 28 | import javax.swing.text.AbstractDocument.BranchElement; |
| 29 | import javax.swing.text.AttributeSet; |
| 30 | import javax.swing.text.BadLocationException; |
| 31 | import javax.swing.text.Element; |
| 32 | import javax.swing.text.MutableAttributeSet; |
| 33 | import javax.swing.text.html.HTML; |
| 34 | import javax.swing.text.html.HTMLDocument; |
| 35 | import javax.swing.text.html.StyleSheet; |
| 36 | import javax.swing.undo.UndoableEdit; |
| 37 | |
| 38 | public class ExtendedHTMLDocument extends HTMLDocument |
| 39 | { |
| 40 | |
| 41 | public ExtendedHTMLDocument(AbstractDocument.Content c, StyleSheet styles) |
| 42 | { |
| 43 | super(c, styles); |
| 44 | } |
| 45 | |
| 46 | public ExtendedHTMLDocument(StyleSheet styles) |
| 47 | { |
| 48 | super(styles); |
| 49 | } |
| 50 | |
| 51 | public ExtendedHTMLDocument() { ; } |
| 52 | |
| 53 | /** Überschreibt die Attribute des Elements. |
| 54 | * |
| 55 | * @param e Element bei dem die Attribute geändert werden sollen |
| 56 | * @param a AttributeSet mit den neuen Attributen |
| 57 | * @param tag Angabe was für ein Tag das Element ist |
| 58 | */ |
| 59 | public void replaceAttributes(Element e, AttributeSet a, HTML.Tag tag) |
| 60 | { |
| 61 | if((e != null) && (a != null)) |
| 62 | { |
| 63 | try |
| 64 | { |
| 65 | writeLock(); |
| 66 | int start = e.getStartOffset(); |
| 67 | DefaultDocumentEvent changes = new DefaultDocumentEvent(start, e.getEndOffset() - start, DocumentEvent.EventType.CHANGE); |
| 68 | AttributeSet sCopy = a.copyAttributes(); |
| 69 | changes.addEdit(new AttributeUndoableEdit(e, sCopy, false)); |
| 70 | MutableAttributeSet attr = (MutableAttributeSet) e.getAttributes(); |
| 71 | Enumeration aNames = attr.getAttributeNames(); |
| 72 | Object value; |
| 73 | Object aName; |
| 74 | while (aNames.hasMoreElements()) |
| 75 | { |
| 76 | aName = aNames.nextElement(); |
| 77 | value = attr.getAttribute(aName); |
| 78 | if(value != null && !value.toString().equalsIgnoreCase(tag.toString())) |
| 79 | { |
| 80 | attr.removeAttribute(aName); |
| 81 | } |
| 82 | } |
| 83 | attr.addAttributes(a); |
| 84 | changes.end(); |
| 85 | fireChangedUpdate(changes); |
| 86 | fireUndoableEditUpdate(new UndoableEditEvent(this, changes)); |
| 87 | } |
| 88 | finally |
| 89 | { |
| 90 | writeUnlock(); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public void removeElements(Element e, int index, int count) |
| 96 | throws BadLocationException |
| 97 | { |
| 98 | writeLock(); |
| 99 | int start = e.getElement(index).getStartOffset(); |
| 100 | int end = e.getElement(index + count - 1).getEndOffset(); |
| 101 | try |
| 102 | { |
| 103 | Element[] removed = new Element[count]; |
| 104 | Element[] added = new Element[0]; |
| 105 | for (int counter = 0; counter < count; counter++) |
| 106 | { |
| 107 | removed[counter] = e.getElement(counter + index); |
| 108 | } |
| 109 | DefaultDocumentEvent dde = new DefaultDocumentEvent(start, end - start, DocumentEvent.EventType.REMOVE); |
| 110 | ((AbstractDocument.BranchElement)e).replace(index, removed.length, added); |
| 111 | dde.addEdit(new ElementEdit(e, index, removed, added)); |
| 112 | UndoableEdit u = getContent().remove(start, end - start); |
| 113 | if(u != null) |
| 114 | { |
| 115 | dde.addEdit(u); |
| 116 | } |
| 117 | postRemoveUpdate(dde); |
| 118 | dde.end(); |
| 119 | fireRemoveUpdate(dde); |
| 120 | if(u != null) |
| 121 | { |
| 122 | fireUndoableEditUpdate(new UndoableEditEvent(this, dde)); |
| 123 | } |
| 124 | } |
| 125 | finally |
| 126 | { |
| 127 | writeUnlock(); |
| 128 | } |
| 129 | } |
| 130 | } |