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

📄 rtextutilities.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * RTextUtilities.java - Standard tools used by several pieces of
 *                       RText.
 */
package org.fife.rtext;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;

import org.fife.ui.rtextfilechooser.Utilities;
import org.fife.ui.rtextfilechooser.filters.ExtensionFileFilter;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rtextarea.IconGroup;
import org.fife.ui.rtextfilechooser.FileTypeInfo;
import org.fife.ui.rtextfilechooser.RTextFileChooser;
import org.fife.ui.search.FindInFilesDialog;
import org.fife.util.DynamicIntArray;

/**
 * Collection of tools for use by any of the RText components.
 */
/*
 * FIXME:  This class should not be public, but currently SourceBrowserPlugin
 * uses getRegularExpressionForLine().
 */
public class RTextUtilities {

  /**
   * The extension at the end of all macro files.
   */
  public static final String MACRO_EXTENSION = ".macro";

  private static final String FILE_FILTERS_FILE = "ExtraFileChooserFilters.xml";

  static final String MACRO_TEMPORARY_PROPERTY = "RText.temporaryMacroProperty";

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

  /**
   * Adds an extension file filter to the specified file chooser.
   *
   * @param chooser The file chooser.
   * @param msg The resource bundle.
   * @param key The key to use in <code>msg</code> for the file filter's
   *        description.
   * @param extensions Either a string representing a single extension or
   *        an array of strings containing multiple extensions.
   */
  private static final void addFilter(RTextFileChooser chooser,
                                      ResourceBundle msg, String key,
                                      Object extensions) {
    ExtensionFileFilter filter = null;
    if (extensions instanceof String) {
      filter = new ExtensionFileFilter(msg.getString(key),
                                       (String) extensions);
    }
    else {
      filter = new ExtensionFileFilter(msg.getString(key),
                                       (String[]) extensions);
    }
    chooser.addChoosableFileFilter(filter);
  }

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

  /**
   * Configures a find-in-files dialog for RText.
   *
   * @param fnfd The <code>FindInFilesDialog</code> to configure.
   */
  public static final void configureFindInFilesDialog(FindInFilesDialog fnfd) {
    fnfd.addInFilesComboBoxFilter("*.asm");
    fnfd.addInFilesComboBoxFilter("*.bat *.cmd");
    fnfd.addInFilesComboBoxFilter("*.c *.cpp *.cxx *.h");
    fnfd.addInFilesComboBoxFilter("*.cs");
    fnfd.addInFilesComboBoxFilter("*.html");
    fnfd.addInFilesComboBoxFilter("*.java");
    fnfd.addInFilesComboBoxFilter("*.js");
    fnfd.addInFilesComboBoxFilter("*.pl *.perl *.pm");
    fnfd.addInFilesComboBoxFilter("*.py");
    fnfd.addInFilesComboBoxFilter("*.sh *.bsh *.csh *.ksh");
    fnfd.addInFilesComboBoxFilter("*.txt");
  }

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

  /**
   * Creates and initializes a file chooser suitable for RText.
   *
   * @param rtext The RText instance that will own this file chooser.
   * @return A file chooser for RText to use.
   */
  public static final RTextFileChooser createFileChooser(RText rtext) {

    rtext.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    RTextFileChooser chooser = null;

    try {

      chooser = new RTextFileChooser();

      ResourceBundle msg = ResourceBundle.getBundle(
          "org.fife.rtext.FileFilters");

      // Add (localized) file filters.
      addFilter(chooser, msg, "AssemblerX86", "asm");
      addFilter(chooser, msg, "CPlusPlus",
                new String[] {"c", "cpp", "cxx", "h"});
      addFilter(chooser, msg, "CSharp", "cs");
      addFilter(chooser, msg, "CSS", "css");
      addFilter(chooser, msg, "Fortran",
                new String[] {"f", "for", "fort", "f77", "f90"});
      addFilter(chooser, msg, "HTML",
                new String[] {"htm", "html"});
      addFilter(chooser, msg, "Java", "java");
      addFilter(chooser, msg, "JavaScript", "js");
      addFilter(chooser, msg, "Perl",
                new String[] {"pl", "perl", "pm"});
      addFilter(chooser, msg, "PropertiesFiles", "properties");
      addFilter(chooser, msg, "Python", "py");
      addFilter(chooser, msg, "SAS", "sas");
      addFilter(chooser, msg, "SQL", "sql");
      addFilter(chooser, msg, "PlainText", "txt");
      addFilter(chooser, msg, "UnixShell",
                new String[] {"sh", "bsh", "csh", "ksh"});
      addFilter(chooser, msg, "WindowsBatch",
                new String[] {"bat", "cmd"});
      addFilter(chooser, msg, "XML", "xml");

      // Add any user-defined file filters.
      File file = new File(rtext.getInstallLocation(), FILE_FILTERS_FILE);
      try {
        Utilities.addFileFilters(file, chooser);
      }
      catch (Exception e) {
        e.printStackTrace();
      }

      chooser.setFileFilter(new ExtensionFileFilter(
          msg.getString("AllSupported"),
          new String[] {
          "asm",
          "c", "cpp", "cxx", "h",
          "cs",
          "css",
          "f", "for", "fort", "f77", "f90",
          "htm", "html",
          "java",
          "js",
          "perl", "pl", "pm",
          "properties",
          "py",
          "sas",
          "sql",
          "txt",
          "sh", "bsh", "csh", "ksh",
          "bat", "cmd",
          "xml",
      }
          ,
          ExtensionFileFilter.SYSTEM_CASE_CHECK,
          false
          ));

      // Have the chooser open initially to RText's working directory.
      chooser.setCurrentDirectory(rtext.getWorkingDirectory());

    }
    finally {
      // Make sure cursor returns to normal.
      rtext.setCursor(Cursor.getPredefinedCursor(
          Cursor.DEFAULT_CURSOR));
    }

    return chooser;

  }

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

  /**
   * Enables or disables template usage in RText text areas.
   *
   * @param enabled Whether templates should be enabled.
   * @return <code>true</code> if everything went okay; <code>false</code> if
   *         the method failed.
   */
  public static boolean enableTemplates(RText rtext, boolean enabled) {
    boolean old = RSyntaxTextArea.getTemplatesEnabled();
    if (old != enabled) {
      RSyntaxTextArea.setTemplatesEnabled(enabled);
      if (enabled) {
        boolean rc = RSyntaxTextArea.setTemplateDirectory(
            rtext.getInstallLocation() + "/templates");
        return true;
      }
      else {
        return true;
      }
    }
    return true;
  }

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

  /**
   * Returns an image from a file in a safe fashion.
   *
   * @param fileName The file from which to get the image (must be .jpg,
   *        .gif or .png).
   * @return The image contained in the file, or <code>null</code> if the
   *         image file was invalid.
   */
  public static Image getImageFromFile(String fileName) {
    BufferedImage image = null;
    try {
      image = ImageIO.read(new URL("file:///" + fileName));
    }
    catch (MalformedURLException mue) {
      mue.printStackTrace(); // This is our fault.
    }
    catch (IOException e) {
      // Do nothing.
    }
    return image; // null if there was an exception thrown.
  }

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

  /**
   * Returns the directory in which the user's macros are stored.
   *
   * @return The macro directory, or <code>null</code> if it cannot be found
   *         or created.
   */
  public static final File getMacroDirectory() {

    File f = new File(System.getProperty("user.home") + "/.rtext/macros");

⌨️ 快捷键说明

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