jtextcomponent.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,854 行 · 第 1/4 页
JAVA
1,854 行
/** * Replace the text between two points with the given string. * * @param start the start position, inclusive * @param end the end position, exclusive * @param s the string to paste */ public void replaceText(int start, int end, String s) { textComp.select(start, end); textComp.replaceSelection(s); } /** * Select the text between two points. * * @param start the start position, inclusive * @param end the end position, exclusive */ public void selectText(int start, int end) { textComp.select(start, end); } /** * Set the attributes of text between two points. * * @param start the start position, inclusive * @param end the end position, exclusive * @param s the new attribute set for the range */ public void setAttributes(int start, int end, AttributeSet s) { // TODO } } public static class KeyBinding { public KeyStroke key; public String actionName; /** * Creates a new <code>KeyBinding</code> instance. * * @param key a <code>KeyStroke</code> value * @param actionName a <code>String</code> value */ public KeyBinding(KeyStroke key, String actionName) { this.key = key; this.actionName = actionName; } } /** * According to <a * href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html">this * report</a>, a pair of private classes wraps a {@link * javax.swing.text.Keymap} in the new {@link InputMap} / {@link * ActionMap} interfaces, such that old Keymap-using code can make use of * the new framework. * * <p>A little bit of experimentation with these classes reveals the following * structure: * * <ul> * * <li>KeymapWrapper extends {@link InputMap} and holds a reference to * the underlying {@link Keymap}.</li> * * <li>KeymapWrapper maps {@link KeyStroke} objects to {@link Action} * objects, by delegation to the underlying {@link Keymap}.</li> * * <li>KeymapActionMap extends {@link ActionMap} also holds a reference to * the underlying {@link Keymap} but only appears to use it for listing * its keys. </li> * * <li>KeymapActionMap maps all {@link Action} objects to * <em>themselves</em>, whether they exist in the underlying {@link * Keymap} or not, and passes other objects to the parent {@link * ActionMap} for resolving. * * </ul> */ private class KeymapWrapper extends InputMap { Keymap map; public KeymapWrapper(Keymap k) { map = k; } public int size() { return map.getBoundKeyStrokes().length + super.size(); } public Object get(KeyStroke ks) { Action mapped = null; Keymap m = map; while(mapped == null && m != null) { mapped = m.getAction(ks); if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED) mapped = m.getDefaultAction(); if (mapped == null) m = m.getResolveParent(); } if (mapped == null) return super.get(ks); else return mapped; } public KeyStroke[] keys() { KeyStroke[] superKeys = super.keys(); KeyStroke[] mapKeys = map.getBoundKeyStrokes(); KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length]; for (int i = 0; i < superKeys.length; ++i) bothKeys[i] = superKeys[i]; for (int i = 0; i < mapKeys.length; ++i) bothKeys[i + superKeys.length] = mapKeys[i]; return bothKeys; } public KeyStroke[] allKeys() { KeyStroke[] superKeys = super.allKeys(); KeyStroke[] mapKeys = map.getBoundKeyStrokes(); int skl = 0; int mkl = 0; if (superKeys != null) skl = superKeys.length; if (mapKeys != null) mkl = mapKeys.length; KeyStroke[] bothKeys = new KeyStroke[skl + mkl]; for (int i = 0; i < skl; ++i) bothKeys[i] = superKeys[i]; for (int i = 0; i < mkl; ++i) bothKeys[i + skl] = mapKeys[i]; return bothKeys; } } private class KeymapActionMap extends ActionMap { Keymap map; public KeymapActionMap(Keymap k) { map = k; } public Action get(Object cmd) { if (cmd instanceof Action) return (Action) cmd; else return super.get(cmd); } public int size() { return map.getBoundKeyStrokes().length + super.size(); } public Object[] keys() { Object[] superKeys = super.keys(); Object[] mapKeys = map.getBoundKeyStrokes(); Object[] bothKeys = new Object[superKeys.length + mapKeys.length]; for (int i = 0; i < superKeys.length; ++i) bothKeys[i] = superKeys[i]; for (int i = 0; i < mapKeys.length; ++i) bothKeys[i + superKeys.length] = mapKeys[i]; return bothKeys; } public Object[] allKeys() { Object[] superKeys = super.allKeys(); Object[] mapKeys = map.getBoundKeyStrokes(); Object[] bothKeys = new Object[superKeys.length + mapKeys.length]; for (int i = 0; i < superKeys.length; ++i) bothKeys[i] = superKeys[i]; for (int i = 0; i < mapKeys.length; ++i) bothKeys[i + superKeys.length] = mapKeys[i]; return bothKeys; } } static class DefaultKeymap implements Keymap { String name; Keymap parent; Hashtable map; Action defaultAction; public DefaultKeymap(String name) { this.name = name; this.map = new Hashtable(); } public void addActionForKeyStroke(KeyStroke key, Action a) { map.put(key, a); } /** * Looks up a KeyStroke either in the current map or the parent Keymap; * does <em>not</em> return the default action if lookup fails. * * @param key The KeyStroke to look up an Action for. * * @return The mapping for <code>key</code>, or <code>null</code> * if no mapping exists in this Keymap or any of its parents. */ public Action getAction(KeyStroke key) { if (map.containsKey(key)) return (Action) map.get(key); else if (parent != null) return parent.getAction(key); else return null; } public Action[] getBoundActions() { Action [] ret = new Action[map.size()]; Enumeration e = map.elements(); int i = 0; while (e.hasMoreElements()) { ret[i++] = (Action) e.nextElement(); } return ret; } public KeyStroke[] getBoundKeyStrokes() { KeyStroke [] ret = new KeyStroke[map.size()]; Enumeration e = map.keys(); int i = 0; while (e.hasMoreElements()) { ret[i++] = (KeyStroke) e.nextElement(); } return ret; } public Action getDefaultAction() { return defaultAction; } public KeyStroke[] getKeyStrokesForAction(Action a) { int i = 0; Enumeration e = map.keys(); while (e.hasMoreElements()) { if (map.get(e.nextElement()).equals(a)) ++i; } KeyStroke [] ret = new KeyStroke[i]; i = 0; e = map.keys(); while (e.hasMoreElements()) { KeyStroke k = (KeyStroke) e.nextElement(); if (map.get(k).equals(a)) ret[i++] = k; } return ret; } public String getName() { return name; } public Keymap getResolveParent() { return parent; } public boolean isLocallyDefined(KeyStroke key) { return map.containsKey(key); } public void removeBindings() { map.clear(); } public void removeKeyStrokeBinding(KeyStroke key) { map.remove(key); } public void setDefaultAction(Action a) { defaultAction = a; } public void setResolveParent(Keymap p) { parent = p; } } class DefaultTransferHandler extends TransferHandler { public boolean canImport(JComponent component, DataFlavor[] flavors) { JTextComponent textComponent = (JTextComponent) component; if (! (textComponent.isEnabled() && textComponent.isEditable() && flavors != null)) return false; for (int i = 0; i < flavors.length; ++i) if (flavors[i].equals(DataFlavor.stringFlavor)) return true; return false; } public void exportToClipboard(JComponent component, Clipboard clipboard, int action) { JTextComponent textComponent = (JTextComponent) component; int start = textComponent.getSelectionStart(); int end = textComponent.getSelectionEnd(); if (start == end) return; try { // Copy text to clipboard. String data = textComponent.getDocument().getText(start, end); StringSelection selection = new StringSelection(data); clipboard.setContents(selection, null); // Delete selected text on cut action. if (action == MOVE) doc.remove(start, end - start); } catch (BadLocationException e) { // Ignore this and do nothing. } } public int getSourceActions() { return NONE; } public boolean importData(JComponent component, Transferable transferable) { DataFlavor flavor = null; DataFlavor[] flavors = transferable.getTransferDataFlavors(); if (flavors == null) return false; for (int i = 0; i < flavors.length; ++i) if (flavors[i].equals(DataFlavor.stringFlavor)) flavor = flavors[i]; if (flavor == null) return false; try { JTextComponent textComponent = (JTextComponent) component; String data = (String) transferable.getTransferData(flavor); textComponent.replaceSelection(data); return true; } catch (IOException e) { // Ignored. } catch (UnsupportedFlavorException e) { // Ignored. } return false; } } private static final long serialVersionUID = -8796518220218978795L; public static final String DEFAULT_KEYMAP = "default"; public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey"; private static DefaultTransferHandler defaultTransferHandler; private static Hashtable keymaps = new Hashtable(); private Keymap keymap; private char focusAccelerator = '\0'; private NavigationFilter navigationFilter; /** * Get a Keymap from the global keymap table, by name. * * @param n The name of the Keymap to look up * * @return A Keymap associated with the provided name, or * <code>null</code> if no such Keymap exists * * @see #addKeymap * @see #removeKeymap * @see #keymaps */ public static Keymap getKeymap(String n) { return (Keymap) keymaps.get(n); } /** * Remove a Keymap from the global Keymap table, by name. * * @param n The name of the Keymap to remove * * @return The keymap removed from the global table * * @see #addKeymap * @see #getKeymap() * @see #keymaps */ public static Keymap removeKeymap(String n) { Keymap km = (Keymap) keymaps.get(n); keymaps.remove(n); return km; } /** * Create a new Keymap with a specific name and parent, and add the new * Keymap to the global keymap table. The name may be <code>null</code>, * in which case the new Keymap will <em>not</em> be added to the global * Keymap table. The parent may also be <code>null</code>, which is * harmless. * * @param n The name of the new Keymap, or <code>null</code> * @param parent The parent of the new Keymap, or <code>null</code> * * @return The newly created Keymap
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?