View Javadoc
1   /*
2    * Copyright (c) 2021 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  
11  package de.funfried.netbeans.plugins.external.formatter.sql.jsqlformatter.ui;
12  
13  import java.util.prefs.Preferences;
14  
15  import javax.swing.DefaultComboBoxModel;
16  import javax.swing.SpinnerNumberModel;
17  
18  import org.apache.commons.lang3.StringUtils;
19  import org.netbeans.api.project.Project;
20  
21  import com.manticore.jsqlformatter.JSQLFormatter;
22  import com.manticore.jsqlformatter.JSQLFormatter.FormattingOption;
23  import com.manticore.jsqlformatter.JSQLFormatter.OutputFormat;
24  import com.manticore.jsqlformatter.JSQLFormatter.Separation;
25  import com.manticore.jsqlformatter.JSQLFormatter.Spelling;
26  import com.manticore.jsqlformatter.JSQLFormatter.SquaredBracketQuotation;
27  
28  import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
29  
30  /**
31   *
32   * @author bahlef
33   */
34  public class JSQLFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
35  	private static final long serialVersionUID = -160868052080748888L;
36  
37  	/**
38  	 * Creates new form {@link JSQLFormatterOptionsPanel}.
39  	 *
40  	 * @param project the {@link Project} if the panel is used to modify project
41  	 *        specific settings, otherwise {@code null}
42  	 */
43  	public JSQLFormatterOptionsPanel(Project project) {
44  		super(project);
45  
46  		initComponents();
47  	}
48  
49  	/**
50  	 * This method is called from within the constructor to
51  	 * initialize the form.
52  	 * WARNING: Do NOT modify this code. The content of this method is
53  	 * always regenerated by the Form Editor.
54  	 */
55  	@SuppressWarnings("unchecked")
56      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
57      private void initComponents() {
58  
59          separationBtnGrp = new javax.swing.ButtonGroup();
60          sbqBtnGrp = new javax.swing.ButtonGroup();
61          outputLbl = new javax.swing.JLabel();
62          formatLbl = new javax.swing.JLabel();
63          formatCmbBox = new javax.swing.JComboBox<>();
64          spellingLbl = new javax.swing.JLabel();
65          keywordsLbl = new javax.swing.JLabel();
66          keywordsCmbBox = new javax.swing.JComboBox<>();
67          functionsLbl = new javax.swing.JLabel();
68          functionsCmbBox = new javax.swing.JComboBox<>();
69          objectsLbl = new javax.swing.JLabel();
70          objectsCmbBox = new javax.swing.JComboBox<>();
71          positioningLbl = new javax.swing.JLabel();
72          indentWidthLbl = new javax.swing.JLabel();
73          indentWidthSpnr = new javax.swing.JSpinner();
74          separationLbl = new javax.swing.JLabel();
75          separationBeforeRdBtn = new javax.swing.JRadioButton();
76          separationAfterRdBtn = new javax.swing.JRadioButton();
77          dialectLbl = new javax.swing.JLabel();
78          sbqLbl = new javax.swing.JLabel();
79          sbqAutoRdBtn = new javax.swing.JRadioButton();
80          sbqYesRdBtn = new javax.swing.JRadioButton();
81          sbqNoRdBtn = new javax.swing.JRadioButton();
82  
83          outputLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
84          org.openide.awt.Mnemonics.setLocalizedText(outputLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.outputLbl.text")); // NOI18N
85  
86          formatLbl.setLabelFor(formatCmbBox);
87          org.openide.awt.Mnemonics.setLocalizedText(formatLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.formatLbl.text")); // NOI18N
88  
89          formatCmbBox.setModel(new DefaultComboBoxModel<OutputFormat>(OutputFormat.values()));
90          formatCmbBox.setSelectedItem(JSQLFormatter.getOutputFormat());
91  
92          spellingLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
93          org.openide.awt.Mnemonics.setLocalizedText(spellingLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.spellingLbl.text")); // NOI18N
94  
95          keywordsLbl.setLabelFor(keywordsCmbBox);
96          org.openide.awt.Mnemonics.setLocalizedText(keywordsLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.keywordsLbl.text")); // NOI18N
97  
98          keywordsCmbBox.setModel(new DefaultComboBoxModel<Spelling>(Spelling.values()));
99          keywordsCmbBox.setSelectedItem(JSQLFormatter.getKeywordSpelling());
100 
101         functionsLbl.setLabelFor(functionsCmbBox);
102         org.openide.awt.Mnemonics.setLocalizedText(functionsLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.functionsLbl.text")); // NOI18N
103 
104         functionsCmbBox.setModel(new DefaultComboBoxModel<Spelling>(Spelling.values()));
105         functionsCmbBox.setSelectedItem(JSQLFormatter.getFunctionSpelling());
106 
107         objectsLbl.setLabelFor(objectsCmbBox);
108         org.openide.awt.Mnemonics.setLocalizedText(objectsLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.objectsLbl.text")); // NOI18N
109 
110         objectsCmbBox.setModel(new DefaultComboBoxModel<Spelling>(Spelling.values()));
111         objectsCmbBox.setSelectedItem(JSQLFormatter.getObjectSpelling());
112 
113         positioningLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
114         org.openide.awt.Mnemonics.setLocalizedText(positioningLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.positioningLbl.text")); // NOI18N
115 
116         indentWidthLbl.setLabelFor(indentWidthSpnr);
117         org.openide.awt.Mnemonics.setLocalizedText(indentWidthLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.indentWidthLbl.text")); // NOI18N
118 
119         indentWidthSpnr.setModel(new SpinnerNumberModel(JSQLFormatter.getIndentWidth(), 1, 24, 1));
120 
121         separationLbl.setLabelFor(separationBeforeRdBtn);
122         org.openide.awt.Mnemonics.setLocalizedText(separationLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.separationLbl.text")); // NOI18N
123 
124         separationBtnGrp.add(separationBeforeRdBtn);
125         separationBeforeRdBtn.setSelected(true);
126         org.openide.awt.Mnemonics.setLocalizedText(separationBeforeRdBtn, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.separationBeforeRdBtn.text")); // NOI18N
127 
128         separationBtnGrp.add(separationAfterRdBtn);
129         org.openide.awt.Mnemonics.setLocalizedText(separationAfterRdBtn, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.separationAfterRdBtn.text")); // NOI18N
130 
131         dialectLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
132         org.openide.awt.Mnemonics.setLocalizedText(dialectLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.dialectLbl.text")); // NOI18N
133 
134         sbqLbl.setLabelFor(sbqAutoRdBtn);
135         org.openide.awt.Mnemonics.setLocalizedText(sbqLbl, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.sbqLbl.text")); // NOI18N
136 
137         sbqBtnGrp.add(sbqAutoRdBtn);
138         sbqAutoRdBtn.setSelected(true);
139         org.openide.awt.Mnemonics.setLocalizedText(sbqAutoRdBtn, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.sbqAutoRdBtn.text")); // NOI18N
140 
141         sbqBtnGrp.add(sbqYesRdBtn);
142         org.openide.awt.Mnemonics.setLocalizedText(sbqYesRdBtn, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.sbqYesRdBtn.text")); // NOI18N
143 
144         sbqBtnGrp.add(sbqNoRdBtn);
145         org.openide.awt.Mnemonics.setLocalizedText(sbqNoRdBtn, org.openide.util.NbBundle.getMessage(JSQLFormatterOptionsPanel.class, "JSQLFormatterOptionsPanel.sbqNoRdBtn.text")); // NOI18N
146 
147         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
148         this.setLayout(layout);
149         layout.setHorizontalGroup(
150             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
151             .addGroup(layout.createSequentialGroup()
152                 .addContainerGap()
153                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
154                     .addComponent(outputLbl)
155                     .addComponent(spellingLbl)
156                     .addComponent(positioningLbl)
157                     .addComponent(dialectLbl)
158                     .addGroup(layout.createSequentialGroup()
159                         .addGap(6, 6, 6)
160                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
161                             .addGroup(layout.createSequentialGroup()
162                                 .addComponent(sbqLbl)
163                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
164                                 .addComponent(sbqAutoRdBtn)
165                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
166                                 .addComponent(sbqYesRdBtn)
167                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
168                                 .addComponent(sbqNoRdBtn))
169                             .addGroup(layout.createSequentialGroup()
170                                 .addComponent(indentWidthLbl)
171                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
172                                 .addComponent(indentWidthSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
173                                 .addGap(18, 18, 18)
174                                 .addComponent(separationLbl)
175                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
176                                 .addComponent(separationBeforeRdBtn)
177                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
178                                 .addComponent(separationAfterRdBtn))
179                             .addGroup(layout.createSequentialGroup()
180                                 .addComponent(keywordsLbl)
181                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
182                                 .addComponent(keywordsCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
183                                 .addGap(18, 18, 18)
184                                 .addComponent(functionsLbl)
185                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
186                                 .addComponent(functionsCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
187                                 .addGap(18, 18, 18)
188                                 .addComponent(objectsLbl)
189                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
190                                 .addComponent(objectsCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
191                             .addGroup(layout.createSequentialGroup()
192                                 .addComponent(formatLbl)
193                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
194                                 .addComponent(formatCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
195                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
196         );
197         layout.setVerticalGroup(
198             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
199             .addGroup(layout.createSequentialGroup()
200                 .addContainerGap()
201                 .addComponent(outputLbl)
202                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
203                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
204                     .addComponent(formatLbl)
205                     .addComponent(formatCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
206                 .addGap(18, 18, 18)
207                 .addComponent(spellingLbl)
208                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
209                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
210                     .addComponent(keywordsLbl)
211                     .addComponent(keywordsCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
212                     .addComponent(functionsLbl)
213                     .addComponent(functionsCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
214                     .addComponent(objectsLbl)
215                     .addComponent(objectsCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
216                 .addGap(18, 18, 18)
217                 .addComponent(positioningLbl)
218                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
219                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
220                     .addComponent(indentWidthLbl)
221                     .addComponent(indentWidthSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
222                     .addComponent(separationLbl)
223                     .addComponent(separationBeforeRdBtn)
224                     .addComponent(separationAfterRdBtn))
225                 .addGap(18, 18, 18)
226                 .addComponent(dialectLbl)
227                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
228                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
229                     .addComponent(sbqLbl)
230                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
231                         .addComponent(sbqAutoRdBtn)
232                         .addComponent(sbqYesRdBtn)
233                         .addComponent(sbqNoRdBtn)))
234                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
235         );
236     }// </editor-fold>//GEN-END:initComponents
237 
238     // Variables declaration - do not modify//GEN-BEGIN:variables
239     private javax.swing.JLabel dialectLbl;
240     private javax.swing.JComboBox<OutputFormat> formatCmbBox;
241     private javax.swing.JLabel formatLbl;
242     private javax.swing.JComboBox<Spelling> functionsCmbBox;
243     private javax.swing.JLabel functionsLbl;
244     private javax.swing.JLabel indentWidthLbl;
245     private javax.swing.JSpinner indentWidthSpnr;
246     private javax.swing.JComboBox<Spelling> keywordsCmbBox;
247     private javax.swing.JLabel keywordsLbl;
248     private javax.swing.JComboBox<Spelling> objectsCmbBox;
249     private javax.swing.JLabel objectsLbl;
250     private javax.swing.JLabel outputLbl;
251     private javax.swing.JLabel positioningLbl;
252     private javax.swing.JRadioButton sbqAutoRdBtn;
253     private javax.swing.ButtonGroup sbqBtnGrp;
254     private javax.swing.JLabel sbqLbl;
255     private javax.swing.JRadioButton sbqNoRdBtn;
256     private javax.swing.JRadioButton sbqYesRdBtn;
257     private javax.swing.JRadioButton separationAfterRdBtn;
258     private javax.swing.JRadioButton separationBeforeRdBtn;
259     private javax.swing.ButtonGroup separationBtnGrp;
260     private javax.swing.JLabel separationLbl;
261     private javax.swing.JLabel spellingLbl;
262     // End of variables declaration//GEN-END:variables
263 
264 	/**
265 	 * {@inheritDoc}
266 	 */
267 	@Override
268 	public void load(Preferences preferences) {
269 		formatCmbBox.setSelectedItem(getEnumValue(preferences, JSQLFormatter.FormattingOption.OUTPUT_FORMAT, JSQLFormatter.getOutputFormat(), OutputFormat.class));
270 		keywordsCmbBox.setSelectedItem(getEnumValue(preferences, JSQLFormatter.FormattingOption.KEYWORD_SPELLING, JSQLFormatter.getKeywordSpelling(), Spelling.class));
271 		functionsCmbBox.setSelectedItem(getEnumValue(preferences, JSQLFormatter.FormattingOption.FUNCTION_SPELLING, JSQLFormatter.getFunctionSpelling(), Spelling.class));
272 		objectsCmbBox.setSelectedItem(getEnumValue(preferences, JSQLFormatter.FormattingOption.OBJECT_SPELLING, JSQLFormatter.getObjectSpelling(), Spelling.class));
273 
274 		Separation separation = getEnumValue(preferences, JSQLFormatter.FormattingOption.SEPARATION, JSQLFormatter.getSeparation(), Separation.class);
275 		switch (separation) {
276 			case AFTER:
277 				separationAfterRdBtn.setSelected(true);
278 				break;
279 			default:
280 				separationBeforeRdBtn.setSelected(true);
281 				break;
282 		}
283 
284 		SquaredBracketQuotation squaredBracketQuotation = getEnumValue(preferences, FormattingOption.SQUARE_BRACKET_QUOTATION, JSQLFormatter.getSquaredBracketQuotation(), SquaredBracketQuotation.class);
285 		switch (squaredBracketQuotation) {
286 			case YES:
287 				sbqYesRdBtn.setSelected(true);
288 				break;
289 			case NO:
290 				sbqNoRdBtn.setSelected(true);
291 				break;
292 			default:
293 				sbqAutoRdBtn.setSelected(true);
294 				break;
295 		}
296 
297 		indentWidthSpnr.setValue(preferences.getInt(FormattingOption.INDENT_WIDTH.toString(), JSQLFormatter.getIndentWidth()));
298 	}
299 
300 	/**
301 	 * {@inheritDoc}
302 	 */
303 	@Override
304 	public void store(Preferences preferences) {
305 		preferences.put(FormattingOption.OUTPUT_FORMAT.toString(), String.valueOf(formatCmbBox.getSelectedItem()));
306 		preferences.put(FormattingOption.KEYWORD_SPELLING.toString(), String.valueOf(keywordsCmbBox.getSelectedItem()));
307 		preferences.put(FormattingOption.FUNCTION_SPELLING.toString(), String.valueOf(functionsCmbBox.getSelectedItem()));
308 		preferences.put(FormattingOption.OBJECT_SPELLING.toString(), String.valueOf(objectsCmbBox.getSelectedItem()));
309 
310 		preferences.putInt(FormattingOption.INDENT_WIDTH.toString(), Integer.parseInt(String.valueOf(indentWidthSpnr.getValue())));
311 
312 		Separation separation = Separation.BEFORE;
313 		if (separationAfterRdBtn.isSelected()) {
314 			separation = Separation.AFTER;
315 		}
316 
317 		preferences.put(FormattingOption.SEPARATION.toString(), separation.toString());
318 
319 		SquaredBracketQuotation squaredBracketQuotation = SquaredBracketQuotation.AUTO;
320 		if (sbqYesRdBtn.isSelected()) {
321 			squaredBracketQuotation = SquaredBracketQuotation.YES;
322 		} else if (sbqNoRdBtn.isSelected()) {
323 			squaredBracketQuotation = SquaredBracketQuotation.NO;
324 		}
325 
326 		preferences.put(FormattingOption.SQUARE_BRACKET_QUOTATION.toString(), squaredBracketQuotation.toString());
327 	}
328 
329 	/**
330 	 * {@inheritDoc}
331 	 */
332 	@Override
333 	public boolean valid() {
334 		return true;
335 	}
336 
337 	private <E extends Enum<E>> E getEnumValue(Preferences preferences, FormattingOption formattingOption, E defaultValue, Class<E> enumType) {
338 		if (preferences != null && formattingOption != null && enumType != null) {
339 			String value = preferences.get(formattingOption.toString(), null);
340 			if (StringUtils.isNotBlank(value)) {
341 				value = StringUtils.upperCase(value);
342 
343 				return Enum.valueOf(enumType, value);
344 			}
345 		}
346 
347 		return defaultValue;
348 	}
349 }