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.java.spring.ui;
12  
13  import java.awt.event.ItemEvent;
14  import java.awt.event.ItemListener;
15  import java.util.Objects;
16  import java.util.prefs.Preferences;
17  
18  import javax.swing.DefaultComboBoxModel;
19  import javax.swing.GroupLayout;
20  import javax.swing.JComboBox;
21  import javax.swing.JLabel;
22  import javax.swing.LayoutStyle;
23  import javax.swing.SwingConstants;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.netbeans.api.project.Project;
27  import org.openide.awt.Mnemonics;
28  import org.openide.util.NbBundle;
29  
30  import de.funfried.netbeans.plugins.external.formatter.java.spring.SpringJavaFormatterSettings;
31  import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
32  
33  /**
34   *
35   * @author bahlef
36   */
37  public class SpringJavaFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
38  	/**
39  	 * Creates new form {@link SpringJavaFormatterOptionsPanel}.
40  	 *
41  	 * @param project the {@link Project} if the panel is used to modify project
42  	 *                specific settings, otherwise {@code null}
43  	 */
44  	public SpringJavaFormatterOptionsPanel(Project project) {
45  		super(project);
46  
47  		initComponents();
48  	}
49  
50  	/**
51  	 * This method is called from within the constructor to
52  	 * initialize the form.
53  	 * WARNING: Do NOT modify this code. The content of this method is
54  	 * always regenerated by the Form Editor.
55  	 */
56  	@SuppressWarnings("unchecked")
57      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
58      private void initComponents() {
59  
60          lblSpringLinefeed = new JLabel();
61          cbLinefeed = new JComboBox<>();
62  
63          lblSpringLinefeed.setHorizontalAlignment(SwingConstants.RIGHT);
64          Mnemonics.setLocalizedText(lblSpringLinefeed, NbBundle.getMessage(SpringJavaFormatterOptionsPanel.class, "SpringJavaFormatterOptionsPanel.lblSpringLinefeed.text")); // NOI18N
65          lblSpringLinefeed.setToolTipText(NbBundle.getMessage(SpringJavaFormatterOptionsPanel.class, "SpringJavaFormatterOptionsPanel.lblSpringLinefeed.toolTipText")); // NOI18N
66  
67          cbLinefeed.setModel(new DefaultComboBoxModel<>(new String[] { "System", "\\n", "\\r\\n", "\\r" }));
68          cbLinefeed.addItemListener(new ItemListener() {
69              public void itemStateChanged(ItemEvent evt) {
70                  cbLinefeedItemStateChanged(evt);
71              }
72          });
73  
74          GroupLayout layout = new GroupLayout(this);
75          this.setLayout(layout);
76          layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
77              .addGroup(layout.createSequentialGroup()
78                  .addContainerGap()
79                  .addComponent(lblSpringLinefeed)
80                  .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
81                  .addComponent(cbLinefeed, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
82                  .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
83          );
84          layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
85              .addGroup(layout.createSequentialGroup()
86                  .addContainerGap()
87                  .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
88                      .addComponent(lblSpringLinefeed)
89                      .addComponent(cbLinefeed, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
90                  .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
91          );
92      }// </editor-fold>//GEN-END:initComponents
93  
94      private void cbLinefeedItemStateChanged(ItemEvent evt) {//GEN-FIRST:event_cbLinefeedItemStateChanged
95  		if (ItemEvent.SELECTED == evt.getStateChange()) {
96  			cbLinefeed.setToolTipText(Objects.toString(cbLinefeed.getSelectedItem(), null));
97  
98  			fireChangedListener();
99  		}
100     }//GEN-LAST:event_cbLinefeedItemStateChanged
101 
102     // Variables declaration - do not modify//GEN-BEGIN:variables
103     private JComboBox<String> cbLinefeed;
104     private JLabel lblSpringLinefeed;
105     // End of variables declaration//GEN-END:variables
106 
107 	private String getLinefeed() {
108 		if (0 == cbLinefeed.getSelectedIndex()) {
109 			return "";
110 		}
111 		return cbLinefeed.getSelectedItem().toString();
112 	}
113 
114 	/**
115 	 * {@inheritDoc}
116 	 */
117 	@Override
118 	public void load(Preferences preferences) {
119 		String springLineFeed = preferences.get(SpringJavaFormatterSettings.LINEFEED, "");
120 
121 		if (StringUtils.isBlank(springLineFeed)) {
122 			//default = system-dependend LF
123 			cbLinefeed.setSelectedIndex(0);
124 		} else {
125 			cbLinefeed.setSelectedItem(springLineFeed);
126 		}
127 
128 		cbLinefeed.setToolTipText(Objects.toString(cbLinefeed.getSelectedItem(), null));
129 	}
130 
131 	/**
132 	 * {@inheritDoc}
133 	 */
134 	@Override
135 	public void store(Preferences preferences) {
136 		preferences.put(SpringJavaFormatterSettings.LINEFEED, getLinefeed());
137 	}
138 }