-
Bug
-
Resolution: Fixed
-
P4
-
1.3.0
-
beta2
-
x86
-
windows_nt
Name: bsC130419 Date: 05/18/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
The program myframe tries to change the tag of the elements in a HTML document.
If there is a empty line before the last line of the text and the user selects
the text to the last element (last line - the element is partially selected),
then the method setOuterHTML() replaces all selected elements as expected. But
the last element will be duplicated.
compile program:
javac VecKeyValue.java
javac myframe.java
run program:
java myframe
error:
select
row1
row
press button:
row 1 row 2
row 2
row 2
ok:
select
row1
press button:
row1
row2
SOURCE CODE:
myframe.java
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.*;
import java.awt.event.*;
import java.util.NoSuchElementException; // NoSuchElementException
public class myframe extends JFrame {
/**
* start and end of selected text
*/
int _intSelStart;
int _intSelEnd;
/**
* text
*/
String strStart;
String strEnd;
/**
* create Editor Pane and HTMLDocument
*/
JEditorPane jEditorPane1 = new JEditorPane("text/html","init");
HTMLEditorKit ek = (HTMLEditorKit) jEditorPane1.getEditorKit();
HTMLDocument doc = (HTMLDocument) ek.createDefaultDocument();
/**
* Vektor of selected elements
*/
VecKeyValue _selElements;
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
public myframe() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
this.setSize(400,300);
this.validate();
this.setVisible(true);
}
public static void main(String[] args) {
myframe myframe1 = new myframe();
}
private void jbInit() throws Exception {
/**
* Use the created document
*/
jEditorPane1.setDocument(doc);
jButton1.setActionCommand("jButton1");
jButton1.setText("underline selected text");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jLabel1.setForeground(Color.red);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("please select row1 .. row and press button => error");
jLabel2.setForeground(Color.green);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("please select row1 and press button => OK");
this.getContentPane().add(jEditorPane1, BorderLayout.CENTER);
this.getContentPane().add(jButton1, BorderLayout.EAST);
this.getContentPane().add(jLabel1, BorderLayout.NORTH);
this.getContentPane().add(jLabel2, BorderLayout.SOUTH);
/**
* fill the editor pane with some text
*/
String str = "<p><b>row 1</b></p><p></p><p>row 2</p>";
jEditorPane1.setText(str);
}
/**
* fill the vector with the elements covered by the selection
*/
public void getSelectedElement() throws NoSuchElementException {
_intSelStart = jEditorPane1.getSelectionStart();
_intSelEnd = jEditorPane1.getSelectionEnd();
Element elt;
Element eltOld;
int lauf = _intSelStart;
/**
* get the element of a selected character
*/
eltOld = doc.getCharacterElement(lauf);
_selElements = new VecKeyValue();
_selElements.addElement(eltOld,eltOld.toString());
while ( lauf < (_intSelEnd - 1) ) {
lauf++;
elt = doc.getCharacterElement(lauf);
if ( ! elt.equals(eltOld) ) {
/**
* add to the vector, because the element changed
*/
_selElements.addElement(elt,elt.toString());
eltOld = elt;
}
}
return;
}
public void chgSelection() {
/**
* get selected elements
*/
try {
getSelectedElement();
} catch ( Exception exc ) {
System.out.println("Exception");
}
/**
* change the selected elements
*/
chgSelectedElement();
}
private void chgSelectedElement() {
Element elt = (Element) _selElements.firstElement();
int lauf = 0;
/**
* go through the selected elements
*/
while ( elt != null ) {
lauf++;
/**
* get the new text (with HTML-Tags) of the element
*/
String res = replaceSelectedText(elt);
try {
doc.setOuterHTML(elt,res);
} catch ( Exception mye ) {
System.out.println("Exception");
}
if ( elt.equals(_selElements.lastElement()) ) {
break;
}
/**
* next selected element
*/
elt = (Element) _selElements.getKey(lauf);
}
_selElements.clear();
}
/**
* get the new text (with HTML-Tags) of the element
*/
public String replaceSelectedText(Element elt) {
String res = "";
strStart = "";
strEnd = "";
/**
* get start and end of the element
*/
int offStart = elt.getStartOffset();
int offEnd = elt.getEndOffset();
/**
* do the not selected parts
*/
if (( offStart < _intSelStart ) || ( offEnd > _intSelEnd )) {
doNotSelectedParts(offStart,offEnd);
}
/**
* get the selected text
*/
int intS = Math.max(offStart,_intSelStart);
int intE = Math.min(offEnd,_intSelEnd);
try {
String strMiddle = jEditorPane1.getText(intS,intE-intS);
/**
* put the not selected and the selected parts together (with the new
Tags)
*/
res = strStart + "<u>" + strMiddle + "</u>" + strEnd;
} catch ( Exception mye) {
System.out.println("ERROR");
}
return res;
}
/**
* the parts of an element, which are not selected shouldn't be changed
*/
private void doNotSelectedParts( int anfa,int ende ) {
if ( anfa < _intSelStart ) {
try {
strStart = jEditorPane1.getText(anfa,_intSelStart-anfa);
} catch ( Exception mye) {
System.out.println("ERROR");
}
}
if ( ende > _intSelEnd ) {
try {
strEnd = jEditorPane1.getText(_intSelEnd,ende-_intSelEnd);
} catch ( Exception mye) {
System.out.println("ERROR");
}
}
}
/**
* underline the selected text
*/
void jButton1_actionPerformed(ActionEvent e) {
String s = jEditorPane1.getSelectedText();
if ( s == null ) {
/**
* no selection
*/
Toolkit.getDefaultToolkit().beep();
return;
}
chgSelection();
}
}
VecKeyValue:
import java.util.Vector;
public class VecKeyValue {
private Vector _key;
private Vector _value;
private int _intAnz;
public VecKeyValue() {
_key = new Vector();
_value = new Vector();
_intAnz = 0;
}
public int getCount() {
return _intAnz;
}
public void addElement(Object oKey,Object oValue) {
_key.addElement(oKey);
_value.addElement(oValue);
_intAnz++;
}
public void clear() {
_key.clear();
_value.clear();
_intAnz = 0;
}
public boolean contains(Object obj) {
return _key.contains(obj);
}
public boolean equals(Object obj) {
return _key.equals(obj);
}
public Object firstElement() {
return _key.firstElement();
}
public Object get(int index) {
return _value.get(index);
}
public Object getKey(int index) {
return _key.get(index);
}
public int indexOf(Object obj) {
return _key.indexOf(obj);
}
public boolean isEmpty() {
return _key.isEmpty();
}
public Object lastElement() {
return _key.lastElement();
}
public void setElementAt(Object obj, int index) {
_value.setElementAt(obj,index);
}
}
(Review ID: 124523)
======================================================================