📄 basictextui.java
字号:
{ // Document changed. modelChanged(); } BasicTextUI.this.propertyChange(event); } } /** * Listens for changes on the underlying model and forwards notifications * to the View. This also updates the caret position of the text component. * * TODO: Maybe this should somehow be handled through EditorKits */ class DocumentHandler implements DocumentListener { /** * Notification about a document change event. * * @param ev the DocumentEvent describing the change */ public void changedUpdate(DocumentEvent ev) { rootView.changedUpdate(ev, getVisibleEditorRect(), rootView.getViewFactory()); } /** * Notification about a document insert event. * * @param ev the DocumentEvent describing the insertion */ public void insertUpdate(DocumentEvent ev) { rootView.insertUpdate(ev, getVisibleEditorRect(), rootView.getViewFactory()); } /** * Notification about a document removal event. * * @param ev the DocumentEvent describing the removal */ public void removeUpdate(DocumentEvent ev) { rootView.removeUpdate(ev, getVisibleEditorRect(), rootView.getViewFactory()); } } /** * The EditorKit used by this TextUI. */ // FIXME: should probably be non-static. static EditorKit kit = new DefaultEditorKit(); /** * The root view. */ RootView rootView = new RootView(); /** * The text component that we handle. */ JTextComponent textComponent; /** * Receives notification when the model changes. */ PropertyChangeHandler updateHandler = new PropertyChangeHandler(); /** The DocumentEvent handler. */ DocumentHandler documentHandler = new DocumentHandler(); /** * The standard background color. This is the color which is used to paint * text in enabled text components. */ Color background; /** * The inactive background color. This is the color which is used to paint * text in disabled text components. */ Color inactiveBackground; /** * Creates a new <code>BasicTextUI</code> instance. */ public BasicTextUI() { // Nothing to do here. } /** * Creates a {@link Caret} that should be installed into the text component. * * @return a caret that should be installed into the text component */ protected Caret createCaret() { return new BasicCaret(); } /** * Creates a {@link Highlighter} that should be installed into the text * component. * * @return a <code>Highlighter</code> for the text component */ protected Highlighter createHighlighter() { return new BasicHighlighter(); } /** * The text component that is managed by this UI. * * @return the text component that is managed by this UI */ protected final JTextComponent getComponent() { return textComponent; } /** * Installs this UI on the text component. * * @param c the text component on which to install the UI */ public void installUI(final JComponent c) { super.installUI(c); c.setOpaque(true); textComponent = (JTextComponent) c; Document doc = textComponent.getDocument(); if (doc == null) { doc = getEditorKit(textComponent).createDefaultDocument(); textComponent.setDocument(doc); } textComponent.addPropertyChangeListener(updateHandler); modelChanged(); installDefaults(); installListeners(); installKeyboardActions(); } /** * Installs UI defaults on the text components. */ protected void installDefaults() { Caret caret = textComponent.getCaret(); if (caret == null) { caret = createCaret(); textComponent.setCaret(caret); } Highlighter highlighter = textComponent.getHighlighter(); if (highlighter == null) textComponent.setHighlighter(createHighlighter()); String prefix = getPropertyPrefix(); LookAndFeel.installColorsAndFont(textComponent, prefix + ".background", prefix + ".foreground", prefix + ".font"); LookAndFeel.installBorder(textComponent, prefix + ".border"); textComponent.setMargin(UIManager.getInsets(prefix + ".margin")); caret.setBlinkRate(UIManager.getInt(prefix + ".caretBlinkRate")); // Fetch the colors for enabled/disabled text components. background = UIManager.getColor(prefix + ".background"); inactiveBackground = UIManager.getColor(prefix + ".inactiveBackground"); textComponent.setDisabledTextColor (UIManager.getColor(prefix + ".inactiveForeground")); textComponent.setSelectedTextColor(UIManager.getColor(prefix + ".selectionForeground")); textComponent.setSelectionColor(UIManager.getColor(prefix + ".selectionBackground")); } /** * This FocusListener triggers repaints on focus shift. */ private FocusListener focuslistener = new FocusListener() { public void focusGained(FocusEvent e) { textComponent.repaint(); } public void focusLost(FocusEvent e) { textComponent.repaint(); } }; /** * Install all listeners on the text component. */ protected void installListeners() { textComponent.addFocusListener(focuslistener); installDocumentListeners(); } /** * Installs the document listeners on the textComponent's model. */ private void installDocumentListeners() { Document doc = textComponent.getDocument(); if (doc != null) doc.addDocumentListener(documentHandler); } /** * Returns the name of the keymap for this type of TextUI. * * This is implemented so that the classname of this TextUI * without the package prefix is returned. This way subclasses * don't have to override this method. * * @return the name of the keymap for this TextUI */ protected String getKeymapName() { String fullClassName = getClass().getName(); int index = fullClassName.lastIndexOf('.'); String className = fullClassName.substring(index + 1); return className; } /** * Creates the {@link Keymap} that is installed on the text component. * * @return the {@link Keymap} that is installed on the text component */ protected Keymap createKeymap() { String prefix = getPropertyPrefix(); JTextComponent.KeyBinding[] bindings = (JTextComponent.KeyBinding[]) UIManager.get(prefix + ".keyBindings"); if (bindings == null) { bindings = new JTextComponent.KeyBinding[0]; // FIXME: Putting something into the defaults map is certainly wrong. // Must be fixed somehow. UIManager.put(prefix + ".keyBindings", bindings); } Keymap km = JTextComponent.addKeymap(getKeymapName(), JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP)); JTextComponent.loadKeymap(km, bindings, textComponent.getActions()); return km; } /** * Installs the keyboard actions on the text components. */ protected void installKeyboardActions() { // load any bindings for the older Keymap interface Keymap km = JTextComponent.getKeymap(getKeymapName()); if (km == null) km = createKeymap(); textComponent.setKeymap(km); // load any bindings for the newer InputMap / ActionMap interface SwingUtilities.replaceUIInputMap(textComponent, JComponent.WHEN_FOCUSED, getInputMap(JComponent.WHEN_FOCUSED)); SwingUtilities.replaceUIActionMap(textComponent, getActionMap()); } /** * Gets the input map for the specified <code>condition</code>. * * @param condition the condition for the InputMap * * @return the InputMap for the specified condition */ InputMap getInputMap(int condition) { String prefix = getPropertyPrefix(); switch (condition) { case JComponent.WHEN_IN_FOCUSED_WINDOW: // FIXME: is this the right string? nobody seems to use it. return (InputMap) UIManager.get(prefix + ".windowInputMap"); case JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT: return (InputMap) UIManager.get(prefix + ".ancestorInputMap"); default: case JComponent.WHEN_FOCUSED: return (InputMap) UIManager.get(prefix + ".focusInputMap"); } } /** * Returns the ActionMap to be installed on the text component. * * @return the ActionMap to be installed on the text component */ // FIXME: The UIDefaults have no entries for .actionMap, so this should // be handled somehow different. ActionMap getActionMap() { String prefix = getPropertyPrefix(); ActionMap am = (ActionMap) UIManager.get(prefix + ".actionMap"); if (am == null) { am = createActionMap(); // FIXME: Putting something in the UIDefaults map is certainly wrong. // However, the whole method seems wrong and must be replaced by // something that is less wrong. UIManager.put(prefix + ".actionMap", am); } return am; } /** * Creates an ActionMap to be installed on the text component. * * @return an ActionMap to be installed on the text component */ ActionMap createActionMap() { Action[] actions = textComponent.getActions(); ActionMap am = new ActionMapUIResource(); for (int i = 0; i < actions.length; ++i) { String name = (String) actions[i].getValue(Action.NAME); if (name != null) am.put(name, actions[i]); } return am; } /** * Uninstalls this TextUI from the text component. * * @param component the text component to uninstall the UI from */ public void uninstallUI(final JComponent component) { super.uninstallUI(component); rootView.setView(null); textComponent.removePropertyChangeListener(updateHandler); uninstallDefaults(); uninstallListeners(); uninstallKeyboardActions(); textComponent = null; } /** * Uninstalls all default properties that have previously been installed by * this UI. */ protected void uninstallDefaults() { // Do nothing here. } /** * Uninstalls all listeners that have previously been installed by * this UI. */ protected void uninstallListeners() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -