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