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.dbeaver;
11  
12  import java.util.Properties;
13  import java.util.SortedSet;
14  import java.util.prefs.Preferences;
15  
16  import javax.swing.SwingUtilities;
17  import javax.swing.text.BadLocationException;
18  import javax.swing.text.StyledDocument;
19  
20  import org.apache.commons.lang3.tuple.Pair;
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   * DBeaver SQL formatter implementation of the {@link AbstractFormatJob} to
31   * format a given document using the {@link DBeaverFormatterWrapper}.
32   *
33   * @author bahlef
34   */
35  class DBeaverFormatterJob extends AbstractFormatJob {
36  	/** The DBeaver SQL Formatter implementation. */
37  	private final DBeaverFormatterWrapper formatter;
38  
39  	/**
40  	 * Package private constructor to create a new instance of {@link DBeaverFormatterJob}.
41  	 *
42  	 * @param document the {@link StyledDocument} which sould be formatted
43  	 * @param formatter the {@link DBeaverFormatterWrapper} to use
44  	 * @param changedElements the ranges which should be formatted
45  	 */
46  	DBeaverFormatterJob(StyledDocument document, DBeaverFormatterWrapper 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 code = getCode();
60  
61  		try {
62  			String formattedContent = formatter.format(code, getProperties(pref));
63  
64  			if (setFormattedCode(code, formattedContent)) {
65  				SwingUtilities.invokeLater(() -> {
66  					if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
67  						NotificationDisplayer.getDefault().notify("Format using DBeaver SQL formatter", Icons.ICON_DBEAVER, "", null);
68  					}
69  					StatusDisplayer.getDefault().setStatusText("Format using DBeaver SQL formatter");
70  				});
71  			}
72  		} catch (FormattingFailedException ex) {
73  			SwingUtilities.invokeLater(() -> {
74  				StatusDisplayer.getDefault().setStatusText("Failed to format using DBeaver SQL formatter: " + ex.getMessage());
75  			});
76  
77  			throw ex;
78  		}
79  	}
80  
81  	private Properties getProperties(Preferences pref) {
82  		Properties props = new Properties();
83  
84  		props.put(DBeaverFormatterSettings.INDENT_SIZE, Integer.toString(pref.getInt(DBeaverFormatterSettings.INDENT_SIZE, DBeaverFormatterSettings.INDENT_SIZE_DEFAULT)));
85  		props.put(DBeaverFormatterSettings.INDENT_TYPE, pref.get(DBeaverFormatterSettings.INDENT_TYPE, DBeaverFormatterSettings.INDENT_TYPE_DEFAULT));
86  		props.put(DBeaverFormatterSettings.KEYWORD_CASE, pref.get(DBeaverFormatterSettings.KEYWORD_CASE, DBeaverFormatterSettings.KEYWORD_CASE_DEFAULT));
87  		props.put(DBeaverFormatterSettings.STATEMENT_DELIMITER, pref.get(DBeaverFormatterSettings.STATEMENT_DELIMITER, DBeaverFormatterSettings.STATEMENT_DELIMITER_DEFAULT));
88  
89  		return props;
90  	}
91  }