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    * bahlef - initial API and implementation and/or initial documentation
9    */
10  package de.funfried.netbeans.plugins.external.formatter.xml.revelc;
11  
12  import java.util.prefs.Preferences;
13  
14  import javax.swing.SwingUtilities;
15  import javax.swing.text.BadLocationException;
16  import javax.swing.text.StyledDocument;
17  
18  import org.netbeans.editor.BaseDocument;
19  import org.openide.awt.NotificationDisplayer;
20  import org.openide.awt.StatusDisplayer;
21  
22  import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
23  import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
24  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
25  import net.revelc.code.formatter.xml.lib.FormattingPreferences;
26  
27  /**
28   * revelc.net formatter implementation of the {@link AbstractFormatJob} to
29   * format a given document using the {@link RevelcXmlFormatterWrapper}.
30   *
31   * @author bahlef
32   */
33  class RevelcFormatJob extends AbstractFormatJob {
34  	/** The {@link RevelcXmlFormatterWrapper} implementation. */
35  	private final RevelcXmlFormatterWrapper formatter;
36  
37  	/**
38  	 * Package private constructor to create a new instance of {@link RevelcFormatJob}.
39  	 *
40  	 * @param document the {@link StyledDocument} which sould be formatted
41  	 * @param formatter the {@link RevelcXmlFormatterWrapper} to use
42  	 */
43  	RevelcFormatJob(StyledDocument document, RevelcXmlFormatterWrapper formatter) {
44  		super(document, null);
45  
46  		this.formatter = formatter;
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	@Override
53  	public void format() throws BadLocationException {
54  		Preferences pref = Settings.getActivePreferences(document);
55  
56  		boolean tabInsteadOfSpaces = pref.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true);
57  		boolean splitMultiAttrs = pref.getBoolean(RevelcXmlFormatterSettings.SPLIT_MULTI_ATTRIBUTES, false);
58  		boolean wrapLongLines = pref.getBoolean(RevelcXmlFormatterSettings.WRAP_LONG_LINES, true);
59  		int tabWidth = pref.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
60  		int maxLineLength = pref.getInt(RevelcXmlFormatterSettings.MAX_LINE_LENGTH, 120);
61  		String wellFormedValidation = pref.get(RevelcXmlFormatterSettings.WELL_FORMED_VALIDATION, FormattingPreferences.WARN);
62  		String lineFeedSetting = pref.get(RevelcXmlFormatterSettings.LINEFEED, "");
63  
64  		FormattingPreferences prefs = new FormattingPreferences();
65  		prefs.setMaxLineLength(maxLineLength);
66  		prefs.setSplitMultiAttrs(splitMultiAttrs);
67  		prefs.setTabInsteadOfSpaces(tabInsteadOfSpaces);
68  		prefs.setTabWidth(tabWidth);
69  		prefs.setWellFormedValidation(wellFormedValidation);
70  		prefs.setWrapLongLines(wrapLongLines);
71  
72  		//save with configured linefeed
73  		String lineFeed = Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
74  		if (null != lineFeed) {
75  			document.putProperty(BaseDocument.READ_LINE_SEPARATOR_PROP, lineFeed);
76  			document.putProperty(BaseDocument.WRITE_LINE_SEPARATOR_PROP, lineFeed);
77  		}
78  
79  		String code = getCode();
80  
81  		String formattedContent = formatter.format(code, lineFeed, prefs);
82  		if (setFormattedCode(code, formattedContent)) {
83  			SwingUtilities.invokeLater(() -> {
84  				if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
85  					NotificationDisplayer.getDefault().notify("Format using revelc XML formatter", Icons.ICON_REVELC, "", null);
86  				}
87  
88  				StatusDisplayer.getDefault().setStatusText("Format using revelc XML formatter");
89  			});
90  		}
91  	}
92  }