Package com.github.masahirosuzuka.PhoneGapIntelliJPlugin.ProjectBuilder

Source Code of com.github.masahirosuzuka.PhoneGapIntelliJPlugin.ProjectBuilder.PhoneGapProjectPeer

package com.github.masahirosuzuka.PhoneGapIntelliJPlugin.ProjectBuilder;

import com.github.masahirosuzuka.PhoneGapIntelliJPlugin.PhoneGapUtil;
import com.github.masahirosuzuka.PhoneGapIntelliJPlugin.commandLine.PhoneGapCommandLine;
import com.intellij.ide.util.projectWizard.SettingsStep;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.platform.WebProjectGenerator;
import com.intellij.ui.TextFieldWithHistoryWithBrowseButton;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

public class PhoneGapProjectPeer implements WebProjectGenerator.GeneratorPeer<PhoneGapProjectTemplateGenerator.PhoneGapProjectSettings> {

  private final List<WebProjectGenerator.SettingsStateListener> myStateListeners = ContainerUtil.createLockFreeCopyOnWriteList();
  private TextFieldWithHistoryWithBrowseButton myExecutablePathField;

  private final ConcurrentMap<String, Boolean> myValidateCache = ContainerUtil.newConcurrentMap();

  PhoneGapProjectPeer() {
  }

  @NotNull
  @Override
  public JComponent getComponent() {
    setFields();
    JPanel panel = FormBuilder.createFormBuilder()
      .addLabeledComponent("PhoneGap/Cordova executable:", myExecutablePathField)
      .getPanel();

    panel.setPreferredSize(new Dimension(600, 40));
    return panel;
  }

  @Override
  public void buildUI(@NotNull SettingsStep settingsStep) {
    setFields();
    settingsStep.addSettingsField("PhoneGap/Cordova executable:", myExecutablePathField);
  }

  @NotNull
  @Override
  public PhoneGapProjectTemplateGenerator.PhoneGapProjectSettings getSettings() {
    PhoneGapProjectTemplateGenerator.PhoneGapProjectSettings settings = new PhoneGapProjectTemplateGenerator.PhoneGapProjectSettings();
    settings.setExecutable(myExecutablePathField.getText());

    return settings;
  }

  private void setFields() {
    myExecutablePathField = PhoneGapUtil.createPhoneGapExecutableTextField(null);
  }

  @Nullable
  @Override
  public ValidationInfo validate() {
    String path = myExecutablePathField.getText();

    boolean error;

    if (myValidateCache.containsKey(path)) {
      error = myValidateCache.get(path);
    }
    else {
      try {
        if (StringUtil.isEmpty(path)) {
          return new ValidationInfo("Please select path to executable");
        }

        new PhoneGapCommandLine(path, null).version();
        error = false;
      }
      catch (Exception e) {
        error = true;
      }
      myValidateCache.put(path, error);
    }
    return error ? new ValidationInfo("Incorrect PhoneGap/Cordova executable") : null;
  }

  @Override
  public boolean isBackgroundJobRunning() {
    return false;
  }

  @Override
  public void addSettingsStateListener(@NotNull WebProjectGenerator.SettingsStateListener listener) {
    myStateListeners.add(listener);
  }
}
TOP

Related Classes of com.github.masahirosuzuka.PhoneGapIntelliJPlugin.ProjectBuilder.PhoneGapProjectPeer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.