EclipseJavaFormatterSettings.java

  1. /*
  2.  * Copyright (c) 2020 bahlef.
  3.  * All rights reserved. This program and the accompanying materials
  4.  * are made available under the terms of the Eclipse Public License v2.0
  5.  * which accompanies this distribution, and is available at
  6.  * http://www.eclipse.org/legal/epl-v20.html
  7.  * Contributors:
  8.  * bahlef - initial API and implementation and/or initial documentation
  9.  */
  10. package de.funfried.netbeans.plugins.external.formatter.java.eclipse;

  11. import java.util.prefs.Preferences;

  12. import javax.swing.text.Document;

  13. import de.funfried.netbeans.plugins.external.formatter.eclipse.xml.EclipseFormatterUtils;

  14. /**
  15.  * Utility class for Eclipse specific settings.
  16.  *
  17.  * @author bahlef
  18.  */
  19. public class EclipseJavaFormatterSettings {
  20.     /** Property key which defines the active profile of the Eclipse configuration file. */
  21.     public static final String ACTIVE_PROFILE = "eclipseFormatterActiveProfile";

  22.     /** Property key which defines the location of the Eclipse formatter configuration file. */
  23.     public static final String CONFIG_FILE_LOCATION = "eclipseFormatterLocation";

  24.     /**
  25.      * Property key which defines whether or not to use Eclipse project specific formatter configuration if available.
  26.      *
  27.      * @since 1.10
  28.      */
  29.     public static final String USE_PROJECT_PREFS = "useProjectPref";

  30.     /**
  31.      * Constant value of the Eclipse project specific formatter configuration file name.
  32.      *
  33.      * @since 1.10
  34.      */
  35.     public static final String PROJECT_PREF_FILE = "org.eclipse.jdt.core.prefs";

  36.     /**
  37.      * Property key which defines the line feed setting for the Eclipse formatter.
  38.      *
  39.      * @since 1.10
  40.      */
  41.     public static final String LINEFEED = "linefeed";

  42.     /**
  43.      * Property key which defines the source level setting for the Eclipse formatter.
  44.      *
  45.      * @since 1.10
  46.      */
  47.     public static final String SOURCELEVEL = "sourcelevel";

  48.     /**
  49.      * Private contructor because of static methods only.
  50.      */
  51.     private EclipseJavaFormatterSettings() {
  52.     }

  53.     /**
  54.      * Returns the Eclipse formatter file for the given {@link Document} from the given {@link Preferences}.
  55.      * If {@link #USE_PROJECT_PREFS} is {@code true} in the given {@link Preferences}, it will be automatically
  56.      * checked if there is a project specific formatter configuration file available.
  57.      *
  58.      * @param preferences the {@link Preferences} where to load from
  59.      * @param document the {@link Document}
  60.      *
  61.      * @return the Eclipse formatter file for the given {@link Document} from the given {@link Preferences}.
  62.      *         If {@link #USE_PROJECT_PREFS} is {@code true} in the given {@link Preferences}, it will be automatically
  63.      *         checked if there is a project specific formatter configuration file available
  64.      */
  65.     public static String getEclipseFormatterFile(Preferences preferences, Document document) {
  66.         return EclipseFormatterUtils.getEclipseFormatterFile(preferences, document, CONFIG_FILE_LOCATION, USE_PROJECT_PREFS, PROJECT_PREF_FILE);
  67.     }

  68.     /**
  69.      * Returns {@code true} if the given {@code filename} ends with the workspace mechanic file extension epf.
  70.      *
  71.      * @param filename the filename to check
  72.      *
  73.      * @return {@code true} if the given {@code filename} ends with the workspace mechanic file extension epf,
  74.      *         otherwise {@code false}
  75.      */
  76.     public static boolean isWorkspaceMechanicFile(String filename) {
  77.         return EclipseFormatterUtils.isWorkspaceMechanicFile(filename);
  78.     }

  79.     /**
  80.      * Returns {@code true} if the given {@code filename} ends with the XML file extension.
  81.      *
  82.      * @param filename the filename to check
  83.      *
  84.      * @return {@code true} if the given {@code filename} ends with the XML file extension, otherwise
  85.      *         {@code false}
  86.      */
  87.     public static boolean isXMLConfigurationFile(String filename) {
  88.         return EclipseFormatterUtils.isXMLConfigurationFile(filename);
  89.     }

  90.     /**
  91.      * Returns {@code true} if the given {@code filename} ends with {@code org.eclipse.jdt.core.prefs}.
  92.      *
  93.      * @param filename the filename to check
  94.      *
  95.      * @return {@code true} if the given {@code filename} ends with {@code org.eclipse.jdt.core.prefs},
  96.      *         otherwise {@code false}
  97.      */
  98.     public static boolean isProjectSetting(String filename) {
  99.         return EclipseFormatterUtils.isProjectSetting(filename, PROJECT_PREF_FILE);
  100.     }
  101. }