0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041: package org.netbeans.modules.form.j2ee.wizard;
0042:
0043: import java.awt.Component;
0044: import java.sql.Connection;
0045: import java.sql.ResultSet;
0046: import java.text.MessageFormat;
0047: import java.util.*;
0048: import java.util.logging.Level;
0049: import java.util.logging.Logger;
0050: import javax.swing.*;
0051: import javax.swing.event.*;
0052: import org.netbeans.api.db.explorer.DatabaseConnection;
0053: import org.netbeans.modules.form.j2ee.J2EEUtils;
0054: import org.openide.WizardDescriptor;
0055: import org.openide.util.HelpCtx;
0056: import org.openide.util.NbBundle;
0057: import org.openide.util.Utilities;
0058:
0059: /**
0060: * Wizard panel for detail information of master/detail wizard.
0061: *
0062: * @author Jan Stola
0063: */
0064: public class DetailPanel implements WizardDescriptor.Panel {
0065: /** Determines whether we can proceed to the next panel. */
0066: private boolean valid;
0067: /** List of <code>ChangeListener</code> objects. */
0068: private EventListenerList listenerList;
0069: /** Connection to the selected database. */
0070: private DatabaseConnection connection;
0071: /** Name of the master table. */
0072: private String masterTable;
0073: /** Names of selected master table columns. */
0074: private List masterColumns;
0075: /** Image with the preview of the fields layout. */
0076: private ImageIcon fieldsIcon;
0077: /** Image with the preview of the table layout. */
0078: private ImageIcon tableIcon;
0079:
0080: /**
0081: * Initializes GUI of this panel.
0082: */
0083: private void initGUI() {
0084: initComponents();
0085: initLists();
0086: fieldsIcon = new javax.swing.ImageIcon(
0087: getClass()
0088: .getResource(
0089: "/org/netbeans/modules/form/j2ee/resources/md_fields.gif")); // NOI18N
0090: tableIcon = new javax.swing.ImageIcon(
0091: getClass()
0092: .getResource(
0093: "/org/netbeans/modules/form/j2ee/resources/md_table.gif")); // NOI18N
0094: previewLabel.setIcon(fieldsIcon);
0095: }
0096:
0097: /**
0098: * Initializes lists of columns.
0099: */
0100: private void initLists() {
0101: availableList.setModel(new DefaultListModel());
0102: includeList.setModel(new DefaultListModel());
0103: ListDataListener listener = new ListDataListener() {
0104: public void intervalAdded(ListDataEvent e) {
0105: contentsChanged(e);
0106: }
0107:
0108: public void intervalRemoved(ListDataEvent e) {
0109: contentsChanged(e);
0110: }
0111:
0112: public void contentsChanged(ListDataEvent e) {
0113: Object source = e.getSource();
0114: if (source == availableList.getModel()) {
0115: addAllButton.setEnabled(availableList.getModel()
0116: .getSize() != 0);
0117: } else if (source == includeList.getModel()) {
0118: boolean empty = includeList.getModel().getSize() == 0;
0119: removeAllButton.setEnabled(!empty);
0120: setValid(!empty);
0121: }
0122: }
0123: };
0124: availableList.getModel().addListDataListener(listener);
0125: includeList.getModel().addListDataListener(listener);
0126: }
0127:
0128: /**
0129: * Fills the content of <code>tableCombo</code>.
0130: */
0131: private void fillTableCombo() {
0132: Connection con = connection.getJDBCConnection();
0133: try {
0134: DefaultComboBoxModel model = new DefaultComboBoxModel();
0135: ResultSet rs = con.getMetaData().getExportedKeys(
0136: con.getCatalog(), connection.getSchema(),
0137: masterTable);
0138: while (rs.next()) {
0139: String tableName = rs.getString("FKTABLE_NAME"); // NOI18N
0140: boolean hasPK = J2EEUtils.hasPrimaryKey(connection,
0141: tableName);
0142: ForeignKey fk = new ForeignKey(rs
0143: .getString("PKTABLE_NAME"), // NOI18N
0144: rs.getString("PKCOLUMN_NAME"), // NOI18N
0145: tableName, rs.getString("FKCOLUMN_NAME"), // NOI18N
0146: hasPK);
0147: model.addElement(fk);
0148: }
0149: tableCombo.setModel(model);
0150: rs.close();
0151: } catch (Exception ex) {
0152: Logger.getLogger(getClass().getName()).log(Level.INFO,
0153: ex.getMessage(), ex);
0154: }
0155: boolean empty = (tableCombo.getModel().getSize() == 0);
0156: if (empty) {
0157: fieldsChoice.setSelected(true);
0158: tableCombo.setEnabled(false);
0159: availableLabel.setText(NbBundle.getMessage(
0160: DetailPanel.class, "LBL_DetailAvailableFields")); // NOI18N
0161: includeLabel.setText(NbBundle.getMessage(DetailPanel.class,
0162: "LBL_DetailFieldsToInclude")); // NOI18N
0163: previewLabel.setIcon(fieldsIcon);
0164: }
0165: tableChoice.setEnabled(!empty);
0166: refreshLists();
0167: }
0168:
0169: /**
0170: * Refreshes the content of the lists.
0171: */
0172: private void refreshLists() {
0173: if (tableChoice.isSelected()) {
0174: tableCombo.setSelectedItem(tableCombo.getSelectedItem());
0175: } else {
0176: fillLists(masterTable);
0177: }
0178: }
0179:
0180: /** This method is called from within the constructor to
0181: * initialize the form.
0182: * WARNING: Do NOT modify this code. The content of this method is
0183: * always regenerated by the Form Editor.
0184: */
0185: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
0186: private void initComponents() {
0187:
0188: masterPanel = new javax.swing.JPanel();
0189: availableLabel = new javax.swing.JLabel();
0190: availablePane = new javax.swing.JScrollPane();
0191: availableList = new javax.swing.JList();
0192: addAllButton = new javax.swing.JButton();
0193: addButton = new javax.swing.JButton();
0194: removeButton = new javax.swing.JButton();
0195: removeAllButton = new javax.swing.JButton();
0196: includePane = new javax.swing.JScrollPane();
0197: includeList = new javax.swing.JList();
0198: upButton = new javax.swing.JButton();
0199: downButton = new javax.swing.JButton();
0200: includeLabel = new javax.swing.JLabel();
0201: detailLabel = new javax.swing.JLabel();
0202: fieldsChoice = new javax.swing.JRadioButton();
0203: tableChoice = new javax.swing.JRadioButton();
0204: tableCombo = new javax.swing.JComboBox();
0205: previewLabel = new javax.swing.JLabel();
0206: buttonGroup = new javax.swing.ButtonGroup();
0207:
0208: FormListener formListener = new FormListener();
0209:
0210: masterPanel.setName(org.openide.util.NbBundle.getMessage(
0211: DetailPanel.class, "TITLE_DetailPanel")); // NOI18N
0212:
0213: availableLabel.setLabelFor(availableList);
0214: org.openide.awt.Mnemonics.setLocalizedText(availableLabel,
0215: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0216: "LBL_DetailAvailableFields")); // NOI18N
0217:
0218: availableList.addListSelectionListener(formListener);
0219: availablePane.setViewportView(availableList);
0220: availableList.getAccessibleContext().setAccessibleDescription(
0221: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0222: "LBL_DetailAvailable_ACSD")); // NOI18N
0223:
0224: org.openide.awt.Mnemonics.setLocalizedText(addAllButton,
0225: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0226: "LBL_DetailAddAll")); // NOI18N
0227: addAllButton.setEnabled(false);
0228: addAllButton.addActionListener(formListener);
0229:
0230: org.openide.awt.Mnemonics.setLocalizedText(addButton,
0231: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0232: "LBL_DetailAdd")); // NOI18N
0233: addButton.setEnabled(false);
0234: addButton.addActionListener(formListener);
0235:
0236: org.openide.awt.Mnemonics.setLocalizedText(removeButton,
0237: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0238: "LBL_DetailRemove")); // NOI18N
0239: removeButton.setEnabled(false);
0240: removeButton.addActionListener(formListener);
0241:
0242: org.openide.awt.Mnemonics.setLocalizedText(removeAllButton,
0243: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0244: "LBL_DetailRemoveAll")); // NOI18N
0245: removeAllButton.setEnabled(false);
0246: removeAllButton.addActionListener(formListener);
0247:
0248: includeList.addListSelectionListener(formListener);
0249: includePane.setViewportView(includeList);
0250: includeList.getAccessibleContext().setAccessibleDescription(
0251: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0252: "LBL_DetailToInclude_ACSD")); // NOI18N
0253:
0254: upButton
0255: .setIcon(new javax.swing.ImageIcon(
0256: getClass()
0257: .getResource(
0258: "/org/netbeans/modules/form/j2ee/resources/up.gif")));
0259: org.openide.awt.Mnemonics.setLocalizedText(upButton,
0260: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0261: "LBL_DetailUp")); // NOI18N
0262: upButton.setEnabled(false);
0263: upButton
0264: .setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
0265: if (!Utilities.isMac()) {
0266: upButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
0267: }
0268: upButton.addActionListener(formListener);
0269:
0270: downButton
0271: .setIcon(new javax.swing.ImageIcon(
0272: getClass()
0273: .getResource(
0274: "/org/netbeans/modules/form/j2ee/resources/down.gif")));
0275: org.openide.awt.Mnemonics.setLocalizedText(downButton,
0276: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0277: "LBL_DetailDown")); // NOI18N
0278: downButton.setEnabled(false);
0279: downButton
0280: .setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
0281: if (!Utilities.isMac()) {
0282: downButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
0283: }
0284: downButton.addActionListener(formListener);
0285:
0286: includeLabel.setLabelFor(includeList);
0287: org.openide.awt.Mnemonics.setLocalizedText(includeLabel,
0288: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0289: "LBL_DetailFieldsToInclude")); // NOI18N
0290:
0291: org.openide.awt.Mnemonics.setLocalizedText(detailLabel,
0292: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0293: "LBL_DetailType")); // NOI18N
0294:
0295: buttonGroup.add(fieldsChoice);
0296: fieldsChoice.setSelected(true);
0297: org.openide.awt.Mnemonics.setLocalizedText(fieldsChoice,
0298: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0299: "LBL_DetailTextfields")); // NOI18N
0300: fieldsChoice.setBorder(javax.swing.BorderFactory
0301: .createEmptyBorder(0, 0, 0, 0));
0302: fieldsChoice.setMargin(new java.awt.Insets(0, 0, 0, 0));
0303: fieldsChoice.addActionListener(formListener);
0304:
0305: buttonGroup.add(tableChoice);
0306: org.openide.awt.Mnemonics.setLocalizedText(tableChoice,
0307: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0308: "LBL_DetailTable")); // NOI18N
0309: tableChoice.setBorder(javax.swing.BorderFactory
0310: .createEmptyBorder(0, 0, 0, 0));
0311: tableChoice.setMargin(new java.awt.Insets(0, 0, 0, 0));
0312: tableChoice.addActionListener(formListener);
0313:
0314: tableCombo.setEnabled(false);
0315: tableCombo.setRenderer(new ForeignKeyRenderer());
0316: tableCombo.addActionListener(formListener);
0317:
0318: org.jdesktop.layout.GroupLayout masterPanelLayout = new org.jdesktop.layout.GroupLayout(
0319: masterPanel);
0320: masterPanel.setLayout(masterPanelLayout);
0321: masterPanelLayout
0322: .setHorizontalGroup(masterPanelLayout
0323: .createParallelGroup(
0324: org.jdesktop.layout.GroupLayout.LEADING)
0325: .add(
0326: masterPanelLayout
0327: .createSequentialGroup()
0328: .addContainerGap()
0329: .add(
0330: masterPanelLayout
0331: .createParallelGroup(
0332: org.jdesktop.layout.GroupLayout.LEADING)
0333: .add(
0334: masterPanelLayout
0335: .createSequentialGroup()
0336: .add(
0337: masterPanelLayout
0338: .createParallelGroup(
0339: org.jdesktop.layout.GroupLayout.LEADING)
0340: .add(
0341: org.jdesktop.layout.GroupLayout.TRAILING,
0342: masterPanelLayout
0343: .createSequentialGroup()
0344: .add(
0345: availablePane,
0346: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0347: 170,
0348: Short.MAX_VALUE)
0349: .addPreferredGap(
0350: org.jdesktop.layout.LayoutStyle.RELATED)
0351: .add(
0352: masterPanelLayout
0353: .createParallelGroup(
0354: org.jdesktop.layout.GroupLayout.LEADING,
0355: false)
0356: .add(
0357: addButton,
0358: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0359: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0360: Short.MAX_VALUE)
0361: .add(
0362: removeButton,
0363: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0364: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0365: Short.MAX_VALUE)
0366: .add(
0367: removeAllButton,
0368: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0369: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0370: Short.MAX_VALUE)
0371: .add(
0372: addAllButton,
0373: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0374: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0375: Short.MAX_VALUE)))
0376: .add(
0377: availableLabel))
0378: .addPreferredGap(
0379: org.jdesktop.layout.LayoutStyle.RELATED)
0380: .add(
0381: masterPanelLayout
0382: .createParallelGroup(
0383: org.jdesktop.layout.GroupLayout.LEADING)
0384: .add(
0385: includeLabel)
0386: .add(
0387: org.jdesktop.layout.GroupLayout.TRAILING,
0388: masterPanelLayout
0389: .createSequentialGroup()
0390: .add(
0391: includePane,
0392: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0393: 170,
0394: Short.MAX_VALUE)
0395: .addPreferredGap(
0396: org.jdesktop.layout.LayoutStyle.RELATED)
0397: .add(
0398: masterPanelLayout
0399: .createParallelGroup(
0400: org.jdesktop.layout.GroupLayout.LEADING,
0401: false)
0402: .add(
0403: upButton,
0404: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0405: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0406: Short.MAX_VALUE)
0407: .add(
0408: downButton,
0409: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0410: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0411: Short.MAX_VALUE)))))
0412: .add(
0413: masterPanelLayout
0414: .createSequentialGroup()
0415: .add(
0416: masterPanelLayout
0417: .createParallelGroup(
0418: org.jdesktop.layout.GroupLayout.LEADING)
0419: .add(
0420: detailLabel)
0421: .add(
0422: masterPanelLayout
0423: .createSequentialGroup()
0424: .add(
0425: 10,
0426: 10,
0427: 10)
0428: .add(
0429: masterPanelLayout
0430: .createParallelGroup(
0431: org.jdesktop.layout.GroupLayout.LEADING)
0432: .add(
0433: masterPanelLayout
0434: .createSequentialGroup()
0435: .add(
0436: tableChoice)
0437: .addPreferredGap(
0438: org.jdesktop.layout.LayoutStyle.RELATED)
0439: .add(
0440: tableCombo,
0441: 0,
0442: 311,
0443: Short.MAX_VALUE))
0444: .add(
0445: fieldsChoice))))
0446: .add(
0447: 10,
0448: 10,
0449: 10)
0450: .add(
0451: previewLabel,
0452: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
0453: 80,
0454: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
0455: .addContainerGap()));
0456:
0457: masterPanelLayout.linkSize(new java.awt.Component[] {
0458: addAllButton, addButton, downButton, removeAllButton,
0459: removeButton, upButton },
0460: org.jdesktop.layout.GroupLayout.HORIZONTAL);
0461:
0462: masterPanelLayout
0463: .setVerticalGroup(masterPanelLayout
0464: .createParallelGroup(
0465: org.jdesktop.layout.GroupLayout.LEADING)
0466: .add(
0467: masterPanelLayout
0468: .createSequentialGroup()
0469: .addContainerGap()
0470: .add(
0471: masterPanelLayout
0472: .createParallelGroup(
0473: org.jdesktop.layout.GroupLayout.LEADING)
0474: .add(
0475: masterPanelLayout
0476: .createSequentialGroup()
0477: .add(
0478: detailLabel)
0479: .addPreferredGap(
0480: org.jdesktop.layout.LayoutStyle.RELATED)
0481: .add(
0482: fieldsChoice)
0483: .addPreferredGap(
0484: org.jdesktop.layout.LayoutStyle.RELATED)
0485: .add(
0486: masterPanelLayout
0487: .createParallelGroup(
0488: org.jdesktop.layout.GroupLayout.BASELINE)
0489: .add(
0490: tableChoice)
0491: .add(
0492: tableCombo,
0493: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
0494: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0495: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
0496: .add(
0497: previewLabel,
0498: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
0499: 60,
0500: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
0501: .addPreferredGap(
0502: org.jdesktop.layout.LayoutStyle.RELATED)
0503: .add(
0504: masterPanelLayout
0505: .createParallelGroup(
0506: org.jdesktop.layout.GroupLayout.BASELINE)
0507: .add(
0508: availableLabel)
0509: .add(
0510: includeLabel))
0511: .addPreferredGap(
0512: org.jdesktop.layout.LayoutStyle.RELATED)
0513: .add(
0514: masterPanelLayout
0515: .createParallelGroup(
0516: org.jdesktop.layout.GroupLayout.LEADING)
0517: .add(
0518: includePane,
0519: 0,
0520: 0,
0521: Short.MAX_VALUE)
0522: .add(
0523: masterPanelLayout
0524: .createSequentialGroup()
0525: .addPreferredGap(
0526: org.jdesktop.layout.LayoutStyle.RELATED,
0527: 18,
0528: Short.MAX_VALUE)
0529: .add(
0530: addButton)
0531: .addPreferredGap(
0532: org.jdesktop.layout.LayoutStyle.RELATED)
0533: .add(
0534: masterPanelLayout
0535: .createParallelGroup(
0536: org.jdesktop.layout.GroupLayout.BASELINE)
0537: .add(
0538: addAllButton)
0539: .add(
0540: upButton))
0541: .addPreferredGap(
0542: org.jdesktop.layout.LayoutStyle.RELATED)
0543: .add(
0544: masterPanelLayout
0545: .createParallelGroup(
0546: org.jdesktop.layout.GroupLayout.BASELINE)
0547: .add(
0548: removeButton)
0549: .add(
0550: downButton))
0551: .addPreferredGap(
0552: org.jdesktop.layout.LayoutStyle.RELATED)
0553: .add(
0554: removeAllButton)
0555: .addPreferredGap(
0556: org.jdesktop.layout.LayoutStyle.RELATED,
0557: 19,
0558: Short.MAX_VALUE))
0559: .add(
0560: availablePane,
0561: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
0562: 147,
0563: Short.MAX_VALUE))
0564: .addContainerGap()));
0565:
0566: addAllButton.getAccessibleContext().setAccessibleDescription(
0567: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0568: "LBL_DetailAddAll_ACSD")); // NOI18N
0569: addButton.getAccessibleContext().setAccessibleDescription(
0570: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0571: "LBL_DetailAdd_ACSD")); // NOI18N
0572: removeButton.getAccessibleContext().setAccessibleDescription(
0573: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0574: "LBL_DetailRemove_ACSD")); // NOI18N
0575: removeAllButton.getAccessibleContext()
0576: .setAccessibleDescription(
0577: org.openide.util.NbBundle.getMessage(
0578: DetailPanel.class,
0579: "LBL_DetailRemoveAll_ACSD")); // NOI18N
0580: upButton.getAccessibleContext().setAccessibleDescription(
0581: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0582: "LBL_DetailUp_ACSD")); // NOI18N
0583: downButton.getAccessibleContext().setAccessibleDescription(
0584: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0585: "LBL_DetailDown_ACSD")); // NOI18N
0586: fieldsChoice.getAccessibleContext().setAccessibleDescription(
0587: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0588: "LBL_DetailTextfields_ACSD")); // NOI18N
0589: tableChoice.getAccessibleContext().setAccessibleDescription(
0590: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0591: "LBL_DetailTable_ACSD")); // NOI18N
0592: tableCombo.getAccessibleContext().setAccessibleName(
0593: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0594: "LBL_DetailTable_ACSN")); // NOI18N
0595: tableCombo.getAccessibleContext().setAccessibleDescription(
0596: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0597: "LBL_DetailTable_ACSD")); // NOI18N
0598:
0599: masterPanel.getAccessibleContext().setAccessibleDescription(
0600: org.openide.util.NbBundle.getMessage(DetailPanel.class,
0601: "TITLE_DetailPanel")); // NOI18N
0602: }
0603:
0604: // Code for dispatching events from components to event handlers.
0605:
0606: private class FormListener implements
0607: java.awt.event.ActionListener,
0608: javax.swing.event.ListSelectionListener {
0609: FormListener() {
0610: }
0611:
0612: public void actionPerformed(java.awt.event.ActionEvent evt) {
0613: if (evt.getSource() == addAllButton) {
0614: DetailPanel.this .addAllButtonActionPerformed(evt);
0615: } else if (evt.getSource() == addButton) {
0616: DetailPanel.this .addButtonActionPerformed(evt);
0617: } else if (evt.getSource() == removeButton) {
0618: DetailPanel.this .removeButtonActionPerformed(evt);
0619: } else if (evt.getSource() == removeAllButton) {
0620: DetailPanel.this .removeAllButtonActionPerformed(evt);
0621: } else if (evt.getSource() == upButton) {
0622: DetailPanel.this .upButtonActionPerformed(evt);
0623: } else if (evt.getSource() == downButton) {
0624: DetailPanel.this .downButtonActionPerformed(evt);
0625: } else if (evt.getSource() == fieldsChoice) {
0626: DetailPanel.this .fieldsChoiceActionPerformed(evt);
0627: } else if (evt.getSource() == tableChoice) {
0628: DetailPanel.this .tableChoiceActionPerformed(evt);
0629: } else if (evt.getSource() == tableCombo) {
0630: DetailPanel.this .tableComboActionPerformed(evt);
0631: }
0632: }
0633:
0634: public void valueChanged(
0635: javax.swing.event.ListSelectionEvent evt) {
0636: if (evt.getSource() == availableList) {
0637: DetailPanel.this .availableListValueChanged(evt);
0638: } else if (evt.getSource() == includeList) {
0639: DetailPanel.this .includeListValueChanged(evt);
0640: }
0641: }
0642: }// </editor-fold>//GEN-END:initComponents
0643:
0644: private void tableComboActionPerformed(
0645: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tableComboActionPerformed
0646: ForeignKey key = getForeignKey();
0647: fillLists(key.isValid() ? getForeignKey().getFKTable() : null);
0648: }//GEN-LAST:event_tableComboActionPerformed
0649:
0650: private void tableChoiceActionPerformed(
0651: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tableChoiceActionPerformed
0652: boolean selected = tableChoice.isSelected();
0653: tableCombo.setEnabled(selected);
0654: if (selected) {
0655: availableLabel.setText(NbBundle.getMessage(
0656: DetailPanel.class, "LBL_DetailAvailableColumns")); // NOI18N
0657: includeLabel.setText(NbBundle.getMessage(DetailPanel.class,
0658: "LBL_DetailColumnsToInclude")); // NOI18N
0659: ForeignKey key = getForeignKey();
0660: fillLists(key.isValid() ? getForeignKey().getFKTable()
0661: : null);
0662: previewLabel.setIcon(tableIcon);
0663: }
0664: }//GEN-LAST:event_tableChoiceActionPerformed
0665:
0666: private void fieldsChoiceActionPerformed(
0667: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldsChoiceActionPerformed
0668: if (fieldsChoice.isSelected()) {
0669: tableCombo.setEnabled(false);
0670: availableLabel.setText(NbBundle.getMessage(
0671: DetailPanel.class, "LBL_DetailAvailableFields")); // NOI18N
0672: includeLabel.setText(NbBundle.getMessage(DetailPanel.class,
0673: "LBL_DetailFieldsToInclude")); // NOI18N
0674: fillLists(masterTable);
0675: previewLabel.setIcon(fieldsIcon);
0676: }
0677: }//GEN-LAST:event_fieldsChoiceActionPerformed
0678:
0679: private void downButtonActionPerformed(
0680: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
0681: DefaultListModel model = (DefaultListModel) includeList
0682: .getModel();
0683: int index = includeList.getSelectedIndex();
0684: Object item = model.remove(index);
0685: model.add(index + 1, item);
0686: includeList.setSelectedIndex(index + 1);
0687: }//GEN-LAST:event_downButtonActionPerformed
0688:
0689: private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
0690: DefaultListModel model = (DefaultListModel) includeList
0691: .getModel();
0692: int index = includeList.getSelectedIndex();
0693: Object item = model.remove(index);
0694: model.add(index - 1, item);
0695: includeList.setSelectedIndex(index - 1);
0696: }//GEN-LAST:event_upButtonActionPerformed
0697:
0698: private void removeAllButtonActionPerformed(
0699: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeAllButtonActionPerformed
0700: moveListItems(includeList, availableList, false);
0701: }//GEN-LAST:event_removeAllButtonActionPerformed
0702:
0703: private void removeButtonActionPerformed(
0704: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
0705: moveListItems(includeList, availableList, true);
0706: }//GEN-LAST:event_removeButtonActionPerformed
0707:
0708: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
0709: moveListItems(availableList, includeList, true);
0710: }//GEN-LAST:event_addButtonActionPerformed
0711:
0712: private void addAllButtonActionPerformed(
0713: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAllButtonActionPerformed
0714: moveListItems(availableList, includeList, false);
0715: }//GEN-LAST:event_addAllButtonActionPerformed
0716:
0717: private void includeListValueChanged(
0718: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_includeListValueChanged
0719: int[] index = includeList.getSelectedIndices();
0720: boolean single = (index.length == 1);
0721: upButton.setEnabled(single && (index[0] != 0));
0722: downButton.setEnabled(single
0723: && (index[0] != includeList.getModel().getSize() - 1));
0724: boolean any = (index.length > 0);
0725: removeButton.setEnabled(any);
0726: if (any) {
0727: availableList.clearSelection();
0728: }
0729: }//GEN-LAST:event_includeListValueChanged
0730:
0731: private void availableListValueChanged(
0732: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_availableListValueChanged
0733: boolean enabled = (availableList.getSelectedIndex() != -1);
0734: addButton.setEnabled(enabled);
0735: if (enabled) {
0736: includeList.clearSelection();
0737: }
0738: }//GEN-LAST:event_availableListValueChanged
0739:
0740: private void fillLists(String tableName) {
0741: Connection con = connection.getJDBCConnection();
0742: try {
0743: DefaultListModel model = (DefaultListModel) availableList
0744: .getModel();
0745: model.clear();
0746: model = (DefaultListModel) includeList.getModel();
0747: model.clear();
0748: if (tableName != null) {
0749: ResultSet rs = con.getMetaData().getColumns(
0750: con.getCatalog(), connection.getSchema(),
0751: tableName, "%"); // NOI18N
0752: while (rs.next()) {
0753: String columnName = rs.getString("COLUMN_NAME"); // NOI18N
0754: model.addElement(columnName);
0755: }
0756: rs.close();
0757: if (defaultPreselect) {
0758: if (masterTable.equals(tableName)) {
0759: // pre-select master columns
0760: preSelectColumns(masterColumns);
0761: } else {
0762: ForeignKey key = getForeignKey();
0763: if (key != null) {
0764: includeList.setSelectedValue(key
0765: .getFKColumn(), false);
0766: moveListItems(includeList, availableList,
0767: true);
0768: }
0769: }
0770: }
0771: }
0772: } catch (Exception ex) {
0773: Logger.getLogger(getClass().getName()).log(Level.INFO,
0774: ex.getMessage(), ex);
0775: }
0776: }
0777:
0778: /**
0779: * Pre-selects specified columns.
0780: *
0781: * @param columnNames names of columns to pre-select.
0782: */
0783: private void preSelectColumns(List columnNames) {
0784: DefaultListModel availableModel = (DefaultListModel) availableList
0785: .getModel();
0786: DefaultListModel includeModel = (DefaultListModel) includeList
0787: .getModel();
0788: for (int i = includeModel.getSize() - 1; i >= 0; i--) {
0789: String column = (String) includeModel.getElementAt(i);
0790: if (!columnNames.contains(column)) {
0791: // The column is not selected
0792: includeModel.removeElementAt(i);
0793: availableModel.add(0, column);
0794: }
0795: }
0796: }
0797:
0798: /**
0799: * Returns selected foreign key.
0800: *
0801: * @return selected foreignkey.
0802: */
0803: private ForeignKey getForeignKey() {
0804: return tableChoice.isSelected() ? (ForeignKey) tableCombo
0805: .getSelectedItem() : null;
0806: }
0807:
0808: /**
0809: * Returns selected columns.
0810: *
0811: * @return selected columns.
0812: */
0813: private List getSelectedColumns() {
0814: DefaultListModel model = (DefaultListModel) includeList
0815: .getModel();
0816: return Arrays.asList(model.toArray());
0817: }
0818:
0819: /**
0820: * Moves items of <code>fromList</code> into <code>toList</code>.
0821: *
0822: * @param fromList list to move the items from.
0823: * @param toList list to move the items to.
0824: * @param selected determines whether to move all items or just the selected ones.
0825: */
0826: private static void moveListItems(JList fromList, JList toList,
0827: boolean selected) {
0828: DefaultListModel fromModel = (DefaultListModel) fromList
0829: .getModel();
0830: DefaultListModel toModel = (DefaultListModel) toList.getModel();
0831: if (selected) {
0832: int[] index = fromList.getSelectedIndices();
0833: for (int i = 0; i < index.length; i++) {
0834: Object item = fromModel.getElementAt(index[i]);
0835: toModel.addElement(item);
0836: }
0837: for (int i = index.length - 1; i >= 0; i--) {
0838: fromModel.removeElementAt(index[i]);
0839: }
0840: } else {
0841: Enumeration items = fromModel.elements();
0842: while (items.hasMoreElements()) {
0843: toModel.addElement(items.nextElement());
0844: }
0845: fromModel.clear();
0846: }
0847: }
0848:
0849: // Variables declaration - do not modify//GEN-BEGIN:variables
0850: private javax.swing.JButton addAllButton;
0851: private javax.swing.JButton addButton;
0852: private javax.swing.JLabel availableLabel;
0853: private javax.swing.JList availableList;
0854: private javax.swing.JScrollPane availablePane;
0855: private javax.swing.ButtonGroup buttonGroup;
0856: private javax.swing.JLabel detailLabel;
0857: private javax.swing.JButton downButton;
0858: private javax.swing.JRadioButton fieldsChoice;
0859: private javax.swing.JLabel includeLabel;
0860: private javax.swing.JList includeList;
0861: private javax.swing.JScrollPane includePane;
0862: private javax.swing.JPanel masterPanel;
0863: private javax.swing.JLabel previewLabel;
0864: private javax.swing.JButton removeAllButton;
0865: private javax.swing.JButton removeButton;
0866: private javax.swing.JRadioButton tableChoice;
0867: private javax.swing.JComboBox tableCombo;
0868: private javax.swing.JButton upButton;
0869:
0870: // End of variables declaration//GEN-END:variables
0871:
0872: /**
0873: * Returns component that represents this wizard panel.
0874: *
0875: * @return component that represents this wizard panel.
0876: */
0877: public Component getComponent() {
0878: if (masterPanel == null) {
0879: initGUI();
0880: }
0881: return masterPanel;
0882: }
0883:
0884: /**
0885: * Returns help context for this wizard panel.
0886: *
0887: * @return default help.
0888: */
0889: public HelpCtx getHelp() {
0890: return new HelpCtx(
0891: "org.netbeans.modules.form.j2ee.wizard.DetailPanel"); // NOI18N
0892: }
0893:
0894: private boolean defaultPreselect = true;
0895:
0896: public void readSettings(Object settings) {
0897: WizardDescriptor wizard = (WizardDescriptor) settings;
0898: connection = (DatabaseConnection) wizard
0899: .getProperty("connection"); // NOI18N
0900: masterTable = (String) wizard.getProperty("master"); // NOI18N
0901: masterColumns = (List) wizard.getProperty("masterColumns"); // NOI18N
0902:
0903: // restore detail settings if the rest remained the same
0904: ForeignKey key = getForeignKey();
0905: List detailColumns = (List) wizard.getProperty("detailColumns"); // NOI18N
0906: if (key == null) {
0907: String detailTable = (String) wizard
0908: .getProperty("detailTable"); // NOI18N
0909: if ((masterTable.equals(detailTable))
0910: && (detailColumns != null)) {
0911: defaultPreselect = false;
0912: }
0913: } else {
0914: String pkTable = (String) wizard
0915: .getProperty("detailPKTable"); // NOI18N
0916: String pkColumn = (String) wizard
0917: .getProperty("detailPKColumn"); // NOI18N
0918: String fkTable = (String) wizard
0919: .getProperty("detailFKTable"); // NOI18N
0920: String fkColumn = (String) wizard
0921: .getProperty("detailFKColumn"); // NOI18N
0922: if (key.getPKTable().equals(pkTable)
0923: && key.getPKColumn().equals(pkColumn)
0924: && key.getFKTable().equals(fkTable)
0925: && key.getFKColumn().equals(fkColumn)) {
0926: defaultPreselect = false;
0927: }
0928: }
0929:
0930: fillTableCombo();
0931: if (!defaultPreselect) {
0932: preSelectColumns(detailColumns);
0933: defaultPreselect = true;
0934: }
0935: }
0936:
0937: /**
0938: * Stores settings of this panel.
0939: *
0940: * @param settings wizard descriptor to store the settings in.
0941: */
0942: public void storeSettings(Object settings) {
0943: WizardDescriptor wizard = (WizardDescriptor) settings;
0944: ForeignKey key = getForeignKey();
0945: wizard.putProperty("detailTable", (key == null) ? masterTable
0946: : null); // NOI18N
0947: wizard.putProperty("detailPKTable", (key == null) ? null : key
0948: .getPKTable()); // NOI18N
0949: wizard.putProperty("detailPKColumn", (key == null) ? null : key
0950: .getPKColumn()); // NOI18N
0951: wizard.putProperty("detailFKTable", (key == null) ? null : key
0952: .getFKTable()); // NOI18N
0953: wizard.putProperty("detailFKColumn", (key == null) ? null : key
0954: .getFKColumn()); // NOI18N
0955: wizard.putProperty("detailColumns", getSelectedColumns()); // NOI18N
0956: }
0957:
0958: /**
0959: * Determines whether we can move to the next panel.
0960: *
0961: * @return <code>true</code> if we can proceed to the next
0962: * panel, returns <code>false</code> otherwise.
0963: */
0964: public boolean isValid() {
0965: return valid;
0966: }
0967:
0968: /**
0969: * Sets the valid property.
0970: *
0971: * @param valid new value of the valid property.
0972: */
0973: void setValid(boolean valid) {
0974: if (valid == this .valid)
0975: return;
0976: this .valid = valid;
0977: fireStateChanged();
0978: }
0979:
0980: /**
0981: * Adds change listener to this wizard panel.
0982: *
0983: * @param listener listener to add.
0984: */
0985: public void addChangeListener(ChangeListener listener) {
0986: if (listenerList == null) {
0987: listenerList = new EventListenerList();
0988: }
0989: listenerList.add(ChangeListener.class, listener);
0990: }
0991:
0992: /**
0993: * Removes change listener from this wizard panel.
0994: *
0995: * @param listener listener to remove.
0996: */
0997: public void removeChangeListener(ChangeListener listener) {
0998: if (listenerList != null) {
0999: listenerList.remove(ChangeListener.class, listener);
1000: }
1001: }
1002:
1003: /**
1004: * Fires change of the valid property.
1005: */
1006: private void fireStateChanged() {
1007: if (listenerList == null)
1008: return;
1009:
1010: ChangeEvent e = null;
1011: ChangeListener[] listeners = listenerList
1012: .getListeners(ChangeListener.class);
1013: for (int i = listeners.length - 1; i >= 0; i--) {
1014: if (e == null) {
1015: e = new ChangeEvent(this );
1016: }
1017: listeners[i].stateChanged(e);
1018: }
1019: }
1020:
1021: /**
1022: * Information about foreign key.
1023: */
1024: static class ForeignKey {
1025: /** Name of the primary table. */
1026: private String pkTable;
1027: /** Name of the primary column. */
1028: private String pkColumn;
1029: /** Name of the foreign table. */
1030: private String fkTable;
1031: /** Name of the foreign column. */
1032: private String fkColumn;
1033: /** Determines whether the foreign table has PK */
1034: private boolean valid;
1035:
1036: /**
1037: * Creates new <code>ForeignKey</code>.
1038: *
1039: * @param pkTable primary table.
1040: * @param pkColumn primary column.
1041: * @param fkTable foreign table.
1042: * @param fkColumn foreign column.
1043: */
1044: ForeignKey(String pkTable, String pkColumn, String fkTable,
1045: String fkColumn, boolean valid) {
1046: this .pkTable = pkTable;
1047: this .pkColumn = pkColumn;
1048: this .fkTable = fkTable;
1049: this .fkColumn = fkColumn;
1050: this .valid = valid;
1051: }
1052:
1053: /**
1054: * Returns name of the primary table.
1055: *
1056: * @return name of the primary table.
1057: */
1058: String getPKTable() {
1059: return pkTable;
1060: }
1061:
1062: /**
1063: * Returns name of the primary column.
1064: *
1065: * @return name of the primary column.
1066: */
1067: String getPKColumn() {
1068: return pkColumn;
1069: }
1070:
1071: /**
1072: * Returns name of the foreign table.
1073: *
1074: * @return name of the foreign table.
1075: */
1076: String getFKTable() {
1077: return fkTable;
1078: }
1079:
1080: /**
1081: * Returns name of the foreign column.
1082: *
1083: * @return name of the foreign column.
1084: */
1085: String getFKColumn() {
1086: return fkColumn;
1087: }
1088:
1089: /**
1090: * Determines whether the foreign table has PK.
1091: *
1092: * @return <code>true</code> if the foreign table has primary key,
1093: * returns <code>false</code> otherwise.
1094: */
1095: boolean isValid() {
1096: return valid;
1097: }
1098:
1099: }
1100:
1101: static class ForeignKeyRenderer extends DefaultListCellRenderer {
1102: private final String FORMAT = NbBundle.getMessage(
1103: ForeignKeyRenderer.class, "FMT_ForeignKey"); //NOI18N
1104: private final String NO_PK = NbBundle.getMessage(
1105: ForeignKeyRenderer.class, "FMT_NoPrimaryKey"); //NOI18N
1106:
1107: @Override
1108: public Component getListCellRendererComponent(JList list,
1109: Object value, int index, boolean isSelected,
1110: boolean cellHasFocus) {
1111: super .getListCellRendererComponent(list, value, index,
1112: isSelected, cellHasFocus);
1113: if (value instanceof ForeignKey) {
1114: ForeignKey key = (ForeignKey) value;
1115: String message = key.isValid() ? "" : NO_PK; // NOI18N
1116: String label = MessageFormat.format(FORMAT, key
1117: .getPKTable(), key.getPKColumn(), key
1118: .getFKTable(), key.getFKColumn(), message);
1119: setText(label);
1120: setEnabled(key.isValid());
1121: }
1122: return this;
1123: }
1124: }
1125:
1126: }
|