jtextcomponent.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,854 行 · 第 1/4 页
JAVA
1,854 行
* * @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); if (defkeymap == null) { defkeymap = addKeymap(DEFAULT_KEYMAP, null); defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction()); } setFocusable(true); setEditable(true); enableEvents(AWTEvent.KEY_EVENT_MASK); setOpaque(true); updateUI(); } 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 new AccessibleJTextComponent(); } 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() { int start = getSelectionStart(); int offset = getSelectionEnd() - start; if (offset <= 0) return null; try { return doc.getText(start, offset); } 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. */ public void updateUI() { setUI((TextUI) UIManager.getUI(this)); } public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } public int getScrollableUnitIncrement(Rectangle visible, int orientation, int direction) { // We return 1/10 of the visible area as documented in Sun's API docs. if (orientation == SwingConstants.HORIZONTAL) return visible.width / 10; else if (orientation == SwingConstants.VERTICAL) return visible.height / 10; else throw new IllegalArgumentException("orientation must be either " + "javax.swing.SwingConstants.VERTICAL " + "or " + "javax.swing.SwingConstants.HORIZONTAL" ); } public int getScrollableBlockIncrement(Rectangle visible, int orientation, int direction) { // We return the whole visible area as documented in Sun's API docs. if (orientation == SwingConstants.HORIZONTAL) return visible.width; else if (orientation == SwingConstants.VERTICAL) return visible.height; else throw new IllegalArgumentException("orientation must be either " + "javax.swing.SwingConstants.VERTICAL " + "or " + "javax.swing.SwingConstants.HORIZONTAL" ); } /** * Checks whether this text component it editable. * * @return true if editable, false otherwise */ public boolean isEditable() { return editable; } /** * Enables/disabled this text component's editability. * * @param newValue true to make it editable, false otherwise. */ public void setEditable(boolean newValue) { if (editable == newValue) return; boolean oldValue = editable; editable = newValue; firePropertyChange("editable", oldValue, newValue); } /** * The <code>Caret</code> object used in this text component. * * @return the caret object */ public Caret getCaret() { return caret; } /** * Sets a new <code>Caret</code> for this text component. * * @param newCaret the new <code>Caret</code> to set */ public void setCaret(Caret newCaret) { if (caret != null) caret.deinstall(this);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?