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.html.jsoup;
12  
13  import java.util.prefs.Preferences;
14  
15  import javax.swing.text.Document;
16  import javax.swing.text.StyledDocument;
17  
18  import org.netbeans.api.annotations.common.CheckForNull;
19  import org.netbeans.api.annotations.common.NonNull;
20  import org.netbeans.api.project.Project;
21  import org.openide.util.NbBundle;
22  import org.openide.util.lookup.ServiceProvider;
23  
24  import de.funfried.netbeans.plugins.external.formatter.FormatJob;
25  import de.funfried.netbeans.plugins.external.formatter.FormatterService;
26  import de.funfried.netbeans.plugins.external.formatter.html.base.AbstractHtmlFormatterService;
27  import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
28  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
29  import de.funfried.netbeans.plugins.external.formatter.xml.base.AbstractXmlFormatterService;
30  import de.funfried.netbeans.plugins.external.formatter.xml.jsoup.ui.JsoupXmlFormatterOptionsPanel;
31  
32  /**
33   * Jsoup XML implementation of the {@link AbstractXmlFormatterService}.
34   *
35   * @author bahlef
36   */
37  @NbBundle.Messages({
38  		"FormatterName=Jsoup HTML Code Formatter"
39  })
40  @ServiceProvider(service = FormatterService.class, position = 500)
41  public class JsoupHtmlFormatterService extends AbstractHtmlFormatterService {
42  	/** The ID of this formatter service. */
43  	public static final String ID = "jsoup-html-formatter";
44  
45  	/** * The {@link JsoupHtmlFormatterWrapper} implementation. */
46  	private final JsoupHtmlFormatterWrapper formatter = new JsoupHtmlFormatterWrapper();
47  
48  	/**
49  	 * {@inheritDoc}
50  	 */
51  	@NonNull
52  	@Override
53  	public String getDisplayName() {
54  		return NbBundle.getMessage(JsoupHtmlFormatterService.class, "FormatterName");
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	@NonNull
61  	@Override
62  	public String getId() {
63  		return ID;
64  	}
65  
66  	/**
67  	 * {@inheritDoc}
68  	 */
69  	@Override
70  	public FormatterOptionsPanel createOptionsPanel(Project project) {
71  		return new JsoupXmlFormatterOptionsPanel(project);
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 */
77  	@CheckForNull
78  	@Override
79  	public Integer getContinuationIndentSize(Document document) {
80  		if (document == null) {
81  			return null;
82  		}
83  
84  		Integer ret = null;
85  
86  		Preferences preferences = Settings.getActivePreferences(document);
87  		if (isUseFormatterIndentationSettings(preferences)) {
88  			ret = preferences.getInt(JsoupHtmlFormatterSettings.INDENT_SIZE, 1);
89  		}
90  
91  		return ret;
92  	}
93  
94  	/**
95  	 * {@inheritDoc}
96  	 */
97  	@CheckForNull
98  	@Override
99  	public Integer getIndentSize(Document document) {
100 		if (document == null) {
101 			return null;
102 		}
103 
104 		Integer ret = null;
105 
106 		Preferences preferences = Settings.getActivePreferences(document);
107 		if (isUseFormatterIndentationSettings(preferences)) {
108 			ret = preferences.getInt(JsoupHtmlFormatterSettings.INDENT_SIZE, 1);
109 		}
110 
111 		return ret;
112 	}
113 
114 	/**
115 	 * {@inheritDoc}
116 	 */
117 	@CheckForNull
118 	@Override
119 	public Integer getRightMargin(Document document) {
120 		if (document == null) {
121 			return null;
122 		}
123 
124 		return 0;
125 	}
126 
127 	/**
128 	 * {@inheritDoc}
129 	 */
130 	@Override
131 	protected FormatJob getFormatJob(StyledDocument document) {
132 		return new JsoupHtmlFormatJob(document, formatter);
133 	}
134 
135 	/**
136 	 * {@inheritDoc}
137 	 */
138 	@CheckForNull
139 	@Override
140 	public Integer getSpacesPerTab(Document document) {
141 		if (document == null) {
142 			return null;
143 		}
144 
145 		Integer ret = null;
146 
147 		Preferences preferences = Settings.getActivePreferences(document);
148 		if (isUseFormatterIndentationSettings(preferences)) {
149 			if (!isExpandTabToSpaces(document, preferences) && preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
150 				ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
151 			} else {
152 				ret = preferences.getInt(JsoupHtmlFormatterSettings.INDENT_SIZE, 1);
153 			}
154 		}
155 
156 		return ret;
157 	}
158 
159 	/**
160 	 * {@inheritDoc}
161 	 */
162 	@CheckForNull
163 	@Override
164 	public Boolean isExpandTabToSpaces(Document document) {
165 		if (document == null) {
166 			return null;
167 		}
168 
169 		return isExpandTabToSpaces(document, Settings.getActivePreferences(document));
170 	}
171 
172 	private Boolean isExpandTabToSpaces(Document document, Preferences preferences) {
173 		if (document == null || preferences == null) {
174 			return null;
175 		}
176 
177 		Boolean ret = null;
178 
179 		if (isUseFormatterIndentationSettings(preferences)) {
180 			ret = true;
181 		}
182 
183 		return ret;
184 	}
185 
186 	/**
187 	 * Returns {@code true} if using the formatter indentation settings from the external
188 	 * formatter is activated, otherwise {@code false}.
189 	 *
190 	 * @param prefs the {@link Preferences} where to check
191 	 *
192 	 * @return {@code true} if using the formatter indentation settings from the external
193 	 *         formatter is activated, otherwise {@code false}
194 	 */
195 	private boolean isUseFormatterIndentationSettings(Preferences prefs) {
196 		return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
197 	}
198 }