FileDocCategorySizeDatePackage
HiddenTagView.javaAPI DocJava SE 5 API9494Fri Aug 26 14:58:18 BST 2005javax.swing.text.html

HiddenTagView

public class HiddenTagView extends EditableView implements DocumentListener
HiddenTagView subclasses EditableView to contain a JTextField showing the element name. When the textfield is edited the element name is reset. As this inherits from EditableView if the JTextComponent is not editable, the textfield will not be visible.
author
Scott Violet
version
1.15, 12/19/03

Fields Summary
float
yAlign
Alignment along the y axis, based on the font of the textfield.
boolean
isSettingAttributes
Set to true when setting attributes.
static final int
circleR
static final int
circleD
static final int
tagSize
static final int
padding
static final Color
UnknownTagBorderColor
static final Border
StartBorder
static final Border
EndBorder
Constructors Summary
HiddenTagView(Element e)

	super(e);
	yAlign = 1;
    
Methods Summary
void_setTextFromModel()
This resets the text on the text component we created to match that of the AttributeSet for the Element we represent.

	Document doc = getDocument();
	try {
	    isSettingAttributes = true;
	    if (doc instanceof AbstractDocument) {
		((AbstractDocument)doc).readLock();
	    }
	    JTextComponent text = getTextComponent();
	    if (text != null) {
		text.setText(getRepresentedText());
		resetBorder();
		Container host = getContainer();
		if (host != null) {
		    preferenceChanged(this, true, true);
		    host.repaint();
		}
	    }
	}
	finally {
	    isSettingAttributes = false;
	    if (doc instanceof AbstractDocument) {
		((AbstractDocument)doc).readUnlock();
	    }
	}
    
void_updateModelFromText()
This copies the text from the text component we've created to the Element's AttributeSet we represent.

	Document doc = getDocument();
	Object name = getElement().getAttributes().getAttribute
	    (StyleConstants.NameAttribute);
	if ((name instanceof HTML.UnknownTag) &&
	    (doc instanceof StyledDocument)) {
	    SimpleAttributeSet sas = new SimpleAttributeSet();
	    JTextComponent textComponent = getTextComponent();
	    if (textComponent != null) {
		String text = textComponent.getText();
		isSettingAttributes = true;
		try {
		    sas.addAttribute(StyleConstants.NameAttribute,
				     new HTML.UnknownTag(text));
		    ((StyledDocument)doc).setCharacterAttributes
			(getStartOffset(), getEndOffset() -
			 getStartOffset(), sas, false);
		}
		finally {
		    isSettingAttributes = false;
		}
	    }
	}
    
public voidchangedUpdate(javax.swing.event.DocumentEvent e, java.awt.Shape a, javax.swing.text.ViewFactory f)

	if (!isSettingAttributes) {
	    setTextFromModel();
	}
    
public voidchangedUpdate(javax.swing.event.DocumentEvent e)

	updateModelFromText();
    
protected java.awt.ComponentcreateComponent()

	JTextField tf = new JTextField(getElement().getName());
	Document doc = getDocument();
	Font font;
	if (doc instanceof StyledDocument) {
	    font = ((StyledDocument)doc).getFont(getAttributes());
	    tf.setFont(font);
	}
	else {
	    font = tf.getFont();
	}
	tf.getDocument().addDocumentListener(this);
	updateYAlign(font);

	// Create a panel to wrap the textfield so that the textfields
	// laf border shows through.
	JPanel panel = new JPanel(new BorderLayout());
	panel.setBackground(null);
	if (isEndTag()) {
	    panel.setBorder(EndBorder);
	}
	else {
	    panel.setBorder(StartBorder);
	}
	panel.add(tf);
	return panel;
    
public floatgetAlignment(int axis)

	if (axis == View.Y_AXIS) {
	    return yAlign;
	}
	return 0.5f;
    
public floatgetMaximumSpan(int axis)

	if (axis == View.X_AXIS && isVisible()) {
	    // Default to preferred.
	    return Math.max(30, super.getMaximumSpan(axis));
	}
	return super.getMaximumSpan(axis);
    
public floatgetMinimumSpan(int axis)

	if (axis == View.X_AXIS && isVisible()) {
	    // Default to preferred.
	    return Math.max(30, super.getPreferredSpan(axis));
	}
	return super.getMinimumSpan(axis);
    
public floatgetPreferredSpan(int axis)

	if (axis == View.X_AXIS && isVisible()) {
	    return Math.max(30, super.getPreferredSpan(axis));
	}
	return super.getPreferredSpan(axis);
    
java.lang.StringgetRepresentedText()

	String retValue = getElement().getName();
	return (retValue == null) ? "" : retValue;
    
javax.swing.text.JTextComponentgetTextComponent()

	Component comp = getComponent();

	return (comp == null) ? null : (JTextComponent)((Container)comp).
	                               getComponent(0);
    
public voidinsertUpdate(javax.swing.event.DocumentEvent e)

	updateModelFromText();
    
booleanisEndTag()

	AttributeSet as = getElement().getAttributes();
	if (as != null) {
	    Object end = as.getAttribute(HTML.Attribute.ENDTAG);
	    if (end != null && (end instanceof String) &&
		((String)end).equals("true")) {
		return true;
	    }
	}
	return false;
    
public voidremoveUpdate(javax.swing.event.DocumentEvent e)

	updateModelFromText();
    
voidresetBorder()

	Component comp = getComponent();

	if (comp != null) {
	    if (isEndTag()) {
		((JPanel)comp).setBorder(EndBorder);
	    }
	    else {
		((JPanel)comp).setBorder(StartBorder);
	    }
	}
    
voidsetTextFromModel()
This resets the text on the text component we created to match that of the AttributeSet for the Element we represent.

If this is invoked on the event dispatching thread, this directly invokes _setTextFromModel, otherwise SwingUtilities.invokeLater is used to schedule execution of _setTextFromModel.

	if (SwingUtilities.isEventDispatchThread()) {
	    _setTextFromModel();
	}
	else {
	    SwingUtilities.invokeLater(new Runnable() {
		public void run() {
		    _setTextFromModel();
		}
	    });
	}
    
voidupdateModelFromText()
This copies the text from the text component we've created to the Element's AttributeSet we represent.

If this is invoked on the event dispatching thread, this directly invokes _updateModelFromText, otherwise SwingUtilities.invokeLater is used to schedule execution of _updateModelFromText.

	if (!isSettingAttributes) {
	    if (SwingUtilities.isEventDispatchThread()) {
		_updateModelFromText();
	    }
	    else {
		SwingUtilities.invokeLater(new Runnable() {
		    public void run() {
			_updateModelFromText();
		    }
		});
	    }
	}
    
voidupdateYAlign(java.awt.Font font)

        Container c = getContainer();
	FontMetrics fm = (c != null) ? c.getFontMetrics(font) :
            Toolkit.getDefaultToolkit().getFontMetrics(font);
	float h = fm.getHeight();
	float d = fm.getDescent();
	yAlign = (h - d) / h;