This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
[GUI] Patch: javax.swing.text.TextComponent
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Date: Wed, 16 Jun 2004 13:14:22 +0200
- Subject: [GUI] Patch: javax.swing.text.TextComponent
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi list,
I just commited the attached patch to fix
javax.swing.text.TextComponent some more. It included much stuff that
were never be supposed to get included in it.
Michael
2004-06-16 Michael Koch <konqueror@gmx.de>
* javax/swing/text/JTextComponent.java: Totally reworked. Removed
many
methods (that were obviously never be intended to get included hi
this
class. Added some methods too.
- --
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFA0CuPWSOgCCdjSDsRAqevAJ933l2Nv3JW721lAX5gVkJsBchYOgCdHgjH
D8K18vpoaNjJcAsBcmJ5+nA=
=Tu5y
-----END PGP SIGNATURE-----
Index: javax/swing/text/JTextComponent.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/JTextComponent.java,v
retrieving revision 1.4.2.6
diff -u -b -B -r1.4.2.6 JTextComponent.java
--- javax/swing/text/JTextComponent.java 16 Jun 2004 10:22:27 -0000 1.4.2.6
+++ javax/swing/text/JTextComponent.java 16 Jun 2004 11:11:02 -0000
@@ -293,9 +293,8 @@
public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
private Document doc;
- private int icon_gap;
- private Icon icon;
- private int align;
+ private Caret caret;
+ private boolean editable;
public JTextComponent()
{
@@ -318,255 +317,332 @@
}
/**
- * Verify that key is a legal value for the horizontalAlignment properties.
+ * Get the AccessibleContext of this object
*/
- protected int checkHorizontalKey(int key, String message)
+ public AccessibleContext getAccessibleContext()
{
- return 0;
+ return null;
}
- /**
- * Verify that key is a legal value for the verticalAlignment
- * or verticalTextPosition properties.
- */
- protected int checkVerticalKey(int key, String message)
+ public Insets getMargin()
{
- return 0;
+ // FIXME: Not implemented.
+ return null;
}
- /**
- * Get the AccessibleContext of this object
- */
- public AccessibleContext getAccessibleContext()
+ public void setText(String text)
{
- return null;
+ try
+ {
+ getDocument().remove(0, doc.getLength());
+ getDocument().insertString(0, text, null);
}
-
- public Icon getDisabledIcon()
+ catch (BadLocationException e)
{
- return null;
+ }
}
/**
- * Return the keycode that indicates a mnemonic key.
+ * Retrieves the current text in this text document.
+ *
+ * @return the text
+ *
+ * @exception NullPointerException if the underlaying document is null
*/
- public int getDisplayedMnemonic()
+ public String getText()
{
- return 0;
+ return getDocument().getText(0, getDocument().getLength());
}
/**
- * Returns the alignment of the label's contents along the X axis.
+ * Retrieves a part of the current text in this document.
+ *
+ * @param offset the postion of the first character
+ * @param length the length of the text to retrieve
+ *
+ * @return the text
+ *
+ * @exception BadLocationException if arguments do not hold pre-conditions
*/
- public int getHorizontalAlignment()
+ public String getText(int offset, int length)
+ throws BadLocationException
{
- return 0;
+ return getDocument().getText(offset, length);
}
/**
- * Returns the horizontal position of the label's text,
- * relative to its image.
+ * Returns a string that specifies the name of the l&f class
+ * that renders this component.
+ *
+ * @return the string "TextComponentUI"
*/
- public int getHorizontalTextPosition()
+ public String getUIClassID()
{
- return 0;
+ return "TextComponentUI";
}
- public Icon getIcon()
+ /**
+ * Returns a string representation of this JTextComponent.
+ */
+ protected String paramString()
{
- return icon;
+ return "JTextComponent";
}
- public int getIconTextGap()
+ public TextUI getUI()
{
- return icon_gap;
+ return (TextUI) UIManager.getUI(this);
}
- /**
- * Get the component this is labelling.
- */
- Component getLabelFor()
+ public void updateUI()
{
- return null;
+ setUI(getUI());
}
- public Insets getMargin()
+ public Dimension getPreferredScrollableViewportSize()
{
- // FIXME: Not implemented.
return null;
}
- public void setText(String text)
- {
- try
- {
- getDocument().remove(0, doc.getLength());
- getDocument().insertString(0, text, null);
- }
- catch (BadLocationException e)
+ public int getScrollableUnitIncrement(Rectangle visible, int orientation,
+ int direction)
{
- }
+ return 0;
}
- public String getText()
+ public int getScrollableBlockIncrement(Rectangle visible, int orientation,
+ int direction)
{
- return getDocument().getText(0, getDocument().getLength());
+ return 0;
}
/**
- * Returns a string that specifies the name of the l&f class
- * that renders this component.
+ * Checks whether this text component it editable.
*
- * @return the string "TextComponentUI"
+ * @return true if editable, false otherwise
*/
- public String getUIClassID()
+ public boolean isEditable()
{
- return "TextComponentUI";
+ return editable;
}
/**
- * Returns the alignment of the label's contents along the Y axis.
+ * Enables/disabled this text component's editability.
+ *
+ * @param editable true to make it editable, false otherwise.
*/
- public int getVerticalAlignment()
+ public void setEditable(boolean editable)
{
- return 0;
+ firePropertyChange("editable", this.editable, editable);
+ this.editable = editable;
}
/**
- * Returns the vertical position of the label's text, relative to its image.
+ * The <code>Caret</code> object used in this text component.
+ *
+ * @return the caret object
*/
- public int getVerticalTextPosition()
+ public Caret getCaret()
{
- return 0;
+ return caret;
}
/**
- * This is overriden to return false if the current Icon's Image is not equal to the passed in Image img.
+ * Retrisves the current caret position.
+ *
+ * @return the current position
*/
- public boolean imageUpdate(Image img, int infoflags, int x, int y, int w,
- int h)
+ public int getCaretPosition()
{
- return (img == icon);
+ return caret.getDot();
}
/**
- * Returns a string representation of this JTextComponent.
+ * Sets the caret to a new position.
+ *
+ * @param position the new position
*/
- protected String paramString()
+ public void setCaretPosition(int position)
{
- return "JTextComponent";
+ if (doc == null)
+ return;
+
+ if (position < 0 || position > doc.getLength())
+ throw new IllegalArgumentException();
+
+ caret.setDot(position);
}
/**
- * Set the icon to be displayed if this JTextComponent is "disabled" (JTextComponent.setEnabled(false)).
+ * Moves the caret to a given position. This selects the text between
+ * the old and the new position of the caret.
*/
- void setDisabledIcon(Icon disabledIcon)
+ public void moveCaretPosition(int position)
{
+ if (doc == null)
+ return;
+
+ if (position < 0 || position > doc.getLength())
+ throw new IllegalArgumentException();
+
+ caret.moveDot(position);
}
/**
- * Specifies the displayedMnemonic as a char value.
+ * Returns the start postion of the currently selected text.
+ *
+ * @return the start postion
*/
- void setDisplayedMnemonic(char aChar)
+ public int getSelectionStart()
{
+ return Math.min(caret.getDot(), caret.getMark());
}
/**
- * Specify a keycode that indicates a mnemonic key.
+ * Selects the text from the given postion to the selection end position.
+ *
+ * @param end the start positon of the selected text.
*/
- void setDisplayedMnemonic(int key)
+ public void setSelectionStart(int start)
{
+ select(start, getSelectionEnd());
}
/**
- * Sets the alignment of the label's contents along the X axis.
+ * Returns the end postion of the currently selected text.
+ *
+ * @return the end postion
*/
- void setHorizontalAlignment(int alignment)
+ public int getSelectionEnd()
{
+ return Math.max(caret.getDot(), caret.getMark());
}
/**
- * Sets the horizontal position of the label's text, relative to its image.
+ * Selects the text from the selection start postion to the given position.
+ *
+ * @param end the end positon of the selected text.
*/
- void setHorizontalTextPosition(int textPosition)
+ public void setSelectionEnd(int end)
{
+ select(getSelectionStart(), end);
}
/**
- * Defines the icon this component will display.
+ * Selects a part of the content of the text component.
+ *
+ * @param start the start position of the selected text
+ * @param ent the end position of the selected text
*/
- void setIcon(Icon icon)
+ public void select(int start, int end)
{
+ int length = doc.getLength();
+
+ start = Math.max(start, 0);
+ start = Math.min(start, length);
+
+ end = Math.max(end, 0);
+ end = Math.min(end, length);
+
+ setCaretPosition(start);
+ moveCaretPosition(end);
}
/**
- * If both the icon and text properties are set, this property defines the space between them.
+ * Selects the whole content of the text component.
*/
- public void setIconTextGap(int iconTextGap)
+ public void selectAll()
{
+ select(0, doc.getLength());
}
- /**
- * Set the component this is labelling.
- */
- public void setLabelFor(Component c)
+ public boolean getScrollableTracksViewportHeight()
{
+ if (getParent() instanceof JViewport)
+ return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
+
+ return false;
}
- /**
- * Sets the alignment of the label's contents along the Y axis.
- */
- public void setVerticalAlignment(int alignment)
+ public boolean getScrollableTracksViewportWidth()
{
+ if (getParent() instanceof JViewport)
+ return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
+
+ return false;
}
/**
- * Sets the vertical position of the label's text, relative to its image.
+ * Adds a <code>CaretListener</code> object to this text component.
+ *
+ * @param listener the listener to add
*/
- public void setVerticalTextPosition(int textPosition)
+ public void addCaretListener(CaretListener listener)
{
+ listenerList.add(CaretListener.class, listener);
}
- public TextUI getUI()
+ /**
+ * Removed a <code>CaretListener</code> object from this text component.
+ *
+ * @param listener the listener to remove
+ */
+ public void removeCaretListener(CaretListener listener)
{
- return (TextUI) UIManager.getUI(this);
+ listenerList.remove(CaretListener.class, listener);
}
- public void updateUI()
+ /**
+ * Returns all added <code>CaretListener</code> objects.
+ *
+ * @return an array of listeners
+ */
+ public CaretListener[] getCaretListeners()
{
- setUI(getUI());
+ return (CaretListener[]) getListeners(CaretListener.class);
}
- public Dimension getPreferredScrollableViewportSize()
+ /**
+ * Notifies all registered <code>CaretListener</code> objects that the caret
+ * was updated.
+ *
+ * @param event the event to send
+ */
+ protected void fireCaretUpdate(CaretEvent event)
{
- return null;
- }
+ CaretListener[] listeners = getCaretListeners();
- public int getScrollableUnitIncrement(Rectangle visible, int orientation,
- int direction)
- {
- return 0;
+ for (int index = 0; index < listeners.length; ++index)
+ listeners[index].caretUpdate(event);
}
- public int getScrollableBlockIncrement(Rectangle visible, int orientation,
- int direction)
+ /**
+ * Adds an <code>InputListener</code> object to this text component.
+ *
+ * @param listener the listener to add
+ */
+ public void addInputMethodListener(InputMethodListener listener)
{
- return 0;
+ listenerList.add(InputMethodListener.class, listener);
}
- public boolean getScrollableTracksViewportHeight()
+ /**
+ * Removes an <code>InputListener</code> object from this text component.
+ *
+ * @param listener the listener to remove
+ */
+ public void removeInputMethodListener(InputMethodListener listener)
{
- if (getParent() instanceof JViewport)
- return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
-
- return false;
+ listenerList.remove(InputMethodListener.class, listener);
}
- public boolean getScrollableTracksViewportWidth()
+ /**
+ * Returns all added <code>InputMethodListener</code> objects.
+ *
+ * @return an array of listeners
+ */
+ public InputMethodListener[] getInputMethodListeners()
{
- if (getParent() instanceof JViewport)
- return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
-
- return false;
+ return (InputMethodListener[]) getListeners(InputMethodListener.class);
}
}