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.revelc;
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.revelc.ui.RevelcXmlFormatterOptionsPanel;
30  
31  /**
32   * revelc.net XML implementation of the {@link AbstractXmlFormatterService}.
33   *
34   * @author bahlef
35   */
36  @NbBundle.Messages({
37  		"FormatterName=revelc.net XML Code Formatter"
38  })
39  @ServiceProvider(service = FormatterService.class, position = 500)
40  public class RevelcXmlFormatterService extends AbstractXmlFormatterService {
41  	/** The ID of this formatter service. */
42  	public static final String ID = "revelc-xml-formatter";
43  
44  	/** * The {@link RevelcXmlFormatterWrapper} implementation. */
45  	private final RevelcXmlFormatterWrapper formatter = new RevelcXmlFormatterWrapper();
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	@NonNull
51  	@Override
52  	public String getDisplayName() {
53  		return NbBundle.getMessage(RevelcXmlFormatterService.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 RevelcXmlFormatterOptionsPanel(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  			if (preferences.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true)) {
88  				ret = 1;
89  			} else {
90  				ret = preferences.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
91  			}
92  
93  			Integer indentSize = getIndentSize(document);
94  			if (indentSize != null) {
95  				ret *= indentSize;
96  			}
97  		}
98  
99  		return ret;
100 	}
101 
102 	/**
103 	 * {@inheritDoc}
104 	 */
105 	@CheckForNull
106 	@Override
107 	public Integer getIndentSize(Document document) {
108 		if (document == null) {
109 			return null;
110 		}
111 
112 		Integer ret = null;
113 
114 		Preferences preferences = Settings.getActivePreferences(document);
115 		if (isUseFormatterIndentationSettings(preferences)) {
116 			if (preferences.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true)) {
117 				ret = 1;
118 			} else {
119 				ret = preferences.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
120 			}
121 		}
122 
123 		return ret;
124 	}
125 
126 	/**
127 	 * {@inheritDoc}
128 	 */
129 	@CheckForNull
130 	@Override
131 	public Integer getRightMargin(Document document) {
132 		if (document == null) {
133 			return null;
134 		}
135 
136 		Preferences preferences = Settings.getActivePreferences(document);
137 		return preferences.getInt(RevelcXmlFormatterSettings.MAX_LINE_LENGTH, 120);
138 	}
139 
140 	/**
141 	 * {@inheritDoc}
142 	 */
143 	@Override
144 	protected FormatJob getFormatJob(StyledDocument document) {
145 		return new RevelcFormatJob(document, formatter);
146 	}
147 
148 	/**
149 	 * {@inheritDoc}
150 	 */
151 	@CheckForNull
152 	@Override
153 	public Integer getSpacesPerTab(Document document) {
154 		if (document == null) {
155 			return null;
156 		}
157 
158 		Integer ret = null;
159 
160 		Preferences preferences = Settings.getActivePreferences(document);
161 		if (isUseFormatterIndentationSettings(preferences)) {
162 			if (!isExpandTabToSpaces(document, preferences) && preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
163 				ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
164 			} else {
165 				ret = preferences.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
166 			}
167 		}
168 
169 		return ret;
170 	}
171 
172 	/**
173 	 * {@inheritDoc}
174 	 */
175 	@CheckForNull
176 	@Override
177 	public Boolean isExpandTabToSpaces(Document document) {
178 		if (document == null) {
179 			return null;
180 		}
181 
182 		return isExpandTabToSpaces(document, Settings.getActivePreferences(document));
183 	}
184 
185 	private Boolean isExpandTabToSpaces(Document document, Preferences preferences) {
186 		if (document == null || preferences == null) {
187 			return null;
188 		}
189 
190 		Boolean ret = null;
191 
192 		if (isUseFormatterIndentationSettings(preferences)) {
193 			ret = !preferences.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true);
194 		}
195 
196 		return ret;
197 	}
198 
199 	/**
200 	 * Returns {@code true} if using the formatter indentation settings from the external
201 	 * formatter is activated, otherwise {@code false}.
202 	 *
203 	 * @param prefs the {@link Preferences} where to check
204 	 *
205 	 * @return {@code true} if using the formatter indentation settings from the external
206 	 *         formatter is activated, otherwise {@code false}
207 	 */
208 	private boolean isUseFormatterIndentationSettings(Preferences prefs) {
209 		return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
210 	}
211 }