📄 jtextcomponent.java
字号:
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 * * @see #removeKeymap * @see #getKeymap() * @see #keymaps */ public static Keymap addKeymap(String n, Keymap parent) { Keymap k = new DefaultKeymap(n); k.setResolveParent(parent); if (n != null) keymaps.put(n, k); return k; } /** * Get the current Keymap of this component. * * @return The component's current Keymap * * @see #setKeymap * @see #keymap */ public Keymap getKeymap() { return keymap; } /** * Set the current Keymap of this component, installing appropriate * {@link KeymapWrapper} and {@link KeymapActionMap} objects in the * {@link InputMap} and {@link ActionMap} parent chains, respectively, * and fire a property change event with name <code>"keymap"</code>. * * @see #getKeymap() * @see #keymap */ public void setKeymap(Keymap k) { // phase 1: replace the KeymapWrapper entry in the InputMap chain. // the goal here is to always maintain the following ordering: // // [InputMap]? -> [KeymapWrapper]? -> [InputMapUIResource]* // // that is to say, component-specific InputMaps need to remain children // of Keymaps, and Keymaps need to remain children of UI-installed // InputMaps (and the order of each group needs to be preserved, of // course). KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k)); InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED); if (childInputMap == null) setInputMap(JComponent.WHEN_FOCUSED, kw); else { while (childInputMap.getParent() != null && !(childInputMap.getParent() instanceof KeymapWrapper) && !(childInputMap.getParent() instanceof InputMapUIResource)) childInputMap = childInputMap.getParent(); // option 1: there is nobody to replace at the end of the chain if (childInputMap.getParent() == null) childInputMap.setParent(kw); // option 2: there is already a KeymapWrapper in the chain which // needs replacing (possibly with its own parents, possibly without) else if (childInputMap.getParent() instanceof KeymapWrapper) { if (kw == null) childInputMap.setParent(childInputMap.getParent().getParent()); else { kw.setParent(childInputMap.getParent().getParent()); childInputMap.setParent(kw); } } // option 3: there is an InputMapUIResource in the chain, which marks // the place where we need to stop and insert ourselves else if (childInputMap.getParent() instanceof InputMapUIResource) { if (kw != null) { kw.setParent(childInputMap.getParent()); childInputMap.setParent(kw); } } } // phase 2: replace the KeymapActionMap entry in the ActionMap chain KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k)); ActionMap childActionMap = getActionMap(); if (childActionMap == null) setActionMap(kam); else { while (childActionMap.getParent() != null && !(childActionMap.getParent() instanceof KeymapActionMap) && !(childActionMap.getParent() instanceof ActionMapUIResource)) childActionMap = childActionMap.getParent(); // option 1: there is nobody to replace at the end of the chain if (childActionMap.getParent() == null) childActionMap.setParent(kam); // option 2: there is already a KeymapActionMap in the chain which // needs replacing (possibly with its own parents, possibly without) else if (childActionMap.getParent() instanceof KeymapActionMap) { if (kam == null) childActionMap.setParent(childActionMap.getParent().getParent()); else { kam.setParent(childActionMap.getParent().getParent()); childActionMap.setParent(kam); } } // option 3: there is an ActionMapUIResource in the chain, which marks // the place where we need to stop and insert ourselves else if (childActionMap.getParent() instanceof ActionMapUIResource) { if (kam != null) { kam.setParent(childActionMap.getParent()); childActionMap.setParent(kam); } } } // phase 3: update the explicit keymap field Keymap old = keymap; keymap = k; firePropertyChange("keymap", old, k); } /** * Resolves a set of bindings against a set of actions and inserts the * results into a {@link Keymap}. Specifically, for each provided binding * <code>b</code>, if there exists a provided action <code>a</code> such * that <code>a.getValue(Action.NAME) == b.ActionName</code> then an * entry is added to the Keymap mapping <code>b</code> to * <code>a</code>. * * @param map The Keymap to add new mappings to * @param bindings The set of bindings to add to the Keymap * @param actions The set of actions to resolve binding names against * * @see Action#NAME * @see Action#getValue * @see KeyBinding#actionName */ public static void loadKeymap(Keymap map, JTextComponent.KeyBinding[] bindings, Action[] actions) { Hashtable acts = new Hashtable(actions.length); for (int i = 0; i < actions.length; ++i) acts.put(actions[i].getValue(Action.NAME), actions[i]); for (int i = 0; i < bindings.length; ++i) if (acts.containsKey(bindings[i].actionName)) map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName)); } /** * Returns the set of available Actions this component's associated * editor can run. Equivalent to calling * <code>getUI().getEditorKit().getActions()</code>. This set of Actions * is a reasonable value to provide as a parameter to {@link * #loadKeymap}, when resolving a set of {@link KeyBinding} objects * against this component. * * @return The set of available Actions on this component's {@link EditorKit} * * @see TextUI#getEditorKit * @see EditorKit#getActions() */ public Action[] getActions() { return getUI().getEditorKit(this).getActions(); } // These are package-private to avoid an accessor method. Document doc; Caret caret; boolean editable; private Highlighter highlighter; private Color caretColor; private Color disabledTextColor; private Color selectedTextColor; private Color selectionColor; private Insets margin; private boolean dragEnabled; /** * Creates a new <code>JTextComponent</code> instance. */ public JTextComponent() { Keymap defkeymap = getKeymap(DEFAULT_KEYMAP); boolean creatingKeymap = false; if (defkeymap == null) { defkeymap = addKeymap(DEFAULT_KEYMAP, null); defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction()); creatingKeymap = true; } setFocusable(true); setEditable(true); enableEvents(AWTEvent.KEY_EVENT_MASK); updateUI(); // need to do this after updateUI() if (creatingKeymap) loadKeymap(defkeymap, new KeyBinding[] { new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), DefaultEditorKit.backwardAction), new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), DefaultEditorKit.forwardAction), new KeyBinding(KeyStroke.getKeyStroke("typed \b"), DefaultEditorKit.deletePrevCharAction), new KeyBinding(KeyStroke.getKeyStroke("typed \u007f"), DefaultEditorKit.deleteNextCharAction) }, getActions()); } public void setDocument(Document newDoc) { Document oldDoc = doc; doc = newDoc; firePropertyChange("document", oldDoc, newDoc); revalidate(); repaint(); } public Document getDocument() { return doc; } /** * Get the <code>AccessibleContext</code> of this object. * * @return an <code>AccessibleContext</code> object */ public AccessibleContext getAccessibleContext() { return null; } public void setMargin(Insets m) { margin = m; } public Insets getMargin() { return margin; } public void setText(String text) { try { if (doc instanceof AbstractDocument) ((AbstractDocument) doc).replace(0, doc.getLength(), text, null); else { doc.remove(0, doc.getLength()); doc.insertString(0, text, null); } } catch (BadLocationException e) { // This can never happen. } } /** * Retrieves the current text in this text document. * * @return the text * * @exception NullPointerException if the underlaying document is null */ public String getText() { if (doc == null) return null; try { return doc.getText(0, doc.getLength()); } catch (BadLocationException e) { // This should never happen. return ""; } } /** * 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 String getText(int offset, int length) throws BadLocationException { return getDocument().getText(offset, length); } /** * Retrieves the currently selected text in this text document. * * @return the selected text * * @exception NullPointerException if the underlaying document is null */ public String getSelectedText() { try { return doc.getText(getSelectionStart(), getSelectionEnd()); } catch (BadLocationException e) { // This should never happen. return null; } } /** * Returns a string that specifies the name of the Look and Feel class * that renders this component. * * @return the string "TextComponentUI" */ public String getUIClassID() { return "TextComponentUI"; } /** * Returns a string representation of this JTextComponent. */ protected String paramString() { // TODO: Do something useful here. return super.paramString(); } /** * This method returns the label's UI delegate. * * @return The label's UI delegate. */ public TextUI getUI() { return (TextUI) ui; } /** * This method sets the label's UI delegate. * * @param newUI The label's UI delegate. */ public void setUI(TextUI newUI) { super.setUI(newUI); } /** * This method resets the label's UI delegate to the default UI for the * current look and feel. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -