View Javadoc
1   /*
2    * Copyright (c) 2022 Airtango.
3    * All rights reserved.
4    */
5   
6   package de.funfried.netbeans.plugins.editor.closeleftright.options;
7   
8   import java.awt.event.ActionEvent;
9   import java.util.Collection;
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  import javax.swing.JCheckBox;
14  import javax.swing.event.ChangeListener;
15  
16  import org.openide.util.ChangeSupport;
17  import org.openide.util.Lookup;
18  
19  import de.funfried.netbeans.plugins.editor.closeleftright.AdditionalCloseActionFactory;
20  
21  final class AdditionalCloseActionsPanel extends javax.swing.JPanel {
22  	private static final long serialVersionUID = 4588982272030513399L;
23  
24  	private final Map<AdditionalCloseActionFactory, JCheckBox> actionChkBoxes = new HashMap<>();
25  
26  	/** {@link ChangeSupport} to notify about changed preference components. */
27  	private final ChangeSupport changeSupport;
28  
29  	AdditionalCloseActionsPanel() {
30  		this.changeSupport = new ChangeSupport(this);
31  
32  		initComponents();
33  		generateComponents();
34  	}
35  
36  	/**
37  	 * {@inheritDoc}
38  	 */
39  	public void addChangeListener(ChangeListener listener) {
40  		changeSupport.addChangeListener(listener);
41  	}
42  
43  	/**
44  	 * {@inheritDoc}
45  	 */
46  	public void removeChangeListener(ChangeListener listener) {
47  		changeSupport.removeChangeListener(listener);
48  	}
49  
50  	/**
51  	 * Fires a change event to all registered {@link ChangeListener}s.
52  	 */
53  	protected void fireChangedListener() {
54  		changeSupport.fireChange();
55  	}
56  
57  	private void updateSelectAllChkBox() {
58  		boolean allSelected = true;
59  
60  		for (AdditionalCloseActionFactory additionalCloseActionFactory : actionChkBoxes.keySet()) {
61  			JCheckBox actionChkBox = actionChkBoxes.get(additionalCloseActionFactory);
62  			if (!actionChkBox.isSelected()) {
63  				allSelected = false;
64  				break;
65  			}
66  		}
67  
68  		selectAllChkBox.setSelected(allSelected);
69  	}
70  
71  	private void generateComponents() {
72  		Collection<? extends AdditionalCloseActionFactory> additionalCloseActionFactories = Lookup.getDefault().lookupAll(AdditionalCloseActionFactory.class);
73  		for (AdditionalCloseActionFactory additionalCloseActionFactory : additionalCloseActionFactories) {
74  			JCheckBox actionChkBox = new JCheckBox(additionalCloseActionFactory.getName() + (!additionalCloseActionFactory.isGlobalAction() ? " (Editor context menu only)" : ""));
75  			actionChkBox.addActionListener((ActionEvent e) -> {
76  				fireChangedListener();
77  
78  				updateSelectAllChkBox();
79  			});
80  
81  			actionChkBoxesPanel.add(actionChkBox);
82  
83  			actionChkBoxes.put(additionalCloseActionFactory, actionChkBox);
84  		}
85  	}
86  
87  	/**
88  	 * This method is called from within the constructor to
89  	 * initialize the form.
90  	 * WARNING: Do NOT modify this code. The content of this method is
91  	 * always regenerated by the Form Editor.
92  	 */
93      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
94      private void initComponents() {
95  
96          selectAllChkBox = new javax.swing.JCheckBox();
97          descriptionLbl = new javax.swing.JLabel();
98          actionChkBoxesPanel = new javax.swing.JPanel();
99  
100         org.openide.awt.Mnemonics.setLocalizedText(selectAllChkBox, org.openide.util.NbBundle.getMessage(AdditionalCloseActionsPanel.class, "AdditionalCloseActionsPanel.selectAllChkBox.text")); // NOI18N
101         selectAllChkBox.addActionListener(new java.awt.event.ActionListener() {
102             public void actionPerformed(java.awt.event.ActionEvent evt) {
103                 selectAllChkBoxActionPerformed(evt);
104             }
105         });
106 
107         org.openide.awt.Mnemonics.setLocalizedText(descriptionLbl, org.openide.util.NbBundle.getMessage(AdditionalCloseActionsPanel.class, "AdditionalCloseActionsPanel.descriptionLbl.text")); // NOI18N
108 
109         actionChkBoxesPanel.setLayout(new javax.swing.BoxLayout(actionChkBoxesPanel, javax.swing.BoxLayout.Y_AXIS));
110 
111         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
112         this.setLayout(layout);
113         layout.setHorizontalGroup(
114             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
115             .addGroup(layout.createSequentialGroup()
116                 .addContainerGap()
117                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
118                     .addComponent(actionChkBoxesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
119                     .addGroup(layout.createSequentialGroup()
120                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
121                             .addComponent(selectAllChkBox)
122                             .addComponent(descriptionLbl))
123                         .addGap(0, 42, Short.MAX_VALUE)))
124                 .addContainerGap())
125         );
126         layout.setVerticalGroup(
127             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
128             .addGroup(layout.createSequentialGroup()
129                 .addContainerGap()
130                 .addComponent(descriptionLbl)
131                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
132                 .addComponent(selectAllChkBox)
133                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
134                 .addComponent(actionChkBoxesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
135                 .addContainerGap())
136         );
137     }// </editor-fold>//GEN-END:initComponents
138 
139     private void selectAllChkBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllChkBoxActionPerformed
140 		for (AdditionalCloseActionFactory additionalCloseActionFactory : actionChkBoxes.keySet()) {
141 			JCheckBox actionChkBox = actionChkBoxes.get(additionalCloseActionFactory);
142 			actionChkBox.setSelected(selectAllChkBox.isSelected());
143 
144 			fireChangedListener();
145 		}
146     }//GEN-LAST:event_selectAllChkBoxActionPerformed
147 
148 		void load() {
149 			for (AdditionalCloseActionFactory additionalCloseActionFactory : actionChkBoxes.keySet()) {
150 				JCheckBox actionChkBox = actionChkBoxes.get(additionalCloseActionFactory);
151 				actionChkBox.setSelected(additionalCloseActionFactory.isActive());
152 			}
153 
154 			updateSelectAllChkBox();
155 		}
156 
157 		void store() {
158 			for (AdditionalCloseActionFactory additionalCloseActionFactory : actionChkBoxes.keySet()) {
159 				JCheckBox actionChkBox = actionChkBoxes.get(additionalCloseActionFactory);
160 				additionalCloseActionFactory.setActive(actionChkBox.isSelected());
161 			}
162 		}
163 
164 		boolean valid() {
165 			return true;
166 		}
167 
168     // Variables declaration - do not modify//GEN-BEGIN:variables
169     private javax.swing.JPanel actionChkBoxesPanel;
170     private javax.swing.JLabel descriptionLbl;
171     private javax.swing.JCheckBox selectAllChkBox;
172     // End of variables declaration//GEN-END:variables
173 
174 	}