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