Java by API/javax.swing.text/DefaultEditorKit — различия между версиями

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

Текущая версия на 14:12, 31 мая 2010

DefaultEditorKit.beepAction

 
import java.awt.Event;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.DefaultEditorKit;
public class MainClass extends JFrame {
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel jp = new JPanel();
    JLabel jl = new JLabel("Name:");
    jp.add(jl);
    JTextField jt = new JTextField(20);
    jp.add(jt);
    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK);
    jt.getInputMap().put(ks, DefaultEditorKit.beepAction);
    getContentPane().add(jp);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    new MainClass("Binding Demo2");
  }
}





DefaultEditorKit.copyAction

 
import java.awt.BorderLayout;
import java.util.Hashtable;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.DefaultEditorKit;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Cut/Paste Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    Action actions[] = textField.getActions();
    Action cutAction = findAction(actions, DefaultEditorKit.cutAction);
    Action copyAction = findAction(actions, DefaultEditorKit.copyAction);
    Action pasteAction = findAction(actions, DefaultEditorKit.pasteAction);
    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.SOUTH);
    JButton cutButton = new JButton(cutAction);
    cutButton.setText("Cut");
    panel.add(cutButton);
    JButton copyButton = new JButton(copyAction);
    copyButton.setText("Copy");
    panel.add(copyButton);
    JButton pasteButton = new JButton(pasteAction);
    pasteButton.setText("Paste");
    panel.add(pasteButton);
    frame.setSize(250, 250);
    frame.setVisible(true);
  }
  public static Action findAction(Action actions[], String key) {
    Hashtable<Object, Action> commands = new Hashtable<Object, Action>();
    for (int i = 0; i < actions.length; i++) {
      Action action = actions[i];
      commands.put(action.getValue(Action.NAME), action);
    }
    return commands.get(key);
  }
}





DefaultEditorKit.cutAction

 
import java.awt.BorderLayout;
import java.util.Hashtable;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.DefaultEditorKit;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Cut/Paste Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    Action actions[] = textField.getActions();
    Action cutAction = findAction(actions, DefaultEditorKit.cutAction);
    Action copyAction = findAction(actions, DefaultEditorKit.copyAction);
    Action pasteAction = findAction(actions, DefaultEditorKit.pasteAction);
    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.SOUTH);
    JButton cutButton = new JButton(cutAction);
    cutButton.setText("Cut");
    panel.add(cutButton);
    JButton copyButton = new JButton(copyAction);
    copyButton.setText("Copy");
    panel.add(copyButton);
    JButton pasteButton = new JButton(pasteAction);
    pasteButton.setText("Paste");
    panel.add(pasteButton);
    frame.setSize(250, 250);
    frame.setVisible(true);
  }
  public static Action findAction(Action actions[], String key) {
    Hashtable<Object, Action> commands = new Hashtable<Object, Action>();
    for (int i = 0; i < actions.length; i++) {
      Action action = actions[i];
      commands.put(action.getValue(Action.NAME), action);
    }
    return commands.get(key);
  }
}





DefaultEditorKit.pasteAction

 
import java.awt.BorderLayout;
import java.util.Hashtable;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.DefaultEditorKit;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Cut/Paste Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    Action actions[] = textField.getActions();
    Action cutAction = findAction(actions, DefaultEditorKit.cutAction);
    Action copyAction = findAction(actions, DefaultEditorKit.copyAction);
    Action pasteAction = findAction(actions, DefaultEditorKit.pasteAction);
    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.SOUTH);
    JButton cutButton = new JButton(cutAction);
    cutButton.setText("Cut");
    panel.add(cutButton);
    JButton copyButton = new JButton(copyAction);
    copyButton.setText("Copy");
    panel.add(copyButton);
    JButton pasteButton = new JButton(pasteAction);
    pasteButton.setText("Paste");
    panel.add(pasteButton);
    frame.setSize(250, 250);
    frame.setVisible(true);
  }
  public static Action findAction(Action actions[], String key) {
    Hashtable<Object, Action> commands = new Hashtable<Object, Action>();
    for (int i = 0; i < actions.length; i++) {
      Action action = actions[i];
      commands.put(action.getValue(Action.NAME), action);
    }
    return commands.get(key);
  }
}





