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.java.spring;
11  
12  import java.util.SortedSet;
13  import java.util.prefs.Preferences;
14  
15  import javax.swing.SwingUtilities;
16  import javax.swing.text.BadLocationException;
17  import javax.swing.text.StyledDocument;
18  
19  import org.apache.commons.lang3.tuple.Pair;
20  import org.netbeans.editor.BaseDocument;
21  import org.openide.awt.NotificationDisplayer;
22  import org.openide.awt.StatusDisplayer;
23  
24  import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
25  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
26  import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
27  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
28  
29  /**
30   * Spring formatter implementation of the {@link AbstractFormatJob} to
31   * format a given document using the {@link SpringJavaFormatterWrapper}.
32   *
33   * @author bahlef
34   */
35  class SpringFormatJob extends AbstractFormatJob {
36  	/** The {@link SpringJavaFormatterWrapper} implementation. */
37  	private final SpringJavaFormatterWrapper formatter;
38  
39  	/**
40  	 * Package private constructor to create a new instance of {@link SpringFormatJob}.
41  	 *
42  	 * @param document the {@link StyledDocument} which sould be formatted
43  	 * @param formatter the {@link SpringJavaFormatterWrapper} to use
44  	 * @param changedElements the ranges which should be formatted
45  	 */
46  	SpringFormatJob(StyledDocument document, SpringJavaFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
47  		super(document, changedElements);
48  
49  		this.formatter = formatter;
50  	}
51  
52  	/**
53  	 * {@inheritDoc}
54  	 */
55  	@Override
56  	public void format() throws BadLocationException {
57  		Preferences pref = Settings.getActivePreferences(document);
58  
59  		String lineFeedSetting = pref.get(SpringJavaFormatterSettings.LINEFEED, "");
60  		String lineFeed = Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
61  
62  		//save with configured linefeed
63  		if (null != lineFeed) {
64  			document.putProperty(BaseDocument.READ_LINE_SEPARATOR_PROP, lineFeed);
65  			document.putProperty(BaseDocument.WRITE_LINE_SEPARATOR_PROP, lineFeed);
66  		}
67  
68  		String code = getCode();
69  
70  		SortedSet<Pair<Integer, Integer>> regions = getFormatableSections(code);
71  
72  		try {
73  			String formattedContent = formatter.format(code, lineFeed, regions);
74  			if (setFormattedCode(code, formattedContent)) {
75  				SwingUtilities.invokeLater(() -> {
76  					if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
77  						NotificationDisplayer.getDefault().notify("Format using Spring formatter", Icons.ICON_SPRING, "", null);
78  					}
79  
80  					StatusDisplayer.getDefault().setStatusText("Format using Spring formatter");
81  				});
82  			}
83  		} catch (FormattingFailedException ex) {
84  			SwingUtilities.invokeLater(() -> {
85  				StatusDisplayer.getDefault().setStatusText("Failed to format using Spring formatter: " + ex.getMessage());
86  			});
87  
88  			throw ex;
89  		}
90  	}
91  }