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.options;
12  
13  import java.beans.PropertyChangeListener;
14  import java.beans.PropertyChangeSupport;
15  import java.util.prefs.Preferences;
16  
17  import javax.swing.SwingUtilities;
18  import javax.swing.event.ChangeEvent;
19  import javax.swing.event.ChangeListener;
20  
21  import org.netbeans.spi.options.OptionsPanelController;
22  import org.openide.util.HelpCtx;
23  import org.openide.util.Lookup;
24  import org.openide.util.NbPreferences;
25  import org.openide.util.WeakListeners;
26  
27  /**
28   * {@link OptionsPanelController} implementation for the {@link ExternalFormatterPanel}.
29   *
30   * @author markiewb
31   * @author bahlef
32   */
33  @OptionsPanelController.SubRegistration(id = "de.funfried.netbeans.plugins.external.formatter.ui.options", location = "Editor", position = 201, displayName = "#AdvancedOption_DisplayName_ExternalFormatter", keywords = "#AdvancedOption_Keywords_ExternalFormatter", keywordsCategory = "Editor/ExternalFormatter")
34  @org.openide.util.NbBundle.Messages({ "AdvancedOption_DisplayName_ExternalFormatter=External Formatter", "AdvancedOption_Keywords_ExternalFormatter=External Formatter" })
35  public final class ExternalFormatterOptionsPanelController extends OptionsPanelController implements ChangeListener {
36  	/** Holder of the {@link ExternalFormatterPanel}. */
37  	private ExternalFormatterPanel panel;
38  
39  	/** Holder of the {@link PropertyChangeSupport}. */
40  	private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
41  
42  	/** Flag which determines if options were changed. */
43  	private boolean changed;
44  
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	@Override
49  	public void update() {
50  		createOrGetPanel().load();
51  		changed = false;
52  	}
53  
54  	/**
55  	 * {@inheritDoc}
56  	 */
57  	@Override
58  	public void applyChanges() {
59  		SwingUtilities.invokeLater(() -> {
60  			createOrGetPanel().store();
61  			changed = false;
62  		});
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	@Override
69  	public void cancel() {
70  		// need not do anything special, if no changes have been persisted yet
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	@Override
77  	public boolean isValid() {
78  		return createOrGetPanel().valid();
79  	}
80  
81  	/**
82  	 * {@inheritDoc}
83  	 */
84  	@Override
85  	public boolean isChanged() {
86  		return changed;
87  	}
88  
89  	/**
90  	 * {@inheritDoc}
91  	 */
92  	@Override
93  	public HelpCtx getHelpCtx() {
94  		return null;
95  	}
96  
97  	/**
98  	 * {@inheritDoc}
99  	 */
100 	@Override
101 	public ExternalFormatterPanel getComponent(Lookup masterLookup) {
102 		return createOrGetPanel();
103 	}
104 
105 	/**
106 	 * Returns the cached {@link ExternalFormatterPanel} or creates a new one and caches it for the next call.
107 	 *
108 	 * @return the cached {@link ExternalFormatterPanel} or creates a new one and caches it for the next call
109 	 */
110 	private ExternalFormatterPanel createOrGetPanel() {
111 		if (null == panel) {
112 			Preferences globalPreferences = NbPreferences.forModule(ExternalFormatterPanel.class);
113 			panel = new ExternalFormatterPanel(globalPreferences, null);
114 			panel.addChangeListener(WeakListeners.change(this, panel));
115 		}
116 
117 		return panel;
118 	}
119 
120 	/**
121 	 * {@inheritDoc}
122 	 */
123 	@Override
124 	public void addPropertyChangeListener(PropertyChangeListener l) {
125 		pcs.addPropertyChangeListener(l);
126 	}
127 
128 	/**
129 	 * {@inheritDoc}
130 	 */
131 	@Override
132 	public void removePropertyChangeListener(PropertyChangeListener l) {
133 		pcs.removePropertyChangeListener(l);
134 	}
135 
136 	/**
137 	 * {@inheritDoc}
138 	 */
139 	@Override
140 	public void stateChanged(ChangeEvent e) {
141 		if (!changed) {
142 			changed = true;
143 			pcs.firePropertyChange(OptionsPanelController.PROP_CHANGED, false, true);
144 		}
145 
146 		pcs.firePropertyChange(OptionsPanelController.PROP_VALID, null, null);
147 	}
148 }