/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed 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
 *
 *      http://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 com.github.alexfalappa.nbspringboot.navigator;

import java.awt.BorderLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.netbeans.spi.navigator.NavigatorPanel.Registration;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;

/**
 * The actual navigator UI.
 *
 * @author Michael J. Simons, 2016-09-14
 */
@Messages({
    "displayName=Request Mappings",
    "displayHint=Displays all @RequestMappings of the current *Controller",
    "resourceUrl=Resource URL",
    "requestMethod=Request Method",
    "handlerMethod=Handler Method"
})
@Registration(mimeType = "text/x-java", displayName = "#displayName")
public class RequestMappingNavigatorPanel implements NavigatorPanel {

    /**
     * template for finding data in given context. Object used as example,
     * replace with your own data source, for example JavaDataObject etc
     */
    private static final Lookup.Template MY_DATA = new Lookup.Template(DataObject.class);

    /**
     * holds UI of this panel.
     */
    private final JComponent component;

    private final ElementScanningTaskFactory mappedElementGatheringTaskFactory;

    /**
     * public no arg constructor needed for system to instantiate provider well
     */
    public RequestMappingNavigatorPanel() {
        final MappedElementsModel mappedElementsModel = new MappedElementsModel();
        this.mappedElementGatheringTaskFactory = new ElementScanningTaskFactory(mappedElementsModel);
        final JTable table = new JTable(mappedElementsModel);
        final JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(new JScrollPane(table), BorderLayout.CENTER);
        this.component = panel;
    }

    @Override
    public String getDisplayHint() {
        return Bundle.displayHint();
    }

    @Override
    public String getDisplayName() {
        return Bundle.displayName();
    }

    @Override
    public JComponent getComponent() {
        return this.component;
    }

    @Override
    public void panelActivated(Lookup context) {
        this.mappedElementGatheringTaskFactory.activate();
    }

    @Override
    public void panelDeactivated() {
        this.mappedElementGatheringTaskFactory.deactivate();
    }

    @Override
    public Lookup getLookup() {
        return null;
    }
}

@@ -710,26 +710,86 @@
 java
-x.swing.JComponent
+.io.IOException;%0Aimport javax.swing.JComponent;%0Aimport javax.swing.JEditorPane
 ;%0Aim
@@ -872,16 +872,200 @@
 JTable;%0A
+import javax.swing.ListSelectionModel;%0Aimport javax.swing.event.ListSelectionEvent;%0Aimport javax.swing.event.ListSelectionListener;%0Aimport org.netbeans.api.java.source.ui.ElementOpen;%0A
 import o
@@ -1165,24 +1165,65 @@
 gistration;%0A
+import org.openide.cookies.EditorCookie;%0A
 import org.o
@@ -1780,263 +1780,8 @@
  %7B%0A%0A
-    /**%0A     * template for finding data in given context. Object used as example,%0A     * replace with your own data source, for example JavaDataObject etc%0A     */%0A    private static final Lookup.Template MY_DATA = new Lookup.Template(DataObject.class);%0A%0A
     
@@ -1864,16 +1864,76 @@
 onent;%0A%0A
+    private final MappedElementsModel mappedElementsModel;%0A%0A
     priv
@@ -2157,34 +2157,13 @@
     
-final MappedElementsModel 
+this.
 mapp
@@ -2523,16 +2523,16 @@
 ENTER);%0A
-
         
@@ -2554,16 +2554,1320 @@
 = panel;
+%0A%0A        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);%0A        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() %7B%0A            @Override%0A            public void valueChanged(ListSelectionEvent event) %7B%0A                final int selectedRow = ((ListSelectionModel) event.getSource()).getMinSelectionIndex();%0A                if (event.getValueIsAdjusting() %7C%7C selectedRow %3C 0) %7B%0A                    return;%0A                %7D%0A                final MappedElement mappedElement = mappedElementsModel.getElementAt(selectedRow);%0A                ElementOpen.open(mappedElement.getFileObject(), mappedElement.getHandle());%0A                try %7B%0A                    final DataObject dataObject = DataObject.find(mappedElement.getFileObject());%0A                    final EditorCookie editorCookie = dataObject.getLookup().lookup(EditorCookie.class);%0A                    if (editorCookie != null) %7B%0A                        editorCookie.openDocument();%0A                        JEditorPane%5B%5D p = editorCookie.getOpenedPanes();%0A                        if (p.length %3E 0) %7B%0A                            p%5B0%5D.requestFocus();%0A                        %7D%0A                    %7D%0A                %7D catch (IOException e) %7B%0A                %7D%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A

Add selection listener to open element