Java by API/javax.accessibility/AccessibleHypertext

Материал из Java эксперт
Перейти к: навигация, поиск

AccessibleHypertext: getLinkCount()

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleHyperlink;
import javax.accessibility.AccessibleHypertext;
import javax.accessibility.AccessibleText;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class MainClass extends JFrame implements HyperlinkListener {
  JEditorPane view;
  MainClass() throws IOException {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    view = new JEditorPane("http://www.jexp.ru");
    view.setEditable(false);
    view.setPreferredSize(new Dimension(400, 400));
    view.addHyperlinkListener(this);
    getContentPane().add(new JScrollPane(view), BorderLayout.CENTER);
    pack();
    setVisible(true);
  }
  public void hyperlinkUpdate(HyperlinkEvent e) {
    try {
      view.setPage(e.getURL());
    } catch (java.io.IOException e2) {
    }
  }
  public static void main(String[] args)throws IOException  {
    MainClass ad4 = new MainClass();
    ad4.dumpHypertextInfo(ad4.getAccessibleContext());
  }
  void dumpHypertextInfo(AccessibleContext ac) {
    AccessibleText at = ac.getAccessibleText();
    AccessibleHypertext ah = null;
    if (at instanceof AccessibleHypertext)
      ah = (AccessibleHypertext) at;
    if (ah != null) {
      int nLinks = ah.getLinkCount();
      for (int i = 0; i < nLinks; i++) {
        AccessibleHyperlink ahl = ah.getLink(i);
        int nActions = ahl.getAccessibleActionCount();
        for (int j = 0; j < nActions; j++) {
          String s = ahl.getAccessibleActionDescription(j);
          System.out.println("Action = " + s);
        }
      }
      return;
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpHypertextInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}