View Javadoc
1   /*
2    * Copyright (c) 2021 Andreas Reichel <a href="mailto:andreas@manticore-projects.com">andreas@manticore-projects.com</a>
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.sql.jsqlformatter;
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.manticore.jsqlformatter.JSQLFormatter;
24  import com.manticore.jsqlformatter.JSQLFormatter.FormattingOption;
25  
26  import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
27  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
28  import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
29  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
30  
31  /**
32   * JSQLFormatter implementation of the {@link AbstractFormatJob} to
33   * format a given document using the {@link JSQLFormatterWrapper}.
34   *
35   * @author Andreas Reichel <a href="mailto:andreas@manticore-projects.com">andreas@manticore-projects.com</a>
36   */
37  class JSQLFormatterJob extends AbstractFormatJob {
38  	/** The {@link JSQLFormatter} implementation. */
39  	private final JSQLFormatterWrapper formatter;
40  
41  	/**
42  	 * Package private constructor to create a new instance of {@link JSQLFormatterJob}.
43  	 *
44  	 * @param document the {@link StyledDocument} which sould be formatted
45  	 * @param formatter the {@link JSQLFormatterWrapper} to use
46  	 * @param changedElements the ranges which should be formatted
47  	 */
48  	JSQLFormatterJob(StyledDocument document, JSQLFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
49  		super(document, changedElements);
50  
51  		this.formatter = formatter;
52  	}
53  
54  	/**
55  	 * {@inheritDoc}
56  	 */
57  	@Override
58  	public void format() throws BadLocationException {
59  		Preferences pref = Settings.getActivePreferences(document);
60  
61  		String code = getCode();
62  
63  		try {
64  			//@todo: hand over the formatting options
65  			//@todo: obey the selected region
66  			String formattedContent = formatter.format(code, getOptions(pref));
67  
68  			if (setFormattedCode(code, formattedContent)) {
69  				SwingUtilities.invokeLater(() -> {
70  					if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
71  						NotificationDisplayer.getDefault().notify("Format using JSQLFormatter", Icons.ICON_MANTICORE, "", null);
72  					}
73  					StatusDisplayer.getDefault().setStatusText("Format using JSQLFormatter");
74  				});
75  			}
76  		} catch (FormattingFailedException ex) {
77  			SwingUtilities.invokeLater(() -> {
78  				StatusDisplayer.getDefault().setStatusText("Failed to format using JSQLFormatter formatter: " + ex.getMessage());
79  			});
80  
81  			throw ex;
82  		}
83  	}
84  
85  	private String[] getOptions(Preferences pref) {
86  		int i = 0;
87  		String[] options = new String[FormattingOption.values().length];
88  		options[i++] = toOption(pref, FormattingOption.OUTPUT_FORMAT, JSQLFormatter.getOutputFormat());
89  		options[i++] = toOption(pref, FormattingOption.KEYWORD_SPELLING, JSQLFormatter.getKeywordSpelling());
90  		options[i++] = toOption(pref, FormattingOption.FUNCTION_SPELLING, JSQLFormatter.getFunctionSpelling());
91  		options[i++] = toOption(pref, FormattingOption.OBJECT_SPELLING, JSQLFormatter.getObjectSpelling());
92  		options[i++] = toOption(pref, FormattingOption.INDENT_WIDTH, JSQLFormatter.getIndentWidth());
93  		options[i++] = toOption(pref, FormattingOption.SEPARATION, JSQLFormatter.getSeparation());
94  		options[i++] = toOption(pref, FormattingOption.SQUARE_BRACKET_QUOTATION, JSQLFormatter.getSquaredBracketQuotation());
95  
96  		return options;
97  	}
98  
99  	private <E extends Enum<E>> String toOption(Preferences pref, JSQLFormatter.FormattingOption option, E defaultValue) {
100 		return toOption(pref, option, String.valueOf(defaultValue));
101 	}
102 
103 	private String toOption(Preferences pref, JSQLFormatter.FormattingOption option, String defaultValue) {
104 		return option.toString() + "=" + pref.get(option.toString(), defaultValue);
105 	}
106 
107 	private String toOption(Preferences pref, JSQLFormatter.FormattingOption option, int defaultValue) {
108 		return String.valueOf(option) + "=" + pref.getInt(String.valueOf(option), defaultValue);
109 	}
110 }