View Javadoc
1   /*
2    * Copyright (c) 2022 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.sqlformatter.ui;
12  
13  import java.util.prefs.Preferences;
14  
15  import javax.swing.DefaultComboBoxModel;
16  import javax.swing.SpinnerNumberModel;
17  
18  import org.netbeans.api.project.Project;
19  
20  import com.github.vertical_blank.sqlformatter.languages.Dialect;
21  
22  import de.funfried.netbeans.plugins.external.formatter.sql.sqlformatter.SQLFormatterSettings;
23  import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
24  
25  /**
26   *
27   * @author bahlef
28   */
29  public class SQLFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
30  	private static final long serialVersionUID = 1734936313207738588L;
31  
32  	/**
33  	 * Creates new form {@link SQLFormatterOptionsPanel}.
34  	 *
35  	 * @param project the {@link Project} if the panel is used to modify project
36  	 *        specific settings, otherwise {@code null}
37  	 */
38  	public SQLFormatterOptionsPanel(Project project) {
39  		super(project);
40  
41  		initComponents();
42  	}
43  
44  	/**
45  	 * This method is called from within the constructor to
46  	 * initialize the form.
47  	 * WARNING: Do NOT modify this code. The content of this method is
48  	 * always regenerated by the Form Editor.
49  	 */
50  	@SuppressWarnings("unchecked")
51      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
52      private void initComponents() {
53  
54          spellingLbl = new javax.swing.JLabel();
55          uppercaseChkBox = new javax.swing.JCheckBox();
56          outputLbl = new javax.swing.JLabel();
57          linesBetweenQueriesLbl = new javax.swing.JLabel();
58          linesBetweenQueriesSpnr = new javax.swing.JSpinner();
59          maxColumnLengthLbl = new javax.swing.JLabel();
60          maxColumnLengthSpnr = new javax.swing.JSpinner();
61          indentWidthLbl = new javax.swing.JLabel();
62          indentWidthSpnr = new javax.swing.JSpinner();
63          positioningLbl = new javax.swing.JLabel();
64          expandTabsToSpacesChkBox = new javax.swing.JCheckBox();
65          dialectLbl = new javax.swing.JLabel();
66          dbDialectLbl = new javax.swing.JLabel();
67          dbDialectCmbBox = new javax.swing.JComboBox<>();
68  
69          spellingLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
70          org.openide.awt.Mnemonics.setLocalizedText(spellingLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.spellingLbl.text")); // NOI18N
71  
72          org.openide.awt.Mnemonics.setLocalizedText(uppercaseChkBox, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.uppercaseChkBox.text")); // NOI18N
73  
74          outputLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
75          org.openide.awt.Mnemonics.setLocalizedText(outputLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.outputLbl.text")); // NOI18N
76  
77          org.openide.awt.Mnemonics.setLocalizedText(linesBetweenQueriesLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.linesBetweenQueriesLbl.text")); // NOI18N
78  
79          linesBetweenQueriesSpnr.setModel(new SpinnerNumberModel(SQLFormatterSettings.LINES_BETWEEN_QUERIES_DEFAULT, 1, 24, 1));
80  
81          org.openide.awt.Mnemonics.setLocalizedText(maxColumnLengthLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.maxColumnLengthLbl.text")); // NOI18N
82  
83          maxColumnLengthSpnr.setModel(new SpinnerNumberModel(SQLFormatterSettings.MAX_COLUMN_LENGTH_DEFAULT, 20, 400, 1));
84  
85          org.openide.awt.Mnemonics.setLocalizedText(indentWidthLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.indentWidthLbl.text")); // NOI18N
86  
87          indentWidthSpnr.setModel(new SpinnerNumberModel(SQLFormatterSettings.INDENT_SIZE_DEFAULT, 1, 24, 1));
88  
89          positioningLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
90          org.openide.awt.Mnemonics.setLocalizedText(positioningLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.positioningLbl.text")); // NOI18N
91  
92          expandTabsToSpacesChkBox.setSelected(true);
93          org.openide.awt.Mnemonics.setLocalizedText(expandTabsToSpacesChkBox, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.expandTabsToSpacesChkBox.text")); // NOI18N
94  
95          dialectLbl.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
96          org.openide.awt.Mnemonics.setLocalizedText(dialectLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.dialectLbl.text")); // NOI18N
97  
98          org.openide.awt.Mnemonics.setLocalizedText(dbDialectLbl, org.openide.util.NbBundle.getMessage(SQLFormatterOptionsPanel.class, "SQLFormatterOptionsPanel.dbDialectLbl.text")); // NOI18N
99  
100         dbDialectCmbBox.setModel(new DefaultComboBoxModel<Dialect>(Dialect.values()));
101         dbDialectCmbBox.setSelectedItem(Dialect.StandardSql);
102 
103         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
104         this.setLayout(layout);
105         layout.setHorizontalGroup(
106             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
107             .addGroup(layout.createSequentialGroup()
108                 .addContainerGap()
109                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
110                     .addComponent(outputLbl)
111                     .addComponent(spellingLbl)
112                     .addComponent(positioningLbl)
113                     .addComponent(dialectLbl)
114                     .addGroup(layout.createSequentialGroup()
115                         .addGap(6, 6, 6)
116                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
117                             .addGroup(layout.createSequentialGroup()
118                                 .addComponent(dbDialectLbl)
119                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
120                                 .addComponent(dbDialectCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
121                             .addGroup(layout.createSequentialGroup()
122                                 .addComponent(linesBetweenQueriesLbl)
123                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
124                                 .addComponent(linesBetweenQueriesSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
125                                 .addGap(18, 18, 18)
126                                 .addComponent(maxColumnLengthLbl)
127                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
128                                 .addComponent(maxColumnLengthSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
129                             .addComponent(uppercaseChkBox)
130                             .addGroup(layout.createSequentialGroup()
131                                 .addComponent(indentWidthLbl)
132                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
133                                 .addComponent(indentWidthSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
134                                 .addGap(18, 18, 18)
135                                 .addComponent(expandTabsToSpacesChkBox)))))
136                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
137         );
138         layout.setVerticalGroup(
139             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
140             .addGroup(layout.createSequentialGroup()
141                 .addContainerGap()
142                 .addComponent(outputLbl)
143                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
144                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
145                     .addComponent(linesBetweenQueriesLbl)
146                     .addComponent(linesBetweenQueriesSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
147                     .addComponent(maxColumnLengthLbl)
148                     .addComponent(maxColumnLengthSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
149                 .addGap(18, 18, 18)
150                 .addComponent(spellingLbl)
151                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
152                 .addComponent(uppercaseChkBox)
153                 .addGap(18, 18, 18)
154                 .addComponent(positioningLbl)
155                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
156                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
157                     .addComponent(indentWidthLbl)
158                     .addComponent(indentWidthSpnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
159                     .addComponent(expandTabsToSpacesChkBox))
160                 .addGap(18, 18, 18)
161                 .addComponent(dialectLbl)
162                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
163                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
164                     .addComponent(dbDialectLbl)
165                     .addComponent(dbDialectCmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
166                 .addContainerGap(34, Short.MAX_VALUE))
167         );
168     }// </editor-fold>//GEN-END:initComponents
169 
170     // Variables declaration - do not modify//GEN-BEGIN:variables
171     private javax.swing.JComboBox<Dialect> dbDialectCmbBox;
172     private javax.swing.JLabel dbDialectLbl;
173     private javax.swing.JLabel dialectLbl;
174     private javax.swing.JCheckBox expandTabsToSpacesChkBox;
175     private javax.swing.JLabel indentWidthLbl;
176     private javax.swing.JSpinner indentWidthSpnr;
177     private javax.swing.JLabel linesBetweenQueriesLbl;
178     private javax.swing.JSpinner linesBetweenQueriesSpnr;
179     private javax.swing.JLabel maxColumnLengthLbl;
180     private javax.swing.JSpinner maxColumnLengthSpnr;
181     private javax.swing.JLabel outputLbl;
182     private javax.swing.JLabel positioningLbl;
183     private javax.swing.JLabel spellingLbl;
184     private javax.swing.JCheckBox uppercaseChkBox;
185     // End of variables declaration//GEN-END:variables
186 
187 	/**
188 	 * {@inheritDoc}
189 	 */
190 	@Override
191 	public void load(Preferences preferences) {
192 		linesBetweenQueriesSpnr.setValue(preferences.getInt(SQLFormatterSettings.LINES_BETWEEN_QUERIES, SQLFormatterSettings.LINES_BETWEEN_QUERIES_DEFAULT));
193 		maxColumnLengthSpnr.setValue(preferences.getInt(SQLFormatterSettings.MAX_COLUMN_LENGTH, SQLFormatterSettings.MAX_COLUMN_LENGTH_DEFAULT));
194 		uppercaseChkBox.setSelected(preferences.getBoolean(SQLFormatterSettings.UPPERCASE, SQLFormatterSettings.UPPERCASE_DEFAULT));
195 		indentWidthSpnr.setValue(preferences.getInt(SQLFormatterSettings.INDENT_SIZE, SQLFormatterSettings.INDENT_SIZE_DEFAULT));
196 		expandTabsToSpacesChkBox.setSelected(preferences.getBoolean(SQLFormatterSettings.EXPAND_TABS_TO_SPACES, SQLFormatterSettings.EXPAND_TABS_TO_SPACES_DEFAULT));
197 		dbDialectCmbBox.setSelectedItem(Dialect.valueOf(preferences.get(SQLFormatterSettings.DIALECT, SQLFormatterSettings.DIALECT_DEFAULT)));
198 	}
199 
200 	/**
201 	 * {@inheritDoc}
202 	 */
203 	@Override
204 	public void store(Preferences preferences) {
205 		preferences.putInt(SQLFormatterSettings.LINES_BETWEEN_QUERIES, Integer.parseInt(String.valueOf(linesBetweenQueriesSpnr.getValue())));
206 		preferences.putInt(SQLFormatterSettings.MAX_COLUMN_LENGTH, Integer.parseInt(String.valueOf(maxColumnLengthSpnr.getValue())));
207 		preferences.putBoolean(SQLFormatterSettings.UPPERCASE, uppercaseChkBox.isSelected());
208 		preferences.putInt(SQLFormatterSettings.INDENT_SIZE, Integer.parseInt(String.valueOf(indentWidthSpnr.getValue())));
209 		preferences.putBoolean(SQLFormatterSettings.EXPAND_TABS_TO_SPACES, expandTabsToSpacesChkBox.isSelected());
210 		preferences.put(SQLFormatterSettings.DIALECT, String.valueOf(dbDialectCmbBox.getSelectedItem()));
211 	}
212 
213 	/**
214 	 * {@inheritDoc}
215 	 */
216 	@Override
217 	public boolean valid() {
218 		return true;
219 	}
220 }