package org.yamcs.studio.ui.links;

import java.util.logging.Logger;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;
import org.yamcs.protobuf.YamcsManagement.LinkInfo;
import org.yamcs.studio.core.ConnectionManager;
import org.yamcs.studio.core.StudioConnectionListener;
import org.yamcs.studio.core.model.InstanceListener;
import org.yamcs.studio.core.model.LinkCatalogue;
import org.yamcs.studio.core.model.LinkListener;
import org.yamcs.studio.core.model.ManagementCatalogue;

public class DataLinkView extends ViewPart implements StudioConnectionListener, InstanceListener, LinkListener {

    private static final Logger log = Logger.getLogger(DataLinkView.class.getName());

    private DataLinkTableViewer tableViewer;
    private DataLinkTableViewerContentProvider contentProvider;

    @Override
    public void createPartControl(Composite parent) {
        Composite tableWrapper = new Composite(parent, SWT.NONE);
        tableWrapper.setLayoutData(new GridData(GridData.FILL_BOTH));
        TableColumnLayout tcl = new TableColumnLayout();
        tableWrapper.setLayout(tcl);
        tableViewer = new DataLinkTableViewer(tableWrapper, tcl);
        contentProvider = new DataLinkTableViewerContentProvider(tableViewer);
        tableViewer.setContentProvider(contentProvider);
        tableViewer.setInput(contentProvider);

        tableViewer.getTable().addListener(SWT.FocusOut, evt -> {
            tableViewer.getTable().deselectAll();
        });

        if (getViewSite() != null)
            getViewSite().setSelectionProvider(tableViewer);

        // Set initial state
        tableViewer.refresh();

        LinkCatalogue.getInstance().addLinkListener(this);
        ConnectionManager.getInstance().addStudioConnectionListener(this);
        ManagementCatalogue.getInstance().addInstanceListener(this);
    }

    @Override
    public void onStudioConnect() {
        Display.getDefault().asyncExec(() -> updateYamcsInstance());
    }

    @Override
    public void instanceChanged(String oldInstance, String newInstance) {
        Display.getDefault().asyncExec(() -> updateYamcsInstance());
    }

    @Override
    public void onStudioDisconnect() {
        // remove-all handled through LinkListener
    }

    private void updateYamcsInstance() {
        String yamcsInstance = ManagementCatalogue.getCurrentYamcsInstance();
        contentProvider.processYamcsInstance(yamcsInstance);
        if (yamcsInstance != null) {
            setContentDescription("Showing links for Yamcs instance " + yamcsInstance);
        } else {
            setContentDescription(null);
        }
    }

    @Override
    public void dispose() {
        LinkCatalogue.getInstance().removeLinkListener(this);
        ManagementCatalogue.getInstance().removeInstanceListener(this);
        ConnectionManager.getInstance().removeStudioConnectionListener(this);
        super.dispose();
    }

    @Override
    public void linkRegistered(LinkInfo linkInfo) {
        linkUpdated(linkInfo);
    }

    @Override
    public void linkUpdated(LinkInfo linkInfo) {
        if (tableViewer.getTable().isDisposed())
            return;

        Display display = tableViewer.getTable().getDisplay();
        display.asyncExec(() -> {
            if (display.isDisposed())
                return;

            contentProvider.processLinkInfo(linkInfo);
        });
    }

    @Override
    public void linkUnregistered(LinkInfo linkInfo) {
        // TODO but not currently sent by Yamcs
    }

    @Override
    public void clearDataLinkData() {
        Display.getDefault().asyncExec(() -> {
            tableViewer.getTable().removeAll();
            contentProvider.clearAll();
        });
    }

    @Override
    public void setFocus() {
        tableViewer.getTable().setFocus();
    }
}

@@ -33,42 +33,8 @@
 s;%0A%0A
-import java.util.logging.Logger;%0A%0A
 impo
@@ -108,16 +108,55 @@
 wt.SWT;%0A
+import org.eclipse.swt.graphics.Point;%0A
 import o
@@ -788,95 +788,8 @@
  %7B%0A%0A
-    private static final Logger log = Logger.getLogger(DataLinkView.class.getName());%0A%0A
     
@@ -1493,27 +1493,292 @@
 SWT.
-FocusOut, evt -%3E %7B%0A
+MouseDown, evt -%3E %7B%0A            // Allow the user to get rid of a selection in small tables%0A            // Note: before this was registered to a FocusOut, but that%0A            // broke the popup menu for me.%0A            if (tableViewer.getCell(new Point(evt.x, evt.y)) == null) %7B%0A    
     
@@ -1819,24 +1819,38 @@
 electAll();%0A
+            %7D%0A
         %7D);%0A

Fix broken popup in data links view