2 GNU Lesser General Public License
5 Copyright (C) 2003 Frits Jalvingh, Jerry Pommer & Howard Kistler
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.
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.
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
22 package com
.hexidec
.ekit
.component
;
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
;
38 public class ExtendedHTMLDocument
extends HTMLDocument
41 public ExtendedHTMLDocument(AbstractDocument
.Content c
, StyleSheet styles
)
46 public ExtendedHTMLDocument(StyleSheet styles
)
51 public ExtendedHTMLDocument() { ; }
53 /** Überschreibt die Attribute des Elements.
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
59 public void replaceAttributes(Element e
, AttributeSet a
, HTML
.Tag tag
)
61 if((e
!= null
) && (a
!= null
))
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();
74 while (aNames
.hasMoreElements())
76 aName
= aNames
.nextElement();
77 value
= attr
.getAttribute(aName
);
78 if(value
!= null
&& !value
.toString().equalsIgnoreCase(tag
.toString()))
80 attr
.removeAttribute(aName
);
83 attr
.addAttributes(a
);
85 fireChangedUpdate(changes
);
86 fireUndoableEditUpdate(new UndoableEditEvent(this, changes
));
95 public void removeElements(Element e
, int index
, int count
)
96 throws BadLocationException
99 int start
= e
.getElement(index
).getStartOffset();
100 int end
= e
.getElement(index
+ count
- 1).getEndOffset();
103 Element
[] removed
= new Element
[count
];
104 Element
[] added
= new Element
[0];
105 for (int counter
= 0; counter
< count
; counter
++)
107 removed
[counter
] = e
.getElement(counter
+ index
);
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
);
117 postRemoveUpdate(dde
);
119 fireRemoveUpdate(dde
);
122 fireUndoableEditUpdate(new UndoableEditEvent(this, dde
));