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.xml.revelc.ui;
12  
13  import java.awt.event.ActionEvent;
14  import java.awt.event.ActionListener;
15  import java.awt.event.ItemEvent;
16  import java.awt.event.ItemListener;
17  import java.util.Objects;
18  import java.util.prefs.Preferences;
19  
20  import javax.swing.DefaultComboBoxModel;
21  import javax.swing.GroupLayout;
22  import javax.swing.JCheckBox;
23  import javax.swing.JComboBox;
24  import javax.swing.JLabel;
25  import javax.swing.JSpinner;
26  import javax.swing.LayoutStyle;
27  import javax.swing.SpinnerNumberModel;
28  import javax.swing.event.ChangeEvent;
29  import javax.swing.event.ChangeListener;
30  
31  import org.apache.commons.lang3.StringUtils;
32  import org.netbeans.api.project.Project;
33  import org.openide.awt.Mnemonics;
34  import org.openide.util.NbBundle;
35  
36  import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
37  import de.funfried.netbeans.plugins.external.formatter.xml.revelc.RevelcXmlFormatterSettings;
38  import net.revelc.code.formatter.xml.lib.FormattingPreferences;
39  
40  /**
41   *
42   * @author bahlef
43   */
44  public class RevelcXmlFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
45  	/**
46  	 * Creates new form {@link RevelcXmlFormatterOptionsPanel}.
47  	 *
48  	 * @param project the {@link Project} if the panel is used to modify project
49  	 *                specific settings, otherwise {@code null}
50  	 */
51  	public RevelcXmlFormatterOptionsPanel(Project project) {
52  		super(project);
53  
54  		initComponents();
55  	}
56  
57  	/**
58  	 * This method is called from within the constructor to
59  	 * initialize the form.
60  	 * WARNING: Do NOT modify this code. The content of this method is
61  	 * always regenerated by the Form Editor.
62  	 */
63  	@SuppressWarnings("unchecked")
64      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
65      private void initComponents() {
66  
67          linefeedLbl = new JLabel();
68          linefeedCmbBox = new JComboBox<>();
69          rightMarginLbl = new JLabel();
70          rightMarginSpn = new JSpinner();
71          wrapLongLinesChkBox = new JCheckBox();
72          wellFormedValidationLbl = new JLabel();
73          wellFormedValidationCmbBox = new JComboBox<>();
74          expandTabsToSpacesChkBox = new JCheckBox();
75          splitMultiAttrsChkBox = new JCheckBox();
76          tabWidthLbl = new JLabel();
77          tabWidthSpn = new JSpinner();
78  
79          Mnemonics.setLocalizedText(linefeedLbl, NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.linefeedLbl.text")); // NOI18N
80          linefeedLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.linefeedLbl.toolTipText")); // NOI18N
81  
82          linefeedCmbBox.setModel(new DefaultComboBoxModel<>(new String[] { "System", "\\n", "\\r\\n", "\\r" }));
83          linefeedCmbBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.linefeedCmbBox.toolTipText")); // NOI18N
84          linefeedCmbBox.addItemListener(new ItemListener() {
85              public void itemStateChanged(ItemEvent evt) {
86                  linefeedCmbBoxItemStateChanged(evt);
87              }
88          });
89  
90          Mnemonics.setLocalizedText(rightMarginLbl, NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.rightMarginLbl.text")); // NOI18N
91          rightMarginLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.rightMarginLbl.toolTipText")); // NOI18N
92  
93          rightMarginSpn.setModel(new SpinnerNumberModel(120, 10, null, 10));
94          rightMarginSpn.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.rightMarginSpn.toolTipText")); // NOI18N
95          rightMarginSpn.addChangeListener(new ChangeListener() {
96              public void stateChanged(ChangeEvent evt) {
97                  rightMarginSpnStateChanged(evt);
98              }
99          });
100 
101         wrapLongLinesChkBox.setSelected(true);
102         Mnemonics.setLocalizedText(wrapLongLinesChkBox, NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.wrapLongLinesChkBox.text")); // NOI18N
103         wrapLongLinesChkBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.wrapLongLinesChkBox.toolTipText")); // NOI18N
104         wrapLongLinesChkBox.addActionListener(new ActionListener() {
105             public void actionPerformed(ActionEvent evt) {
106                 wrapLongLinesChkBoxActionPerformed(evt);
107             }
108         });
109 
110         Mnemonics.setLocalizedText(wellFormedValidationLbl, NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.wellFormedValidationLbl.text")); // NOI18N
111         wellFormedValidationLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.wellFormedValidationLbl.toolTipText")); // NOI18N
112 
113         wellFormedValidationCmbBox.setModel(new DefaultComboBoxModel<>(new String[] { "IGNORE", "WARN", "FAIL" }));
114         wellFormedValidationCmbBox.setSelectedIndex(1);
115         wellFormedValidationCmbBox.addItemListener(new ItemListener() {
116             public void itemStateChanged(ItemEvent evt) {
117                 wellFormedValidationCmbBoxItemStateChanged(evt);
118             }
119         });
120 
121         Mnemonics.setLocalizedText(expandTabsToSpacesChkBox, NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.expandTabsToSpacesChkBox.text")); // NOI18N
122         expandTabsToSpacesChkBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.expandTabsToSpacesChkBox.toolTipText")); // NOI18N
123         expandTabsToSpacesChkBox.addActionListener(new ActionListener() {
124             public void actionPerformed(ActionEvent evt) {
125                 expandTabsToSpacesChkBoxActionPerformed(evt);
126             }
127         });
128 
129         Mnemonics.setLocalizedText(splitMultiAttrsChkBox, NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.splitMultiAttrsChkBox.text")); // NOI18N
130         splitMultiAttrsChkBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.splitMultiAttrsChkBox.toolTipText")); // NOI18N
131         splitMultiAttrsChkBox.addActionListener(new ActionListener() {
132             public void actionPerformed(ActionEvent evt) {
133                 splitMultiAttrsChkBoxActionPerformed(evt);
134             }
135         });
136 
137         Mnemonics.setLocalizedText(tabWidthLbl, NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.tabWidthLbl.text")); // NOI18N
138         tabWidthLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.tabWidthLbl.toolTipText")); // NOI18N
139 
140         tabWidthSpn.setModel(new SpinnerNumberModel(4, null, null, 1));
141         tabWidthSpn.addChangeListener(new ChangeListener() {
142             public void stateChanged(ChangeEvent evt) {
143                 tabWidthSpnStateChanged(evt);
144             }
145         });
146 
147         GroupLayout layout = new GroupLayout(this);
148         this.setLayout(layout);
149         layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
150             .addGroup(layout.createSequentialGroup()
151                 .addContainerGap()
152                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
153                     .addGroup(layout.createSequentialGroup()
154                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
155                             .addComponent(expandTabsToSpacesChkBox)
156                             .addComponent(splitMultiAttrsChkBox)
157                             .addComponent(wrapLongLinesChkBox))
158                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
159                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
160                             .addComponent(wellFormedValidationLbl, GroupLayout.Alignment.TRAILING)
161                             .addComponent(rightMarginLbl, GroupLayout.Alignment.TRAILING)
162                             .addComponent(tabWidthLbl, GroupLayout.Alignment.TRAILING)))
163                     .addGroup(layout.createSequentialGroup()
164                         .addGap(0, 0, Short.MAX_VALUE)
165                         .addComponent(linefeedLbl)))
166                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
167                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
168                     .addComponent(wellFormedValidationCmbBox, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
169                     .addGroup(GroupLayout.Alignment.TRAILING, layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
170                         .addComponent(rightMarginSpn, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
171                         .addComponent(tabWidthSpn))
172                     .addComponent(linefeedCmbBox, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
173                 .addContainerGap())
174         );
175         layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
176             .addGroup(layout.createSequentialGroup()
177                 .addContainerGap()
178                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
179                     .addGroup(layout.createSequentialGroup()
180                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
181                             .addComponent(tabWidthLbl)
182                             .addComponent(tabWidthSpn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
183                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
184                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
185                             .addComponent(rightMarginLbl)
186                             .addComponent(rightMarginSpn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
187                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
188                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
189                             .addComponent(wellFormedValidationLbl)
190                             .addComponent(wellFormedValidationCmbBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
191                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
192                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
193                             .addComponent(linefeedLbl)
194                             .addComponent(linefeedCmbBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
195                     .addGroup(layout.createSequentialGroup()
196                         .addComponent(expandTabsToSpacesChkBox)
197                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
198                         .addComponent(splitMultiAttrsChkBox)
199                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
200                         .addComponent(wrapLongLinesChkBox)))
201                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
202         );
203     }// </editor-fold>//GEN-END:initComponents
204 
205     private void expandTabsToSpacesChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_expandTabsToSpacesChkBoxActionPerformed
206 		fireChangedListener();
207     }//GEN-LAST:event_expandTabsToSpacesChkBoxActionPerformed
208 
209     private void splitMultiAttrsChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_splitMultiAttrsChkBoxActionPerformed
210 		fireChangedListener();
211     }//GEN-LAST:event_splitMultiAttrsChkBoxActionPerformed
212 
213     private void wrapLongLinesChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_wrapLongLinesChkBoxActionPerformed
214 		fireChangedListener();
215     }//GEN-LAST:event_wrapLongLinesChkBoxActionPerformed
216 
217     private void tabWidthSpnStateChanged(ChangeEvent evt) {//GEN-FIRST:event_tabWidthSpnStateChanged
218 		tabWidthSpn.setToolTipText(Objects.toString(tabWidthSpn.getValue(), null));
219 
220 		fireChangedListener();
221     }//GEN-LAST:event_tabWidthSpnStateChanged
222 
223     private void rightMarginSpnStateChanged(ChangeEvent evt) {//GEN-FIRST:event_rightMarginSpnStateChanged
224 		rightMarginSpn.setToolTipText(Objects.toString(rightMarginSpn.getValue(), null));
225 
226 		fireChangedListener();
227     }//GEN-LAST:event_rightMarginSpnStateChanged
228 
229     private void linefeedCmbBoxItemStateChanged(ItemEvent evt) {//GEN-FIRST:event_linefeedCmbBoxItemStateChanged
230 		if (ItemEvent.SELECTED == evt.getStateChange()) {
231 			linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
232 
233 			fireChangedListener();
234 		}
235     }//GEN-LAST:event_linefeedCmbBoxItemStateChanged
236 
237     private void wellFormedValidationCmbBoxItemStateChanged(ItemEvent evt) {//GEN-FIRST:event_wellFormedValidationCmbBoxItemStateChanged
238 		if (ItemEvent.SELECTED == evt.getStateChange()) {
239 			wellFormedValidationCmbBox.setToolTipText(Objects.toString(wellFormedValidationCmbBox.getSelectedItem(), null));
240 
241 			fireChangedListener();
242 		}
243     }//GEN-LAST:event_wellFormedValidationCmbBoxItemStateChanged
244 
245     // Variables declaration - do not modify//GEN-BEGIN:variables
246     private JCheckBox expandTabsToSpacesChkBox;
247     private JComboBox<String> linefeedCmbBox;
248     private JLabel linefeedLbl;
249     private JLabel rightMarginLbl;
250     private JSpinner rightMarginSpn;
251     private JCheckBox splitMultiAttrsChkBox;
252     private JLabel tabWidthLbl;
253     private JSpinner tabWidthSpn;
254     private JComboBox<String> wellFormedValidationCmbBox;
255     private JLabel wellFormedValidationLbl;
256     private JCheckBox wrapLongLinesChkBox;
257     // End of variables declaration//GEN-END:variables
258 
259 	private String getLinefeed() {
260 		if (0 == linefeedCmbBox.getSelectedIndex()) {
261 			return "";
262 		}
263 		return linefeedCmbBox.getSelectedItem().toString();
264 	}
265 
266 	/**
267 	 * {@inheritDoc}
268 	 */
269 	@Override
270 	public void load(Preferences preferences) {
271 		boolean expandTabsToSpaces = !preferences.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true);
272 		boolean splitMultiAttrs = preferences.getBoolean(RevelcXmlFormatterSettings.SPLIT_MULTI_ATTRIBUTES, false);
273 		boolean wrapLongLines = preferences.getBoolean(RevelcXmlFormatterSettings.WRAP_LONG_LINES, true);
274 		int tabWidth = preferences.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
275 		int rightMargin = preferences.getInt(RevelcXmlFormatterSettings.MAX_LINE_LENGTH, 120);
276 		String wellFormedValidation = preferences.get(RevelcXmlFormatterSettings.WELL_FORMED_VALIDATION, FormattingPreferences.WARN);
277 		String lineFeed = preferences.get(RevelcXmlFormatterSettings.LINEFEED, "");
278 
279 		expandTabsToSpacesChkBox.setSelected(expandTabsToSpaces);
280 		splitMultiAttrsChkBox.setSelected(splitMultiAttrs);
281 		wrapLongLinesChkBox.setSelected(wrapLongLines);
282 		tabWidthSpn.setValue(tabWidth);
283 		tabWidthSpn.setToolTipText(Objects.toString(tabWidthSpn.getValue(), null));
284 		rightMarginSpn.setValue(rightMargin);
285 		rightMarginSpn.setToolTipText(Objects.toString(rightMarginSpn.getValue(), null));
286 		wellFormedValidationCmbBox.setSelectedItem(wellFormedValidation);
287 		wellFormedValidationCmbBox.setToolTipText(Objects.toString(wellFormedValidationCmbBox.getSelectedItem(), null));
288 
289 		if (StringUtils.isBlank(lineFeed)) {
290 			//default = system-dependend LF
291 			linefeedCmbBox.setSelectedIndex(0);
292 		} else {
293 			linefeedCmbBox.setSelectedItem(lineFeed);
294 		}
295 
296 		linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
297 	}
298 
299 	/**
300 	 * {@inheritDoc}
301 	 */
302 	@Override
303 	public void store(Preferences preferences) {
304 		preferences.putBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, !expandTabsToSpacesChkBox.isSelected());
305 		preferences.putBoolean(RevelcXmlFormatterSettings.SPLIT_MULTI_ATTRIBUTES, splitMultiAttrsChkBox.isSelected());
306 		preferences.putBoolean(RevelcXmlFormatterSettings.WRAP_LONG_LINES, wrapLongLinesChkBox.isSelected());
307 		preferences.putInt(RevelcXmlFormatterSettings.TAB_WIDTH, (int) tabWidthSpn.getValue());
308 		preferences.putInt(RevelcXmlFormatterSettings.MAX_LINE_LENGTH, (int) rightMarginSpn.getValue());
309 		preferences.put(RevelcXmlFormatterSettings.WELL_FORMED_VALIDATION, (String) wellFormedValidationCmbBox.getSelectedItem());
310 		preferences.put(RevelcXmlFormatterSettings.LINEFEED, getLinefeed());
311 	}
312 
313 	/**
314 	 * {@inheritDoc}
315 	 */
316 	@Override
317 	public boolean valid() {
318 		return true;
319 	}
320 }