1
2
3
4
5
6
7
8
9
10
11 package de.funfried.netbeans.plugins.external.formatter.json.jackson.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.json.jackson.JacksonJsonFormatterSettings;
37 import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
38
39
40
41
42
43 public class JacksonJsonFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
44
45
46
47
48
49
50 public JacksonJsonFormatterOptionsPanel(Project project) {
51 super(project);
52
53 initComponents();
54 }
55
56
57
58
59
60
61
62 @SuppressWarnings("unchecked")
63
64 private void initComponents() {
65
66 linefeedLbl = new JLabel();
67 linefeedCmbBox = new JComboBox<>();
68 spacesPerTabLbl = new JLabel();
69 spacesPerTabSpn = new JSpinner();
70 expandTabsToSpacesChkBox = new JCheckBox();
71 spacesBeforeSeparatorChkBox = new JCheckBox();
72 indentSizeLbl = new JLabel();
73 indentSizeSpn = new JSpinner();
74
75 Mnemonics.setLocalizedText(linefeedLbl, NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.linefeedLbl.text"));
76 linefeedLbl.setToolTipText(NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.linefeedLbl.toolTipText"));
77
78 linefeedCmbBox.setModel(new DefaultComboBoxModel<>(new String[] { "System", "\\n", "\\r\\n", "\\r" }));
79 linefeedCmbBox.addItemListener(new ItemListener() {
80 public void itemStateChanged(ItemEvent evt) {
81 linefeedCmbBoxItemStateChanged(evt);
82 }
83 });
84
85 Mnemonics.setLocalizedText(spacesPerTabLbl, NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.spacesPerTabLbl.text"));
86 spacesPerTabLbl.setToolTipText(NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.spacesPerTabLbl.toolTipText"));
87
88 spacesPerTabSpn.setModel(new SpinnerNumberModel(2, 1, null, 1));
89 spacesPerTabSpn.addChangeListener(new ChangeListener() {
90 public void stateChanged(ChangeEvent evt) {
91 spacesPerTabSpnStateChanged(evt);
92 }
93 });
94
95 expandTabsToSpacesChkBox.setSelected(true);
96 Mnemonics.setLocalizedText(expandTabsToSpacesChkBox, NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.expandTabsToSpacesChkBox.text"));
97 expandTabsToSpacesChkBox.setToolTipText(NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.expandTabsToSpacesChkBox.toolTipText"));
98 expandTabsToSpacesChkBox.addActionListener(new ActionListener() {
99 public void actionPerformed(ActionEvent evt) {
100 expandTabsToSpacesChkBoxActionPerformed(evt);
101 }
102 });
103
104 Mnemonics.setLocalizedText(spacesBeforeSeparatorChkBox, NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.spacesBeforeSeparatorChkBox.text"));
105 spacesBeforeSeparatorChkBox.setToolTipText(NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.spacesBeforeSeparatorChkBox.toolTipText"));
106 spacesBeforeSeparatorChkBox.addActionListener(new ActionListener() {
107 public void actionPerformed(ActionEvent evt) {
108 spacesBeforeSeparatorChkBoxActionPerformed(evt);
109 }
110 });
111
112 Mnemonics.setLocalizedText(indentSizeLbl, NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.indentSizeLbl.text"));
113 indentSizeLbl.setToolTipText(NbBundle.getMessage(JacksonJsonFormatterOptionsPanel.class, "JacksonJsonFormatterOptionsPanel.indentSizeLbl.toolTipText"));
114
115 indentSizeSpn.setModel(new SpinnerNumberModel(2, 0, null, 1));
116 indentSizeSpn.addChangeListener(new ChangeListener() {
117 public void stateChanged(ChangeEvent evt) {
118 indentSizeSpnStateChanged(evt);
119 }
120 });
121
122 GroupLayout layout = new GroupLayout(this);
123 this.setLayout(layout);
124 layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
125 .addGroup(layout.createSequentialGroup()
126 .addContainerGap()
127 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
128 .addComponent(expandTabsToSpacesChkBox)
129 .addComponent(spacesBeforeSeparatorChkBox))
130 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
131 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
132 .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
133 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
134 .addComponent(spacesPerTabLbl, GroupLayout.Alignment.TRAILING)
135 .addComponent(indentSizeLbl, GroupLayout.Alignment.TRAILING))
136 .addGap(43, 43, 43)
137 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
138 .addComponent(spacesPerTabSpn, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
139 .addComponent(indentSizeSpn)))
140 .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
141 .addComponent(linefeedLbl)
142 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
143 .addComponent(linefeedCmbBox, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))
144 .addContainerGap())
145 );
146 layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
147 .addGroup(layout.createSequentialGroup()
148 .addContainerGap()
149 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
150 .addGroup(layout.createSequentialGroup()
151 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
152 .addComponent(indentSizeLbl)
153 .addComponent(indentSizeSpn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
154 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
155 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
156 .addComponent(spacesPerTabLbl)
157 .addComponent(spacesPerTabSpn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
158 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
159 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
160 .addComponent(linefeedLbl)
161 .addComponent(linefeedCmbBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
162 .addGroup(layout.createSequentialGroup()
163 .addComponent(expandTabsToSpacesChkBox)
164 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
165 .addComponent(spacesBeforeSeparatorChkBox)))
166 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
167 );
168 }
169
170 private void expandTabsToSpacesChkBoxActionPerformed(ActionEvent evt) {
171 fireChangedListener();
172 }
173
174 private void spacesBeforeSeparatorChkBoxActionPerformed(ActionEvent evt) {
175 fireChangedListener();
176 }
177
178 private void indentSizeSpnStateChanged(ChangeEvent evt) {
179 indentSizeSpn.setToolTipText(Objects.toString(indentSizeSpn.getValue(), null));
180
181 fireChangedListener();
182 }
183
184 private void spacesPerTabSpnStateChanged(ChangeEvent evt) {
185 spacesPerTabSpn.setToolTipText(Objects.toString(spacesPerTabSpn.getValue(), null));
186
187 fireChangedListener();
188 }
189
190 private void linefeedCmbBoxItemStateChanged(ItemEvent evt) {
191 if (ItemEvent.SELECTED == evt.getStateChange()) {
192 linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
193
194 fireChangedListener();
195 }
196 }
197
198
199 private JCheckBox expandTabsToSpacesChkBox;
200 private JLabel indentSizeLbl;
201 private JSpinner indentSizeSpn;
202 private JComboBox<String> linefeedCmbBox;
203 private JLabel linefeedLbl;
204 private JCheckBox spacesBeforeSeparatorChkBox;
205 private JLabel spacesPerTabLbl;
206 private JSpinner spacesPerTabSpn;
207
208
209 private String getLinefeed() {
210 if (0 == linefeedCmbBox.getSelectedIndex()) {
211 return "";
212 }
213 return linefeedCmbBox.getSelectedItem().toString();
214 }
215
216
217
218
219 @Override
220 public void load(Preferences preferences) {
221 boolean expandTabsToSpaces = preferences.getBoolean(JacksonJsonFormatterSettings.EXPAND_TABS_TO_SPACES, true);
222 boolean spacesBeforeSeparator = preferences.getBoolean(JacksonJsonFormatterSettings.SPACE_BEFORE_SEPARATOR, false);
223 int indentSize = preferences.getInt(JacksonJsonFormatterSettings.INDENT_SIZE, 2);
224 int spacesPerTab = preferences.getInt(JacksonJsonFormatterSettings.SPACES_PER_TAB, 2);
225 String lineFeed = preferences.get(JacksonJsonFormatterSettings.LINEFEED, "");
226
227 expandTabsToSpacesChkBox.setSelected(expandTabsToSpaces);
228 spacesBeforeSeparatorChkBox.setSelected(spacesBeforeSeparator);
229 indentSizeSpn.setValue(indentSize);
230 indentSizeSpn.setToolTipText(Objects.toString(indentSizeSpn.getValue(), null));
231 spacesPerTabSpn.setValue(spacesPerTab);
232 spacesPerTabSpn.setToolTipText(Objects.toString(spacesPerTabSpn.getValue(), null));
233
234 if (StringUtils.isBlank(lineFeed)) {
235
236 linefeedCmbBox.setSelectedIndex(0);
237 } else {
238 linefeedCmbBox.setSelectedItem(lineFeed);
239 }
240
241 linefeedCmbBox.setToolTipText(Objects.toString(linefeedCmbBox.getSelectedItem(), null));
242 }
243
244
245
246
247 @Override
248 public void store(Preferences preferences) {
249 preferences.putBoolean(JacksonJsonFormatterSettings.EXPAND_TABS_TO_SPACES, expandTabsToSpacesChkBox.isSelected());
250 preferences.putBoolean(JacksonJsonFormatterSettings.SPACE_BEFORE_SEPARATOR, spacesBeforeSeparatorChkBox.isSelected());
251 preferences.putInt(JacksonJsonFormatterSettings.INDENT_SIZE, (int) indentSizeSpn.getValue());
252 preferences.putInt(JacksonJsonFormatterSettings.SPACES_PER_TAB, (int) spacesPerTabSpn.getValue());
253 preferences.put(JacksonJsonFormatterSettings.LINEFEED, getLinefeed());
254 }
255
256
257
258
259 @Override
260 public boolean valid() {
261 return true;
262 }
263 }