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

io.fabric8.forge.camel.commands.project.CamelEditEndpointCommand Maven / Gradle / Ivy

There is a newer version: 2.3.90
Show newest version
/**
 *  Copyright 2005-2015 Red Hat, Inc.
 *
 *  Red Hat 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
 *
 *     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 io.fabric8.forge.camel.commands.project;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;

import io.fabric8.forge.camel.commands.project.completer.CurrentLineCompleter;
import io.fabric8.forge.camel.commands.project.completer.RouteBuilderEndpointsCompleter;
import io.fabric8.forge.camel.commands.project.completer.XmlEndpointsCompleter;
import io.fabric8.forge.camel.commands.project.helper.PoorMansLogger;
import io.fabric8.forge.camel.commands.project.model.CamelEndpointDetails;
import io.fabric8.forge.camel.commands.project.model.InputOptionByGroup;
import org.jboss.forge.addon.projects.dependencies.DependencyInstaller;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UINavigationContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.InputComponentFactory;
import org.jboss.forge.addon.ui.input.UISelectOne;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.addon.ui.wizard.UIWizard;

import static io.fabric8.forge.camel.commands.project.helper.CamelCommandsHelper.createUIInputsForCamelEndpoint;

public class CamelEditEndpointCommand extends AbstractCamelProjectCommand implements UIWizard {

    private static final PoorMansLogger LOG = new PoorMansLogger(false);

    private static final int MAX_OPTIONS = 20;

    @Inject
    @WithAttributes(label = "Endpoints", required = true, description = "The endpoints from the project")
    private UISelectOne endpoints;

    @Inject
    private InputComponentFactory componentFactory;

    @Inject
    private DependencyInstaller dependencyInstaller;

    private RouteBuilderEndpointsCompleter javaCompleter;
    private XmlEndpointsCompleter xmlCompleter;
    private CurrentLineCompleter currentLineCompleter;

    @Override
    public UICommandMetadata getMetadata(UIContext context) {
        return Metadata.forCommand(CamelEditEndpointCommand.class).name(
                "Camel: Edit Endpoint").category(Categories.create(CATEGORY))
                .description("Edit Camel endpoint in the current file");
    }

    @Override
    public boolean isEnabled(UIContext context) {
        boolean answer = super.isEnabled(context);
        if (answer) {
            // we are only enabled if there is a file open in the editor
            final String currentFile = getSelectedFile(context);
            answer = currentFile != null;
        }
        return answer;
    }

    @Override
    public void initializeUI(UIBuilder builder) throws Exception {
        Map attributeMap = builder.getUIContext().getAttributeMap();
        attributeMap.remove("navigationResult");

        final String currentFile = getSelectedFile(builder.getUIContext());
        final int cursorLineNumber = getCurrentCursorLine(builder.getUIContext());

        boolean javaFile = currentFile.endsWith(".java");
        boolean xmlFile = currentFile.endsWith(".xml");

        if (javaFile) {
            // find all endpoints
            javaCompleter = createRouteBuilderEndpointsCompleter(builder.getUIContext(), currentFile::equals);

            boolean found = false;
            if (cursorLineNumber != -1) {
                for (CamelEndpointDetails detail : javaCompleter.getEndpoints()) {
                    if (detail.getLineNumber() != null && Integer.valueOf(detail.getLineNumber()) == cursorLineNumber) {
                        endpoints.setValue(detail.getEndpointUri());
                        endpoints.setRequired(false);
                        found = true;
                        break;
                    }
                }
            }
            if (!found && !javaCompleter.getEndpoints().isEmpty()) {

                // must add dummy ");
                endpoints.setValueChoices(uris);

                // show the UI where you can chose the endpoint to select
                builder.add(endpoints);
            }
        } else if (xmlFile) {
            // find all endpoints
            xmlCompleter = createXmlEndpointsCompleter(builder.getUIContext(), currentFile::equals);

            boolean found = false;
            if (cursorLineNumber != -1) {
                for (CamelEndpointDetails detail : xmlCompleter.getEndpoints()) {
                    if (detail.getLineNumber() != null && Integer.valueOf(detail.getLineNumber()) == cursorLineNumber) {
                        endpoints.setValue(detail.getEndpointUri());
                        endpoints.setRequired(false);
                        found = true;
                        break;
                    }
                }
            }
            if (!found && !xmlCompleter.getEndpoints().isEmpty()) {

                // must add dummy ");
                endpoints.setValueChoices(uris);

                // show the UI where you can chose the endpoint to select
                builder.add(endpoints);
            }
        } else {
            currentLineCompleter = createCurrentLineCompleter(cursorLineNumber, currentFile, builder.getUIContext());
            CamelEndpointDetails detail = currentLineCompleter.getEndpoint();
            if (detail != null && detail.getEndpointUri() != null) {
                endpoints.setValue(detail.getEndpointUri());
                endpoints.setRequired(false);
            }
        }
    }

    @Override
    public NavigationResult next(UINavigationContext context) throws Exception {
        Map attributeMap = context.getUIContext().getAttributeMap();

        String selectedUri = endpoints.getValue();
        if ("