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.google;
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.openide.awt.NotificationDisplayer;
21  import org.openide.awt.StatusDisplayer;
22  
23  import com.google.googlejavaformat.java.JavaFormatterOptions;
24  
25  import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
26  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
27  import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
28  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
29  
30  /**
31   * Google formatter implementation of the {@link AbstractFormatJob} to
32   * format a given document using the {@link GoogleJavaFormatterWrapper}.
33   *
34   * @author bahlef
35   */
36  class GoogleFormatJob extends AbstractFormatJob {
37  	/** The {@link GoogleJavaFormatterWrapper} implementation. */
38  	private final GoogleJavaFormatterWrapper formatter;
39  
40  	/**
41  	 * Package private constructor to create a new instance of {@link GoogleFormatJob}.
42  	 *
43  	 * @param document the {@link StyledDocument} which sould be formatted
44  	 * @param formatter the {@link GoogleJavaFormatterWrapper} to use
45  	 * @param changedElements the ranges which should be formatted
46  	 */
47  	GoogleFormatJob(StyledDocument document, GoogleJavaFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
48  		super(document, changedElements);
49  
50  		this.formatter = formatter;
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	@Override
57  	public void format() throws BadLocationException {
58  		Preferences pref = Settings.getActivePreferences(document);
59  
60  		String codeStylePref = pref.get(GoogleJavaFormatterSettings.CODE_STYLE, JavaFormatterOptions.Style.GOOGLE.name());
61  
62  		String code = getCode();
63  
64  		SortedSet<Pair<Integer, Integer>> regions = getFormatableSections(code);
65  
66  		try {
67  			JavaFormatterOptions.Style codeStyle = JavaFormatterOptions.Style.valueOf(codeStylePref);
68  			String formattedContent = formatter.format(code, codeStyle, regions);
69  
70  			if (setFormattedCode(code, formattedContent)) {
71  				SwingUtilities.invokeLater(() -> {
72  					if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
73  						NotificationDisplayer.getDefault().notify("Format using Google formatter", Icons.ICON_GOOGLE, "", null);
74  					}
75  
76  					StatusDisplayer.getDefault().setStatusText("Format using Google formatter");
77  				});
78  			}
79  		} catch (FormattingFailedException ex) {
80  			SwingUtilities.invokeLater(() -> {
81  				StatusDisplayer.getDefault().setStatusText("Failed to format using Google formatter: " + ex.getMessage());
82  			});
83  
84  			throw ex;
85  		}
86  	}
87  
88  	public void organizeImports() throws BadLocationException {
89  		Preferences pref = Settings.getActivePreferences(document);
90  
91  		String codeStylePref = pref.get(GoogleJavaFormatterSettings.CODE_STYLE, JavaFormatterOptions.Style.GOOGLE.name());
92  
93  		String code = getCode();
94  
95  		try {
96  			JavaFormatterOptions.Style codeStyle = JavaFormatterOptions.Style.valueOf(codeStylePref);
97  			String formattedContent = formatter.organizeImports(code, codeStyle);
98  
99  			if (setFormattedCode(code, formattedContent)) {
100 				SwingUtilities.invokeLater(() -> {
101 					if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
102 						NotificationDisplayer.getDefault().notify("Organizing imports using Google formatter", Icons.ICON_GOOGLE, "", null);
103 					}
104 
105 					StatusDisplayer.getDefault().setStatusText("Organizing imports using Google formatter");
106 				});
107 			}
108 		} catch (FormattingFailedException ex) {
109 			SwingUtilities.invokeLater(() -> {
110 				StatusDisplayer.getDefault().setStatusText("Failed to organize imports using Google formatter: " + ex.getMessage());
111 			});
112 
113 			throw ex;
114 		}
115 	}
116 }