OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 110 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.openconcerto.modules.reports.olap;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

import org.jopendocument.panel.Messages;
import org.olap4j.OlapConnection;
import org.olap4j.metadata.Schema;
import org.openconcerto.modules.reports.olap.renderer.Matrix;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.text.CSVWriter;

public class OLAPMainPanel extends JPanel {
    OLAPMainPanel(OlapConnection oConnection, Schema schema, final OLAPPanel olapPanel) {
        this.setLayout(new GridBagLayout());
        this.setOpaque(false);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 3, 2, 2);
        JLabel label = new JLabel("Sélectionnez au minimum une colonne et une ligne pour visualiser l'hypercube.");
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        final JLabelBold title = new JLabelBold("Analyse multidimensionnelle des données");
        title.setToolTipText(olapPanel.getLoadInfo());
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {

                final JPopupMenu menu = new JPopupMenu();
                final JMenuItem menuItem = new JMenuItem("Recharger la configuration");
                menu.add(menuItem);
                menuItem.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        olapPanel.reload();

                    }
                });
                menu.show(title, e.getX(), e.getY());

            }
        });
        this.add(title, c);
        c.gridy++;
        this.add(label, c);
        c.gridy++;
        c.insets = new Insets(2, 0, 0, 0);
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
        c.gridy++;
        c.insets = new Insets(0, 0, 0, 0);
        c.fill = GridBagConstraints.BOTH;
        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        split.setBorder(null);
        final OLAPParametersPanel parameters = new OLAPParametersPanel(schema);
        parameters.setMinimumSize(new Dimension(200, 200));
        parameters.setOpaque(false);
        split.setLeftComponent(parameters);
        split.setDividerLocation(200);
        split.setOpaque(true);

        JPanel rightPanel = new JPanel();
        rightPanel.setOpaque(false);
        rightPanel.setLayout(new BorderLayout());

        final OLAPRenderer renderer = new OLAPRenderer();

        final OLAPMDXPanel mdxPanel = new OLAPMDXPanel(parameters, oConnection, renderer);

        final OLAPSQLPanel sqlPanel = new OLAPSQLPanel(renderer);

        final OLAPConfigurationPanel confPanel = new OLAPConfigurationPanel(parameters, oConnection, renderer, mdxPanel);
        parameters.setConfigurationPanel(confPanel);
        JTabbedPane t = new JTabbedPane();
        t.add("Mode simplifié", confPanel);
        t.add("Requête MDX", mdxPanel);
        t.add("Requête SQL", sqlPanel);
        renderer.setOpaque(false);
        t.setOpaque(false);
        // Bottom panel
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout(FlowLayout.LEFT));
        JButton bExport = new JButton("Exporter");
        bExport.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                Matrix m = renderer.getMatrix();
                if (m == null) {
                    return;
                }
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setSelectedFile(new File("export_olap.csv"));
                fileChooser.setDialogTitle(Messages.getString("ODSViewerPanel.saveAs"));

                int userSelection = fileChooser.showSaveDialog(OLAPMainPanel.this);

                if (userSelection == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();

                    try {
                        int witdth = m.getWidth();
                        final FileWriter writer = new FileWriter(file);
                        CSVWriter w = new CSVWriter(writer);
                        for (int y = 0; y < m.getHeight(); y++) {
                            String[] a = new String[witdth];
                            for (int x = 0; x < witdth; x++) {
                                a[x] = m.get(x, y).getValue();
                            }
                            w.writeNext(a);
                            w.flush();
                        }
                        w.close();
                        writer.close();
                    } catch (IOException e1) {
                        ExceptionHandler.handle("Erreur lors de l'export", e1);
                    }
                }
            }
        });

        p.add(bExport);
        //
        rightPanel.add(t, BorderLayout.NORTH);
        rightPanel.add(renderer, BorderLayout.CENTER);
        rightPanel.add(p, BorderLayout.SOUTH);
        split.setRightComponent(rightPanel);
        c.weightx = 1;
        c.weighty = 1;
        split.setDividerLocation(0.33D);
        this.add(split, c);

    }

}