summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/javax/swing/AbstractButton.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/javax/swing/AbstractButton.java')
-rw-r--r--libjava/classpath/javax/swing/AbstractButton.java229
1 files changed, 193 insertions, 36 deletions
diff --git a/libjava/classpath/javax/swing/AbstractButton.java b/libjava/classpath/javax/swing/AbstractButton.java
index 63f827a1ae0..cb0f458b89f 100644
--- a/libjava/classpath/javax/swing/AbstractButton.java
+++ b/libjava/classpath/javax/swing/AbstractButton.java
@@ -37,8 +37,6 @@ exception statement from your version. */
package javax.swing;
-import gnu.classpath.NotImplementedException;
-
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
@@ -74,7 +72,10 @@ import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.Element;
import javax.swing.text.Position;
+import javax.swing.text.StyledDocument;
import javax.swing.text.View;
@@ -187,9 +188,32 @@ public abstract class AbstractButton extends JComponent
*/
public void stateChanged(ChangeEvent ev)
{
- AbstractButton.this.fireStateChanged();
+ getEventHandler().stateChanged(ev);
+ }
+ }
+
+ /**
+ * The combined event handler for ActionEvent, ChangeEvent and
+ * ItemEvent. This combines ButtonChangeListener, ActionListener
+ */
+ private class EventHandler
+ implements ActionListener, ChangeListener, ItemListener
+ {
+ public void actionPerformed(ActionEvent ev)
+ {
+ fireActionPerformed(ev);
+ }
+
+ public void stateChanged(ChangeEvent ev)
+ {
+ fireStateChanged();
repaint();
}
+
+ public void itemStateChanged(ItemEvent ev)
+ {
+ fireItemStateChanged(ev);
+ }
}
/** The icon displayed by default. */
@@ -264,16 +288,29 @@ public abstract class AbstractButton extends JComponent
*/
int mnemonicIndex;
- /** Listener the button uses to receive ActionEvents from its model. */
+ /**
+ * Listener the button uses to receive ActionEvents from its model.
+ */
protected ActionListener actionListener;
- /** Listener the button uses to receive ItemEvents from its model. */
+ /**
+ * Listener the button uses to receive ItemEvents from its model.
+ */
protected ItemListener itemListener;
- /** Listener the button uses to receive ChangeEvents from its model. */
+ /**
+ * Listener the button uses to receive ChangeEvents from its model.
+ */
protected ChangeListener changeListener;
/**
+ * The event handler for ActionEvent, ItemEvent and ChangeEvent.
+ * This replaces the above three handlers and combines them
+ * into one for efficiency.
+ */
+ private EventHandler eventHandler;
+
+ /**
* The time in milliseconds in which clicks get coalesced into a single
* <code>ActionEvent</code>.
*/
@@ -768,22 +805,127 @@ public abstract class AbstractButton extends JComponent
return -1;
}
- public String getAtIndex(int value0, int value1)
- throws NotImplementedException
+ /**
+ * Returns the character, word or sentence at the specified index. The
+ * <code>part</code> parameter determines what is returned, the character,
+ * word or sentence after the index.
+ *
+ * @param part one of {@link AccessibleText#CHARACTER},
+ * {@link AccessibleText#WORD} or
+ * {@link AccessibleText#SENTENCE}, specifying what is returned
+ * @param index the index
+ *
+ * @return the character, word or sentence after <code>index</code>
+ */
+ public String getAtIndex(int part, int index)
{
- return null; // TODO
+ String result = "";
+ int startIndex = -1;
+ int endIndex = -1;
+ switch(part)
+ {
+ case AccessibleText.CHARACTER:
+ result = String.valueOf(text.charAt(index));
+ break;
+ case AccessibleText.WORD:
+ startIndex = text.lastIndexOf(' ', index);
+ endIndex = text.indexOf(' ', startIndex + 1);
+ if (endIndex == -1)
+ endIndex = startIndex + 1;
+ result = text.substring(startIndex + 1, endIndex);
+ break;
+ case AccessibleText.SENTENCE:
+ default:
+ startIndex = text.lastIndexOf('.', index);
+ endIndex = text.indexOf('.', startIndex + 1);
+ if (endIndex == -1)
+ endIndex = startIndex + 1;
+ result = text.substring(startIndex + 1, endIndex);
+ break;
+ }
+ return result;
}
- public String getAfterIndex(int value0, int value1)
- throws NotImplementedException
+ /**
+ * Returns the character, word or sentence after the specified index. The
+ * <code>part</code> parameter determines what is returned, the character,
+ * word or sentence after the index.
+ *
+ * @param part one of {@link AccessibleText#CHARACTER},
+ * {@link AccessibleText#WORD} or
+ * {@link AccessibleText#SENTENCE}, specifying what is returned
+ * @param index the index
+ *
+ * @return the character, word or sentence after <code>index</code>
+ */
+ public String getAfterIndex(int part, int index)
{
- return null; // TODO
+ String result = "";
+ int startIndex = -1;
+ int endIndex = -1;
+ switch(part)
+ {
+ case AccessibleText.CHARACTER:
+ result = String.valueOf(text.charAt(index + 1));
+ break;
+ case AccessibleText.WORD:
+ startIndex = text.indexOf(' ', index);
+ endIndex = text.indexOf(' ', startIndex + 1);
+ if (endIndex == -1)
+ endIndex = startIndex + 1;
+ result = text.substring(startIndex + 1, endIndex);
+ break;
+ case AccessibleText.SENTENCE:
+ default:
+ startIndex = text.indexOf('.', index);
+ endIndex = text.indexOf('.', startIndex + 1);
+ if (endIndex == -1)
+ endIndex = startIndex + 1;
+ result = text.substring(startIndex + 1, endIndex);
+ break;
+ }
+ return result;
}
- public String getBeforeIndex(int value0, int value1)
- throws NotImplementedException
+ /**
+ * Returns the character, word or sentence before the specified index. The
+ * <code>part</code> parameter determines what is returned, the character,
+ * word or sentence before the index.
+ *
+ * @param part one of {@link AccessibleText#CHARACTER},
+ * {@link AccessibleText#WORD} or
+ * {@link AccessibleText#SENTENCE}, specifying what is returned
+ * @param index the index
+ *
+ * @return the character, word or sentence before <code>index</code>
+ */
+ public String getBeforeIndex(int part, int index)
{
- return null; // TODO
+ String result = "";
+ int startIndex = -1;
+ int endIndex = -1;
+ switch(part)
+ {
+ case AccessibleText.CHARACTER:
+ result = String.valueOf(text.charAt(index - 1));
+ break;
+ case AccessibleText.WORD:
+ endIndex = text.lastIndexOf(' ', index);
+ if (endIndex == -1)
+ endIndex = 0;
+ startIndex = text.lastIndexOf(' ', endIndex - 1);
+ result = text.substring(startIndex + 1, endIndex);
+ break;
+ case AccessibleText.SENTENCE:
+ default:
+ endIndex = text.lastIndexOf('.', index);
+ if (endIndex == -1)
+ endIndex = 0;
+ startIndex = text.lastIndexOf('.', endIndex - 1);
+ result = text.substring(startIndex + 1, endIndex);
+ break;
+ }
+ return result;
}
/**
@@ -801,7 +943,14 @@ public abstract class AbstractButton extends JComponent
View view = (View) getClientProperty(BasicHTML.propertyKey);
if (view != null)
{
-
+ Document doc = view.getDocument();
+ if (doc instanceof StyledDocument)
+ {
+ StyledDocument sDoc = (StyledDocument) doc;
+ Element charEl = sDoc.getCharacterElement(i);
+ if (charEl != null)
+ atts = charEl.getAttributes();
+ }
}
return atts;
}
@@ -855,10 +1004,6 @@ public abstract class AbstractButton extends JComponent
*/
public AbstractButton()
{
- actionListener = createActionListener();
- changeListener = createChangeListener();
- itemListener = createItemListener();
-
horizontalAlignment = CENTER;
horizontalTextPosition = TRAILING;
verticalAlignment = CENTER;
@@ -872,7 +1017,10 @@ public abstract class AbstractButton extends JComponent
setDisplayedMnemonicIndex(-1);
setOpaque(true);
text = "";
- updateUI();
+ // testing on JRE1.5 shows that the iconTextGap default value is
+ // hard-coded here and the 'Button.iconTextGap' setting in the
+ // UI defaults is ignored, at least by the MetalLookAndFeel
+ iconTextGap = 4;
}
/**
@@ -900,15 +1048,21 @@ public abstract class AbstractButton extends JComponent
if (model != null)
{
model.removeActionListener(actionListener);
+ actionListener = null;
model.removeChangeListener(changeListener);
+ changeListener = null;
model.removeItemListener(itemListener);
+ itemListener = null;
}
ButtonModel old = model;
model = newModel;
if (model != null)
{
+ actionListener = createActionListener();
model.addActionListener(actionListener);
+ changeListener = createChangeListener();
model.addChangeListener(changeListener);
+ itemListener = createItemListener();
model.addItemListener(itemListener);
}
firePropertyChange(MODEL_CHANGED_PROPERTY, old, model);
@@ -927,6 +1081,8 @@ public abstract class AbstractButton extends JComponent
if (icon != null)
default_icon = icon;
+
+ updateUI();
}
/**
@@ -1923,13 +2079,7 @@ public abstract class AbstractButton extends JComponent
*/
protected ActionListener createActionListener()
{
- return new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- AbstractButton.this.fireActionPerformed(e);
- }
- };
+ return getEventHandler();
}
/**
@@ -1995,7 +2145,7 @@ public abstract class AbstractButton extends JComponent
*/
protected ChangeListener createChangeListener()
{
- return new ButtonChangeListener();
+ return getEventHandler();
}
/**
@@ -2021,13 +2171,7 @@ public abstract class AbstractButton extends JComponent
*/
protected ItemListener createItemListener()
{
- return new ItemListener()
- {
- public void itemStateChanged(ItemEvent e)
- {
- AbstractButton.this.fireItemStateChanged(e);
- }
- };
+ return getEventHandler();
}
/**
@@ -2490,4 +2634,17 @@ public abstract class AbstractButton extends JComponent
super.setUIProperty(propertyName, value);
}
}
+
+ /**
+ * Returns the combined event handler. The instance is created if
+ * necessary.
+ *
+ * @return the combined event handler
+ */
+ EventHandler getEventHandler()
+ {
+ if (eventHandler == null)
+ eventHandler = new EventHandler();
+ return eventHandler;
+ }
}
OpenPOWER on IntegriCloud