All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datacleaner.panels.DatastorePanel Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Free Software Foundation, Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.panels;

import java.awt.GridBagConstraints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

import org.apache.metamodel.schema.Table;
import org.datacleaner.bootstrap.WindowContext;
import org.datacleaner.connection.AccessDatastore;
import org.datacleaner.connection.CassandraDatastore;
import org.datacleaner.connection.CompositeDatastore;
import org.datacleaner.connection.CouchDbDatastore;
import org.datacleaner.connection.CsvDatastore;
import org.datacleaner.connection.Datastore;
import org.datacleaner.connection.DatastoreConnection;
import org.datacleaner.connection.DbaseDatastore;
import org.datacleaner.connection.DynamoDbDatastore;
import org.datacleaner.connection.ElasticSearchDatastore;
import org.datacleaner.connection.ExcelDatastore;
import org.datacleaner.connection.FileDatastore;
import org.datacleaner.connection.FixedWidthDatastore;
import org.datacleaner.connection.HBaseDatastore;
import org.datacleaner.connection.JdbcDatastore;
import org.datacleaner.connection.JsonDatastore;
import org.datacleaner.connection.MongoDbDatastore;
import org.datacleaner.connection.Neo4jDatastore;
import org.datacleaner.connection.OdbDatastore;
import org.datacleaner.connection.PojoDatastore;
import org.datacleaner.connection.ResourceDatastore;
import org.datacleaner.connection.SalesforceDatastore;
import org.datacleaner.connection.SasDatastore;
import org.datacleaner.connection.SugarCrmDatastore;
import org.datacleaner.connection.XmlDatastore;
import org.datacleaner.guice.DCModule;
import org.datacleaner.guice.InjectorBuilder;
import org.datacleaner.user.MutableDatastoreCatalog;
import org.datacleaner.user.UserPreferences;
import org.datacleaner.util.IconUtils;
import org.datacleaner.util.StringUtils;
import org.datacleaner.util.WidgetFactory;
import org.datacleaner.util.WidgetUtils;
import org.datacleaner.widgets.DCLabel;
import org.datacleaner.windows.AccessDatastoreDialog;
import org.datacleaner.windows.CassandraDatastoreDialog;
import org.datacleaner.windows.CompositeDatastoreDialog;
import org.datacleaner.windows.CouchDbDatastoreDialog;
import org.datacleaner.windows.CsvDatastoreDialog;
import org.datacleaner.windows.DbaseDatastoreDialog;
import org.datacleaner.windows.DynamoDbDatastoreDialog;
import org.datacleaner.windows.ElasticSearchDatastoreDialog;
import org.datacleaner.windows.ExcelDatastoreDialog;
import org.datacleaner.windows.FixedWidthDatastoreDialog;
import org.datacleaner.windows.HBaseDatastoreDialog;
import org.datacleaner.windows.JdbcDatastoreDialog;
import org.datacleaner.windows.JsonDatastoreDialog;
import org.datacleaner.windows.MongoDbDatastoreDialog;
import org.datacleaner.windows.Neo4jDatastoreDialog;
import org.datacleaner.windows.OdbDatastoreDialog;
import org.datacleaner.windows.QueryWindow;
import org.datacleaner.windows.SalesforceDatastoreDialog;
import org.datacleaner.windows.SasDatastoreDialog;
import org.datacleaner.windows.SugarCrmDatastoreDialog;
import org.datacleaner.windows.XmlDatastoreDialog;

import com.google.inject.Injector;

/**
 * A panel that presents a datastore and shows edit/remove buttons. This panel
 * is placed as a child inside the {@link DatastoreManagementPanel}.
 */
public class DatastorePanel extends DCPanel {

    public static final int LABEL_MAX_WIDTH = 450;
    private static final long serialVersionUID = 1L;
    private final Datastore _datastore;
    private final MutableDatastoreCatalog _datastoreCatalog;
    private final DatastoreManagementPanel _datastoreListPanel;
    private final JCheckBox _checkBox;
    private final WindowContext _windowContext;
    private final UserPreferences _userPreferences;
    private final DCModule _dcModule;

