/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 ****************************************************************/

package org.apache.cayenne.modeler.dialog.db;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import org.apache.cayenne.modeler.Application;
import org.apache.cayenne.modeler.dialog.pref.DBConnectionInfoEditor;
import org.apache.cayenne.modeler.util.CayenneController;

import javax.swing.*;
import java.awt.*;

/**
 */
public class DataSourceWizardView extends JDialog {

    private static final byte OK_BUTTON_INDEX = 0;
    private static final byte CANCEL_BUTTON_INDEX = 1;

    protected JComboBox<String> dataSources;
    protected JButton configButton;
    protected JButton okButton;
    protected JButton cancelButton;
    protected DBConnectionInfoEditor connectionInfo;

    public DataSourceWizardView(CayenneController controller, String[] labels) {
        super(Application.getFrame());
        
        this.dataSources = new JComboBox<>();

        this.configButton = new JButton("...");
        this.configButton.setToolTipText("configure local DataSource");
        this.okButton = new JButton(labels[OK_BUTTON_INDEX]);
        this.cancelButton = new JButton(labels[CANCEL_BUTTON_INDEX]);
        this.connectionInfo = new DBConnectionInfoEditor(controller);

        getRootPane().setDefaultButton(okButton);

        CellConstraints cc = new CellConstraints();
        PanelBuilder builder = new PanelBuilder(new FormLayout(
                "20dlu:grow, pref, 3dlu, fill:max(150dlu;pref), 3dlu, fill:20dlu",
                "p"));
        builder.setDefaultDialogBorder();

        builder.addLabel("Saved DataSources:", cc.xy(2, 1));
        builder.add(dataSources, cc.xy(4, 1));
        builder.add(configButton, cc.xy(6, 1));

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttons.add(cancelButton);
        buttons.add(okButton);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(builder.getPanel(), BorderLayout.NORTH);
        getContentPane().add(connectionInfo.getView(), BorderLayout.CENTER);
        getContentPane().add(buttons, BorderLayout.SOUTH);

        setTitle("DB Connection Info");
    }

    public JComboBox<String> getDataSources() {
        return dataSources;
    }

    public JButton getCancelButton() {
        return cancelButton;
    }

    public JButton getConfigButton() {
        return configButton;
    }

    public JButton getOkButton() {
        return okButton;
    }

    public DBConnectionInfoEditor getConnectionInfo() {
        return connectionInfo;
    }
}
