/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2009 Pentaho Corporation. All rights reserved.
*/
package org.pentaho.reporting.ui.datasources.olap4j;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.pentaho.reporting.engine.classic.core.DataFactory;
import org.pentaho.reporting.engine.classic.core.ReportDataFactoryException;
import org.pentaho.reporting.engine.classic.core.AbstractReportDefinition;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.ReportEnvironment;
import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot;
import org.pentaho.reporting.engine.classic.core.DefaultReportEnvironment;
import org.pentaho.reporting.engine.classic.core.designtime.DesignTimeContext;
import org.pentaho.reporting.engine.classic.core.designtime.DesignTimeUtil;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.ExceptionDialog;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.KeyedComboBoxModel;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.SwingUtil;
import org.pentaho.reporting.engine.classic.extensions.datasources.olap4j.AbstractMDXDataFactory;
import org.pentaho.reporting.engine.classic.extensions.datasources.olap4j.AbstractNamedMDXDataFactory;
import org.pentaho.reporting.engine.classic.extensions.datasources.olap4j.connections.OlapConnectionProvider;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
import org.pentaho.reporting.libraries.base.config.Configuration;
import org.pentaho.reporting.libraries.designtime.swing.BorderlessButton;
import org.pentaho.reporting.libraries.designtime.swing.background.DataPreviewDialog;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.ui.datasources.jdbc.connection.JdbcConnectionDefinition;
import org.pentaho.reporting.ui.datasources.jdbc.connection.JdbcConnectionDefinitionManager;
import org.pentaho.reporting.ui.datasources.jdbc.ui.ConnectionPanel;
import org.pentaho.reporting.ui.datasources.jdbc.ui.NamedDataSourceDialogModel;
/**
* @author Michael D'Amour
*/
public abstract class Olap4JDataSourceEditor extends JDialog
{
private class QueryTextFieldActivationHandler implements PropertyChangeListener
{
private QueryTextFieldActivationHandler()
{
}
public void propertyChange(final PropertyChangeEvent evt)
{
final NamedDataSourceDialogModel dialogModel = getDialogModel();
queryTextArea.setEnabled(dialogModel.isQuerySelected());
}
}
private class QueryDocumentListener implements DocumentListener
{
private QueryDocumentListener()
{
}
public void insertUpdate(final DocumentEvent e)
{
update();
}
public void removeUpdate(final DocumentEvent e)
{
update();
}
public void changedUpdate(final DocumentEvent e)
{
update();
}
private void update()
{
final NamedDataSourceDialogModel dialogModel = getDialogModel();
final KeyedComboBoxModel model = dialogModel.getQueries();
final int itemIndex = model.getSelectedItemIndex();
if (itemIndex == -1)
{
return;
}
model.update(itemIndex, getQuery(), getQueryName());
}
}
private class LimitRowsCheckBoxActionListener extends AbstractAction implements ItemListener
{
private JSpinner maxPreviewRowsSpinner;
private LimitRowsCheckBoxActionListener(final JSpinner maxPreviewRowsSpinner)
{
this.maxPreviewRowsSpinner = maxPreviewRowsSpinner;
putValue(Action.NAME, Messages.getString("Olap4JDataSourceEditor.LimitRowsCheckBox"));
putValue(Action.MNEMONIC_KEY, Messages.getMnemonic("Olap4JDataSourceEditor.LimitRowsCheckBox.Mnemonic"));
maxPreviewRowsSpinner.setEnabled(false);
}
public void itemStateChanged(final ItemEvent e)
{
final Object source = e.getSource();
if (source instanceof AbstractButton)
{
final AbstractButton b = (AbstractButton) source;
maxPreviewRowsSpinner.setEnabled(b.isSelected());
}
}
public void actionPerformed(final ActionEvent e)
{
final Object source = e.getSource();
if (source instanceof AbstractButton)
{
final AbstractButton b = (AbstractButton) source;
maxPreviewRowsSpinner.setEnabled(b.isSelected());
}
}
}
private class PreviewAction extends AbstractAction implements PropertyChangeListener
{
private PreviewAction()
{
putValue(Action.NAME, Messages.getString("Olap4JDataSourceEditor.Preview.Name"));
setEnabled(dialogModel.isConnectionSelected() && dialogModel.isQuerySelected());
}
public void propertyChange(final PropertyChangeEvent evt)
{
setEnabled(dialogModel.isConnectionSelected() && dialogModel.isQuerySelected());
}
public void actionPerformed(final ActionEvent evt)
{
final JdbcConnectionDefinition connectionDefinition =
(JdbcConnectionDefinition) dialogModel.getConnections().getSelectedItem();
if (connectionDefinition == null)
{
return;
}
try
{
final String query = queryNameTextField.getText();
Integer theMaxRows = 0;
if (maxPreviewRowsSpinner.isEnabled())
{
theMaxRows = (Integer) maxPreviewRowsSpinner.getValue();
}
if (previewDialog == null)
{
previewDialog = new DataPreviewDialog(Olap4JDataSourceEditor.this);
}
final AbstractMDXDataFactory dataFactory = createAndConfigureDataFactory();
final AbstractReportDefinition report = context.getReport();
final MasterReport masterReport = DesignTimeUtil.getMasterReport(report);
final Configuration configuration;
final ResourceKey contentBase;
final ReportEnvironment reportEnvironment;
if (masterReport == null)
{
contentBase = null;
configuration = ClassicEngineBoot.getInstance().getGlobalConfig();
reportEnvironment = new DefaultReportEnvironment(configuration);
}
else
{
contentBase = masterReport.getContentBase();
configuration = masterReport.getConfiguration();
reportEnvironment = masterReport.getReportEnvironment();
}
dataFactory.initialize(configuration,
report.getResourceManager(), contentBase, MasterReport.computeAndInitResourceBundleFactory
(report.getResourceBundleFactory(), reportEnvironment));
final Olap4JPreviewWorker worker = new Olap4JPreviewWorker(dataFactory, query, 0, theMaxRows);
previewDialog.showData(worker);
final ReportDataFactoryException factoryException = worker.getException();
if (factoryException != null)
{
ExceptionDialog.showExceptionDialog(Olap4JDataSourceEditor.this,
Messages.getString("MondrianDataSourceEditor.PreviewError.Title"),
Messages.getString("MondrianDataSourceEditor.PreviewError.Message"), factoryException);
}
}
catch (Exception e)
{
ExceptionDialog.showExceptionDialog(Olap4JDataSourceEditor.this,
Messages.getString("MondrianDataSourceEditor.PreviewError.Title"),
Messages.getString("MondrianDataSourceEditor.PreviewError.Message"), e);
}
}
}
private class QueryNameListSelectionListener implements ListSelectionListener
{
private QueryNameListSelectionListener()
{
}
public void valueChanged(final ListSelectionEvent e)
{
getDialogModel().getQueries().setSelectedItem(queryNameList.getSelectedValue());
final boolean querySelected = queryNameList.getSelectedIndex() != -1;
queryNameTextField.setEnabled(querySelected);
}
}
private class CancelAction extends AbstractAction
{
/**
* Defines an <code>Action</code> object with a default
* description string and default icon.
*/
private CancelAction()
{
putValue(Action.NAME, Messages.getString("Olap4JDataSourceEditor.Cancel.Name"));
}
public void actionPerformed(final ActionEvent e)
{
dispose();
}
}
private class ConfirmAction extends AbstractAction implements PropertyChangeListener
{
/**
* Defines an <code>Action</code> object with a default
* description string and default icon.
*/
private ConfirmAction()
{
putValue(Action.NAME, Messages.getString("Olap4JDataSourceEditor.OK.Name"));
}
public void propertyChange(final PropertyChangeEvent evt)
{
final NamedDataSourceDialogModel dialogModel = getDialogModel();
setEnabled(dialogModel.isConnectionSelected());
}
public void actionPerformed(final ActionEvent e)
{
confirmed = true;
dispose();
}
}
private class AddQueryAction extends AbstractAction
{
protected AddQueryAction()
{
final URL resource = ConnectionPanel.class.getResource
("/org/pentaho/reporting/ui/datasources/olap4j/resources/Add.png");
if (resource != null)
{
putValue(Action.SMALL_ICON, new ImageIcon(resource));
}
else
{
putValue(Action.NAME, Messages.getString("Olap4JDataSourceEditor.AddQuery.Name"));
}
putValue(Action.SHORT_DESCRIPTION, Messages.getString("Olap4JDataSourceEditor.AddQuery.Description"));
}
public void actionPerformed(final ActionEvent e)
{
String queryName = Messages.getString("Olap4JDataSourceEditor.Query");
// Find a unique query name
final NamedDataSourceDialogModel dialogModel = getDialogModel();
for (int i = 1; i < 1000; ++i)
{
final String newQueryName = queryName + ' ' + i;
if (dialogModel.getQueries().findElementIndex(newQueryName) == -1)
{
queryName = newQueryName;
break;
}
}
dialogModel.addQuery(queryName, "");
queryNameList.setSelectedValue(queryName, true);
}
}
private class RemoveQueryAction extends AbstractAction implements PropertyChangeListener
{
protected RemoveQueryAction()
{
final URL resource = ConnectionPanel.class.getResource
("/org/pentaho/reporting/ui/datasources/olap4j/resources/Remove.png");
if (resource != null)
{
putValue(Action.SMALL_ICON, new ImageIcon(resource));
}
else
{
putValue(Action.NAME, Messages.getString("Olap4JDataSourceEditor.RemoveQuery.Name"));
}
putValue(Action.SHORT_DESCRIPTION, Messages.getString("Olap4JDataSourceEditor.RemoveQuery.Description"));
}
public void propertyChange(final PropertyChangeEvent evt)
{
final NamedDataSourceDialogModel dialogModel = getDialogModel();
setEnabled(dialogModel.isQuerySelected());
}
public void actionPerformed(final ActionEvent e)
{
final NamedDataSourceDialogModel dialogModel = getDialogModel();
final KeyedComboBoxModel queries = dialogModel.getQueries();
queries.remove(queries.getSelectedItemIndex());
queries.setSelectedItem(null);
queryNameList.clearSelection();
}
}
private class QueryNameTextFieldDocumentListener implements DocumentListener, ListDataListener
{
private boolean inUpdate;
private QueryNameTextFieldDocumentListener()
{
}
public void intervalAdded(final ListDataEvent e)
{
}
public void intervalRemoved(final ListDataEvent e)
{
}
public void contentsChanged(final ListDataEvent e)
{
if (inUpdate)
{
return;
}
if (e.getIndex0() != -1)
{
return;
}
final NamedDataSourceDialogModel dialogModel = getDialogModel();
try
{
inUpdate = true;
final String currentName = getQueryName();
final String selectedName = (String) dialogModel.getQueries().getSelectedItem();
if (ObjectUtilities.equal(currentName, selectedName) == false)
{
setQueryName(selectedName);
}
setQuery((String) dialogModel.getQueries().getSelectedKey());
}
finally
{
inUpdate = false;
}
}
public void insertUpdate(final DocumentEvent e)
{
update();
}
public void removeUpdate(final DocumentEvent e)
{
update();
}
public void changedUpdate(final DocumentEvent e)
{
update();
}
private void update()
{
if (inUpdate)
{
return;
}
final NamedDataSourceDialogModel dialogModel = getDialogModel();
try
{
final KeyedComboBoxModel model = dialogModel.getQueries();
final int itemIndex = model.getSelectedItemIndex();
if (itemIndex == -1)
{
return;
}
inUpdate = true;
model.update(itemIndex, getQuery(), getQueryName());
}
finally
{
inUpdate = false;
}
}
}
private boolean confirmed;
private JList queryNameList;
private JTextField queryNameTextField;
private JTextArea queryTextArea;
private NamedDataSourceDialogModel dialogModel;
private JSpinner maxPreviewRowsSpinner;
private DataPreviewDialog previewDialog;
private OlapConnectionPanel connectionComponent;
private DesignTimeContext context;
public Olap4JDataSourceEditor(final DesignTimeContext context)
{
init(context);
}
public Olap4JDataSourceEditor(final DesignTimeContext context, final Dialog owner)
{
super(owner);
init(context);
}
public Olap4JDataSourceEditor(final DesignTimeContext context, final Frame owner)
{
super(owner);
init(context);
}
protected void init(final DesignTimeContext designTimeContext)
{
setModal(true);
this.context = designTimeContext;
dialogModel = new NamedDataSourceDialogModel
(new JdbcConnectionDefinitionManager("org/pentaho/reporting/ui/datasources/olap4j/Settings"));
connectionComponent = new OlapConnectionPanel(dialogModel, designTimeContext);
connectionComponent.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 8));
maxPreviewRowsSpinner = new JSpinner(new SpinnerNumberModel(10000, 1, Integer.MAX_VALUE, 1));
queryNameTextField = new JTextField();
queryNameTextField.setColumns(35);
queryNameTextField.setEnabled(dialogModel.isQuerySelected());
final QueryNameTextFieldDocumentListener updateHandler = new QueryNameTextFieldDocumentListener();
queryNameTextField.getDocument().addDocumentListener(updateHandler);
dialogModel.getQueries().addListDataListener(updateHandler);
queryTextArea = new JTextArea((String) null);
queryTextArea.setWrapStyleWord(true);
queryTextArea.setLineWrap(true);
queryTextArea.setRows(10);
queryTextArea.setColumns(50);
queryTextArea.setEnabled(dialogModel.isQuerySelected());
queryTextArea.getDocument().addDocumentListener(new QueryDocumentListener());
dialogModel.addPropertyChangeListener(new QueryTextFieldActivationHandler());
queryNameList = new JList(dialogModel.getQueries());
queryNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
queryNameList.setVisibleRowCount(5);
queryNameList.addListSelectionListener(new QueryNameListSelectionListener());
// Create the connection panel
final JPanel queryContentPanel = new JPanel(new BorderLayout());
queryContentPanel.add(BorderLayout.NORTH, createQueryListPanel());
queryContentPanel.add(BorderLayout.CENTER, createQueryDetailsPanel());
// Create the button panel
final ConfirmAction confirmAction = new ConfirmAction();
dialogModel.addPropertyChangeListener(confirmAction);
// Create the content panel
final JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.add(BorderLayout.WEST, connectionComponent);
contentPanel.add(BorderLayout.CENTER, queryContentPanel);
contentPanel.add(BorderLayout.SOUTH, createPreviewButtonsPanel());
contentPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
final JButton okButton = new JButton(confirmAction);
final JButton cancelButton = new JButton(new CancelAction());
final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
buttonPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
// Create the center panel
final JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(BorderLayout.CENTER, contentPanel);
centerPanel.add(BorderLayout.SOUTH, buttonPanel);
// Return the center panel
setContentPane(centerPanel);
pack();
SwingUtil.centerDialogInParent(this);
}
private JPanel createPreviewButtonsPanel()
{
final JPanel previewButtonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
previewButtonsPanel.add(new JCheckBox(new LimitRowsCheckBoxActionListener(maxPreviewRowsSpinner)));
previewButtonsPanel.add(maxPreviewRowsSpinner);
final PreviewAction thePreviewAction = new PreviewAction();
dialogModel.addPropertyChangeListener(thePreviewAction);
previewButtonsPanel.add(new JButton(thePreviewAction));
return previewButtonsPanel;
}
private JPanel createQueryDetailsPanel()
{
final JPanel theQueryNamePanel = new JPanel(new BorderLayout());
theQueryNamePanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 0, 8));
theQueryNamePanel.add(new JLabel(Messages.getString("Olap4JDataSourceEditor.QueryNameLabel")), BorderLayout.NORTH);
theQueryNamePanel.add(queryNameTextField, BorderLayout.SOUTH);
final JPanel theQueryControlsPanel = new JPanel(new BorderLayout());
theQueryControlsPanel.add(new JLabel(Messages.getString("Olap4JDataSourceEditor.QueryLabel")), BorderLayout.WEST);
final JPanel theQueryPanel = new JPanel(new BorderLayout());
theQueryPanel.add(theQueryControlsPanel, BorderLayout.NORTH);
theQueryPanel.add(new JScrollPane(queryTextArea), BorderLayout.CENTER);
theQueryPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 0, 8));
// Create the query details panel
final JPanel queryDetailsPanel = new JPanel(new BorderLayout());
queryDetailsPanel.add(BorderLayout.NORTH, theQueryNamePanel);
queryDetailsPanel.add(BorderLayout.CENTER, theQueryPanel);
return queryDetailsPanel;
}
private JPanel createQueryListPanel()
{
// Create the query list panel
final RemoveQueryAction queryRemoveAction = new RemoveQueryAction();
dialogModel.addPropertyChangeListener(queryRemoveAction);
final JPanel theQueryButtonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
theQueryButtonsPanel.add(new BorderlessButton(new AddQueryAction()));
theQueryButtonsPanel.add(new BorderlessButton(queryRemoveAction));
final JPanel theQueryControlsPanel = new JPanel(new BorderLayout());
theQueryControlsPanel.add(new JLabel(Messages.getString("Olap4JDataSourceEditor.AvailableQueriesLabel")), BorderLayout.WEST);
theQueryControlsPanel.add(theQueryButtonsPanel, BorderLayout.EAST);
final JPanel queryListPanel = new JPanel(new BorderLayout());
queryListPanel.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 8));
queryListPanel.add(BorderLayout.NORTH, theQueryControlsPanel);
queryListPanel.add(BorderLayout.CENTER, new JScrollPane(queryNameList));
return queryListPanel;
}
public DataFactory performConfiguration(final AbstractNamedMDXDataFactory dataFactory, final String selectedQuery)
{
// Reset the ok / cancel flag
dialogModel.clear();
connectionComponent.setRoleField(null);
confirmed = false;
// Initialize the internal storage
// Load the current configuration
if (dataFactory != null)
{
final String[] queryNames = dataFactory.getQueryNames();
for (int i = 0; i < queryNames.length; i++)
{
final String queryName = queryNames[i];
final String query = dataFactory.getQuery(queryName);
getDialogModel().addQuery(queryName, query);
}
setSelectedQuery(selectedQuery);
final OlapConnectionProvider currentJNDISource = dataFactory.getConnectionProvider();
final JdbcConnectionDefinition definition = getConnectionPanel().createConnectionDefinition(currentJNDISource);
getDialogModel().addConnection(definition);
getDialogModel().getConnections().setSelectedItem(definition);
getDialogModel().setJdbcUserField(dataFactory.getJdbcUserField());
getDialogModel().setJdbcPasswordField(dataFactory.getJdbcPasswordField());
connectionComponent.setRoleField(dataFactory.getRoleField());
}
// Prepare the data and the enable the proper buttons
setSelectedQuery(selectedQuery);
// Enable the dialog
pack();
setLocationRelativeTo(getParent());
setVisible(true);
if (!isConfirmed())
{
return null;
}
return createAndConfigureDataFactory();
}
protected AbstractNamedMDXDataFactory createAndConfigureDataFactory()
{
final AbstractNamedMDXDataFactory factory = createDataFactory();
if (factory == null)
{
return null;
}
final KeyedComboBoxModel queries = getDialogModel().getQueries();
for (int i = 0; i < queries.getSize(); i++)
{
factory.setQuery((String) queries.getElementAt(i), (String) queries.getKeyAt(i));
}
factory.setJdbcUserField(getDialogModel().getJdbcUserField());
factory.setJdbcPasswordField(getDialogModel().getJdbcPasswordField());
factory.setRoleField(connectionComponent.getRoleField());
return factory;
}
protected abstract AbstractNamedMDXDataFactory createDataFactory();
protected String getQuery()
{
return queryTextArea.getText();
}
protected void setQuery(final String query)
{
this.queryTextArea.setText(query);
}
protected String getQueryName()
{
return queryNameTextField.getText();
}
protected void setQueryName(final String queryName)
{
this.queryNameTextField.setText(queryName);
}
protected NamedDataSourceDialogModel getDialogModel()
{
return dialogModel;
}
protected OlapConnectionPanel getConnectionPanel()
{
return connectionComponent;
}
protected void setSelectedQuery(final String queryName)
{
queryNameList.setSelectedValue(queryName, true);
}
public boolean isConfirmed()
{
return confirmed;
}
}