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.google.ui;
12  
13  import java.awt.event.ActionEvent;
14  import java.awt.event.ActionListener;
15  import java.util.prefs.Preferences;
16  
17  import javax.swing.ButtonGroup;
18  import javax.swing.GroupLayout;
19  import javax.swing.JCheckBox;
20  import javax.swing.JLabel;
21  import javax.swing.JRadioButton;
22  import javax.swing.LayoutStyle;
23  
24  import org.netbeans.api.project.Project;
25  import org.openide.awt.Mnemonics;
26  import org.openide.util.NbBundle;
27  
28  import com.google.googlejavaformat.java.JavaFormatterOptions;
29  
30  import de.funfried.netbeans.plugins.external.formatter.java.google.GoogleJavaFormatterSettings;
31  import de.funfried.netbeans.plugins.external.formatter.ui.options.AbstractFormatterOptionsPanel;
32  
33  /**
34   *
35   * @author bahlef
36   */
37  public class GoogleJavaFormatterOptionsPanel extends AbstractFormatterOptionsPanel {
38  	/**
39  	 * Creates new form {@link GoogleJavaFormatterOptionsPanel}.
40  	 *
41  	 * @param project the {@link Project} if the panel is used to modify project
42  	 *        specific settings, otherwise {@code null}
43  	 */
44  	public GoogleJavaFormatterOptionsPanel(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          googleCodeStyleBtnGrp = new ButtonGroup();
61          googleCodeStyleRdBtn = new JRadioButton();
62          aospRdBtn = new JRadioButton();
63          googleCodeStyleLbl = new JLabel();
64          organizeImportsChkBox = new JCheckBox();
65          organizeImportsAfterFixImportsChkBox = new JCheckBox();
66  
67          googleCodeStyleBtnGrp.add(googleCodeStyleRdBtn);
68          googleCodeStyleRdBtn.setSelected(true);
69          Mnemonics.setLocalizedText(googleCodeStyleRdBtn, NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.googleCodeStyleRdBtn.text")); // NOI18N
70          googleCodeStyleRdBtn.setToolTipText(NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.googleCodeStyleRdBtn.toolTipText")); // NOI18N
71          googleCodeStyleRdBtn.addActionListener(new ActionListener() {
72              public void actionPerformed(ActionEvent evt) {
73                  googleCodeStyleRdBtnActionPerformed(evt);
74              }
75          });
76  
77          googleCodeStyleBtnGrp.add(aospRdBtn);
78          Mnemonics.setLocalizedText(aospRdBtn, NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.aospRdBtn.text")); // NOI18N
79          aospRdBtn.setToolTipText(NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.aospRdBtn.toolTipText")); // NOI18N
80          aospRdBtn.addActionListener(new ActionListener() {
81              public void actionPerformed(ActionEvent evt) {
82                  aospRdBtnActionPerformed(evt);
83              }
84          });
85  
86          Mnemonics.setLocalizedText(googleCodeStyleLbl, NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.googleCodeStyleLbl.text")); // NOI18N
87          googleCodeStyleLbl.setToolTipText(NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.googleCodeStyleLbl.toolTipText")); // NOI18N
88  
89          Mnemonics.setLocalizedText(organizeImportsChkBox, NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.organizeImportsChkBox.text")); // NOI18N
90          organizeImportsChkBox.addActionListener(new ActionListener() {
91              public void actionPerformed(ActionEvent evt) {
92                  organizeImportsChkBoxActionPerformed(evt);
93              }
94          });
95  
96          Mnemonics.setLocalizedText(organizeImportsAfterFixImportsChkBox, NbBundle.getMessage(GoogleJavaFormatterOptionsPanel.class, "GoogleJavaFormatterOptionsPanel.organizeImportsAfterFixImportsChkBox.text")); // NOI18N
97          organizeImportsAfterFixImportsChkBox.setEnabled(false);
98          organizeImportsAfterFixImportsChkBox.addActionListener(new ActionListener() {
99              public void actionPerformed(ActionEvent evt) {
100                 organizeImportsAfterFixImportsChkBoxActionPerformed(evt);
101             }
102         });
103 
104         GroupLayout layout = new GroupLayout(this);
105         this.setLayout(layout);
106         layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
107             .addGroup(layout.createSequentialGroup()
108                 .addContainerGap()
109                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
110                     .addGroup(layout.createSequentialGroup()
111                         .addComponent(googleCodeStyleLbl)
112                         .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
113                         .addComponent(googleCodeStyleRdBtn)
114                         .addGap(18, 18, 18)
115                         .addComponent(aospRdBtn))
116                     .addComponent(organizeImportsChkBox)
117                     .addGroup(layout.createSequentialGroup()
118                         .addGap(21, 21, 21)
119                         .addComponent(organizeImportsAfterFixImportsChkBox)))
120                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
121         );
122         layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
123             .addGroup(layout.createSequentialGroup()
124                 .addContainerGap()
125                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
126                     .addComponent(googleCodeStyleRdBtn)
127                     .addComponent(aospRdBtn)
128                     .addComponent(googleCodeStyleLbl))
129                 .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
130                 .addComponent(organizeImportsChkBox)
131                 .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
132                 .addComponent(organizeImportsAfterFixImportsChkBox)
133                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
134         );
135     }// </editor-fold>//GEN-END:initComponents
136 
137     private void googleCodeStyleRdBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_googleCodeStyleRdBtnActionPerformed
138 		fireChangedListener();
139     }//GEN-LAST:event_googleCodeStyleRdBtnActionPerformed
140 
141     private void aospRdBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_aospRdBtnActionPerformed
142 		fireChangedListener();
143     }//GEN-LAST:event_aospRdBtnActionPerformed
144 
145     private void organizeImportsChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_organizeImportsChkBoxActionPerformed
146         fireChangedListener();
147     }//GEN-LAST:event_organizeImportsChkBoxActionPerformed
148 
149     private void organizeImportsAfterFixImportsChkBoxActionPerformed(ActionEvent evt) {//GEN-FIRST:event_organizeImportsAfterFixImportsChkBoxActionPerformed
150         fireChangedListener();
151     }//GEN-LAST:event_organizeImportsAfterFixImportsChkBoxActionPerformed
152 
153     // Variables declaration - do not modify//GEN-BEGIN:variables
154     private JRadioButton aospRdBtn;
155     private ButtonGroup googleCodeStyleBtnGrp;
156     private JLabel googleCodeStyleLbl;
157     private JRadioButton googleCodeStyleRdBtn;
158     private JCheckBox organizeImportsAfterFixImportsChkBox;
159     private JCheckBox organizeImportsChkBox;
160     // End of variables declaration//GEN-END:variables
161 
162 		/**
163 		 * {@inheritDoc}
164 		 */
165 		@Override
166 		public void load(Preferences preferences) {
167 			String googleFormatterCodeStyle = preferences.get(GoogleJavaFormatterSettings.CODE_STYLE, JavaFormatterOptions.Style.GOOGLE.name());
168 
169 			if (JavaFormatterOptions.Style.AOSP.name().equals(googleFormatterCodeStyle)) {
170 				googleCodeStyleBtnGrp.setSelected(aospRdBtn.getModel(), true);
171 			} else {
172 				googleCodeStyleBtnGrp.setSelected(googleCodeStyleRdBtn.getModel(), true);
173 			}
174 
175 			boolean organizeImports = preferences.getBoolean(GoogleJavaFormatterSettings.ORGANIZE_IMPORTS, false);
176 
177 			organizeImportsChkBox.setSelected(organizeImports);
178 
179 			organizeImportsAfterFixImportsChkBox.setEnabled(organizeImports);
180 			if (!organizeImports) {
181 				organizeImportsAfterFixImportsChkBox.setSelected(false);
182 			} else {
183 				boolean organizeImportsAfterFixImports = preferences.getBoolean(GoogleJavaFormatterSettings.ORGANIZE_IMPORTS_AFTER_FIX_IMPORTS, false);
184 
185 				organizeImportsAfterFixImportsChkBox.setSelected(organizeImportsAfterFixImports);
186 			}
187 		}
188 
189 		/**
190 		 * {@inheritDoc}
191 		 */
192 		@Override
193 		public void store(Preferences preferences) {
194 			preferences.put(GoogleJavaFormatterSettings.CODE_STYLE, googleCodeStyleRdBtn.isSelected() ? JavaFormatterOptions.Style.GOOGLE.name() : JavaFormatterOptions.Style.AOSP.name());
195 
196 			boolean organizeImports = organizeImportsChkBox.isSelected();
197 			preferences.putBoolean(GoogleJavaFormatterSettings.ORGANIZE_IMPORTS, organizeImports);
198 			preferences.putBoolean(GoogleJavaFormatterSettings.ORGANIZE_IMPORTS_AFTER_FIX_IMPORTS, organizeImports && organizeImportsAfterFixImportsChkBox.isSelected());
199 		}
200 
201 		/**
202 		 * {@inheritDoc}
203 		 */
204 		@Override
205 		public boolean valid() {
206 			boolean organizeImports = organizeImportsChkBox.isSelected();
207 			organizeImportsAfterFixImportsChkBox.setEnabled(organizeImports);
208 			if (!organizeImports) {
209 				organizeImportsAfterFixImportsChkBox.setEnabled(false);
210 			}
211 
212 			return googleCodeStyleBtnGrp.getSelection() != null;
213 		}
214 	}