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:
042: package org.netbeans.modules.dbschema.jdbcimpl.wizard;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.awt.event.KeyEvent;
047: import java.sql.Connection;
048: import java.sql.ResultSet;
049: import java.sql.SQLException;
050: import java.util.ArrayList;
051: import java.util.LinkedList;
052: import java.util.List;
053: import java.util.ResourceBundle;
054: import java.util.logging.Level;
055: import java.util.logging.Logger;
056:
057: import javax.swing.*;
058: import javax.swing.event.*;
059: import org.netbeans.api.db.explorer.ConnectionManager;
060: import org.netbeans.api.db.explorer.DatabaseConnection;
061: import org.netbeans.api.progress.ProgressHandle;
062: import org.netbeans.api.progress.ProgressHandleFactory;
063: import org.openide.util.NbBundle;
064:
065: import org.netbeans.modules.dbschema.jdbcimpl.DDLBridge;
066: import org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider;
067: import org.openide.util.Exceptions;
068: import org.openide.util.Mutex;
069: import org.openide.util.RequestProcessor;
070:
071: public class DBSchemaTablesPanel extends JPanel implements
072: ListDataListener {
073:
074: private static final Logger LOGGER = Logger
075: .getLogger("org.netbeans.modules.dbschema.jdbcimpl.wizard"); // NOI18N
076: private static final boolean LOG = LOGGER.isLoggable(Level.INFO);
077:
078: private final ResourceBundle bundle = NbBundle
079: .getBundle("org.netbeans.modules.dbschema.jdbcimpl.resources.Bundle"); //NOI18N
080:
081: private LinkedList tables;
082: private LinkedList views;
083: private ConnectionProvider cp;
084: private String schema;
085: private String driver;
086:
087: private DatabaseConnection dbconnOld;
088: private Connection conn;
089:
090: private DBSchemaWizardData data;
091:
092: private int tablesCount;
093:
094: private ArrayList list;
095:
096: /** Creates new form DBSchemaTablesPanel */
097: public DBSchemaTablesPanel(DBSchemaWizardData data, ArrayList list) {
098: this .list = list;
099: this .data = data;
100: tables = new LinkedList();
101: views = new LinkedList();
102: cp = null;
103:
104: putClientProperty("WizardPanel_contentSelectedIndex",
105: new Integer(2)); //NOI18N
106: setName(bundle.getString("TablesChooser")); //NOI18N
107:
108: initComponents();
109: initAccessibility();
110:
111: jListAvailableTables.registerKeyboardAction(
112: new ActionListener() {
113: public void actionPerformed(ActionEvent e) {
114: jListAvailableTables.requestFocus();
115: }
116: }, KeyStroke.getKeyStroke(KeyEvent.VK_T,
117: KeyEvent.ALT_MASK),
118: JComponent.WHEN_IN_FOCUSED_WINDOW);
119: jListSelectedTables.registerKeyboardAction(
120: new ActionListener() {
121: public void actionPerformed(ActionEvent e) {
122: jListSelectedTables.requestFocus();
123: }
124: }, KeyStroke.getKeyStroke(KeyEvent.VK_S,
125: KeyEvent.ALT_MASK),
126: JComponent.WHEN_IN_FOCUSED_WINDOW);
127:
128: jListSelectedTables.getModel().addListDataListener(this );
129:
130: (jListAvailableTables.getSelectionModel())
131: .addListSelectionListener(new ListSelectionListener() {
132: public void valueChanged(ListSelectionEvent e) {
133: jButtonAdd.setEnabled(jListAvailableTables
134: .getSelectedIndex() == -1 ? false
135: : true);
136: }
137: });
138: (jListSelectedTables.getSelectionModel())
139: .addListSelectionListener(new ListSelectionListener() {
140: public void valueChanged(ListSelectionEvent e) {
141: jButtonRemove.setEnabled(jListSelectedTables
142: .getSelectedIndex() == -1 ? false
143: : true);
144: }
145: });
146: }
147:
148: protected boolean init() {
149:
150: List handlers = new ArrayList();
151: Parameters params = new Parameters();
152:
153: boolean init = true;
154:
155: if (data.getConnectionProvider() != null) {
156: if (data.getDatabaseConnection() == dbconnOld)
157: init = false;
158:
159: if (init) {
160: cp = data.getConnectionProvider();
161: handlers.add(new Handler() {
162: public void handle(Parameters params) {
163: uninit();
164: }
165:
166: public String getMessage() {
167: return NbBundle.getMessage(
168: DBSchemaTablesPanel.class,
169: "MSG_ClosingPrevious");
170: }
171: });
172: }
173: }
174:
175: if (!init) {
176: updateButtons();
177: return true;
178: }
179:
180: data.setConnected(false);
181: if (!data.isExistingConn()) {
182: return false;
183: }
184:
185: // the init starts here
186:
187: final DatabaseConnection dbconn = data.getDatabaseConnection();
188: conn = dbconn.getJDBCConnection();
189:
190: //fix for bug #4746507 - if the connection was broken outside of the IDE, set the connection to null and try to reconnect
191: if (conn != null) {
192: handlers.add(new Handler() {
193: public void handle(Parameters params) {
194: try {
195: conn.getCatalog(); //test if the connection is alive - if it is alive, it should return something
196: } catch (SQLException exc) {
197: conn = null;
198: }
199: }
200:
201: public String getMessage() {
202: return NbBundle.getMessage(
203: DBSchemaTablesPanel.class,
204: "MSG_CheckingExisting");
205: }
206: });
207: }
208:
209: handlers.add(new Handler() {
210: public void handle(Parameters params) {
211: ConnectionManager.getDefault().showConnectionDialog(
212: dbconn);
213: conn = dbconn.getJDBCConnection();
214: }
215:
216: public boolean getRunInEDT() {
217: return true;
218: }
219:
220: public boolean isRunnable() {
221: return conn == null;
222: }
223: });
224:
225: handlers.add(new Handler() {
226: public void handle(Parameters params) {
227:
228: //fix for bug #4746507 - if the connection was broken outside of the IDE, set the connection to null and try to reconnect
229: if (conn != null) {
230: try {
231: conn.getCatalog(); //test if the connection is alive - if it is alive, it should return something
232: } catch (SQLException exc) {
233: conn = null;
234: data.setConnected(false);
235: params.setResult(false);
236: return;
237: }
238: }
239:
240: data.setConnected(true);
241:
242: schema = dbconn.getSchema();
243: driver = dbconn.getDriverClass();
244:
245: dbconnOld = dbconn;
246:
247: try {
248: if (conn == null) {
249: params.setResult(false);
250: return;
251: }
252:
253: cp = new ConnectionProvider(conn, driver);
254: cp.setSchema(schema);
255: } catch (SQLException exc) {
256: //PENDING
257: }
258:
259: data.setConnectionProvider(cp);
260:
261: tables.clear();
262: views.clear();
263:
264: try {
265: DDLBridge bridge = new DDLBridge(
266: cp.getConnection(), cp.getSchema(), cp
267: .getDatabaseMetaData());
268:
269: ResultSet rs;
270: bridge.getDriverSpecification().getTables("%",
271: new String[] { "TABLE" }); //NOI18N
272: rs = bridge.getDriverSpecification().getResultSet();
273: if (rs != null) {
274: while (rs.next()) {
275: tables.add(rs.getString("TABLE_NAME")
276: .trim()); //NOI18N
277: }
278: rs.close();
279: }
280:
281: rs = null;
282: if (bridge.getDriverSpecification()
283: .areViewsSupported()) {
284: bridge.getDriverSpecification().getTables("%",
285: new String[] { "VIEW" }); //NOI18N
286: rs = bridge.getDriverSpecification()
287: .getResultSet();
288: }
289: if (rs != null) {
290: while (rs.next()) {
291: views
292: .add(rs.getString("TABLE_NAME")
293: .trim()); //NOI18N
294: }
295: rs.close();
296: }
297: } catch (SQLException exc) {
298: Exceptions.printStackTrace(exc);
299: }
300:
301: ((SortedListModel) jListAvailableTables.getModel())
302: .clear();
303: ((SortedListModel) jListSelectedTables.getModel())
304: .clear();
305:
306: tablesCount = tables.size();
307:
308: for (int i = 0; i < tables.size(); i++) {
309: ((SortedListModel) jListAvailableTables.getModel())
310: .add(bundle.getString("TablePrefix") + " "
311: + tables.get(i).toString()); //NOI18N
312: }
313: for (int i = 0; i < views.size(); i++) {
314: ((SortedListModel) jListAvailableTables.getModel())
315: .add(bundle.getString("ViewPrefix") + " "
316: + views.get(i).toString()); //NOI18N
317: }
318: if (jListAvailableTables.getModel().getSize() > 0) {
319: jListAvailableTables.setSelectedIndex(0);
320: }
321: tables.clear();
322: views.clear();
323:
324: params.setResult(true);
325: }
326:
327: public String getMessage() {
328: return NbBundle.getMessage(DBSchemaTablesPanel.class,
329: "MSG_RetrievingTables");
330: }
331:
332: public boolean isRunnable() {
333: return conn != null;
334: }
335: });
336:
337: invokeHandlers(handlers, params);
338:
339: updateButtons();
340:
341: return params.getResult();
342: }
343:
344: private void invokeHandlers(final List/*<Handler>*/handlers,
345: final Parameters params) {
346: final ProgressPanel progressPanel = new ProgressPanel();
347:
348: ProgressHandle progressHandle = ProgressHandleFactory
349: .createHandle(null);
350: JComponent progressComponent = ProgressHandleFactory
351: .createProgressComponent(progressHandle);
352:
353: progressHandle.start();
354: progressHandle.switchToIndeterminate();
355:
356: final int[] index = new int[1];
357:
358: try {
359: RequestProcessor.Task task = RequestProcessor.getDefault()
360: .create(new Runnable() {
361: public void run() {
362: index[0] = invokeHandlers(handlers,
363: index[0], params, progressPanel);
364: SwingUtilities.invokeLater(new Runnable() {
365: public void run() {
366: progressPanel.close();
367: }
368: });
369: }
370: });
371:
372: while (index[0] < handlers.size()) {
373: index[0] = invokeHandlers(handlers, index[0], params,
374: null);
375: if (index[0] < handlers.size()) {
376: task.schedule(0);
377: progressPanel.open(progressComponent);
378: }
379: }
380: } finally {
381: progressHandle.finish();
382: }
383: }
384:
385: private int invokeHandlers(List/*<Handler>*/handlers, int start,
386: Parameters params, final ProgressPanel progressPanel) {
387: boolean isEDT = SwingUtilities.isEventDispatchThread();
388: int i;
389:
390: for (i = start; i < handlers.size(); i++) {
391: Handler h = (Handler) handlers.get(i);
392: if (!h.isRunnable()) {
393: if (LOG) {
394: LOGGER.log(Level.FINE, "Skipping " + h);
395: }
396: continue;
397: }
398: if (h.getRunInEDT() != isEDT) {
399: break;
400: }
401: if (LOG) {
402: LOGGER.log(Level.FINE, "Invoking " + h);
403: }
404: if (progressPanel != null) {
405: final String message = h.getMessage();
406: if (message != null) {
407: Mutex.EVENT.readAccess(new Runnable() {
408: public void run() {
409: progressPanel.setText(message);
410: }
411: });
412: }
413: }
414: h.handle(params);
415: }
416:
417: return i;
418: }
419:
420: public void uninit() {
421: try {
422: if (cp != null)
423: if (data.isConnected())
424: if (data.isExistingConn())
425: ConnectionManager.getDefault().disconnect(
426: dbconnOld);
427: else if (dbconnOld.getJDBCConnection() != null)
428: ConnectionManager.getDefault().disconnect(
429: dbconnOld);
430: else
431: cp.closeConnection();
432: } catch (Exception exc) {
433: //unable to disconnect
434: }
435: }
436:
437: private void initAccessibility() {
438: this .getAccessibleContext().setAccessibleDescription(
439: bundle.getString("ACS_TablesPanelA11yDesc")); // NOI18N
440: jLabelAvailableTables
441: .getAccessibleContext()
442: .setAccessibleDescription(
443: bundle.getString("ACS_AvailableTablesA11yDesc")); // NOI18N
444: jListAvailableTables.getAccessibleContext().setAccessibleName(
445: bundle.getString("ACS_AvailableTablesListA11yName")); // NOI18N
446: jLabelSelectedTables.getAccessibleContext()
447: .setAccessibleDescription(
448: bundle.getString("ACS_SelectedTablesA11yDesc")); // NOI18N
449: jListSelectedTables.getAccessibleContext().setAccessibleName(
450: bundle.getString("ACS_SelectedTablesListA11yName")); // NOI18N
451: jLabelNote.getAccessibleContext().setAccessibleDescription(
452: bundle.getString("ACS_FKReferenceNoteA11yDesc")); // NOI18N
453: }
454:
455: /** This method is called from within the constructor to
456: * initialize the form.
457: * WARNING: Do NOT modify this code. The content of this method is
458: * always regenerated by the Form Editor.
459: */
460: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
461: private void initComponents() {
462: java.awt.GridBagConstraints gridBagConstraints;
463:
464: jLabelAvailableTables = new javax.swing.JLabel();
465: jScrollPaneAvailableTables = new javax.swing.JScrollPane();
466: jListAvailableTables = new javax.swing.JList();
467: jPanelButtons = new javax.swing.JPanel();
468: jButtonAdd = new javax.swing.JButton();
469: jButtonRemove = new javax.swing.JButton();
470: jButtonAddAll = new javax.swing.JButton();
471: jButtonRemoveAll = new javax.swing.JButton();
472: jLabelSelectedTables = new javax.swing.JLabel();
473: jScrollPaneSelectedTables = new javax.swing.JScrollPane();
474: jListSelectedTables = new javax.swing.JList();
475: jLabelNote = new javax.swing.JLabel();
476:
477: FormListener formListener = new FormListener();
478:
479: setPreferredSize(new java.awt.Dimension(400, 199));
480: setLayout(new java.awt.GridBagLayout());
481:
482: jLabelAvailableTables.setLabelFor(jListAvailableTables);
483: org.openide.awt.Mnemonics.setLocalizedText(
484: jLabelAvailableTables, bundle
485: .getString("AvailableTables")); // NOI18N
486: gridBagConstraints = new java.awt.GridBagConstraints();
487: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
488: gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 0);
489: add(jLabelAvailableTables, gridBagConstraints);
490:
491: jListAvailableTables.setToolTipText(bundle
492: .getString("ACS_AvailableTablesListA11yDesc")); // NOI18N
493: jListAvailableTables.setModel(new SortedListModel());
494: jScrollPaneAvailableTables
495: .setViewportView(jListAvailableTables);
496:
497: gridBagConstraints = new java.awt.GridBagConstraints();
498: gridBagConstraints.gridx = 0;
499: gridBagConstraints.gridy = 1;
500: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
501: gridBagConstraints.weightx = 1.0;
502: gridBagConstraints.weighty = 1.0;
503: gridBagConstraints.insets = new java.awt.Insets(11, 12, 0, 0);
504: add(jScrollPaneAvailableTables, gridBagConstraints);
505:
506: jPanelButtons.setLayout(new java.awt.GridBagLayout());
507:
508: org.openide.awt.Mnemonics.setLocalizedText(jButtonAdd, bundle
509: .getString("AddButton")); // NOI18N
510: jButtonAdd.setToolTipText(bundle
511: .getString("ACS_AddButtonA11yDesc")); // NOI18N
512: jButtonAdd.addActionListener(formListener);
513: gridBagConstraints = new java.awt.GridBagConstraints();
514: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
515: jPanelButtons.add(jButtonAdd, gridBagConstraints);
516:
517: jButtonRemove.setToolTipText(bundle
518: .getString("ACS_RemoveButtonA11yDesc")); // NOI18N
519: org.openide.awt.Mnemonics.setLocalizedText(jButtonRemove,
520: bundle.getString("RemoveButton")); // NOI18N
521: jButtonRemove.addActionListener(formListener);
522: gridBagConstraints = new java.awt.GridBagConstraints();
523: gridBagConstraints.gridx = 0;
524: gridBagConstraints.gridy = 1;
525: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
526: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
527: jPanelButtons.add(jButtonRemove, gridBagConstraints);
528:
529: jButtonAddAll.setToolTipText(bundle
530: .getString("ACS_AddAllButtonA11yDesc")); // NOI18N
531: org.openide.awt.Mnemonics.setLocalizedText(jButtonAddAll,
532: bundle.getString("AddAllButton")); // NOI18N
533: jButtonAddAll.addActionListener(formListener);
534: gridBagConstraints = new java.awt.GridBagConstraints();
535: gridBagConstraints.gridx = 0;
536: gridBagConstraints.gridy = 2;
537: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
538: gridBagConstraints.insets = new java.awt.Insets(17, 0, 0, 0);
539: jPanelButtons.add(jButtonAddAll, gridBagConstraints);
540:
541: jButtonRemoveAll.setToolTipText(bundle
542: .getString("ACS_RemoveAllButtonA11yDesc")); // NOI18N
543: org.openide.awt.Mnemonics.setLocalizedText(jButtonRemoveAll,
544: bundle.getString("RemoveAllButton")); // NOI18N
545: jButtonRemoveAll.addActionListener(formListener);
546: gridBagConstraints = new java.awt.GridBagConstraints();
547: gridBagConstraints.gridx = 0;
548: gridBagConstraints.gridy = 3;
549: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
550: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
551: jPanelButtons.add(jButtonRemoveAll, gridBagConstraints);
552:
553: gridBagConstraints = new java.awt.GridBagConstraints();
554: gridBagConstraints.gridx = 1;
555: gridBagConstraints.gridy = 0;
556: gridBagConstraints.gridheight = 2;
557: gridBagConstraints.insets = new java.awt.Insets(12, 11, 0, 11);
558: add(jPanelButtons, gridBagConstraints);
559:
560: jLabelSelectedTables.setLabelFor(jListSelectedTables);
561: org.openide.awt.Mnemonics.setLocalizedText(
562: jLabelSelectedTables, bundle
563: .getString("SelectedTables")); // NOI18N
564: gridBagConstraints = new java.awt.GridBagConstraints();
565: gridBagConstraints.gridx = 2;
566: gridBagConstraints.gridy = 0;
567: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
568: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 11);
569: add(jLabelSelectedTables, gridBagConstraints);
570:
571: jListSelectedTables.setModel(new SortedListModel());
572: jListSelectedTables.setToolTipText(bundle
573: .getString("ACS_SelectedTablesListA11yDesc")); // NOI18N
574: jScrollPaneSelectedTables.setViewportView(jListSelectedTables);
575:
576: gridBagConstraints = new java.awt.GridBagConstraints();
577: gridBagConstraints.gridx = 2;
578: gridBagConstraints.gridy = 1;
579: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
580: gridBagConstraints.weightx = 1.0;
581: gridBagConstraints.weighty = 1.0;
582: gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 11);
583: add(jScrollPaneSelectedTables, gridBagConstraints);
584:
585: org.openide.awt.Mnemonics.setLocalizedText(jLabelNote, bundle
586: .getString("FKReferenceNote")); // NOI18N
587: gridBagConstraints = new java.awt.GridBagConstraints();
588: gridBagConstraints.gridx = 0;
589: gridBagConstraints.gridy = 2;
590: gridBagConstraints.gridwidth = 3;
591: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
592: gridBagConstraints.insets = new java.awt.Insets(11, 12, 0, 11);
593: add(jLabelNote, gridBagConstraints);
594: }
595:
596: // Code for dispatching events from components to event handlers.
597:
598: private class FormListener implements java.awt.event.ActionListener {
599: FormListener() {
600: }
601:
602: public void actionPerformed(java.awt.event.ActionEvent evt) {
603: if (evt.getSource() == jButtonAdd) {
604: DBSchemaTablesPanel.this .jButtonAddActionPerformed(evt);
605: } else if (evt.getSource() == jButtonRemove) {
606: DBSchemaTablesPanel.this
607: .jButtonRemoveActionPerformed(evt);
608: } else if (evt.getSource() == jButtonAddAll) {
609: DBSchemaTablesPanel.this
610: .jButtonAddAllActionPerformed(evt);
611: } else if (evt.getSource() == jButtonRemoveAll) {
612: DBSchemaTablesPanel.this
613: .jButtonRemoveAllActionPerformed(evt);
614: }
615: }
616: }// </editor-fold>//GEN-END:initComponents
617:
618: private void jButtonRemoveAllActionPerformed(
619: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveAllActionPerformed
620: SortedListModel ulm = (SortedListModel) jListAvailableTables
621: .getModel();
622: SortedListModel slm = (SortedListModel) jListSelectedTables
623: .getModel();
624:
625: Object[] values = slm.toArray();
626: for (int i = 0; i < values.length; i++) {
627: ulm.add(values[i]);
628: slm.remove(values[i]);
629: }
630:
631: tables.clear();
632: views.clear();
633:
634: int[] sel = new int[values.length];
635: for (int i = 0; i < values.length; i++)
636: sel[i] = ulm.indexOf(values[i]);
637: jListAvailableTables.setSelectedIndices(sel);
638:
639: setSelection();
640: updateButtons();
641: }//GEN-LAST:event_jButtonRemoveAllActionPerformed
642:
643: private void jButtonAddAllActionPerformed(
644: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddAllActionPerformed
645: SortedListModel ulm = (SortedListModel) jListAvailableTables
646: .getModel();
647: SortedListModel slm = (SortedListModel) jListSelectedTables
648: .getModel();
649:
650: String name;
651: Object[] values = ulm.toArray();
652: for (int i = 0; i < values.length; i++) {
653: slm.add(values[i]);
654: ulm.remove(values[i]);
655:
656: name = values[i].toString();
657: if (name.startsWith(bundle.getString("TablePrefix"))) //NOI18N
658: tables.add(name.substring(name.indexOf(" ") + 1)); //NOI18N
659: else
660: views.add(name.substring(name.indexOf(" ") + 1)); //NOI18N
661: }
662:
663: int[] sel = new int[values.length];
664: for (int i = 0; i < values.length; i++)
665: sel[i] = slm.indexOf(values[i]);
666: jListSelectedTables.setSelectedIndices(sel);
667:
668: setSelection();
669: updateButtons();
670: }//GEN-LAST:event_jButtonAddAllActionPerformed
671:
672: private void jButtonRemoveActionPerformed(
673: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
674: SortedListModel ulm = (SortedListModel) jListAvailableTables
675: .getModel();
676: SortedListModel slm = (SortedListModel) jListSelectedTables
677: .getModel();
678:
679: String name;
680: Object[] values = jListSelectedTables.getSelectedValues();
681: for (int i = 0; i < values.length; i++) {
682: ulm.add(values[i]);
683: slm.remove(values[i]);
684:
685: name = values[i].toString();
686: name = (name.substring(name.indexOf(" "))).trim(); //NOI18N
687: if (tables.contains(name))
688: tables.remove(name);
689: else
690: views.remove(name);
691: }
692:
693: int[] sel = new int[values.length];
694: for (int i = 0; i < values.length; i++)
695: sel[i] = ulm.indexOf(values[i]);
696: jListAvailableTables.setSelectedIndices(sel);
697:
698: setSelection();
699: updateButtons();
700: }//GEN-LAST:event_jButtonRemoveActionPerformed
701:
702: private void jButtonAddActionPerformed(
703: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
704: SortedListModel ulm = (SortedListModel) jListAvailableTables
705: .getModel();
706: SortedListModel slm = (SortedListModel) jListSelectedTables
707: .getModel();
708:
709: String name;
710: Object[] values = jListAvailableTables.getSelectedValues();
711: for (int i = 0; i < values.length; i++) {
712: slm.add(values[i]);
713: ulm.remove(values[i]);
714:
715: name = values[i].toString();
716: if (name.startsWith(bundle.getString("TablePrefix"))) //NOI18N
717: tables.add(name.substring(name.indexOf(" ") + 1)); //NOI18N
718: else
719: views.add(name.substring(name.indexOf(" ") + 1)); //NOI18N
720: }
721:
722: int[] sel = new int[values.length];
723: for (int i = 0; i < values.length; i++)
724: sel[i] = slm.indexOf(values[i]);
725: jListSelectedTables.setSelectedIndices(sel);
726:
727: setSelection();
728: updateButtons();
729: }//GEN-LAST:event_jButtonAddActionPerformed
730:
731: // Variables declaration - do not modify//GEN-BEGIN:variables
732: private javax.swing.JButton jButtonAdd;
733: private javax.swing.JButton jButtonAddAll;
734: private javax.swing.JButton jButtonRemove;
735: private javax.swing.JButton jButtonRemoveAll;
736: private javax.swing.JLabel jLabelAvailableTables;
737: private javax.swing.JLabel jLabelNote;
738: private javax.swing.JLabel jLabelSelectedTables;
739: private javax.swing.JList jListAvailableTables;
740: private javax.swing.JList jListSelectedTables;
741: private javax.swing.JPanel jPanelButtons;
742: private javax.swing.JScrollPane jScrollPaneAvailableTables;
743: private javax.swing.JScrollPane jScrollPaneSelectedTables;
744:
745: // End of variables declaration//GEN-END:variables
746:
747: private void setSelection() {
748: data.setTables(tables);
749: data.setViews(views);
750:
751: if (tablesCount == tables.size())
752: data.setAllTables(true);
753: else
754: data.setAllTables(false);
755: }
756:
757: private void updateButtons() {
758: jButtonAdd
759: .setEnabled(jListAvailableTables.getSelectedIndex() == -1 ? false
760: : true);
761: jButtonAddAll
762: .setEnabled(((SortedListModel) jListAvailableTables
763: .getModel()).isEmpty() ? false : true);
764: jButtonRemove
765: .setEnabled(jListSelectedTables.getSelectedIndex() == -1 ? false
766: : true);
767: jButtonRemoveAll
768: .setEnabled(((SortedListModel) jListSelectedTables
769: .getModel()).isEmpty() ? false : true);
770: }
771:
772: public boolean isValid() {
773: if (jListSelectedTables.getModel().getSize() > 0)
774: return true;
775: else
776: return false;
777: }
778:
779: public void intervalAdded(javax.swing.event.ListDataEvent p1) {
780: fireChange(this );
781: }
782:
783: public void intervalRemoved(javax.swing.event.ListDataEvent p1) {
784: fireChange(this );
785: }
786:
787: public void contentsChanged(javax.swing.event.ListDataEvent p1) {
788: fireChange(this );
789: }
790:
791: public void fireChange(Object source) {
792: ArrayList lst;
793:
794: synchronized (this ) {
795: lst = (ArrayList) this .list.clone();
796: }
797:
798: ChangeEvent event = new ChangeEvent(source);
799: for (int i = 0; i < lst.size(); i++) {
800: ChangeListener listener = (ChangeListener) lst.get(i);
801: listener.stateChanged(event);
802: }
803: }
804:
805: private static abstract class Handler {
806:
807: public abstract void handle(Parameters params);
808:
809: public String getMessage() {
810: return null;
811: }
812:
813: public boolean getRunInEDT() {
814: return false;
815: }
816:
817: public boolean isRunnable() {
818: return true;
819: }
820:
821: public String toString() {
822: return "Handler[message='" + getMessage() + "',runInEDT="
823: + getRunInEDT() + ",runnable=" + isRunnable() + "]"; // NOI18N
824: }
825: }
826:
827: private static final class Parameters {
828:
829: private boolean result;
830:
831: public boolean getResult() {
832: return result;
833: }
834:
835: public void setResult(boolean result) {
836: this.result = result;
837: }
838: }
839: }
|