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    * markiewb - initial API and implementation and/or initial documentation
9    * bahlef
10   */
11  package de.funfried.netbeans.plugins.external.formatter.ui.customizer;
12  
13  import java.awt.BorderLayout;
14  import java.awt.Dimension;
15  import java.awt.event.ActionEvent;
16  import java.awt.event.ActionListener;
17  import java.awt.event.MouseAdapter;
18  import java.awt.event.MouseEvent;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.prefs.Preferences;
22  
23  import javax.swing.JCheckBox;
24  import javax.swing.JLabel;
25  import javax.swing.JPanel;
26  import javax.swing.JScrollPane;
27  import javax.swing.SwingConstants;
28  import javax.swing.event.ChangeListener;
29  
30  import org.netbeans.api.options.OptionsDisplayer;
31  import org.openide.awt.Mnemonics;
32  import org.openide.util.NbBundle;
33  
34  import de.funfried.netbeans.plugins.external.formatter.ui.options.ExternalFormatterPanel;
35  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
36  
37  /**
38   * {@link VerifiableConfigPanel} {@link JPanel} implementation for project specific external formatting properties panel.
39   *
40   * @author markiewb
41   * @author bahlef
42   */
43  @NbBundle.Messages({ "ProjectSpecificSettingsPanel.cbOverrideGlobalSettings.text=Override global settings",
44  		"ProjectSpecificSettingsPanel.lblJumpToGlobalOptions.text=<html><a href=\"#\">Configure global options...</a>" })
45  public class ProjectSpecificSettingsPanel extends JPanel implements VerifiableConfigPanel {
46  	/** The unique serial version ID. */
47  	private static final long serialVersionUID = 1L;
48  
49  	/** {@link Collection} holding all registered {@link ChangeListener}s. */
50  	private transient final Collection<ChangeListener> changeListeners = new ArrayList<>();
51  
52  	/** The project specific {@link Preferences}. */
53  	private transient final Preferences projectPreferences;
54  
55  	/** The {@link ExternalFormatterPanel}. */
56  	private final ExternalFormatterPanel innerComponent;
57  
58  	/**
59  	 * Package private constructor to create a new instance of {@link ProjectSpecificSettingsPanel}.
60  	 *
61  	 * @param innerComponent the {@link ExternalFormatterPanel}
62  	 * @param projectPreferences the project specific {@link Preferences}
63  	 */
64  	ProjectSpecificSettingsPanel(ExternalFormatterPanel innerComponent, Preferences projectPreferences) {
65  		initComponents();
66  		this.innerComponent = innerComponent;
67  		this.projectPreferences = projectPreferences;
68  
69  		innerPanel.setLayout(new BorderLayout());
70  		innerPanel.add(this.innerComponent, BorderLayout.CENTER);
71  	}
72  
73  	/**
74  	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
75  	 */
76  	@SuppressWarnings("unchecked")
77      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
78      private void initComponents() {
79  
80          scrollPane = new JScrollPane();
81          scrollContainer = new JPanel();
82          headerPanel = new JPanel();
83          cbOverrideGlobalSettings = new JCheckBox();
84          lblJumpToGlobalOptions = new JLabel();
85          innerPanel = new JPanel();
86  
87          setLayout(new BorderLayout());
88  
89          scrollPane.setPreferredSize(new Dimension(100, 200));
90  
91          scrollContainer.setLayout(new BorderLayout());
92  
93          headerPanel.setLayout(new BorderLayout());
94  
95          Mnemonics.setLocalizedText(cbOverrideGlobalSettings, NbBundle.getMessage(ProjectSpecificSettingsPanel.class, "ProjectSpecificSettingsPanel.cbOverrideGlobalSettings.text")); // NOI18N
96          cbOverrideGlobalSettings.addActionListener(new ActionListener() {
97              public void actionPerformed(ActionEvent evt) {
98                  cbOverrideGlobalSettingsActionPerformed(evt);
99              }
100         });
101         headerPanel.add(cbOverrideGlobalSettings, BorderLayout.WEST);
102 
103         lblJumpToGlobalOptions.setHorizontalAlignment(SwingConstants.RIGHT);
104         Mnemonics.setLocalizedText(lblJumpToGlobalOptions, NbBundle.getMessage(ProjectSpecificSettingsPanel.class, "ProjectSpecificSettingsPanel.lblJumpToGlobalOptions.text")); // NOI18N
105         lblJumpToGlobalOptions.addMouseListener(new MouseAdapter() {
106             public void mouseClicked(MouseEvent evt) {
107                 lblJumpToGlobalOptionsMouseClicked(evt);
108             }
109         });
110         headerPanel.add(lblJumpToGlobalOptions, BorderLayout.EAST);
111 
112         scrollContainer.add(headerPanel, BorderLayout.NORTH);
113         scrollContainer.add(innerPanel, BorderLayout.CENTER);
114 
115         scrollPane.setViewportView(scrollContainer);
116 
117         add(scrollPane, BorderLayout.CENTER);
118     }// </editor-fold>//GEN-END:initComponents
119 
120 	private void cbOverrideGlobalSettingsActionPerformed(ActionEvent evt) {//GEN-FIRST:event_cbOverrideGlobalSettingsActionPerformed
121 		innerPanel.setVisible(cbOverrideGlobalSettings.isSelected());
122 		fireStateChanged();
123 	}//GEN-LAST:event_cbOverrideGlobalSettingsActionPerformed
124 
125 	private void lblJumpToGlobalOptionsMouseClicked(MouseEvent evt) {//GEN-FIRST:event_lblJumpToGlobalOptionsMouseClicked
126 		OptionsDisplayer.getDefault().open("Editor/de.funfried.netbeans.plugins.external.formatter.ui.options");
127 	}//GEN-LAST:event_lblJumpToGlobalOptionsMouseClicked
128 
129     // Variables declaration - do not modify//GEN-BEGIN:variables
130     private JCheckBox cbOverrideGlobalSettings;
131     private JPanel headerPanel;
132     private JPanel innerPanel;
133     private JLabel lblJumpToGlobalOptions;
134     private JPanel scrollContainer;
135     private JScrollPane scrollPane;
136     // End of variables declaration//GEN-END:variables
137 
138 	/**
139 	 * {@inheritDoc}
140 	 */
141 	@Override
142 	public void load() {
143 		boolean useProjectSettings = projectPreferences.getBoolean(Settings.USE_PROJECT_SETTINGS, false);
144 		cbOverrideGlobalSettings.setSelected(useProjectSettings);
145 		innerPanel.setVisible(useProjectSettings);
146 	}
147 
148 	/**
149 	 * {@inheritDoc}
150 	 */
151 	@Override
152 	public void store() {
153 		projectPreferences.putBoolean(Settings.USE_PROJECT_SETTINGS, cbOverrideGlobalSettings.isSelected());
154 	}
155 
156 	/**
157 	 * Adds a {@link ChangeListener} to this {@link ProjectSpecificSettingsPanel}.
158 	 *
159 	 * @param listener the {@link ChangeListener}
160 	 */
161 	public void addChangeListener(ChangeListener listener) {
162 		changeListeners.add(listener);
163 	}
164 
165 	private void fireStateChanged() {
166 		changeListeners.forEach((changeListener) -> {
167 			changeListener.stateChanged(null);
168 		});
169 	}
170 
171 	/**
172 	 * Removes a {@link ChangeListener} to this {@link ProjectSpecificSettingsPanel}.
173 	 *
174 	 * @param listener the {@link ChangeListener}
175 	 */
176 	public void removeChangeListener(ChangeListener listener) {
177 		changeListeners.remove(listener);
178 	}
179 
180 	/**
181 	 * {@inheritDoc}
182 	 */
183 	@Override
184 	public boolean valid() {
185 		boolean result = true;
186 		if (cbOverrideGlobalSettings.isSelected()) {
187 			return innerComponent.valid();
188 		}
189 
190 		return result;
191 	}
192 }