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: * ExportImportDataSourcesDialog.java
043: *
044: * Created on March 8, 2004, 12:09 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.ui;
048:
049: import java.awt.Component;
050: import java.awt.event.KeyAdapter;
051: import java.awt.event.KeyEvent;
052: import java.awt.event.MouseAdapter;
053: import java.awt.event.MouseEvent;
054:
055: import javax.swing.JCheckBox;
056: import javax.swing.JList;
057: import javax.swing.JPanel;
058: import javax.swing.ListCellRenderer;
059: import javax.swing.ListSelectionModel;
060:
061: /**
062: * A panel used to allow the user to select EJB datasources for exporting or importing
063: *
064: * @author cao
065: */
066: public class EjbDataSourcesSelectionPanel extends JPanel {
067:
068: private EjbDataSourcePropertiesPanel propsPanel;
069:
070: private PortableEjbDataSource[] ejbDataSources;
071:
072: private int prevSelectedIndex = 0;
073:
074: public EjbDataSourcesSelectionPanel(
075: EjbDataSourcePropertiesPanel propsPanel) {
076: initComponents();
077:
078: this .propsPanel = propsPanel;
079: listScrollPane
080: .setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
081: datasourceList.setCellRenderer(new CustomListRenderer());
082: datasourceList
083: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
084: datasourceList.addMouseListener(new CustomMouseListener());
085: datasourceList.addKeyListener(new CustomKeyListener());
086: selectAllButton.setEnabled(false);
087: clearAllButton.setEnabled(false);
088: }
089:
090: public void clear() {
091: datasourceList.setListData(new Object[] {});
092: datasourceList.repaint();
093: propsPanel.clear();
094: }
095:
096: /**
097: * This method fills the list with the given EJB groups
098: */
099: public void setEjbDataSources(PortableEjbDataSource[] ejbDataSources) {
100: this .ejbDataSources = ejbDataSources;
101:
102: if (ejbDataSources != null && ejbDataSources.length > 0) {
103: propsPanel.setDataSourceProperties(ejbDataSources[0]
104: .getEjbGroup());
105: datasourceList.setListData(ejbDataSources);
106: datasourceList.setSelectedIndex(0);
107: selectAllButton.setEnabled(true);
108: clearAllButton.setEnabled(true);
109: }
110:
111: }
112:
113: /**
114: * Called to repaint the list after changing the name of the Datasource Config Info
115: */
116: public void repaintList() {
117: datasourceList.repaint();
118: }
119:
120: /**
121: * This method is called from within the constructor to initialize the form. WARNING: Do NOT
122: * modify this code. The content of this method is always regenerated by the Form Editor.
123: */
124: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
125: private void initComponents() {
126: java.awt.GridBagConstraints gridBagConstraints;
127:
128: listScrollPane = new javax.swing.JScrollPane();
129: datasourceList = new javax.swing.JList();
130: buttonPanel = new javax.swing.JPanel();
131: selectAllButton = new javax.swing.JButton();
132: clearAllButton = new javax.swing.JButton();
133: datasourceLabel = new javax.swing.JLabel();
134:
135: setLayout(new java.awt.GridBagLayout());
136:
137: listScrollPane.setBorder(null);
138: listScrollPane.setMinimumSize(new java.awt.Dimension(200, 130));
139:
140: datasourceList
141: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
142: datasourceList.setMaximumSize(null);
143: datasourceList.setMinimumSize(new java.awt.Dimension(500, 500));
144: datasourceList.setPreferredSize(null);
145: datasourceList
146: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
147: public void valueChanged(
148: javax.swing.event.ListSelectionEvent evt) {
149: datasourceListValueChanged(evt);
150: }
151: });
152: listScrollPane.setViewportView(datasourceList);
153: java.util.ResourceBundle bundle = java.util.ResourceBundle
154: .getBundle("org/netbeans/modules/visualweb/ejb/ui/Bundle"); // NOI18N
155: datasourceList.getAccessibleContext().setAccessibleName(
156: bundle.getString("EJB_GROUPS")); // NOI18N
157: datasourceList.getAccessibleContext().setAccessibleDescription(
158: bundle.getString("EJB_GROUPS")); // NOI18N
159:
160: gridBagConstraints = new java.awt.GridBagConstraints();
161: gridBagConstraints.gridx = 0;
162: gridBagConstraints.gridy = 1;
163: gridBagConstraints.gridwidth = 2;
164: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
165: gridBagConstraints.weightx = 1.0;
166: gridBagConstraints.weighty = 1.0;
167: add(listScrollPane, gridBagConstraints);
168:
169: buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 5, 0));
170:
171: selectAllButton.setMnemonic(java.util.ResourceBundle.getBundle(
172: "org/netbeans/modules/visualweb/ejb/ui/Bundle")
173: .getString("SELECT_ALL_BUTTON_MNEMONIC").charAt(0));
174: selectAllButton.setText(bundle
175: .getString("SELECT_ALL_BUTTON_LABEL")); // NOI18N
176: selectAllButton.setPreferredSize(null);
177: selectAllButton
178: .addActionListener(new java.awt.event.ActionListener() {
179: public void actionPerformed(
180: java.awt.event.ActionEvent evt) {
181: selectAllButtonActionPerformed(evt);
182: }
183: });
184: buttonPanel.add(selectAllButton);
185: selectAllButton.getAccessibleContext()
186: .setAccessibleDescription(
187: bundle.getString("SELECT_ALL_BUTTON_DESC")); // NOI18N
188:
189: clearAllButton.setMnemonic(java.util.ResourceBundle.getBundle(
190: "org/netbeans/modules/visualweb/ejb/ui/Bundle")
191: .getString("CLEAR_BUTTON_MNEMONIC").charAt(0));
192: clearAllButton.setText(bundle.getString("CLEAR_BUTTON_LABEL")); // NOI18N
193: clearAllButton.setPreferredSize(null);
194: clearAllButton
195: .addActionListener(new java.awt.event.ActionListener() {
196: public void actionPerformed(
197: java.awt.event.ActionEvent evt) {
198: clearAllButtonActionPerformed(evt);
199: }
200: });
201: buttonPanel.add(clearAllButton);
202: clearAllButton.getAccessibleContext().setAccessibleDescription(
203: bundle.getString("CLEAR_BUTTON_DESC")); // NOI18N
204:
205: gridBagConstraints = new java.awt.GridBagConstraints();
206: gridBagConstraints.gridx = 1;
207: gridBagConstraints.gridy = 2;
208: gridBagConstraints.gridwidth = 2;
209: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
210: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
211: add(buttonPanel, gridBagConstraints);
212:
213: datasourceLabel.setLabelFor(datasourceList);
214: org.openide.awt.Mnemonics
215: .setLocalizedText(datasourceLabel,
216: org.openide.util.NbBundle.getMessage(
217: EjbDataSourcesSelectionPanel.class,
218: "EJB_SETS")); // NOI18N
219: gridBagConstraints = new java.awt.GridBagConstraints();
220: gridBagConstraints.gridx = 0;
221: gridBagConstraints.gridy = 0;
222: add(datasourceLabel, gridBagConstraints);
223: datasourceLabel.getAccessibleContext()
224: .setAccessibleDescription(
225: org.openide.util.NbBundle.getMessage(
226: EjbDataSourcesSelectionPanel.class,
227: "EJB_SETS")); // NOI18N
228: }// </editor-fold>//GEN-END:initComponents
229:
230: private void datasourceListValueChanged(
231: javax.swing.event.ListSelectionEvent evt) {// GEN-FIRST:event_datasourceListValueChanged
232:
233: int index = ((JList) evt.getSource()).getSelectedIndex();
234: if (index == prevSelectedIndex)
235: return;
236:
237: // Save the changes the user has made
238: if (!propsPanel.saveChange()) {
239: datasourceList.setSelectedIndex(prevSelectedIndex);
240: } else
241: prevSelectedIndex = index;
242: }// GEN-LAST:event_datasourceListValueChanged
243:
244: private void clearAllButtonActionPerformed(
245: java.awt.event.ActionEvent evt) {// GEN-FIRST:event_clearAllButtonActionPerformed
246:
247: if (ejbDataSources != null) {
248: for (int i = 0; i < ejbDataSources.length; i++)
249: ejbDataSources[i].setIsPortable(false);
250:
251: datasourceList.repaint();
252: }
253: }// GEN-LAST:event_clearAllButtonActionPerformed
254:
255: private void selectAllButtonActionPerformed(
256: java.awt.event.ActionEvent evt) {// GEN-FIRST:event_selectAllButtonActionPerformed
257:
258: if (ejbDataSources != null) {
259: for (int i = 0; i < ejbDataSources.length; i++)
260: ejbDataSources[i].setIsPortable(true);
261:
262: datasourceList.repaint();
263: }
264: }// GEN-LAST:event_selectAllButtonActionPerformed
265:
266: class CustomListRenderer extends JCheckBox implements
267: ListCellRenderer {
268:
269: public CustomListRenderer() {
270: setHorizontalAlignment(JCheckBox.LEFT);
271: setVerticalAlignment(JCheckBox.CENTER);
272: }
273:
274: public Component getListCellRendererComponent(JList list,
275: Object value, int index, boolean isSelected,
276: boolean cellHasFocus) {
277:
278: if (isSelected) {
279: setBackground(list.getSelectionBackground());
280: setForeground(list.getSelectionForeground());
281: } else {
282: setBackground(list.getBackground());
283: setForeground(list.getForeground());
284: }
285:
286: setText(((PortableEjbDataSource) value).getName());
287:
288: if (((PortableEjbDataSource) value).isPortable())
289: setSelected(true);
290: else
291: setSelected(false);
292:
293: return this ;
294: }
295: }
296:
297: /**
298: * Let us have our own mouse listener to change the checkbox selection since checkbox will never
299: * get the mouse events for itself to render.
300: */
301: class CustomMouseListener extends MouseAdapter {
302:
303: public void mouseClicked(MouseEvent e) {
304: JList list = (JList) e.getSource();
305: int index = list.getSelectedIndex();
306: if (index < 0)
307: return;
308: Object selection = list.getModel().getElementAt(index);
309: if (e.getX() < 20) {
310:
311: // Check it if it is not checked
312: // Uncheck it if it is checked
313: PortableEjbDataSource ejbSrc = (PortableEjbDataSource) selection;
314: if (ejbSrc.isPortable())
315: ejbSrc.setIsPortable(false);
316: else
317: ejbSrc.setIsPortable(true);
318:
319: list.repaint();
320: }
321: propsPanel
322: .setDataSourceProperties(((PortableEjbDataSource) selection)
323: .getEjbGroup());
324: }
325: }
326:
327: private static class CustomKeyListener extends KeyAdapter {
328: @Override
329: public void keyReleased(KeyEvent e) {
330: if (e.getKeyCode() == KeyEvent.VK_SPACE) {
331: JList list = (JList) e.getSource();
332: int index = list.getSelectedIndex();
333: if (index != -1) {
334: // Toggle the selection state
335: PortableEjbDataSource ejbSrc = (PortableEjbDataSource) list
336: .getModel().getElementAt(index);
337: ejbSrc.setIsPortable(!ejbSrc.isPortable());
338: list.repaint();
339: }
340: }
341: }
342: }
343:
344: // Variables declaration - do not modify//GEN-BEGIN:variables
345: private javax.swing.JPanel buttonPanel;
346: private javax.swing.JButton clearAllButton;
347: private javax.swing.JLabel datasourceLabel;
348: private javax.swing.JList datasourceList;
349: private javax.swing.JScrollPane listScrollPane;
350: private javax.swing.JButton selectAllButton;
351: // End of variables declaration//GEN-END:variables
352:
353: }
|