DefaultEditorKit.readOnlyAction

 
import java.awt.BorderLayout;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultEditorKit;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Use TextAction");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JTextArea leftArea = new JTextArea();
    final JTextArea rightArea = new JTextArea();
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(leftArea),
        new JScrollPane(rightArea));
    splitPane.setDividerLocation(.5);
    
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu menu = new JMenu("Options");
    menuBar.add(menu);
    JMenuItem menuItem;
    Action readAction = leftArea.getActionMap().get(DefaultEditorKit.readOnlyAction);
    menuItem = menu.add(readAction);
    menuItem.setText("Make read-only");
    Action writeAction = leftArea.getActionMap().get(DefaultEditorKit.writableAction);
    menuItem = menu.add(writeAction);
    menuItem.setText("Make writable");
    menu.addSeparator();
    Action cutAction = leftArea.getActionMap().get(DefaultEditorKit.cutAction);
    menuItem = menu.add(cutAction);
    menuItem.setText("Cut");
    Action copyAction = leftArea.getActionMap().get(DefaultEditorKit.copyAction);
    menuItem = menu.add(copyAction);
    menuItem.setText("Copy");
    Action pasteAction = leftArea.getActionMap().get(DefaultEditorKit.pasteAction);
    menuItem = menu.add(pasteAction);
    menuItem.setText("Paste");
    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(400, 250);
    frame.setVisible(true);
  }
}





DefaultEditorKit.selectLineAction

 
import javax.swing.*;
import javax.swing.text.*;
import java.util.Hashtable;
import java.awt.event.*;
import java.awt.BorderLayout;
public class KeymapExample {
  public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);
    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord(); 
    newmap.addActionForKeyStroke(u, actionU);
    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j=0; j < actionList.length; j+=1)
      lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);
    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action)lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);
    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action)lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);
    area.setKeymap(newmap);
    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("www.\n jexp \n .ru.");
    f.pack();
    f.setVisible(true);
  }
  public static class UpWord extends TextAction {
    public UpWord() {
      super("uppercase-word-action");
    }
    public void actionPerformed(ActionEvent e) {
      JTextComponent comp = getTextComponent(e);
      if (comp == null) return;
      Document doc = comp.getDocument();
      int start = comp.getSelectionStart();
      int end = comp.getSelectionEnd();
      try {
        int left = javax.swing.text.Utilities.getWordStart(comp, start);
        int right = javax.swing.text.Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right-left);
        doc.remove(left, right-left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start); // restore previous position/selection
        comp.setSelectionEnd(end);
      } catch (BadLocationException ble) { return; }
    }
  }
}





DefaultEditorKit.selectWordAction

 
import javax.swing.*;
import javax.swing.text.*;
import java.util.Hashtable;
import java.awt.event.*;
import java.awt.BorderLayout;
public class KeymapExample {
  public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);
    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord(); 
    newmap.addActionForKeyStroke(u, actionU);
    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j=0; j < actionList.length; j+=1)
      lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);
    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action)lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);
    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action)lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);
    area.setKeymap(newmap);
    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("www.\n jexp \n .ru.");
    f.pack();
    f.setVisible(true);
  }
  public static class UpWord extends TextAction {
    public UpWord() {
      super("uppercase-word-action");
    }
    public void actionPerformed(ActionEvent e) {
      JTextComponent comp = getTextComponent(e);
      if (comp == null) return;
      Document doc = comp.getDocument();
      int start = comp.getSelectionStart();
      int end = comp.getSelectionEnd();
      try {
        int left = javax.swing.text.Utilities.getWordStart(comp, start);
        int right = javax.swing.text.Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right-left);
        doc.remove(left, right-left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start); // restore previous position/selection
        comp.setSelectionEnd(end);
      } catch (BadLocationException ble) { return; }
    }
  }
}





DefaultEditorKit.writableAction

 

import java.awt.BorderLayout;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultEditorKit;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Use TextAction");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JTextArea leftArea = new JTextArea();
    final JTextArea rightArea = new JTextArea();
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(leftArea),
        new JScrollPane(rightArea));
    splitPane.setDividerLocation(.5);
    
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu menu = new JMenu("Options");
    menuBar.add(menu);
    JMenuItem menuItem;
    Action readAction = leftArea.getActionMap().get(DefaultEditorKit.readOnlyAction);
    menuItem = menu.add(readAction);
    menuItem.setText("Make read-only");
    Action writeAction = leftArea.getActionMap().get(DefaultEditorKit.writableAction);
    menuItem = menu.add(writeAction);
    menuItem.setText("Make writable");
    menu.addSeparator();
    Action cutAction = leftArea.getActionMap().get(DefaultEditorKit.cutAction);
    menuItem = menu.add(cutAction);
    menuItem.setText("Cut");
    Action copyAction = leftArea.getActionMap().get(DefaultEditorKit.copyAction);
    menuItem = menu.add(copyAction);
    menuItem.setText("Copy");
    Action pasteAction = leftArea.getActionMap().get(DefaultEditorKit.pasteAction);
    menuItem = menu.add(pasteAction);
    menuItem.setText("Paste");
    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(400, 250);
    frame.setVisible(true);
  }
}