001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project.editors;
021    
022    import java.awt.BorderLayout;
023    import java.awt.FlowLayout;
024    import java.awt.Frame;
025    import java.awt.event.ActionEvent;
026    
027    import javax.swing.AbstractAction;
028    import javax.swing.JButton;
029    import javax.swing.JDialog;
030    import javax.swing.JLabel;
031    import javax.swing.JPanel;
032    import javax.swing.JScrollPane;
033    import javax.swing.JTable;
034    import javax.swing.table.AbstractTableModel;
035    import javax.swing.table.DefaultTableModel;
036    
037    import org.dom4j.Element;
038    import org.mactor.framework.MactorException;
039    import org.mactor.framework.data.DataProviderFactory;
040    import org.mactor.framework.data.DataTable;
041    import org.mactor.ui.gui.GuiUtil;
042    
043    public class DataSourceTestDlg extends JDialog {
044            JTable table = new JTable();
045            JLabel messageLabel = new JLabel("Press \"Load Data\" to verif that data can be read from the specified data source");
046            Element commandElement;
047            JButton okButton = new JButton(new AbstractAction("Ok") {
048                    public void actionPerformed(ActionEvent arg0) {
049                            dispose();
050                    };
051            });
052            JButton loadButton = new JButton(new AbstractAction("Load Data") {
053                    public void actionPerformed(ActionEvent arg0) {
054                            try {
055                                    DataTable data = DataProviderFactory.getDataProvider(commandElement.attributeValue("data_source")).loadData();
056                                    if (data == null) {
057                                            table.setModel(new DefaultTableModel());
058                                            messageLabel.setVisible(true);
059                                            messageLabel.setText("The data source specification is OK, but no data was found");
060                                    } else {
061                                            table.setVisible(true);
062                                            messageLabel.setVisible(false);
063                                            table.setModel(new TestResultTableModel(data));
064                                    }
065                            } catch (MactorException me) {
066                                    GuiUtil.showGuiError(DataSourceTestDlg.this, me);
067                            }
068                    };
069            });
070            private String evaluateCommand() {
071                    return null;
072            }
073            public DataSourceTestDlg(Frame parent, Element commandElement) throws MactorException {
074                    super(parent);
075                    setLayout(new BorderLayout());
076                    setModal(true);
077                    setTitle("Test Data Source");
078                    this.commandElement = commandElement;
079                    SimpleFormPanel form = new SimpleFormPanel();
080                    JPanel bp = new JPanel(new FlowLayout());
081                    bp.add(loadButton);
082                    form.add(bp);
083                    SimpleFormPanel resultForm = new SimpleFormPanel();
084                    resultForm.add(new JLabel("Data:"));
085                    resultForm.add(new JScrollPane(table));
086                    resultForm.add(messageLabel);
087                    form.add(resultForm);
088                    add(form, BorderLayout.CENTER);
089                    JPanel buttonPanel = new JPanel(new FlowLayout());
090                    buttonPanel.add(okButton);
091                    add(buttonPanel, BorderLayout.SOUTH);
092                    pack();
093            }
094            private class TestResultTableModel extends AbstractTableModel {
095                    private int columnCount = 0;
096                    private DataTable data;
097                    public TestResultTableModel(DataTable data) {
098                            this.columnCount = data.getColumnCount();
099                            this.data = data;
100                    }
101                    @Override
102                    public String getColumnName(int index) {
103                            return data.getColumn(index);
104                    }
105                    public int getColumnCount() {
106                            return columnCount;
107                    }
108                    public int getRowCount() {
109                            return data.getRowCount();
110                    }
111                    @Override
112                    public Class<?> getColumnClass(int arg0) {
113                            return String.class;
114                    }
115                    public Object getValueAt(int row, int col) {
116                            return data.getValue(row, col);
117                    }
118            }
119    }