1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.xml.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.ui.options.AbstractFormatterOptionsPanel;
36 import de.funfried.netbeans.plugins.external.formatter.xml.jsoup.JsoupXmlFormatterSettings;
37
38
39
40
41
42 public class JsoupXmlFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
43
44
45
46
47
48
49 public JsoupXmlFormatterOptionsPanel(Project project) {
50 super(project);
51
52 initComponents();
53 }
54
55
56
57
58
59
60
61 @SuppressWarnings("unchecked")
62
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(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.linefeedLbl.text"));
73 linefeedLbl.setToolTipText(NbBundle.getMessage(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.linefeedLbl.toolTipText"));
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(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.prettyPrintChkBox.text"));
84 prettyPrintChkBox.setToolTipText(NbBundle.getMessage(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.prettyPrintChkBox.toolTipText"));
85 prettyPrintChkBox.addActionListener(new ActionListener() {
86 public void actionPerformed(ActionEvent evt) {
87 prettyPrintChkBoxActionPerformed(evt);
88 }
89 });
90
91 Mnemonics.setLocalizedText(outlineChkBox, NbBundle.getMessage(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.outlineChkBox.text"));
92 outlineChkBox.setToolTipText(NbBundle.getMessage(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.outlineChkBox.toolTipText"));
93 outlineChkBox.addActionListener(new ActionListener() {
94 public void actionPerformed(ActionEvent evt) {
95 outlineChkBoxActionPerformed(evt);
96 }
97 });
98
99 Mnemonics.setLocalizedText(indentSizeLbl, NbBundle.getMessage(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.indentSizeLbl.text"));
100 indentSizeLbl.setToolTipText(NbBundle.getMessage(JsoupXmlFormatterOptionsPanel.class, "JsoupXmlFormatterOptionsPanel.indentSizeLbl.toolTipText"));
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 }
149
150 private void prettyPrintChkBoxActionPerformed(ActionEvent evt) {
151 fireChangedListener();
152 }
153
154 private void outlineChkBoxActionPerformed(ActionEvent evt) {
155 fireChangedListener();
156 }
157
158 private void indentSizeSpnStateChanged(ChangeEvent evt) {
159 indentSizeSpn.setToolTipText(Objects.toString(indentSizeSpn.getValue(), null));
160
161 fireChangedListener();
162 }
163
164 private void linefeedCmbBoxItemStateChanged(ItemEvent evt) {
165 if (ItemEvent.SELECTED == evt.getStateChange()) {
166 linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
167
168 fireChangedListener();
169 }
170 }
171
172
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
180
181 private String getLinefeed() {
182 if (0 == linefeedCmbBox.getSelectedIndex()) {
183 return "";
184 }
185 return linefeedCmbBox.getSelectedItem().toString();
186 }
187
188
189
190
191 @Override
192 public void load(Preferences preferences) {
193 boolean prettyPrint = preferences.getBoolean(JsoupXmlFormatterSettings.PRETTY_PRINT, true);
194 boolean outline = preferences.getBoolean(JsoupXmlFormatterSettings.OUTLINE, false);
195 int indentSize = preferences.getInt(JsoupXmlFormatterSettings.INDENT_SIZE, 1);
196 String lineFeed = preferences.get(JsoupXmlFormatterSettings.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
205 linefeedCmbBox.setSelectedIndex(0);
206 } else {
207 linefeedCmbBox.setSelectedItem(lineFeed);
208 }
209
210 linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
211 }
212
213
214
215
216 @Override
217 public void store(Preferences preferences) {
218 preferences.putBoolean(JsoupXmlFormatterSettings.PRETTY_PRINT, prettyPrintChkBox.isSelected());
219 preferences.putBoolean(JsoupXmlFormatterSettings.OUTLINE, outlineChkBox.isSelected());
220 preferences.putInt(JsoupXmlFormatterSettings.INDENT_SIZE, (int) indentSizeSpn.getValue());
221 preferences.put(JsoupXmlFormatterSettings.LINEFEED, getLinefeed());
222 }
223
224
225
226
227 @Override
228 public boolean valid() {
229 return true;
230 }
231 }