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  package de.funfried.netbeans.plugins.external.formatter.html.jsoup.ui;
11  
12  import java.awt.event.ActionEvent;
13  import java.awt.event.ActionListener;
14  import java.awt.event.ItemEvent;
15  import java.awt.event.ItemListener;
16  import java.util.Objects;
17  import java.util.prefs.Preferences;
18  
19  import javax.swing.DefaultComboBoxModel;
20  import javax.swing.GroupLayout;
21  import javax.swing.JCheckBox;
22  import javax.swing.JComboBox;
23  import javax.swing.JLabel;
24  import javax.swing.JSpinner;
25  import javax.swing.LayoutStyle;
26  import javax.swing.SpinnerNumberModel;
27  import javax.swing.event.ChangeEvent;
28  import javax.swing.event.ChangeListener;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.netbeans.api.project.Project;
32  import org.openide.awt.Mnemonics;
33  import org.openide.util.NbBundle;
34  
35  import de.funfried.netbeans.plugins.external.formatter.html.jsoup.JsoupHtmlFormatterSettings;
36  import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
37  
38  /**
39   *
40   * @author bahlef
41   */
42  public class JsoupHtmlFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
43  	/**
44  	 * Creates new form {@link JsoupHtmlFormatterOptionsPanel}.
45  	 *
46  	 * @param project the {@link Project} if the panel is used to modify project
47  	 *        specific settings, otherwise {@code null}
48  	 */
49  	public JsoupHtmlFormatterOptionsPanel(Project project) {
50  		super(project);
51  
52  		initComponents();
53  	}
54  
55  	/**
56  	 * This method is called from within the constructor to
57  	 * initialize the form.
58  	 * WARNING: Do NOT modify this code. The content of this method is
59  	 * always regenerated by the Form Editor.
60  	 */
61  	@SuppressWarnings("unchecked")
62      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
63      private void initComponents() {
64  
65          linefeedLbl = new JLabel();
66          linefeedCmbBox = new JComboBox<>();
67          prettyPrintChkBox = new JCheckBox();
68          outlineChkBox = new JCheckBox();
69          indentSizeLbl = new JLabel();
70          indentSizeSpn = new JSpinner();
71  
72          Mnemonics.setLocalizedText(linefeedLbl, NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.linefeedLbl.text")); // NOI18N
73          linefeedLbl.setToolTipText(NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.linefeedLbl.toolTipText")); // NOI18N
74  
75          linefeedCmbBox.setModel(new DefaultComboBoxModel<>(new String[] { "System", "\\n", "\\r\\n", "\\r" }));
76          linefeedCmbBox.addItemListener(new ItemListener() {
77              public void itemStateChanged(ItemEvent evt) {
78                  linefeedCmbBoxItemStateChanged(evt);
79              }
80          });
81  
82          prettyPrintChkBox.setSelected(true);
83          Mnemonics.setLocalizedText(prettyPrintChkBox, NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.prettyPrintChkBox.text")); // NOI18N
84          prettyPrintChkBox.setToolTipText(NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.prettyPrintChkBox.toolTipText")); // NOI18N
85          prettyPrintChkBox.addActionListener(new ActionListener() {
86              public void actionPerformed(ActionEvent evt) {
87                  prettyPrintChkBoxActionPerformed(evt);
88              }
89          });
90  
91          Mnemonics.setLocalizedText(outlineChkBox, NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.outlineChkBox.text")); // NOI18N
92          outlineChkBox.setToolTipText(NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.outlineChkBox.toolTipText")); // NOI18N
93          outlineChkBox.addActionListener(new ActionListener() {
94              public void actionPerformed(ActionEvent evt) {
95                  outlineChkBoxActionPerformed(evt);
96              }
97          });
98  
99          Mnemonics.setLocalizedText(indentSizeLbl, NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.indentSizeLbl.text")); // NOI18N
100         indentSizeLbl.setToolTipText(NbBundle.getMessage(JsoupHtmlFormatterOptionsPanel.class, "JsoupHtmlFormatterOptionsPanel.indentSizeLbl.toolTipText")); // NOI18N
101 
102         indentSizeSpn.setModel(new SpinnerNumberModel(1, 0, null, 1));
103         indentSizeSpn.addChangeListener(new ChangeListener() {
104             public void stateChanged(ChangeEvent evt) {
105                 indentSizeSpnStateChanged(evt);
106             }
107         });
108 
109         GroupLayout layout = new GroupLayout(this);
110         this.setLayout(layout);
111         layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
112             .addGroup(layout.createSequentialGroup()
113                 .addContainerGap()
114                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
115                     .addGroup(layout.createSequentialGroup()
116                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
117                             .addComponent(prettyPrintChkBox)
118                             .addComponent(outlineChkBox))
119                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
120                         .addComponent(indentSizeLbl))
121                     .addGroup(layout.createSequentialGroup()
122                         .addGap(0, 0, Short.MAX_VALUE)
123                         .addComponent(linefeedLbl)))
124                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
125                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
126                     .addComponent(indentSizeSpn, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
127                     .addComponent(linefeedCmbBox, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
128                 .addContainerGap())
129         );
130         layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
131             .addGroup(layout.createSequentialGroup()
132                 .addContainerGap()
133                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
134                     .addGroup(layout.createSequentialGroup()
135                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
136                             .addComponent(indentSizeLbl)
137                             .addComponent(indentSizeSpn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
138                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
139                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
140                             .addComponent(linefeedCmbBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
141                             .addComponent(linefeedLbl)))
142                     .addGroup(layout.createSequentialGroup()
143                         .addComponent(prettyPrintChkBox)
144                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
145                         .addComponent(outlineChkBox)))
146                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
147         );
148     }// </editor-fold>//GEN-END:initComponents
149 
150     private void prettyPrintChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_prettyPrintChkBoxActionPerformed
151 		fireChangedListener();
152     }//GEN-LAST:event_prettyPrintChkBoxActionPerformed
153 
154     private void outlineChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_outlineChkBoxActionPerformed
155 		fireChangedListener();
156     }//GEN-LAST:event_outlineChkBoxActionPerformed
157 
158     private void indentSizeSpnStateChanged(ChangeEvent evt) {//GEN-FIRST:event_indentSizeSpnStateChanged
159 		indentSizeSpn.setToolTipText(Objects.toString(indentSizeSpn.getValue(), null));
160 
161 		fireChangedListener();
162     }//GEN-LAST:event_indentSizeSpnStateChanged
163 
164     private void linefeedCmbBoxItemStateChanged(ItemEvent evt) {//GEN-FIRST:event_linefeedCmbBoxItemStateChanged
165 		if (ItemEvent.SELECTED == evt.getStateChange()) {
166 			linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
167 
168 			fireChangedListener();
169 		}
170     }//GEN-LAST:event_linefeedCmbBoxItemStateChanged
171 
172     // Variables declaration - do not modify//GEN-BEGIN:variables
173     private JLabel indentSizeLbl;
174     private JSpinner indentSizeSpn;
175     private JComboBox<String> linefeedCmbBox;
176     private JLabel linefeedLbl;
177     private JCheckBox outlineChkBox;
178     private JCheckBox prettyPrintChkBox;
179     // End of variables declaration//GEN-END:variables
180 
181 		private String getLinefeed() {
182 			if (0 == linefeedCmbBox.getSelectedIndex()) {
183 				return "";
184 			}
185 			return linefeedCmbBox.getSelectedItem().toString();
186 		}
187 
188 		/**
189 		 * {@inheritDoc}
190 		 */
191 		@Override
192 		public void load(Preferences preferences) {
193 			boolean prettyPrint = preferences.getBoolean(JsoupHtmlFormatterSettings.PRETTY_PRINT, true);
194 			boolean outline = preferences.getBoolean(JsoupHtmlFormatterSettings.OUTLINE, false);
195 			int indentSize = preferences.getInt(JsoupHtmlFormatterSettings.INDENT_SIZE, 1);
196 			String lineFeed = preferences.get(JsoupHtmlFormatterSettings.LINEFEED, "");
197 
198 			prettyPrintChkBox.setSelected(prettyPrint);
199 			outlineChkBox.setSelected(outline);
200 			indentSizeSpn.setValue(indentSize);
201 			indentSizeSpn.setToolTipText(Objects.toString(indentSizeSpn.getValue(), null));
202 
203 			if (StringUtils.isBlank(lineFeed)) {
204 				//default = system-dependend LF
205 				linefeedCmbBox.setSelectedIndex(0);
206 			} else {
207 				linefeedCmbBox.setSelectedItem(lineFeed);
208 			}
209 
210 			linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
211 		}
212 
213 		/**
214 		 * {@inheritDoc}
215 		 */
216 		@Override
217 		public void store(Preferences preferences) {
218 			preferences.putBoolean(JsoupHtmlFormatterSettings.PRETTY_PRINT, prettyPrintChkBox.isSelected());
219 			preferences.putBoolean(JsoupHtmlFormatterSettings.OUTLINE, outlineChkBox.isSelected());
220 			preferences.putInt(JsoupHtmlFormatterSettings.INDENT_SIZE, (int) indentSizeSpn.getValue());
221 			preferences.put(JsoupHtmlFormatterSettings.LINEFEED, getLinefeed());
222 		}
223 
224 		/**
225 		 * {@inheritDoc}
226 		 */
227 		@Override
228 		public boolean valid() {
229 			return true;
230 		}
231 	}