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

org.bidib.wizard.localhost.client.view.LocalHostClientView Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.localhost.client.view;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.client.common.table.AbstractEmptyTable;
import org.bidib.wizard.client.common.view.DockKeys;
import org.bidib.wizard.localhost.client.controller.listener.LocalHostClientControllerListener;
import org.bidib.wizard.localhost.client.model.SubscribedClientModel;
import org.bidib.wizard.localhost.client.model.SubscribedClientTableModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jgoodies.binding.adapter.SingleListSelectionAdapter;
import com.jgoodies.binding.list.SelectionInList;
import com.jgoodies.forms.builder.FormBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.factories.Paddings;
import com.jidesoft.grid.SortableTableModel;
import com.jidesoft.swing.DefaultOverlayable;
import com.jidesoft.swing.StyledLabelBuilder;
import com.vlsolutions.swing.docking.DockKey;
import com.vlsolutions.swing.docking.Dockable;
import com.vlsolutions.swing.docking.DockableState;
import com.vlsolutions.swing.docking.DockingDesktop;
import com.vlsolutions.swing.docking.event.DockableSelectionListener;
import com.vlsolutions.swing.docking.event.DockableStateChangeEvent;
import com.vlsolutions.swing.docking.event.DockableStateChangeListener;

public class LocalHostClientView implements Dockable {

    private static final Logger LOGGER = LoggerFactory.getLogger(LocalHostClientView.class);

    private static final String ENCODED_DIALOG_COLUMN_SPECS = "pref, 3dlu, pref, 3dlu, fill:pref:grow";

    // private static final String ENCODED_DIALOG_ROW_SPECS = "pref, 5dlu, fill:50dlu:grow";
    private static final String ENCODED_DIALOG_ROW_SPECS = "fill:50dlu:grow";

    private JComponent contentPanel;

    private final DockingDesktop desktop;

    private final LocalHostClientControllerListener controllerListener;

    private final DockableStateChangeListener dockableStateChangeListener;

    private DockableSelectionListener dockableSelectionListener;

    private SelectionInList subscriberSelection;

    private final SubscribedClientTableModel subscribedClientTableModel;

    public LocalHostClientView(final DockingDesktop desktop, final LocalHostClientControllerListener controllerListener,
        final SubscribedClientTableModel subscribedClientTableModel) {
        this.desktop = desktop;
        this.controllerListener = controllerListener;
        this.subscribedClientTableModel = subscribedClientTableModel;

        DockKeys.DOCKKEY_LOCALHOST_CLIENT_VIEW.setName(Resources.getString(getClass(), "title"));
        DockKeys.DOCKKEY_LOCALHOST_CLIENT_VIEW.setFloatEnabled(true);
        DockKeys.DOCKKEY_LOCALHOST_CLIENT_VIEW.setAutoHideEnabled(false);

        dockableStateChangeListener = new DockableStateChangeListener() {

            @Override
            public void dockableStateChanged(DockableStateChangeEvent event) {
                LOGGER
                    .info("The state has changed, newState: {}, prevState: {}", event.getNewState(),
                        event.getPreviousState());

                DockableState newState = event.getNewState();
                if (newState.getDockable().equals(LocalHostClientView.this) && newState.isClosed()) {
                    LOGGER.info("The NodesClientView is closed.");
                    // we are closed
                    desktop.removeDockableStateChangeListener(dockableStateChangeListener);

                    if (LocalHostClientView.this.dockableSelectionListener != null) {
                        desktop.removeDockableSelectionListener(LocalHostClientView.this.dockableSelectionListener);

                        LocalHostClientView.this.dockableSelectionListener = null;
                    }

                    // removeToolBar(toolbarNodesClient);

                    if (controllerListener != null) {
                        LOGGER.info("Close the view.");

                        controllerListener.viewClosed();
                    }
                }

            }
        };
        desktop.addDockableStateChangeListener(dockableStateChangeListener);
    }

    public JComponent initComponents() {

        // create form builder
        FormBuilder dialogBuilder = null;
        boolean debugDialog = false;
        if (debugDialog) {
            JPanel panel = new FormDebugPanel();
            dialogBuilder =
                FormBuilder.create().columns(ENCODED_DIALOG_COLUMN_SPECS).rows(ENCODED_DIALOG_ROW_SPECS).panel(panel);
        }
        else {
            JPanel panel = new JPanel(new BorderLayout());
            dialogBuilder =
                FormBuilder.create().columns(ENCODED_DIALOG_COLUMN_SPECS).rows(ENCODED_DIALOG_ROW_SPECS).panel(panel);
        }
        dialogBuilder.border(Paddings.TABBED_DIALOG);

        // TODO

        subscriberSelection =
            new SelectionInList(
                (ListModel) this.subscribedClientTableModel.getSubscriberListModel());

        final TableModel tableModel = new SortableTableModel(new SubscribedClientTableTableModel(subscriberSelection));

        // create a booster table
        final AbstractEmptyTable subscriberTable =
            new AbstractEmptyTable(tableModel, Resources.getString(getClass(), "empty_table")) {
                private static final long serialVersionUID = 1L;
            };
        subscriberTable.adjustRowHeight();
        subscriberTable
            .setSelectionModel(new SingleListSelectionAdapter(subscriberSelection.getSelectionIndexHolder()));

        final DefaultOverlayable overlayTable = new DefaultOverlayable(new JScrollPane(subscriberTable));
        tableModel.addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                overlayTable.setOverlayVisible(tableModel.getRowCount() == 0);
            }
        });

        overlayTable
            .addOverlayComponent(
                StyledLabelBuilder.createStyledLabel("{" + subscriberTable.getEmptyTableText() + ":f:gray}"));

        dialogBuilder.add(subscriberTable).xyw(1, 1, 5);

        this.contentPanel = dialogBuilder.build();

        return this.contentPanel;
    }

    @Override
    public DockKey getDockKey() {
        return DockKeys.DOCKKEY_LOCALHOST_CLIENT_VIEW;
    }

    @Override
    public Component getComponent() {
        return this.contentPanel;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy