001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.makeproject.ui.utils;
043:
044: import java.awt.GridBagConstraints;
045: import java.util.ResourceBundle;
046: import java.util.Vector;
047: import javax.swing.JButton;
048: import javax.swing.JCheckBox;
049: import org.netbeans.modules.cnd.makeproject.api.configurations.Configuration;
050: import org.openide.util.NbBundle;
051:
052: public class ConfSelectorPanel extends javax.swing.JPanel {
053:
054: private static Configuration[] lastConfigurationItems;
055: private static JCheckBox[] lastCheckBoxes;
056: private Configuration[] configurationItems;
057: private JCheckBox[] checkBoxes;
058:
059: JButton[] actionButtons;
060:
061: public ConfSelectorPanel(String labelText, char mn,
062: Configuration[] configurationItems, JButton[] actionButtons) {
063: initComponents();
064: GridBagConstraints gridBagConstraints;
065:
066: this .configurationItems = configurationItems;
067: this .actionButtons = actionButtons;
068:
069: // Set the label
070: label.setText(labelText);
071: label.setDisplayedMnemonic(mn);
072:
073: // Add the comboboxes
074: CheckBoxActionListener checkBoxActionListener = new CheckBoxActionListener();
075: checkBoxes = new JCheckBox[configurationItems.length];
076: for (int i = 0; i < configurationItems.length; i++) {
077: JCheckBox checkBox = new JCheckBox();
078: checkBox.addActionListener(checkBoxActionListener);
079: checkBox.setBackground(new java.awt.Color(255, 255, 255));
080: checkBox.setText(configurationItems[i].toString());
081: if (sameAsLastTime(configurationItems))
082: checkBox.setSelected(lastCheckBoxes[i].isSelected());
083: else
084: checkBox.setSelected(true);
085: gridBagConstraints = new java.awt.GridBagConstraints();
086: gridBagConstraints.gridx = 0;
087: gridBagConstraints.gridy = i;
088: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
089: if (i == configurationItems.length - 1) {
090: gridBagConstraints.weightx = 1.0;
091: gridBagConstraints.weighty = 1.0;
092: }
093: innerPanel.add(checkBox, gridBagConstraints);
094: checkBoxes[i] = checkBox;
095: checkBox.getAccessibleContext()
096: .setAccessibleDescription(""); // NOI18N
097: }
098:
099: // Add the action buttons
100: if (actionButtons != null) {
101: gridBagConstraints = new java.awt.GridBagConstraints();
102: gridBagConstraints.gridx = 0;
103: gridBagConstraints.gridy = 0;
104: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
105: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
106: for (int i = 0; i < actionButtons.length; i++) {
107: gridBagConstraints.gridy++;
108: buttonPanel.add(actionButtons[i], gridBagConstraints);
109: }
110: }
111:
112: // Set background
113: innerPanel.setBackground(new java.awt.Color(255, 255, 255));
114:
115: // Set size
116: setPreferredSize(new java.awt.Dimension(350, 250));
117:
118: // Accessibility
119: getAccessibleContext().setAccessibleDescription(
120: getString("SELECTED_CONF_AD"));
121: label.setDisplayedMnemonic(getString("SELECTED_CONF_MN")
122: .charAt(0));
123: selectAllButton.getAccessibleContext()
124: .setAccessibleDescription(
125: getString("SELECT_ALL_BUTTON_AD"));
126: deselectAllButton.getAccessibleContext()
127: .setAccessibleDescription(
128: getString("DESELECT_ALL_BUTTON_AD"));
129:
130: // Verify any checked
131: checkCheckBoxes();
132: }
133:
134: private boolean sameAsLastTime(Configuration[] configurationItems) {
135: if (lastConfigurationItems == null || lastCheckBoxes == null)
136: return false;
137: if (configurationItems.length != lastConfigurationItems.length)
138: return false;
139: if (configurationItems.length != lastCheckBoxes.length)
140: return false;
141: for (int i = 0; i < configurationItems.length; i++) {
142: if (configurationItems[i] != lastConfigurationItems[i])
143: return false;
144: }
145: return true;
146: }
147:
148: class CheckBoxActionListener implements
149: java.awt.event.ActionListener {
150: public void actionPerformed(java.awt.event.ActionEvent evt) {
151: checkCheckBoxes();
152: }
153: }
154:
155: public void checkCheckBoxes() {
156: boolean oneSelected = false;
157: for (int i = 0; i < checkBoxes.length; i++) {
158: if (checkBoxes[i].isSelected()) {
159: oneSelected = true;
160: break;
161: }
162: }
163: if (actionButtons != null) {
164: for (int i = 0; i < actionButtons.length; i++)
165: actionButtons[i].setEnabled(oneSelected);
166: }
167: }
168:
169: public Configuration[] getSelectedConfs() {
170: lastConfigurationItems = configurationItems;
171: lastCheckBoxes = checkBoxes;
172:
173: Vector vector = new Vector();
174: for (int i = 0; i < configurationItems.length; i++) {
175: if (checkBoxes[i].isSelected())
176: vector.add(configurationItems[i]);
177: }
178:
179: return (Configuration[]) vector
180: .toArray(new Configuration[vector.size()]);
181: }
182:
183: /** This method is called from within the constructor to
184: * initialize the form.
185: * WARNING: Do NOT modify this code. The content of this method is
186: * always regenerated by the Form Editor.
187: */
188: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
189: private void initComponents() {
190: java.awt.GridBagConstraints gridBagConstraints;
191:
192: label = new javax.swing.JLabel();
193: confPanel = new javax.swing.JPanel();
194: scrollPanel = new javax.swing.JScrollPane();
195: innerPanel = new javax.swing.JPanel();
196: buttonPanel = new javax.swing.JPanel();
197: selectAllButton = new javax.swing.JButton();
198: deselectAllButton = new javax.swing.JButton();
199:
200: setLayout(new java.awt.GridBagLayout());
201:
202: label.setDisplayedMnemonic(java.util.ResourceBundle.getBundle(
203: "org/netbeans/modules/cnd/makeproject/ui/utils/Bundle")
204: .getString("SELECTED_CONF_MN").charAt(0));
205: label.setLabelFor(innerPanel);
206: label.setText(java.util.ResourceBundle.getBundle(
207: "org/netbeans/modules/cnd/makeproject/ui/utils/Bundle")
208: .getString("SELECTED_CONF_LBL"));
209: gridBagConstraints = new java.awt.GridBagConstraints();
210: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
211: gridBagConstraints.insets = new java.awt.Insets(12, 12, 2, 0);
212: add(label, gridBagConstraints);
213:
214: confPanel.setLayout(new java.awt.GridBagLayout());
215:
216: innerPanel.setLayout(new java.awt.GridBagLayout());
217:
218: innerPanel.setBackground(new java.awt.Color(255, 255, 255));
219: scrollPanel.setViewportView(innerPanel);
220:
221: gridBagConstraints = new java.awt.GridBagConstraints();
222: gridBagConstraints.gridx = 0;
223: gridBagConstraints.gridy = 0;
224: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
225: gridBagConstraints.weightx = 1.0;
226: gridBagConstraints.weighty = 1.0;
227: confPanel.add(scrollPanel, gridBagConstraints);
228:
229: gridBagConstraints = new java.awt.GridBagConstraints();
230: gridBagConstraints.gridx = 0;
231: gridBagConstraints.gridy = 1;
232: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
233: gridBagConstraints.weightx = 1.0;
234: gridBagConstraints.weighty = 1.0;
235: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
236: add(confPanel, gridBagConstraints);
237:
238: buttonPanel.setLayout(new java.awt.GridBagLayout());
239:
240: selectAllButton.setMnemonic(java.util.ResourceBundle.getBundle(
241: "org/netbeans/modules/cnd/makeproject/ui/utils/Bundle")
242: .getString("SELECT_ALL_BUTTON_MN").charAt(0));
243: selectAllButton.setText(java.util.ResourceBundle.getBundle(
244: "org/netbeans/modules/cnd/makeproject/ui/utils/Bundle")
245: .getString("SELECT_ALL_BUTTON_TXT"));
246: selectAllButton
247: .addActionListener(new java.awt.event.ActionListener() {
248: public void actionPerformed(
249: java.awt.event.ActionEvent evt) {
250: selectAllButtonActionPerformed(evt);
251: }
252: });
253:
254: gridBagConstraints = new java.awt.GridBagConstraints();
255: gridBagConstraints.gridx = 0;
256: gridBagConstraints.gridy = 5;
257: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
258: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
259: buttonPanel.add(selectAllButton, gridBagConstraints);
260:
261: deselectAllButton
262: .setMnemonic(java.util.ResourceBundle
263: .getBundle(
264: "org/netbeans/modules/cnd/makeproject/ui/utils/Bundle")
265: .getString("DESELECT_ALL_BUTTON_MN").charAt(0));
266: deselectAllButton.setText(java.util.ResourceBundle.getBundle(
267: "org/netbeans/modules/cnd/makeproject/ui/utils/Bundle")
268: .getString("DESELECT_ALL_BUTTON_TXT"));
269: deselectAllButton
270: .addActionListener(new java.awt.event.ActionListener() {
271: public void actionPerformed(
272: java.awt.event.ActionEvent evt) {
273: deselectAllButtonActionPerformed(evt);
274: }
275: });
276:
277: gridBagConstraints = new java.awt.GridBagConstraints();
278: gridBagConstraints.gridx = 0;
279: gridBagConstraints.gridy = 6;
280: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
281: buttonPanel.add(deselectAllButton, gridBagConstraints);
282:
283: gridBagConstraints = new java.awt.GridBagConstraints();
284: gridBagConstraints.gridx = 1;
285: gridBagConstraints.gridy = 1;
286: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
287: gridBagConstraints.weighty = 1.0;
288: gridBagConstraints.insets = new java.awt.Insets(0, 6, 12, 12);
289: add(buttonPanel, gridBagConstraints);
290:
291: }// </editor-fold>//GEN-END:initComponents
292:
293: private void deselectAllButtonActionPerformed(
294: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectAllButtonActionPerformed
295: for (int i = 0; i < checkBoxes.length; i++) {
296: checkBoxes[i].setSelected(false);
297: }
298: checkCheckBoxes();
299: }//GEN-LAST:event_deselectAllButtonActionPerformed
300:
301: private void selectAllButtonActionPerformed(
302: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
303: for (int i = 0; i < checkBoxes.length; i++) {
304: checkBoxes[i].setSelected(true);
305: }
306: checkCheckBoxes();
307: }//GEN-LAST:event_selectAllButtonActionPerformed
308:
309: // Variables declaration - do not modify//GEN-BEGIN:variables
310: private javax.swing.JPanel buttonPanel;
311: private javax.swing.JPanel confPanel;
312: private javax.swing.JButton deselectAllButton;
313: private javax.swing.JPanel innerPanel;
314: private javax.swing.JLabel label;
315: private javax.swing.JScrollPane scrollPanel;
316: private javax.swing.JButton selectAllButton;
317: // End of variables declaration//GEN-END:variables
318:
319: /** Look up i18n strings here */
320: private static ResourceBundle bundle;
321:
322: private static String getString(String s) {
323: if (bundle == null) {
324: bundle = NbBundle.getBundle(ConfSelectorPanel.class);
325: }
326: return bundle.getString(s);
327: }
328: }
|