    public DatastorePanel(final Datastore datastore, final MutableDatastoreCatalog datastoreCatalog,
            final DatastoreManagementPanel datastoreListPanel, final WindowContext windowContext,
            final UserPreferences userPreferences, final DCModule dcModule) {
        super(WidgetUtils.BG_COLOR_BRIGHT);
        _datastore = datastore;
        _datastoreCatalog = datastoreCatalog;
        _datastoreListPanel = datastoreListPanel;
        _windowContext = windowContext;
        _userPreferences = userPreferences;
        _dcModule = dcModule;

        setOpaque(false);

        final Icon icon = IconUtils.getDatastoreIcon(datastore, IconUtils.ICON_SIZE_LARGE);
        final String description = getDescription(datastore);

        _checkBox = new JCheckBox();
        _checkBox.setOpaque(false);
        _checkBox.addActionListener(e -> _datastoreListPanel.setSelectedDatastorePanel(DatastorePanel.this));
        _checkBox.addChangeListener(e -> {
            setOpaque(isSelected());
            updateUI();
        });

        final String datastoreName = datastore.getName();
        final DCLabel datastoreNameLabel =
                DCLabel.dark("" + datastoreName + "
" + description + ""); datastoreNameLabel.setIconTextGap(10); datastoreNameLabel.setIcon(icon); datastoreNameLabel.setMaximumWidth(LABEL_MAX_WIDTH); final MouseAdapter invokeCheckBoxMouseListener = new MouseAdapter() { @Override public void mouseClicked(final MouseEvent e) { _checkBox.doClick(); _datastoreListPanel.requestSearchFieldFocus(); if (e.getClickCount() > 1) { // begin job on double click _datastoreListPanel.clickAnalyzeButton(); } } }; addMouseListener(invokeCheckBoxMouseListener); datastoreNameLabel.addMouseListener(invokeCheckBoxMouseListener); final JButton editButton = createEditButton(datastore); final JButton queryButton = createQueryButton(datastore); final JButton removeButton = createRemoveButton(datastore); setBorder(WidgetUtils.BORDER_LIST_ITEM_SUBTLE); WidgetUtils.addToGridBag(_checkBox, this, 0, 0, GridBagConstraints.WEST); WidgetUtils.addToGridBag(DCPanel.flow(datastoreNameLabel), this, 1, 0, GridBagConstraints.WEST, 1.0, 1.0); WidgetUtils.addToGridBag(editButton, this, 2, 0, GridBagConstraints.EAST); WidgetUtils.addToGridBag(queryButton, this, 3, 0, GridBagConstraints.EAST); WidgetUtils.addToGridBag(removeButton, this, 4, 0, GridBagConstraints.EAST); } public static String getDescription(final Datastore datastore) { if (datastore.getDescription() != null) { return datastore.getDescription(); } if (datastore instanceof ResourceDatastore) { return ((ResourceDatastore) datastore).getResource().getQualifiedPath(); } else if (datastore instanceof FileDatastore) { return ((FileDatastore) datastore).getFilename(); } else if (datastore instanceof JdbcDatastore) { final JdbcDatastore jdbcDatastore = (JdbcDatastore) datastore; final String jdbcUrl = jdbcDatastore.getJdbcUrl(); final String datasourceJndiUrl = jdbcDatastore.getDatasourceJndiUrl(); if (StringUtils.isNullOrEmpty(datasourceJndiUrl)) { return jdbcUrl; } return datasourceJndiUrl; } else if (datastore instanceof ElasticSearchDatastore) { final ElasticSearchDatastore elasticSearchDatastore = (ElasticSearchDatastore) datastore; return elasticSearchDatastore.getIndexName(); } else if (datastore instanceof CassandraDatastore) { final CassandraDatastore cassandraDatastore = (CassandraDatastore) datastore; return cassandraDatastore.getKeyspace(); } else if (datastore instanceof MongoDbDatastore) { final MongoDbDatastore mongoDbDatastore = (MongoDbDatastore) datastore; return mongoDbDatastore.getHostname() + ":" + mongoDbDatastore.getPort() + " - " + mongoDbDatastore .getDatabaseName(); } else if (datastore instanceof Neo4jDatastore) { final Neo4jDatastore neo4jDbDatastore = (Neo4jDatastore) datastore; return neo4jDbDatastore.getHostname() + ":" + neo4jDbDatastore.getPort(); } else if (datastore instanceof CouchDbDatastore) { final CouchDbDatastore couchDbDatastore = (CouchDbDatastore) datastore; return (couchDbDatastore.isSslEnabled() ? "https://" : "http://") + couchDbDatastore.getHostname() + ":" + couchDbDatastore.getPort(); } else if (datastore instanceof PojoDatastore) { return "In-memory collection of records."; } else if (datastore instanceof CompositeDatastore) { final List datastores = ((CompositeDatastore) datastore).getDatastores(); final StringBuilder sb = new StringBuilder(); for (final Datastore ds : datastores) { if (sb.length() != 0) { sb.append(", "); } sb.append(ds.getName()); } return sb.toString(); } return ""; } public Datastore getDatastore() { return _datastore; } private JButton createRemoveButton(final Datastore datastore) { final String name = datastore.getName(); final JButton removeButton = WidgetFactory.createDefaultButton("Remove", IconUtils.ACTION_REMOVE_DARK); removeButton.setToolTipText("Remove datastore"); removeButton.addActionListener(e -> { final int result = JOptionPane.showConfirmDialog(DatastorePanel.this, "Are you sure you wish to remove the datastore '" + name + "'?", "Confirm remove", JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { _datastoreCatalog.removeDatastore(datastore); } }); return removeButton; } private JButton createQueryButton(final Datastore datastore) { final JButton queryButton = WidgetFactory.createDefaultButton("Query", IconUtils.MODEL_QUERY); queryButton.setToolTipText("Query datastore"); queryButton.addActionListener(e -> { final String queryString; try (DatastoreConnection connection = datastore.openConnection()) { final List tables = connection.getSchemaNavigator().getDefaultSchema().getTables(); if (tables.size() > 0) { queryString = "SELECT *\nFROM " + tables.get(0).getQualifiedLabel(); } else { queryString = "SELECT *\nFROM ?"; } } final QueryWindow queryWindow = new QueryWindow(_windowContext, datastore, queryString); queryWindow.open(); }); return queryButton; } private JButton createEditButton(final Datastore datastore) { final JButton editButton = WidgetFactory.createDefaultButton("Edit", IconUtils.ACTION_EDIT); editButton.setToolTipText("Edit datastore"); if (datastore instanceof JdbcDatastore) { editButton.addActionListener(e -> { final Injector injectorWithDatastore = getInjectorBuilder().with(JdbcDatastore.class, datastore).createInjector(); final JdbcDatastoreDialog dialog = injectorWithDatastore.getInstance(JdbcDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof CsvDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(CsvDatastore.class, datastore).createInjector(); final CsvDatastoreDialog dialog = injector.getInstance(CsvDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof AccessDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(AccessDatastore.class, datastore).createInjector(); final AccessDatastoreDialog dialog = injector.getInstance(AccessDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof ExcelDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(ExcelDatastore.class, datastore).createInjector(); final ExcelDatastoreDialog dialog = injector.getInstance(ExcelDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof SasDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(SasDatastore.class, datastore).createInjector(); final SasDatastoreDialog dialog = injector.getInstance(SasDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof XmlDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(XmlDatastore.class, datastore).createInjector(); final XmlDatastoreDialog dialog = injector.getInstance(XmlDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof OdbDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(OdbDatastore.class, datastore).createInjector(); final OdbDatastoreDialog dialog = injector.getInstance(OdbDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof FixedWidthDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(FixedWidthDatastore.class, datastore).createInjector(); final FixedWidthDatastoreDialog dialog = injector.getInstance(FixedWidthDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof DbaseDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(DbaseDatastore.class, datastore).createInjector(); final DbaseDatastoreDialog dialog = injector.getInstance(DbaseDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof HBaseDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(HBaseDatastore.class, datastore).createInjector(); final HBaseDatastoreDialog dialog = injector.getInstance(HBaseDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof CassandraDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(CassandraDatastore.class, datastore).createInjector(); final CassandraDatastoreDialog dialog = injector.getInstance(CassandraDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof ElasticSearchDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(ElasticSearchDatastore.class, datastore).createInjector(); final ElasticSearchDatastoreDialog dialog = injector.getInstance(ElasticSearchDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof CouchDbDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(CouchDbDatastore.class, datastore).createInjector(); final CouchDbDatastoreDialog dialog = injector.getInstance(CouchDbDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof DynamoDbDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(DynamoDbDatastore.class, datastore).createInjector(); final DynamoDbDatastoreDialog dialog = injector.getInstance(DynamoDbDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof MongoDbDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(MongoDbDatastore.class, datastore).createInjector(); final MongoDbDatastoreDialog dialog = injector.getInstance(MongoDbDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof SalesforceDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(SalesforceDatastore.class, datastore).createInjector(); final SalesforceDatastoreDialog dialog = injector.getInstance(SalesforceDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof SugarCrmDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(SugarCrmDatastore.class, datastore).createInjector(); final SugarCrmDatastoreDialog dialog = injector.getInstance(SugarCrmDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof Neo4jDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(Neo4jDatastore.class, datastore).createInjector(); final Neo4jDatastoreDialog dialog = injector.getInstance(Neo4jDatastoreDialog.class); dialog.open(); }); } else if (datastore instanceof CompositeDatastore) { editButton.addActionListener(e -> { final CompositeDatastoreDialog dialog = new CompositeDatastoreDialog((CompositeDatastore) datastore, _datastoreCatalog, _windowContext, _userPreferences); dialog.open(); }); } else if (datastore instanceof JsonDatastore) { editButton.addActionListener(e -> { final Injector injector = getInjectorBuilder().with(JsonDatastore.class, datastore).createInjector(); final JsonDatastoreDialog dialog = injector.getInstance(JsonDatastoreDialog.class); dialog.open(); }); } else { editButton.setEnabled(false); } return editButton; } private InjectorBuilder getInjectorBuilder() { return _dcModule.createInjectorBuilder(); } public boolean isSelected() { return _checkBox.isSelected(); } public void setSelected(final boolean selected) { _checkBox.setSelected(selected); } }