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-2006 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: package org.netbeans.modules.form.j2ee.wizard;
042:
043: import java.awt.Component;
044: import java.io.IOException;
045: import java.sql.Connection;
046: import java.sql.ResultSet;
047: import java.util.*;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import javax.swing.*;
051: import javax.swing.event.*;
052: import org.netbeans.api.db.explorer.ConnectionManager;
053: import org.netbeans.api.db.explorer.DatabaseConnection;
054: import org.netbeans.api.db.explorer.support.DatabaseExplorerUIs;
055: import org.netbeans.api.java.classpath.ClassPath;
056: import org.netbeans.modules.form.j2ee.J2EEUtils;
057: import org.openide.WizardDescriptor;
058: import org.openide.filesystems.FileObject;
059: import org.openide.loaders.DataFolder;
060: import org.openide.loaders.TemplateWizard;
061: import org.openide.util.HelpCtx;
062: import org.openide.util.NbBundle;
063: import org.openide.util.Utilities;
064:
065: /**
066: * Wizard panel for master information of master/detail wizard.
067: *
068: * @author Jan Stola
069: */
070: public class MasterPanel implements WizardDescriptor.Panel {
071: /** Determines whether we can proceed to the next panel. */
072: private boolean valid;
073: /** List of <code>ChangeListener</code> objects. */
074: private EventListenerList listenerList;
075: /** Determines whether the master/detail form is created as a part of a new project. */
076: private boolean inNewProject;
077:
078: /**
079: * Creates new <code>MasterPanel</code>.
080: *
081: * @param inNewProject determines whether the master/detail form is created
082: * as a part of a new project.
083: */
084: public MasterPanel(boolean inNewProject) {
085: this .inNewProject = inNewProject;
086: }
087:
088: /**
089: * Initializes GUI of this panel.
090: */
091: private void initGUI() {
092: initComponents();
093: initLists();
094: DatabaseExplorerUIs.connect(connectionCombo, ConnectionManager
095: .getDefault());
096: }
097:
098: /**
099: * Initializes lists of columns.
100: */
101: private void initLists() {
102: availableList.setModel(new DefaultListModel());
103: includeList.setModel(new DefaultListModel());
104: ListDataListener listener = new ListDataListener() {
105: public void intervalAdded(ListDataEvent e) {
106: contentsChanged(e);
107: }
108:
109: public void intervalRemoved(ListDataEvent e) {
110: contentsChanged(e);
111: }
112:
113: public void contentsChanged(ListDataEvent e) {
114: Object source = e.getSource();
115: if (source == availableList.getModel()) {
116: addAllButton.setEnabled(availableList.getModel()
117: .getSize() != 0);
118: } else if (source == includeList.getModel()) {
119: boolean empty = includeList.getModel().getSize() == 0;
120: removeAllButton.setEnabled(!empty);
121: setValid(!empty);
122: }
123: }
124: };
125: availableList.getModel().addListDataListener(listener);
126: includeList.getModel().addListDataListener(listener);
127: }
128:
129: /** This method is called from within the constructor to
130: * initialize the form.
131: * WARNING: Do NOT modify this code. The content of this method is
132: * always regenerated by the Form Editor.
133: */
134: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
135: private void initComponents() {
136:
137: masterPanel = new javax.swing.JPanel();
138: connectionCombo = new javax.swing.JComboBox();
139: connectionLabel = new javax.swing.JLabel();
140: tableLabel = new javax.swing.JLabel();
141: tableCombo = new javax.swing.JComboBox();
142: availableLabel = new javax.swing.JLabel();
143: availablePane = new javax.swing.JScrollPane();
144: availableList = new javax.swing.JList();
145: addAllButton = new javax.swing.JButton();
146: addButton = new javax.swing.JButton();
147: removeButton = new javax.swing.JButton();
148: removeAllButton = new javax.swing.JButton();
149: includePane = new javax.swing.JScrollPane();
150: includeList = new javax.swing.JList();
151: upButton = new javax.swing.JButton();
152: downButton = new javax.swing.JButton();
153: includeLabel = new javax.swing.JLabel();
154:
155: FormListener formListener = new FormListener();
156:
157: masterPanel.setName(org.openide.util.NbBundle.getMessage(
158: MasterPanel.class, "TITLE_MasterPanel")); // NOI18N
159:
160: connectionCombo.addActionListener(formListener);
161:
162: connectionLabel.setLabelFor(connectionCombo);
163: org.openide.awt.Mnemonics.setLocalizedText(connectionLabel,
164: org.openide.util.NbBundle.getMessage(MasterPanel.class,
165: "LBL_DatabaseConnection")); // NOI18N
166:
167: tableLabel.setLabelFor(tableCombo);
168: org.openide.awt.Mnemonics.setLocalizedText(tableLabel,
169: org.openide.util.NbBundle.getMessage(MasterPanel.class,
170: "LBL_DatabaseTable")); // NOI18N
171:
172: tableCombo.setEnabled(false);
173: tableCombo.setRenderer(J2EEUtils.DBColumnInfo.getRenderer());
174: tableCombo.addActionListener(formListener);
175:
176: availableLabel.setLabelFor(availableList);
177: org.openide.awt.Mnemonics.setLocalizedText(availableLabel,
178: org.openide.util.NbBundle.getMessage(MasterPanel.class,
179: "LBL_AvailableColumns")); // NOI18N
180:
181: availableList.addListSelectionListener(formListener);
182: availablePane.setViewportView(availableList);
183: availableList.getAccessibleContext().setAccessibleDescription(
184: org.openide.util.NbBundle.getMessage(MasterPanel.class,
185: "LBL_AvailableColumns_ACSD")); // NOI18N
186:
187: org.openide.awt.Mnemonics.setLocalizedText(addAllButton,
188: org.openide.util.NbBundle.getMessage(MasterPanel.class,
189: "LBL_MasterAddAll")); // NOI18N
190: addAllButton.setEnabled(false);
191: addAllButton.addActionListener(formListener);
192:
193: org.openide.awt.Mnemonics.setLocalizedText(addButton,
194: org.openide.util.NbBundle.getMessage(MasterPanel.class,
195: "LBL_MasterAdd")); // NOI18N
196: addButton.setEnabled(false);
197: addButton.addActionListener(formListener);
198:
199: org.openide.awt.Mnemonics.setLocalizedText(removeButton,
200: org.openide.util.NbBundle.getMessage(MasterPanel.class,
201: "LBL_MasterRemove")); // NOI18N
202: removeButton.setEnabled(false);
203: removeButton.addActionListener(formListener);
204:
205: org.openide.awt.Mnemonics.setLocalizedText(removeAllButton,
206: org.openide.util.NbBundle.getMessage(MasterPanel.class,
207: "LBL_MasterRemoveAll")); // NOI18N
208: removeAllButton.setEnabled(false);
209: removeAllButton.addActionListener(formListener);
210:
211: includeList.addListSelectionListener(formListener);
212: includePane.setViewportView(includeList);
213: includeList.getAccessibleContext().setAccessibleDescription(
214: org.openide.util.NbBundle.getMessage(MasterPanel.class,
215: "LBL_ColumnsToInclude_ACSD")); // NOI18N
216:
217: upButton
218: .setIcon(new javax.swing.ImageIcon(
219: getClass()
220: .getResource(
221: "/org/netbeans/modules/form/j2ee/resources/up.gif")));
222: org.openide.awt.Mnemonics.setLocalizedText(upButton,
223: org.openide.util.NbBundle.getMessage(MasterPanel.class,
224: "LBL_MasterUp")); // NOI18N
225: upButton.setEnabled(false);
226: upButton
227: .setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
228: if (!Utilities.isMac()) {
229: upButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
230: }
231: upButton.addActionListener(formListener);
232:
233: downButton
234: .setIcon(new javax.swing.ImageIcon(
235: getClass()
236: .getResource(
237: "/org/netbeans/modules/form/j2ee/resources/down.gif")));
238: org.openide.awt.Mnemonics.setLocalizedText(downButton,
239: org.openide.util.NbBundle.getMessage(MasterPanel.class,
240: "LBL_MasterDown")); // NOI18N
241: downButton.setEnabled(false);
242: downButton
243: .setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
244: if (!Utilities.isMac()) {
245: downButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
246: }
247: downButton.addActionListener(formListener);
248:
249: includeLabel.setLabelFor(includeList);
250: org.openide.awt.Mnemonics.setLocalizedText(includeLabel,
251: org.openide.util.NbBundle.getMessage(MasterPanel.class,
252: "LBL_ColumnsToInclude")); // NOI18N
253:
254: org.jdesktop.layout.GroupLayout masterPanelLayout = new org.jdesktop.layout.GroupLayout(
255: masterPanel);
256: masterPanel.setLayout(masterPanelLayout);
257: masterPanelLayout
258: .setHorizontalGroup(masterPanelLayout
259: .createParallelGroup(
260: org.jdesktop.layout.GroupLayout.LEADING)
261: .add(
262: masterPanelLayout
263: .createSequentialGroup()
264: .addContainerGap()
265: .add(
266: masterPanelLayout
267: .createParallelGroup(
268: org.jdesktop.layout.GroupLayout.LEADING)
269: .add(
270: masterPanelLayout
271: .createSequentialGroup()
272: .add(
273: masterPanelLayout
274: .createParallelGroup(
275: org.jdesktop.layout.GroupLayout.LEADING)
276: .add(
277: connectionLabel)
278: .add(
279: tableLabel))
280: .addPreferredGap(
281: org.jdesktop.layout.LayoutStyle.RELATED)
282: .add(
283: masterPanelLayout
284: .createParallelGroup(
285: org.jdesktop.layout.GroupLayout.LEADING)
286: .add(
287: tableCombo,
288: 0,
289: 290,
290: Short.MAX_VALUE)
291: .add(
292: connectionCombo,
293: 0,
294: 290,
295: Short.MAX_VALUE)))
296: .add(
297: org.jdesktop.layout.GroupLayout.TRAILING,
298: masterPanelLayout
299: .createSequentialGroup()
300: .add(
301: masterPanelLayout
302: .createParallelGroup(
303: org.jdesktop.layout.GroupLayout.LEADING)
304: .add(
305: availablePane,
306: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
307: 141,
308: Short.MAX_VALUE)
309: .add(
310: availableLabel))
311: .addPreferredGap(
312: org.jdesktop.layout.LayoutStyle.RELATED)
313: .add(
314: masterPanelLayout
315: .createParallelGroup(
316: org.jdesktop.layout.GroupLayout.LEADING,
317: false)
318: .add(
319: addButton,
320: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
321: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
322: Short.MAX_VALUE)
323: .add(
324: addAllButton,
325: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
326: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
327: Short.MAX_VALUE)
328: .add(
329: removeButton,
330: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
331: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
332: Short.MAX_VALUE)
333: .add(
334: removeAllButton))
335: .addPreferredGap(
336: org.jdesktop.layout.LayoutStyle.RELATED)
337: .add(
338: masterPanelLayout
339: .createParallelGroup(
340: org.jdesktop.layout.GroupLayout.LEADING)
341: .add(
342: masterPanelLayout
343: .createSequentialGroup()
344: .add(
345: includePane,
346: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
347: 140,
348: Short.MAX_VALUE)
349: .addPreferredGap(
350: org.jdesktop.layout.LayoutStyle.RELATED)
351: .add(
352: masterPanelLayout
353: .createParallelGroup(
354: org.jdesktop.layout.GroupLayout.LEADING,
355: false)
356: .add(
357: upButton,
358: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
359: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
360: Short.MAX_VALUE)
361: .add(
362: downButton,
363: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
364: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
365: Short.MAX_VALUE)))
366: .add(
367: includeLabel))))
368: .addContainerGap()));
369:
370: masterPanelLayout.linkSize(new java.awt.Component[] {
371: addAllButton, addButton, downButton, removeAllButton,
372: removeButton, upButton },
373: org.jdesktop.layout.GroupLayout.HORIZONTAL);
374:
375: masterPanelLayout
376: .setVerticalGroup(masterPanelLayout
377: .createParallelGroup(
378: org.jdesktop.layout.GroupLayout.LEADING)
379: .add(
380: masterPanelLayout
381: .createSequentialGroup()
382: .addContainerGap()
383: .add(
384: masterPanelLayout
385: .createParallelGroup(
386: org.jdesktop.layout.GroupLayout.BASELINE)
387: .add(
388: connectionLabel)
389: .add(
390: connectionCombo,
391: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
392: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
393: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
394: .addPreferredGap(
395: org.jdesktop.layout.LayoutStyle.RELATED)
396: .add(
397: masterPanelLayout
398: .createParallelGroup(
399: org.jdesktop.layout.GroupLayout.BASELINE)
400: .add(tableLabel)
401: .add(
402: tableCombo,
403: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
404: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
405: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
406: .add(10, 10, 10)
407: .add(
408: masterPanelLayout
409: .createParallelGroup(
410: org.jdesktop.layout.GroupLayout.BASELINE)
411: .add(
412: availableLabel)
413: .add(
414: includeLabel))
415: .addPreferredGap(
416: org.jdesktop.layout.LayoutStyle.RELATED)
417: .add(
418: masterPanelLayout
419: .createParallelGroup(
420: org.jdesktop.layout.GroupLayout.LEADING)
421: .add(
422: availablePane,
423: 0,
424: 140,
425: Short.MAX_VALUE)
426: .add(
427: includePane,
428: 0,
429: 140,
430: Short.MAX_VALUE)
431: .add(
432: org.jdesktop.layout.GroupLayout.TRAILING,
433: masterPanelLayout
434: .createSequentialGroup()
435: .addPreferredGap(
436: org.jdesktop.layout.LayoutStyle.RELATED,
437: 15,
438: Short.MAX_VALUE)
439: .add(
440: addButton)
441: .addPreferredGap(
442: org.jdesktop.layout.LayoutStyle.RELATED)
443: .add(
444: masterPanelLayout
445: .createParallelGroup(
446: org.jdesktop.layout.GroupLayout.BASELINE)
447: .add(
448: addAllButton)
449: .add(
450: upButton))
451: .addPreferredGap(
452: org.jdesktop.layout.LayoutStyle.RELATED)
453: .add(
454: masterPanelLayout
455: .createParallelGroup(
456: org.jdesktop.layout.GroupLayout.BASELINE)
457: .add(
458: removeButton)
459: .add(
460: downButton))
461: .addPreferredGap(
462: org.jdesktop.layout.LayoutStyle.RELATED)
463: .add(
464: removeAllButton)
465: .addPreferredGap(
466: org.jdesktop.layout.LayoutStyle.RELATED,
467: 15,
468: Short.MAX_VALUE)))
469: .addContainerGap()));
470:
471: connectionCombo.getAccessibleContext()
472: .setAccessibleDescription(
473: org.openide.util.NbBundle.getMessage(
474: MasterPanel.class,
475: "LBL_DatabaseConnection_ACSD")); // NOI18N
476: tableCombo.getAccessibleContext().setAccessibleDescription(
477: org.openide.util.NbBundle.getMessage(MasterPanel.class,
478: "LBL_DatabaseTable_ACSD")); // NOI18N
479: addAllButton.getAccessibleContext().setAccessibleDescription(
480: org.openide.util.NbBundle.getMessage(MasterPanel.class,
481: "LBL_MasterAddAll_ACSD")); // NOI18N
482: addButton.getAccessibleContext().setAccessibleDescription(
483: org.openide.util.NbBundle.getMessage(MasterPanel.class,
484: "LBL_MasterAdd_ACSD")); // NOI18N
485: removeButton.getAccessibleContext().setAccessibleDescription(
486: org.openide.util.NbBundle.getMessage(MasterPanel.class,
487: "LBL_MasterRemove_ACSD")); // NOI18N
488: removeAllButton.getAccessibleContext()
489: .setAccessibleDescription(
490: org.openide.util.NbBundle.getMessage(
491: MasterPanel.class,
492: "LBL_MasterRemoveAll_ACSD")); // NOI18N
493: upButton.getAccessibleContext().setAccessibleDescription(
494: org.openide.util.NbBundle.getMessage(MasterPanel.class,
495: "LBL_MasterUp_ACSD")); // NOI18N
496: downButton.getAccessibleContext().setAccessibleDescription(
497: org.openide.util.NbBundle.getMessage(MasterPanel.class,
498: "LBL_MasterDown_ACSD")); // NOI18N
499:
500: masterPanel.getAccessibleContext().setAccessibleDescription(
501: org.openide.util.NbBundle.getMessage(MasterPanel.class,
502: "TITLE_MasterPanel")); // NOI18N
503: }
504:
505: // Code for dispatching events from components to event handlers.
506:
507: private class FormListener implements
508: java.awt.event.ActionListener,
509: javax.swing.event.ListSelectionListener {
510: FormListener() {
511: }
512:
513: public void actionPerformed(java.awt.event.ActionEvent evt) {
514: if (evt.getSource() == connectionCombo) {
515: MasterPanel.this .connectionComboActionPerformed(evt);
516: } else if (evt.getSource() == tableCombo) {
517: MasterPanel.this .tableComboActionPerformed(evt);
518: } else if (evt.getSource() == addAllButton) {
519: MasterPanel.this .addAllButtonActionPerformed(evt);
520: } else if (evt.getSource() == addButton) {
521: MasterPanel.this .addButtonActionPerformed(evt);
522: } else if (evt.getSource() == removeButton) {
523: MasterPanel.this .removeButtonActionPerformed(evt);
524: } else if (evt.getSource() == removeAllButton) {
525: MasterPanel.this .removeAllButtonActionPerformed(evt);
526: } else if (evt.getSource() == upButton) {
527: MasterPanel.this .upButtonActionPerformed(evt);
528: } else if (evt.getSource() == downButton) {
529: MasterPanel.this .downButtonActionPerformed(evt);
530: }
531: }
532:
533: public void valueChanged(
534: javax.swing.event.ListSelectionEvent evt) {
535: if (evt.getSource() == availableList) {
536: MasterPanel.this .availableListValueChanged(evt);
537: } else if (evt.getSource() == includeList) {
538: MasterPanel.this .includeListValueChanged(evt);
539: }
540: }
541: }// </editor-fold>//GEN-END:initComponents
542:
543: private void downButtonActionPerformed(
544: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
545: DefaultListModel model = (DefaultListModel) includeList
546: .getModel();
547: int index = includeList.getSelectedIndex();
548: Object item = model.remove(index);
549: model.add(index + 1, item);
550: includeList.setSelectedIndex(index + 1);
551: }//GEN-LAST:event_downButtonActionPerformed
552:
553: private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
554: DefaultListModel model = (DefaultListModel) includeList
555: .getModel();
556: int index = includeList.getSelectedIndex();
557: Object item = model.remove(index);
558: model.add(index - 1, item);
559: includeList.setSelectedIndex(index - 1);
560: }//GEN-LAST:event_upButtonActionPerformed
561:
562: private void removeAllButtonActionPerformed(
563: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeAllButtonActionPerformed
564: moveListItems(includeList, availableList, false);
565: }//GEN-LAST:event_removeAllButtonActionPerformed
566:
567: private void removeButtonActionPerformed(
568: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
569: moveListItems(includeList, availableList, true);
570: }//GEN-LAST:event_removeButtonActionPerformed
571:
572: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
573: moveListItems(availableList, includeList, true);
574: }//GEN-LAST:event_addButtonActionPerformed
575:
576: private void addAllButtonActionPerformed(
577: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAllButtonActionPerformed
578: moveListItems(availableList, includeList, false);
579: }//GEN-LAST:event_addAllButtonActionPerformed
580:
581: private void includeListValueChanged(
582: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_includeListValueChanged
583: int[] index = includeList.getSelectedIndices();
584: boolean single = (index.length == 1);
585: upButton.setEnabled(single && (index[0] != 0));
586: downButton.setEnabled(single
587: && (index[0] != includeList.getModel().getSize() - 1));
588: boolean any = (index.length > 0);
589: removeButton.setEnabled(any);
590: if (any) {
591: availableList.clearSelection();
592: }
593: }//GEN-LAST:event_includeListValueChanged
594:
595: private void availableListValueChanged(
596: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_availableListValueChanged
597: boolean enabled = (availableList.getSelectedIndex() != -1);
598: addButton.setEnabled(enabled);
599: if (enabled) {
600: includeList.clearSelection();
601: }
602: }//GEN-LAST:event_availableListValueChanged
603:
604: private void tableComboActionPerformed(
605: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tableComboActionPerformed
606: J2EEUtils.DBColumnInfo table = getTable();
607: DatabaseConnection connection = getConnection();
608: Connection con = ((connection == null) || (table == null) || !table
609: .isValid()) ? null : connection.getJDBCConnection();
610: try {
611: DefaultListModel model = (DefaultListModel) availableList
612: .getModel();
613: model.clear();
614: model = (DefaultListModel) includeList.getModel();
615: model.clear();
616: if (con != null) {
617: ResultSet rs = con.getMetaData().getColumns(
618: con.getCatalog(), connection.getSchema(),
619: table.getName(), "%"); // NOI18N
620: while (rs.next()) {
621: String columnName = rs.getString("COLUMN_NAME"); // NOI18N
622: model.addElement(columnName);
623: }
624: rs.close();
625: }
626: } catch (Exception ex) {
627: Logger.getLogger(getClass().getName()).log(Level.INFO,
628: ex.getMessage(), ex);
629: }
630: }//GEN-LAST:event_tableComboActionPerformed
631:
632: private void connectionComboActionPerformed(
633: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectionComboActionPerformed
634: DatabaseConnection connection = getConnection();
635: if (connection != null) {
636: Connection con = J2EEUtils.establishConnection(connection);
637: if (con == null)
638: return; // User canceled the connection dialog
639: fillTableCombo(connection);
640: } else {
641: fillTableCombo(null);
642: }
643: }//GEN-LAST:event_connectionComboActionPerformed
644:
645: /**
646: * Returns selected database connection.
647: *
648: * @return selected database connection.
649: */
650: private DatabaseConnection getConnection() {
651: Object selItem = connectionCombo.getSelectedItem();
652: return (selItem instanceof DatabaseConnection) ? (DatabaseConnection) selItem
653: : null;
654: }
655:
656: /**
657: * Returns selected database table.
658: *
659: * @return selected database table.
660: */
661: private J2EEUtils.DBColumnInfo getTable() {
662: return (J2EEUtils.DBColumnInfo) tableCombo.getSelectedItem();
663: }
664:
665: /**
666: * Returns selected columns.
667: *
668: * @return selected columns.
669: */
670: private List getSelectedColumns() {
671: DefaultListModel model = (DefaultListModel) includeList
672: .getModel();
673: return Arrays.asList(model.toArray());
674: }
675:
676: /**
677: * Fills the content of <code>tableCombo</code>.
678: *
679: * @param connection database connection.
680: */
681: private void fillTableCombo(DatabaseConnection connection) {
682: DefaultComboBoxModel model = new DefaultComboBoxModel();
683: if (connection != null) {
684: for (J2EEUtils.DBColumnInfo tableName : J2EEUtils
685: .tableNamesForConnection(connection)) {
686: model.addElement(tableName);
687: }
688: }
689: tableCombo.setModel(model);
690: tableCombo.setEnabled(tableCombo.getModel().getSize() != 0);
691: tableCombo.setSelectedItem(tableCombo.getSelectedItem());
692: }
693:
694: /**
695: * Moves items of <code>fromList</code> into <code>toList</code>.
696: *
697: * @param fromList list to move the items from.
698: * @param toList list to move the items to.
699: * @param selected determines whether to move all items or just the selected ones.
700: */
701: private static void moveListItems(JList fromList, JList toList,
702: boolean selected) {
703: DefaultListModel fromModel = (DefaultListModel) fromList
704: .getModel();
705: DefaultListModel toModel = (DefaultListModel) toList.getModel();
706: if (selected) {
707: int[] index = fromList.getSelectedIndices();
708: for (int i = 0; i < index.length; i++) {
709: Object item = fromModel.getElementAt(index[i]);
710: toModel.addElement(item);
711: }
712: for (int i = index.length - 1; i >= 0; i--) {
713: fromModel.removeElementAt(index[i]);
714: }
715: } else {
716: Enumeration items = fromModel.elements();
717: while (items.hasMoreElements()) {
718: toModel.addElement(items.nextElement());
719: }
720: fromModel.clear();
721: }
722: }
723:
724: // Variables declaration - do not modify//GEN-BEGIN:variables
725: private javax.swing.JButton addAllButton;
726: private javax.swing.JButton addButton;
727: private javax.swing.JLabel availableLabel;
728: private javax.swing.JList availableList;
729: private javax.swing.JScrollPane availablePane;
730: private javax.swing.JComboBox connectionCombo;
731: private javax.swing.JLabel connectionLabel;
732: private javax.swing.JButton downButton;
733: private javax.swing.JLabel includeLabel;
734: private javax.swing.JList includeList;
735: private javax.swing.JScrollPane includePane;
736: private javax.swing.JPanel masterPanel;
737: private javax.swing.JButton removeAllButton;
738: private javax.swing.JButton removeButton;
739: private javax.swing.JComboBox tableCombo;
740: private javax.swing.JLabel tableLabel;
741: private javax.swing.JButton upButton;
742:
743: // End of variables declaration//GEN-END:variables
744:
745: /**
746: * Returns component that represents this wizard panel.
747: *
748: * @return component that represents this wizard panel.
749: */
750: public Component getComponent() {
751: if (masterPanel == null) {
752: initGUI();
753: }
754: return masterPanel;
755: }
756:
757: /**
758: * Returns help context for this wizard panel.
759: *
760: * @return default help.
761: */
762: public HelpCtx getHelp() {
763: return new HelpCtx(
764: "org.netbeans.modules.form.j2ee.wizard.MasterPanel"); // NOI18N
765: }
766:
767: public void readSettings(Object settings) {
768: boolean valid = true;
769: if (!inNewProject && (settings instanceof TemplateWizard)) {
770: try {
771: TemplateWizard wizard = (TemplateWizard) settings;
772: DataFolder folder = wizard.getTargetFolder();
773: FileObject fob = folder.getPrimaryFile();
774: ClassPath cp = ClassPath.getClassPath(fob,
775: ClassPath.SOURCE);
776: String name = cp.getResourceName(fob).trim();
777: valid = (name.length() != 0);
778: wizard.putProperty("WizardPanel_errorMessage",
779: valid ? null : NbBundle.getMessage(getClass(),
780: "MSG_MasterDefaultPackage")); // NOI18N
781: } catch (IOException ioex) {
782: Logger.getLogger(getClass().getName()).log(Level.INFO,
783: ioex.getMessage(), ioex);
784: }
785: }
786: connectionCombo.setEnabled(valid);
787: if (!valid) {
788: setValid(false);
789: }
790: }
791:
792: /**
793: * Stores settings of this panel.
794: *
795: * @param settings wizard descriptor to store the settings in.
796: */
797: public void storeSettings(Object settings) {
798: WizardDescriptor wizard = (WizardDescriptor) settings;
799: wizard.putProperty("connection", getConnection()); // NOI18N
800: J2EEUtils.DBColumnInfo table = getTable();
801: if (table != null) {
802: wizard.putProperty("master", table.getName()); // NOI18N
803: }
804: wizard.putProperty("masterColumns", getSelectedColumns()); // NOI18N
805: }
806:
807: /**
808: * Determines whether we can move to the next panel.
809: *
810: * @return <code>true</code> if we can proceed to the next
811: * panel, returns <code>false</code> otherwise.
812: */
813: public boolean isValid() {
814: return valid;
815: }
816:
817: /**
818: * Sets the valid property.
819: *
820: * @param valid new value of the valid property.
821: */
822: void setValid(boolean valid) {
823: if (valid == this .valid)
824: return;
825: this .valid = valid;
826: fireStateChanged();
827: }
828:
829: /**
830: * Adds change listener to this wizard panel.
831: *
832: * @param listener listener to add.
833: */
834: public void addChangeListener(ChangeListener listener) {
835: if (listenerList == null) {
836: listenerList = new EventListenerList();
837: }
838: listenerList.add(ChangeListener.class, listener);
839: }
840:
841: /**
842: * Removes change listener from this wizard panel.
843: *
844: * @param listener listener to remove.
845: */
846: public void removeChangeListener(ChangeListener listener) {
847: if (listenerList != null) {
848: listenerList.remove(ChangeListener.class, listener);
849: }
850: }
851:
852: /**
853: * Fires change of the valid property.
854: */
855: private void fireStateChanged() {
856: if (listenerList == null)
857: return;
858:
859: ChangeEvent e = null;
860: ChangeListener[] listeners = listenerList
861: .getListeners(ChangeListener.class);
862: for (int i = listeners.length - 1; i >= 0; i--) {
863: if (e == null) {
864: e = new ChangeEvent(this);
865: }
866: listeners[i].stateChanged(e);
867: }
868: }
869:
870: }
|