⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtextpreferences.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * RTextPreferences.java - A class representing several properties of
 *                        an RText session.
 */
package org.fife.rtext;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.prefs.Preferences;
import javax.swing.Action;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

import org.fife.RUtilities;
import org.fife.ui.StatusBar;
import org.fife.ui.app.GUIApplication;
import org.fife.ui.app.GUIApplicationPreferences;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxHighlightingColorScheme;
import org.fife.ui.rsyntaxtextarea.SyntaxScheme;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rtextarea.ConfigurableCaret;
import org.fife.ui.rtextarea.RTextArea;

/**
 * A class representing several properties of an RText session.  This class
 * is used for saving user preferences between RText sessions. It consists
 * only of public data members for ease of use.
 */
public class RTextPreferences
    extends GUIApplicationPreferences
    implements RTextActionInfo {

  private static final String[] mainViewAcceleratorKeys = {
      "findActionAccelerator",
      "findNextActionAccelerator",
      "replaceActionAccelerator",
      "replaceNextActionAccelerator",
      "replaceAllActionAccelerator",
      "findInFilesActionAccelerator",
      "printActionAccelerator",
      "printPreviewActionAccelerator",
      "closeActionAccelerator",
      "closeAllActionAccelerator",
      "gotoActionAccelerator"
  };

  private static final int defaultModifier = Toolkit.getDefaultToolkit().
      getMenuShortcutKeyMask();

  public static final KeyStroke[] defaultMainViewActionAccelerators = {
      KeyStroke.getKeyStroke(KeyEvent.VK_F, defaultModifier),
      KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0),
      KeyStroke.getKeyStroke(KeyEvent.VK_H, defaultModifier),
      KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0),
      null,
      null,
      KeyStroke.getKeyStroke(KeyEvent.VK_P, defaultModifier),
      null,
      KeyStroke.getKeyStroke(KeyEvent.VK_L, defaultModifier),
      null,
      KeyStroke.getKeyStroke(KeyEvent.VK_G, defaultModifier)
  };

  private static final String NOTHING_STRING = "-";

  public String iconGroupName;
  public boolean lineNumbersVisible;
  public int tabSize; // In spaces.
  public boolean emulateTabsWithSpaces; // Whether or not to emulate tabs with spaces.
  public int textMode; // Either RTextArea.INSERT_MODE (1) or RTextArea.OVERWRITE_MODE (2).
  public int tabPlacement; // One of JTabbedPane.TOP/LEFT/BOTTOM/RIGHT.
  public Font printFont; // The font ot use when printing.
  public Object backgroundObject; // Either a Color or an Image.
  public float imageAlpha; // How "translucent" to make a background image (0.0f - 1.0f).
  public boolean wordWrap; // Whether or not word wrap is enabled.
  public Color caretColor;
  public Color selectionColor;
  public SyntaxHighlightingColorScheme colorScheme; // Color scheme used in syntax highlighting.
  public String syntaxFiltersString; // String representing the syntax filters.
  public int maxFileHistorySize;
  public String fileHistoryString; // String representing the file history.
  public boolean currentLineHighlightEnabled;
  public Color currentLineHighlightColor;
  public int mainView; // Either RText.TABBED_VIEW or RText.SPLIT_PANE_VIEW.
  public boolean highlightModifiedDocNames;
  public Color modifiedDocumentNamesColor;
  public boolean bracketMatchingEnabled;
  public Color matchedBracketBGColor;
  public Color matchedBracketBorderColor;
  public boolean marginLineEnabled;
  public int marginLinePosition;
  public Color marginLineColor;
  public boolean visibleWhitespace;
  public boolean smoothTextEnabled;
  public boolean fractionalMetricsEnabled;
  public Color markAllHighlightColor;
  public int statusBarStyle;
  public boolean roundedSelectionEdges;
  public String workingDirectory;
  public int[] carets; // Index 0=>insert, 1=>overwrite.
  public int caretBlinkRate;
  public boolean searchToolBarVisible;
  public int[] dividerLocations; // Dividers for plugin JSplitPanes.

  public KeyStroke[] mainViewActionAccelerators;

      /*****************************************************************************/

  /**
   * Constructor; initializes all values to "defaults."
   */
  private RTextPreferences() {
    setDefaults();
  }

      /*****************************************************************************/

  /**
   * Creates a properties object with all fields initialized to the values
   * that the specified RText instance is currently running with.
   *
   * @return An <code>RTextPreferences</code> object initialized to contain
   *         the properties the specified RText instance is running with.
   */
  public static GUIApplicationPreferences generatePreferences(RText rtext) {

    AbstractMainView mainView = rtext.getMainView();
    RTextMenuBar menuBar = (RTextMenuBar) rtext.getJMenuBar();

    String lnfString = UIManager.getLookAndFeel().getClass().getName();

    RTextPreferences props = new RTextPreferences();
    props.location = rtext.getLocation();
    props.location.translate(15, 15);
    props.size = rtext.isMaximized() ? new Dimension( -1, -1) : rtext.getSize();
    props.lookAndFeel = lnfString;
    props.iconGroupName = rtext.getIconGroup().getName();
    props.toolbarVisible = rtext.getToolBar().isVisible();
    props.statusBarVisible = rtext.getStatusBar().isVisible();
    props.lineNumbersVisible = mainView.getLineNumbersEnabled();
    props.tabSize = mainView.getTabSize();
    props.emulateTabsWithSpaces = mainView.areTabsEmulated();
    props.textMode = mainView.getTextMode();
    props.tabPlacement = mainView.getDocumentSelectionPlacement();
    props.printFont = mainView.getPrintFont();
    props.backgroundObject = mainView.getBackgroundObject();
    props.imageAlpha = mainView.getBackgroundImageAlpha();
    props.wordWrap = mainView.getLineWrap();
    props.caretColor = mainView.getCaretColor();
    props.selectionColor = mainView.getSelectionColor();
    props.colorScheme = rtext.getSyntaxHighlightingColorScheme();
    props.syntaxFiltersString = mainView.getSyntaxFilters().toString();
    props.maxFileHistorySize = menuBar.getMaximumFileHistorySize();
    props.fileHistoryString = menuBar.getFileHistoryString();
    props.currentLineHighlightEnabled = mainView.isCurrentLineHighlightEnabled();
    props.currentLineHighlightColor = mainView.getCurrentLineHighlightColor();
    props.mainView = rtext.getMainViewStyle();
    props.highlightModifiedDocNames = mainView.
        highlightModifiedDocumentDisplayNames();
    props.modifiedDocumentNamesColor = mainView.
        getModifiedDocumentDisplayNamesColor();
    props.language = rtext.getLanguage();
    props.bracketMatchingEnabled = mainView.isBracketMatchingEnabled();
    props.matchedBracketBGColor = mainView.getMatchedBracketBGColor();
    props.matchedBracketBorderColor = mainView.getMatchedBracketBorderColor();
    props.marginLineEnabled = mainView.isMarginLineEnabled();
    props.marginLinePosition = mainView.getMarginLinePosition();
    props.marginLineColor = mainView.getMarginLineColor();
    props.visibleWhitespace = mainView.isWhitespaceVisible();
    props.smoothTextEnabled = mainView.isSmoothTextEnabled();
    props.fractionalMetricsEnabled = mainView.isFractionalFontMetricsEnabled();
    props.markAllHighlightColor = mainView.getMarkAllHighlightColor();
    props.statusBarStyle = rtext.getStatusBar().getStyle();
    props.roundedSelectionEdges = mainView.getRoundedSelectionEdges();
    props.workingDirectory = rtext.getWorkingDirectory();
    props.carets[RTextArea.INSERT_MODE] = mainView.getCaretStyle(RTextArea.
        INSERT_MODE);
    props.carets[RTextArea.OVERWRITE_MODE] = mainView.getCaretStyle(RTextArea.
        OVERWRITE_MODE);
    props.caretBlinkRate = mainView.getCaretBlinkRate();
    props.searchToolBarVisible = rtext.isSearchToolBarVisible();
    props.dividerLocations[RText.TOP] = rtext.getSplitPaneDividerLocation(RText.
        TOP);
    props.dividerLocations[RText.LEFT] = rtext.getSplitPaneDividerLocation(
        RText.LEFT);
    props.dividerLocations[RText.BOTTOM] = rtext.getSplitPaneDividerLocation(
        RText.BOTTOM);
    props.dividerLocations[RText.RIGHT] = rtext.getSplitPaneDividerLocation(
        RText.RIGHT);

    // Save the actions owned by RText.
    props.accelerators = new HashMap();
    for (int i = 0; i < RText.actionNames.length; i++) {
      String actionName = rtext.actionNames[i];
      props.accelerators.put(actionName,
                             (KeyStroke) rtext.getAction(actionName).
                             getValue(Action.ACCELERATOR_KEY));
    }

    // Save the actions owned by the main view.
    Action[] mainViewActions = mainView.getActions();
    for (int i = 0; i < AbstractMainView.NUM_ACTIONS; i++) {
      props.mainViewActionAccelerators[i] =
          (KeyStroke) mainViewActions[i].getValue(Action.ACCELERATOR_KEY);

    }
    return props;

  }

      /*****************************************************************************/

  /**
   * Returns the keystroke from the passed-in string of the form
   * "<keycode> <modifiers>".
   *
   * @param string The string from which to get the keystroke.  This string
   *               was saved by a previous <code>RTextPreferences</code>.
   * @return The keystroke.
   * @see #getKeyStrokeString
   */
  private static final KeyStroke getKeyStrokeFromString(String string) {
    int space = string.indexOf(' ');
    if (space > -1) {
      return KeyStroke.getKeyStroke(
          Integer.parseInt(string.substring(0, space)),
          Integer.parseInt(string.substring(space + 1)));
    }
    return null;
  }

      /*****************************************************************************/

  /**
   * Returns a string suitable for saving this keystroke via the Java
   * Preferences API of the form "<keycode> <modifiers>".
   *
   * @param stroke The keystroke for which to get the string.
   * @return A <code>String</code> representing the keystroke.
   * @see #getKeySTrokeFromString
   */
  private static final String getKeyStrokeString(KeyStroke stroke) {
    if (stroke != null) {
      return stroke.getKeyCode() + " " + stroke.getModifiers();
    }
    else {
      return NOTHING_STRING;
    }
  }

      /*****************************************************************************/

  /**
   * Returns a preferences instance with data saved previously via the
   * Java Preferences API.  If the load fails, this preferences instance
   * will be populated with default values.
   *
   * @return If the load went okay, <code>true</code> is returned.  If the
   *         load failed default values will be set.
   */
  public static GUIApplicationPreferences loadPreferences() {

    RTextPreferences props = new RTextPreferences();

    try {

      // Get all properties assiciated with the RText class.
      Preferences prefs = Preferences.userNodeForPackage(RText.class);
      loadCommonPreferences(props, prefs);
      props.iconGroupName = prefs.get("iconGroupName", props.iconGroupName);
      props.lineNumbersVisible = prefs.getBoolean("lineNumbersVisible",
                                                  props.lineNumbersVisible);
      props.mainView = prefs.getInt("mainView", props.mainView);
      String temp = prefs.get("colorScheme", null);
      props.colorScheme = SyntaxHighlightingColorScheme.loadFromString(temp);
      props.statusBarStyle = prefs.getInt("statusBarStyle",
                                          props.statusBarStyle);
      props.workingDirectory = prefs.get("workingDirectory",
                                         props.workingDirectory);
      props.searchToolBarVisible = prefs.getBoolean("searchToolBarVisible",
          props.searchToolBarVisible);
      props.dividerLocations[RText.TOP] = prefs.getInt(
          "pluginDividerLocation.top", props.dividerLocations[RText.TOP]);
      props.dividerLocations[RText.LEFT] = prefs.getInt(
          "pluginDividerLocation.left", props.dividerLocations[RText.LEFT]);
      props.dividerLocations[RText.BOTTOM] = prefs.getInt(
          "pluginDividerLocation.bottom", props.dividerLocations[RText.BOTTOM]);
      props.dividerLocations[RText.RIGHT] = prefs.getInt(
          "pluginDividerLocation.right", props.dividerLocations[RText.RIGHT]);

      // Get all properties associated with the MainPanel class.
      prefs = Preferences.userNodeForPackage(AbstractMainView.class);
      props.bracketMatchingEnabled = prefs.getBoolean("bracketMatchingEnabled",
          props.bracketMatchingEnabled);
      props.currentLineHighlightEnabled = prefs.getBoolean(
          "currentLineHighlightEnabled", props.currentLineHighlightEnabled);
      props.emulateTabsWithSpaces = prefs.getBoolean("emulateTabs",
          props.emulateTabsWithSpaces);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -