?? basictextui.java
字號:
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); } } /** * 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); } } // --- 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> * * @param c the editor component * @see ComponentUI#installUI */ public void installUI(JComponent c) { if (c instanceof JTextComponent) { editor = (JTextComponent) c; // install defaults installDefaults(); installDefaults2(); // common case is background painted... this can // easily be changed by subclasses or from outside // of the component. LookAndFeel.installProperty(editor, "opaque", Boolean.TRUE); LookAndFeel.installProperty(editor, "autoscrolls", Boolean.TRUE); // attach to the model and editor editor.addPropertyChangeListener(updateHandler); Document doc = editor.getDocument(); if (doc == null) { // no model, create a default one. This will // fire a notification to the updateHandler // which takes care of the rest. editor.setDocument(getEditorKit(editor).createDefaultDocument()); } else { doc.addDocumentListener(updateHandler); modelChanged(); } // install keymap installListeners(); installKeyboardActions(); LayoutManager oldLayout = editor.getLayout(); if ((oldLayout == null) || (oldLayout instanceof UIResource)) { // by default, use default LayoutManger implementation that // will position the components associated with a View object. editor.setLayout(updateHandler); } } else { throw new Error("TextUI needs JTextComponent"); } } /** * Deinstalls the UI for a component. This removes the listeners, * uninstalls the highlighter, removes views, and nulls out the keymap. * * @param c the editor component * @see ComponentUI#uninstallUI */ public void uninstallUI(JComponent c) { // detach from the model editor.removePropertyChangeListener(updateHandler); editor.getDocument().removeDocumentListener(updateHandler); // view part painted = false; uninstallDefaults(); rootView.setView(null); c.removeAll(); LayoutManager lm = c.getLayout(); if (lm instanceof UIResource) { c.setLayout(null); } // controller part uninstallKeyboardActions(); uninstallListeners(); editor = null; } /** * Superclass paints background in an uncontrollable way * (i.e. one might want an image tiled into the background). * To prevent this from happening twice, this method is * reimplemented to simply paint. * <p> * <em>NOTE:</em> Superclass is also not thread-safe in * it's rendering of the background, although that's not * an issue with the default rendering. */ public void update(Graphics g, JComponent c) { paint(g, c); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -