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.json.jackson;
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.json.base.AbstractJsonFormatterService;
27  import de.funfried.netbeans.plugins.external.formatter.json.jackson.ui.JacksonJsonFormatterOptionsPanel;
28  import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
29  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
30  
31  /**
32   * Jackson Json implementation of the {@link AbstractJsonFormatterService}.
33   *
34   * @author bahlef
35   */
36  @NbBundle.Messages({
37  		"FormatterName=Jackson Json Code Formatter"
38  })
39  @ServiceProvider(service = FormatterService.class, position = 500)
40  public class JacksonJsonFormatterService extends AbstractJsonFormatterService {
41  	/** The ID of this formatter service. */
42  	public static final String ID = "jackson-json-formatter";
43  
44  	/** * The {@link JacksonJsonFormatterWrapper} implementation. */
45  	private final JacksonJsonFormatterWrapper formatter = new JacksonJsonFormatterWrapper();
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	@NonNull
51  	@Override
52  	public String getDisplayName() {
53  		return NbBundle.getMessage(JacksonJsonFormatterService.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 JacksonJsonFormatterOptionsPanel(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(JacksonJsonFormatterSettings.INDENT_SIZE, 2);
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(JacksonJsonFormatterSettings.INDENT_SIZE, 2);
108 		}
109 
110 		return ret;
111 	}
112 
113 	/**
114 	 * Not supported by this formatter returns always {@code 0} except the given
115 	 * {@link Document} is {@code null}, then also {@code null will be returned}.
116 	 *
117 	 * @param document the {@link Document} for which the right margin is requested
118 	 *
119 	 * @return {@code 0} except the given {@link Document} is {@code null}, then
120 	 *         also {@code null will be returned}
121 	 */
122 	@CheckForNull
123 	@Override
124 	public Integer getRightMargin(Document document) {
125 		if (document == null) {
126 			return null;
127 		}
128 
129 		return 0;
130 	}
131 
132 	/**
133 	 * {@inheritDoc}
134 	 */
135 	@Override
136 	protected FormatJob getFormatJob(StyledDocument document) {
137 		return new JacksonFormatJob(document, formatter);
138 	}
139 
140 	/**
141 	 * {@inheritDoc}
142 	 */
143 	@CheckForNull
144 	@Override
145 	public Integer getSpacesPerTab(Document document) {
146 		if (document == null) {
147 			return null;
148 		}
149 
150 		Integer ret = null;
151 
152 		Preferences preferences = Settings.getActivePreferences(document);
153 		if (isUseFormatterIndentationSettings(preferences)) {
154 			if (!isExpandTabToSpaces(document, preferences) && preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
155 				ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
156 			} else {
157 				ret = preferences.getInt(JacksonJsonFormatterSettings.SPACES_PER_TAB, 2);
158 			}
159 		}
160 
161 		return ret;
162 	}
163 
164 	/**
165 	 * {@inheritDoc}
166 	 */
167 	@CheckForNull
168 	@Override
169 	public Boolean isExpandTabToSpaces(Document document) {
170 		if (document == null) {
171 			return null;
172 		}
173 
174 		return isExpandTabToSpaces(document, Settings.getActivePreferences(document));
175 	}
176 
177 	private Boolean isExpandTabToSpaces(Document document, Preferences preferences) {
178 		if (document == null || preferences == null) {
179 			return null;
180 		}
181 
182 		Boolean ret = null;
183 
184 		if (isUseFormatterIndentationSettings(preferences)) {
185 			ret = preferences.getBoolean(JacksonJsonFormatterSettings.EXPAND_TABS_TO_SPACES, true);
186 		}
187 
188 		return ret;
189 	}
190 
191 	/**
192 	 * Returns {@code true} if using the formatter indentation settings from the external
193 	 * formatter is activated, otherwise {@code false}.
194 	 *
195 	 * @param prefs the {@link Preferences} where to check
196 	 *
197 	 * @return {@code true} if using the formatter indentation settings from the external
198 	 *         formatter is activated, otherwise {@code false}
199 	 */
200 	private boolean isUseFormatterIndentationSettings(Preferences prefs) {
201 		return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
202 	}
203 }