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  
11  package de.funfried.netbeans.plugins.external.formatter.css.cssparser.ui;
12  
13  import java.awt.event.ActionEvent;
14  import java.awt.event.ActionListener;
15  import java.util.Objects;
16  import java.util.prefs.Preferences;
17  
18  import javax.swing.GroupLayout;
19  import javax.swing.JCheckBox;
20  import javax.swing.JLabel;
21  import javax.swing.JSpinner;
22  import javax.swing.LayoutStyle;
23  import javax.swing.SpinnerNumberModel;
24  import javax.swing.event.ChangeEvent;
25  import javax.swing.event.ChangeListener;
26  
27  import org.netbeans.api.project.Project;
28  import org.openide.awt.Mnemonics;
29  import org.openide.util.NbBundle;
30  
31  import de.funfried.netbeans.plugins.external.formatter.css.cssparser.CssParserFormatterSettings;
32  import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
33  
34  /**
35   *
36   * @author bahlef
37   */
38  public class CssParserFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
39  	/**
40  	 * Creates new form {@link CssParserFormatterOptionsPanel}.
41  	 *
42  	 * @param project the {@link Project} if the panel is used to modify project
43  	 *        specific settings, otherwise {@code null}
44  	 */
45  	public CssParserFormatterOptionsPanel(Project project) {
46  		super(project);
47  
48  		initComponents();
49  	}
50  
51  	/**
52  	 * This method is called from within the constructor to
53  	 * initialize the form.
54  	 * WARNING: Do NOT modify this code. The content of this method is
55  	 * always regenerated by the Form Editor.
56  	 */
57  	@SuppressWarnings("unchecked")
58      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
59      private void initComponents() {
60  
61          rgbAsHexChkBox = new JCheckBox();
62          useSingleQuotesChkBox = new JCheckBox();
63          indentSizeLbl = new JLabel();
64          indentSizeSpn = new JSpinner();
65          useSourceStringValuesChkBox = new JCheckBox();
66  
67          rgbAsHexChkBox.setSelected(true);
68          Mnemonics.setLocalizedText(rgbAsHexChkBox, NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.rgbAsHexChkBox.text")); // NOI18N
69          rgbAsHexChkBox.setToolTipText(NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.rgbAsHexChkBox.toolTipText")); // NOI18N
70          rgbAsHexChkBox.addActionListener(new ActionListener() {
71              public void actionPerformed(ActionEvent evt) {
72                  rgbAsHexChkBoxActionPerformed(evt);
73              }
74          });
75  
76          Mnemonics.setLocalizedText(useSingleQuotesChkBox, NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.useSingleQuotesChkBox.text")); // NOI18N
77          useSingleQuotesChkBox.setToolTipText(NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.useSingleQuotesChkBox.toolTipText")); // NOI18N
78          useSingleQuotesChkBox.addActionListener(new ActionListener() {
79              public void actionPerformed(ActionEvent evt) {
80                  useSingleQuotesChkBoxActionPerformed(evt);
81              }
82          });
83  
84          Mnemonics.setLocalizedText(indentSizeLbl, NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.indentSizeLbl.text")); // NOI18N
85          indentSizeLbl.setToolTipText(NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.indentSizeLbl.toolTipText")); // NOI18N
86  
87          indentSizeSpn.setModel(new SpinnerNumberModel(4, 0, null, 1));
88          indentSizeSpn.addChangeListener(new ChangeListener() {
89              public void stateChanged(ChangeEvent evt) {
90                  indentSizeSpnStateChanged(evt);
91              }
92          });
93  
94          Mnemonics.setLocalizedText(useSourceStringValuesChkBox, NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.useSourceStringValuesChkBox.text")); // NOI18N
95          useSourceStringValuesChkBox.setToolTipText(NbBundle.getMessage(CssParserFormatterOptionsPanel.class, "CssParserFormatterOptionsPanel.useSourceStringValuesChkBox.toolTipText")); // NOI18N
96          useSourceStringValuesChkBox.addActionListener(new ActionListener() {
97              public void actionPerformed(ActionEvent evt) {
98                  useSourceStringValuesChkBoxActionPerformed(evt);
99              }
100         });
101 
102         GroupLayout layout = new GroupLayout(this);
103         this.setLayout(layout);
104         layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
105             .addGroup(layout.createSequentialGroup()
106                 .addContainerGap()
107                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
108                     .addComponent(rgbAsHexChkBox)
109                     .addComponent(useSingleQuotesChkBox))
110                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
111                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
112                     .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
113                         .addComponent(indentSizeLbl)
114                         .addGap(43, 43, 43)
115                         .addComponent(indentSizeSpn, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE))
116                     .addComponent(useSourceStringValuesChkBox, GroupLayout.Alignment.TRAILING))
117                 .addContainerGap())
118         );
119         layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
120             .addGroup(layout.createSequentialGroup()
121                 .addContainerGap()
122                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
123                     .addGroup(layout.createSequentialGroup()
124                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
125                             .addComponent(indentSizeLbl)
126                             .addComponent(indentSizeSpn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
127                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
128                         .addComponent(useSourceStringValuesChkBox))
129                     .addGroup(layout.createSequentialGroup()
130                         .addComponent(rgbAsHexChkBox)
131                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
132                         .addComponent(useSingleQuotesChkBox)))
133                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
134         );
135     }// </editor-fold>//GEN-END:initComponents
136 
137     private void rgbAsHexChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_rgbAsHexChkBoxActionPerformed
138 		fireChangedListener();
139     }//GEN-LAST:event_rgbAsHexChkBoxActionPerformed
140 
141     private void useSingleQuotesChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_useSingleQuotesChkBoxActionPerformed
142 		fireChangedListener();
143     }//GEN-LAST:event_useSingleQuotesChkBoxActionPerformed
144 
145     private void indentSizeSpnStateChanged(ChangeEvent evt) {//GEN-FIRST:event_indentSizeSpnStateChanged
146 		indentSizeSpn.setToolTipText(Objects.toString(indentSizeSpn.getValue(), null));
147 
148 		fireChangedListener();
149     }//GEN-LAST:event_indentSizeSpnStateChanged
150 
151     private void useSourceStringValuesChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_useSourceStringValuesChkBoxActionPerformed
152         // TODO add your handling code here:
153     }//GEN-LAST:event_useSourceStringValuesChkBoxActionPerformed
154 
155     // Variables declaration - do not modify//GEN-BEGIN:variables
156     private JLabel indentSizeLbl;
157     private JSpinner indentSizeSpn;
158     private JCheckBox rgbAsHexChkBox;
159     private JCheckBox useSingleQuotesChkBox;
160     private JCheckBox useSourceStringValuesChkBox;
161     // End of variables declaration//GEN-END:variables
162 
163 		/**
164 		 * {@inheritDoc}
165 		 */
166 		@Override
167 		public void load(Preferences preferences) {
168 			boolean rgbAsHex = preferences.getBoolean(CssParserFormatterSettings.RGB_AS_HEX, CssParserFormatterSettings.RGB_AS_HEX_DEFAULT);
169 			boolean useSingleQuotes = preferences.getBoolean(CssParserFormatterSettings.USE_SINGLE_QUOTES, CssParserFormatterSettings.USE_SINGLE_QUOTES_DEFAULT);
170 			boolean useSourceStringValues = preferences.getBoolean(CssParserFormatterSettings.USE_SOURCE_STRING_VALUES, CssParserFormatterSettings.USE_SOURCE_STRING_VALUES_DEFAULT);
171 			int indentSize = preferences.getInt(CssParserFormatterSettings.INDENT, CssParserFormatterSettings.INDENT_DEFAULT);
172 
173 			rgbAsHexChkBox.setSelected(rgbAsHex);
174 			useSingleQuotesChkBox.setSelected(useSingleQuotes);
175 			useSourceStringValuesChkBox.setSelected(useSourceStringValues);
176 			indentSizeSpn.setValue(indentSize);
177 			indentSizeSpn.setToolTipText(Objects.toString(indentSizeSpn.getValue(), null));
178 		}
179 
180 		/**
181 		 * {@inheritDoc}
182 		 */
183 		@Override
184 		public void store(Preferences preferences) {
185 			preferences.putBoolean(CssParserFormatterSettings.RGB_AS_HEX, rgbAsHexChkBox.isSelected());
186 			preferences.putBoolean(CssParserFormatterSettings.USE_SINGLE_QUOTES, useSingleQuotesChkBox.isSelected());
187 			preferences.putBoolean(CssParserFormatterSettings.USE_SOURCE_STRING_VALUES, useSourceStringValuesChkBox.isSelected());
188 			preferences.putInt(CssParserFormatterSettings.INDENT, (int) indentSizeSpn.getValue());
189 		}
190 
191 		/**
192 		 * {@inheritDoc}
193 		 */
194 		@Override
195 		public boolean valid() {
196 			return true;
197 		}
198 	}