📄 basictextui.java
字号:
if (editor.getSelectionColor() instanceof UIResource) { editor.setSelectionColor(null); } if (editor.getDisabledTextColor() instanceof UIResource) { editor.setDisabledTextColor(null); } if (editor.getSelectedTextColor() instanceof UIResource) { editor.setSelectedTextColor(null); } if (editor.getBorder() instanceof UIResource) { editor.setBorder(null); } if (editor.getMargin() instanceof UIResource) { editor.setMargin(null); } if (editor.getCaret() instanceof UIResource) { editor.setCaret(null); } if (editor.getHighlighter() instanceof UIResource) { editor.setHighlighter(null); } if (editor.getTransferHandler() instanceof UIResource) { editor.setTransferHandler(null); } if (editor.getCursor() instanceof UIResource) { editor.setCursor(null); } } /** * Installs listeners for the UI. */ protected void installListeners() { } /** * Uninstalls listeners for the UI. */ protected void uninstallListeners() { } protected void installKeyboardActions() { // backward compatibility support... keymaps for the UI // are now installed in the more friendly input map. editor.setKeymap(createKeymap()); InputMap km = getInputMap(); if (km != null) { SwingUtilities.replaceUIInputMap(editor, JComponent.WHEN_FOCUSED, km); } ActionMap map = getActionMap(); if (map != null) { SwingUtilities.replaceUIActionMap(editor, map); } updateFocusAcceleratorBinding(false); } /** * Get the InputMap to use for the UI. */ InputMap getInputMap() { InputMap map = new InputMapUIResource(); InputMap shared = (InputMap)DefaultLookup.get(editor, this, getPropertyPrefix() + ".focusInputMap"); if (shared != null) { map.setParent(shared); } return map; } /** * Invoked when the focus accelerator changes, this will update the * key bindings as necessary. */ void updateFocusAcceleratorBinding(boolean changed) { char accelerator = editor.getFocusAccelerator(); if (changed || accelerator != '\0') { InputMap km = SwingUtilities.getUIInputMap (editor, JComponent.WHEN_IN_FOCUSED_WINDOW); if (km == null && accelerator != '\0') { km = new ComponentInputMapUIResource(editor); SwingUtilities.replaceUIInputMap(editor, JComponent. WHEN_IN_FOCUSED_WINDOW, km); ActionMap am = getActionMap(); SwingUtilities.replaceUIActionMap(editor, am); } if (km != null) { km.clear(); if (accelerator != '\0') { km.put(KeyStroke.getKeyStroke(accelerator, ActionEvent.ALT_MASK), "requestFocus"); } } } } /** * Invoked when editable property is changed. * * removing 'TAB' and 'SHIFT-TAB' from traversalKeysSet in case * editor is editable * adding 'TAB' and 'SHIFT-TAB' to traversalKeysSet in case * editor is non editable */ void updateFocusTraversalKeys() { /* * Fix for 4514331 Non-editable JTextArea and similar * should allow Tab to keyboard - accessibility */ EditorKit editorKit = getEditorKit(editor); if ( editorKit != null && editorKit instanceof DefaultEditorKit) { Set storedForwardTraversalKeys = editor. getFocusTraversalKeys(KeyboardFocusManager. FORWARD_TRAVERSAL_KEYS); Set storedBackwardTraversalKeys = editor. getFocusTraversalKeys(KeyboardFocusManager. BACKWARD_TRAVERSAL_KEYS); Set forwardTraversalKeys = new HashSet(storedForwardTraversalKeys); Set backwardTraversalKeys = new HashSet(storedBackwardTraversalKeys); if (editor.isEditable()) { forwardTraversalKeys. remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); backwardTraversalKeys. remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK)); } else { forwardTraversalKeys.add(KeyStroke. getKeyStroke(KeyEvent.VK_TAB, 0)); backwardTraversalKeys. add(KeyStroke. getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK)); } LookAndFeel.installProperty(editor, "focusTraversalKeysForward", forwardTraversalKeys); LookAndFeel.installProperty(editor, "focusTraversalKeysBackward", backwardTraversalKeys); } } /** * As needed updates cursor for the target editor. */ private void updateCursor() { if ((! editor.isCursorSet()) || editor.getCursor() instanceof UIResource) { Cursor cursor = (editor.isEditable()) ? textCursor : null; editor.setCursor(cursor); } } /** * Returns the <code>TransferHandler</code> that will be installed if * their isn't one installed on the <code>JTextComponent</code>. */ TransferHandler getTransferHandler() { return defaultTransferHandler; } /** * Fetch an action map to use. */ ActionMap getActionMap() { String mapName = getPropertyPrefix() + ".actionMap"; ActionMap map = (ActionMap)UIManager.get(mapName); if (map == null) { map = createActionMap(); if (map != null) { UIManager.getLookAndFeelDefaults().put(mapName, map); } } ActionMap componentMap = new ActionMapUIResource(); componentMap.put("requestFocus", new FocusAction()); /* * fix for bug 4515750 * JTextField & non-editable JTextArea bind return key - default btn not accessible * * Wrap the return action so that it is only enabled when the * component is editable. This allows the default button to be * processed when the text component has focus and isn't editable. * */ if (getEditorKit(editor) instanceof DefaultEditorKit) { if (map != null) { Object obj = map.get(DefaultEditorKit.insertBreakAction); if (obj != null && obj instanceof DefaultEditorKit.InsertBreakAction) { Action action = new TextActionWrapper((TextAction)obj); componentMap.put(action.getValue(Action.NAME),action); } } } if (map != null) { componentMap.setParent(map); } return componentMap; } /** * Create a default action map. This is basically the * set of actions found exported by the component. */ ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); Action[] actions = editor.getActions(); //System.out.println("building map for UI: " + getPropertyPrefix()); int n = actions.length; for (int i = 0; i < n; i++) { Action a = actions[i]; map.put(a.getValue(Action.NAME), a); //System.out.println(" " + a.getValue(Action.NAME)); } map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); return map; } protected void uninstallKeyboardActions() { editor.setKeymap(null); SwingUtilities.replaceUIInputMap(editor, JComponent. WHEN_IN_FOCUSED_WINDOW, null); SwingUtilities.replaceUIActionMap(editor, null); } /** * Paints a background for the view. This will only be * called if isOpaque() on the associated component is * true. The default is to paint the background color * of the component. * * @param g the graphics context */ protected void paintBackground(Graphics g) { g.setColor(editor.getBackground()); g.fillRect(0, 0, editor.getWidth(), editor.getHeight()); } /** * Fetches the text component associated with this * UI implementation. This will be null until * the ui has been installed. * * @return the editor component */ protected final JTextComponent getComponent() { return editor; } /** * Flags model changes. * This is called whenever the model has changed. * It is implemented to rebuild the view hierarchy * to represent the default root element of the * associated model. */ protected void modelChanged() { // create a view hierarchy ViewFactory f = rootView.getViewFactory(); Document doc = editor.getDocument(); Element elem = doc.getDefaultRootElement(); setView(f.create(elem)); } /** * Sets the current root of the view hierarchy and calls invalidate(). * If there were any child components, they will be removed (i.e. * there are assumed to have come from components embedded in views). * * @param v the root view */ protected final void setView(View v) { rootView.setView(v); painted = false; editor.revalidate(); editor.repaint(); } /** * Paints the interface safely with a guarantee that * the model won't change from the view of this thread. * This does the following things, rendering from * back to front. * <ol> * <li> * If the component is marked as opaque, the background * is painted in the current background color of the * component. * <li> * The highlights (if any) are painted. * <li> * The view hierarchy is painted. * <li> * The caret is painted. * </ol> * * @param g the graphics context */ protected void paintSafely(Graphics g) { painted = true; Highlighter highlighter = editor.getHighlighter(); Caret caret = editor.getCaret(); // paint the background if (editor.isOpaque()) { paintBackground(g); } // paint the highlights if (highlighter != null) { highlighter.paint(g); } // paint the view hierarchy Rectangle alloc = getVisibleEditorRect(); if (alloc != null) { rootView.paint(g, alloc); } // paint the caret if (caret != null) { caret.paint(g); } if (dropCaret != null) { dropCaret.paint(g); } } // --- ComponentUI methods -------------------------------------------- /** * Installs the UI for a component. This does the following * things. * <ol> * <li> * Set the associated component to opaque (can be changed * easily by a subclass or on JTextComponent directly), * which is the most common case. This will cause the * component's background color to be painted. * <li> * Install the default caret and highlighter into the * associated component. * <li> * Attach to the editor and model. If there is no * model, a default one is created. * <li> * create the view factory and the view hierarchy used * to represent the model. * </ol> *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -