View Javadoc
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    * markiewb - initial API and implementation and/or initial documentation
9    * bahlef
10   */
11  package de.funfried.netbeans.plugins.external.formatter.java.eclipse;
12  
13  import java.util.SortedSet;
14  import java.util.prefs.Preferences;
15  
16  import javax.swing.text.StyledDocument;
17  
18  import org.apache.commons.lang3.tuple.Pair;
19  import org.netbeans.api.annotations.common.NonNull;
20  
21  import de.funfried.netbeans.plugins.external.formatter.eclipse.AbstractEclipseFormatJob;
22  import de.funfried.netbeans.plugins.external.formatter.exceptions.CannotLoadConfigurationException;
23  import de.funfried.netbeans.plugins.external.formatter.exceptions.ConfigReadException;
24  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
25  import de.funfried.netbeans.plugins.external.formatter.exceptions.ProfileNotFoundException;
26  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
27  
28  /**
29   * Eclipse formatter implementation of the {@link AbstractEclipseFormatJob} to
30   * format a given document using the {@link EclipseJavaFormatterWrapper}.
31   *
32   * @author markiewb
33   * @author bahlef
34   */
35  class EclipseFormatJob extends AbstractEclipseFormatJob {
36  	/** * The {@link EclipseJavaFormatterWrapper} implementation. */
37  	private final EclipseJavaFormatterWrapper formatter;
38  
39  	/**
40  	 * Package private constructor to create a new instance of {@link EclipseFormatJob}.
41  	 *
42  	 * @param document the {@link StyledDocument} which sould be formatted
43  	 * @param formatter the {@link EclipseJavaFormatterWrapper} to use
44  	 * @param changedElements the ranges which should be formatted
45  	 */
46  	EclipseFormatJob(StyledDocument document, EclipseJavaFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
47  		super(document, changedElements);
48  
49  		this.formatter = formatter;
50  	}
51  
52  	/**
53  	 * {@inheritDoc}
54  	 */
55  	@Override
56  	protected String getFormattedContent(Preferences pref, String formatterFile, String formatterProfile, String code)
57  			throws ConfigReadException, ProfileNotFoundException, CannotLoadConfigurationException, FormattingFailedException {
58  		String sourceLevel = pref.get(EclipseJavaFormatterSettings.SOURCELEVEL, "");
59  
60  		SortedSet<Pair<Integer, Integer>> regions = getFormatableSections(code);
61  
62  		return formatter.format(formatterFile, formatterProfile, code, getLineFeed(pref), sourceLevel, regions);
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	@Override
69  	protected String getFormatterFile(Preferences pref) {
70  		return EclipseJavaFormatterSettings.getEclipseFormatterFile(pref, document);
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	@Override
77  	protected String getFormatterProfile(Preferences pref) {
78  		return pref.get(EclipseJavaFormatterSettings.ACTIVE_PROFILE, "");
79  	}
80  
81  	/**
82  	 * {@inheritDoc}
83  	 */
84  	@Override
85  	protected String getLineFeed(Preferences pref) {
86  		String lineFeedSetting = pref.get(EclipseJavaFormatterSettings.LINEFEED, "");
87  		return Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
88  	}
89  
90  	/**
91  	 * {@inheritDoc}
92  	 */
93  	@NonNull
94  	@Override
95  	protected String getNotificationMessageForEclipseFormatterConfigurationFileType(String formatterFile, String formatterProfile) {
96  		String msg = "";
97  		if (EclipseJavaFormatterSettings.isWorkspaceMechanicFile(formatterFile)) {
98  			//Workspace mechanic file
99  			msg = String.format("Using %s", formatterFile);
100 		} else if (EclipseJavaFormatterSettings.isXMLConfigurationFile(formatterFile)) {
101 			//XML file
102 			msg = String.format("Using profile '%s' from %s", formatterProfile, formatterFile);
103 		} else if (EclipseJavaFormatterSettings.isProjectSetting(formatterFile)) {
104 			//org.eclipse.jdt.core.prefs
105 			msg = String.format("Using %s", formatterFile);
106 		}
107 
108 		return msg;
109 	}
110 }