All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jtattoo.plaf.BaseTextPaneUI Maven / Gradle / Ivy

There is a newer version: 3.1
Show newest version
/*
 * Copyright (c) 2002 and later by MH Software-Entwicklung. All Rights Reserved.
 *
 * JTattoo is multiple licensed. If your are an open source developer you can use
 * it under the terms and conditions of the GNU General Public License version 2.0
 * or later as published by the Free Software Foundation.
 *
 * see: gpl-2.0.txt
 *
 * If you pay for a license you will become a registered user who could use the
 * software under the terms and conditions of the GNU Lesser General Public License
 * version 2.0 or later with classpath exception as published by the Free Software
 * Foundation.
 *
 * see: lgpl-2.0.txt
 * see: classpath-exception.txt
 *
 * Registered users could also use JTattoo under the terms and conditions of the
 * Apache License, Version 2.0 as published by the Apache Software Foundation.
 *
 * see: APACHE-LICENSE-2.0.txt
 */

package com.jtattoo.plaf;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;

import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTextPaneUI;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;

/**
 * @author Michael Hagen
 */
public class BaseTextPaneUI extends BasicTextPaneUI {

  private Border        orgBorder     = null;
  private FocusListener focusListener = null;

  public static ComponentUI createUI(JComponent c) {
    return new BaseTextPaneUI();
  }

  public void installDefaults() {
    super.installDefaults();
    updateBackground();
  }

  protected void installKeyboardActions() {
    super.installKeyboardActions();
    if (JTattooUtilities.isMac()) {
      InputMap im = (InputMap) UIManager.get("TextPane.focusInputMap");
      int commandKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, commandKey), DefaultEditorKit.copyAction);
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, commandKey), DefaultEditorKit.pasteAction);
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, commandKey), DefaultEditorKit.cutAction);
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_DOWN_MASK), DefaultEditorKit.nextWordAction);
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_DOWN_MASK), DefaultEditorKit.previousWordAction);
    }
  }

  protected void installListeners() {
    super.installListeners();

    if (AbstractLookAndFeel.getTheme().doShowFocusFrame()) {
      focusListener = new FocusListener() {

        public void focusGained(FocusEvent e) {
          if (getComponent() != null) {
            orgBorder = getComponent().getBorder();
            LookAndFeel laf = UIManager.getLookAndFeel();
            if (laf instanceof AbstractLookAndFeel && orgBorder instanceof UIResource) {
              Border focusBorder = ((AbstractLookAndFeel) laf).getBorderFactory().getFocusFrameBorder();
              getComponent().setBorder(focusBorder);
            }
            getComponent().invalidate();
            getComponent().repaint();
          }
        }

        public void focusLost(FocusEvent e) {
          if (getComponent() != null) {
            if (orgBorder instanceof UIResource) {
              getComponent().setBorder(orgBorder);
            }
            getComponent().invalidate();
            getComponent().repaint();
          }
        }
      };
      getComponent().addFocusListener(focusListener);
    }
  }

  protected void uninstallListeners() {
    getComponent().removeFocusListener(focusListener);
    focusListener = null;
    super.uninstallListeners();
  }

  protected void paintBackground(Graphics g) {
    g.setColor(getComponent().getBackground());
    if (AbstractLookAndFeel.getTheme().doShowFocusFrame()) {
      if (getComponent().hasFocus() && getComponent().isEditable()) {
        g.setColor(AbstractLookAndFeel.getTheme().getFocusBackgroundColor());
      }
    }
    g.fillRect(0, 0, getComponent().getWidth(), getComponent().getHeight());
  }

  protected void paintSafely(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Object savedRenderingHint = null;
    if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) {
      savedRenderingHint = g2D.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
      g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, AbstractLookAndFeel.getTheme().getTextAntiAliasingHint());
    }
    super.paintSafely(g);
    if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) {
      g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, savedRenderingHint);
    }
  }

  protected void propertyChange(PropertyChangeEvent evt) {
    if (evt.getPropertyName().equals("editable") || evt.getPropertyName().equals("enabled")) {
      updateBackground();
    }
    super.propertyChange(evt);
  }

  private void updateBackground() {
    JTextComponent c = getComponent();
    if (c.getBackground() instanceof UIResource) {
      if (!c.isEnabled() || !c.isEditable()) {
        c.setBackground(AbstractLookAndFeel.getDisabledBackgroundColor());
      }
      else {
        c.setBackground(AbstractLookAndFeel.getInputBackgroundColor());
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy