1
2
3
4
5
6
7
8
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
43
44 public class RevelcXmlFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
45
46
47
48
49
50
51 public RevelcXmlFormatterOptionsPanel(Project project) {
52 super(project);
53
54 initComponents();
55 }
56
57
58
59
60
61
62
63 @SuppressWarnings("unchecked")
64
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"));
80 linefeedLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.linefeedLbl.toolTipText"));
81
82 linefeedCmbBox.setModel(new DefaultComboBoxModel<>(new String[] { "System", "\\n", "\\r\\n", "\\r" }));
83 linefeedCmbBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.linefeedCmbBox.toolTipText"));
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"));
91 rightMarginLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.rightMarginLbl.toolTipText"));
92
93 rightMarginSpn.setModel(new SpinnerNumberModel(120, 10, null, 10));
94 rightMarginSpn.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.rightMarginSpn.toolTipText"));
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"));
103 wrapLongLinesChkBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.wrapLongLinesChkBox.toolTipText"));
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"));
111 wellFormedValidationLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.wellFormedValidationLbl.toolTipText"));
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"));
122 expandTabsToSpacesChkBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.expandTabsToSpacesChkBox.toolTipText"));
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"));
130 splitMultiAttrsChkBox.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.splitMultiAttrsChkBox.toolTipText"));
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"));
138 tabWidthLbl.setToolTipText(NbBundle.getMessage(RevelcXmlFormatterOptionsPanel.class, "RevelcXmlFormatterOptionsPanel.tabWidthLbl.toolTipText"));
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 }
204
205 private void expandTabsToSpacesChkBoxActionPerformed(ActionEvent evt) {
206 fireChangedListener();
207 }
208
209 private void splitMultiAttrsChkBoxActionPerformed(ActionEvent evt) {
210 fireChangedListener();
211 }
212
213 private void wrapLongLinesChkBoxActionPerformed(ActionEvent evt) {
214 fireChangedListener();
215 }
216
217 private void tabWidthSpnStateChanged(ChangeEvent evt) {
218 tabWidthSpn.setToolTipText(Objects.toString(tabWidthSpn.getValue(), null));
219
220 fireChangedListener();
221 }
222
223 private void rightMarginSpnStateChanged(ChangeEvent evt) {
224 rightMarginSpn.setToolTipText(Objects.toString(rightMarginSpn.getValue(), null));
225
226 fireChangedListener();
227 }
228
229 private void linefeedCmbBoxItemStateChanged(ItemEvent evt) {
230 if (ItemEvent.SELECTED == evt.getStateChange()) {
231 linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
232
233 fireChangedListener();
234 }
235 }
236
237 private void wellFormedValidationCmbBoxItemStateChanged(ItemEvent evt) {
238 if (ItemEvent.SELECTED == evt.getStateChange()) {
239 wellFormedValidationCmbBox.setToolTipText(Objects.toString(wellFormedValidationCmbBox.getSelectedItem(), null));
240
241 fireChangedListener();
242 }
243 }
244
245
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
258
259 private String getLinefeed() {
260 if (0 == linefeedCmbBox.getSelectedIndex()) {
261 return "";
262 }
263 return linefeedCmbBox.getSelectedItem().toString();
264 }
265
266
267
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
291 linefeedCmbBox.setSelectedIndex(0);
292 } else {
293 linefeedCmbBox.setSelectedItem(lineFeed);
294 }
295
296 linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
297 }
298
299
300
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
315
316 @Override
317 public boolean valid() {
318 return true;
319 }
320 }