<commit_before>/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * 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.jetbrains.micropython.settings

import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.module.Module
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.SimpleListCellRenderer
import com.intellij.ui.components.CheckBox
import com.intellij.util.text.nullize
import com.intellij.util.ui.FormBuilder
import com.intellij.util.ui.SwingHelper
import com.intellij.util.ui.UIUtil
import com.jetbrains.micropython.devices.MicroPythonDeviceProvider
import java.awt.BorderLayout
import javax.swing.JButton
import javax.swing.JList
import javax.swing.JPanel

/**
 * @author vlan
 */
class MicroPythonSettingsPanel(private val module: Module) : JPanel() {
  private val deviceTypeCombo = ComboBox(MicroPythonDeviceProvider.providers.toTypedArray())
  private val docsHyperlink = SwingHelper.createWebHyperlink("")
  private val devicePath = TextFieldWithBrowseButton()
  private val autoDetectDevicePath = CheckBox("Auto-detect device path").apply {
    addActionListener {
      update()
    }
  }

  private val devicePathPanel: JPanel by lazy {
    FormBuilder.createFormBuilder()
        .addLabeledComponent("Device path:", JPanel(BorderLayout()).apply {
          add(devicePath, BorderLayout.CENTER)
          add(JButton("Detect").apply {
            addActionListener {
              devicePath.text = module.microPythonFacet?.detectDevicePathSynchronously(selectedProvider) ?: ""
            }
          }, BorderLayout.EAST)
        })
        .panel
  }

  init {
    layout = BorderLayout()
    border = IdeBorderFactory.createEmptyBorder(UIUtil.PANEL_SMALL_INSETS)

    val contentPanel = FormBuilder.createFormBuilder()
        .addLabeledComponent("Device type:", deviceTypeCombo)
        .addComponent(autoDetectDevicePath)
        .addComponent(devicePathPanel)
        .addComponent(docsHyperlink)
        .panel

    add(contentPanel, BorderLayout.NORTH)

    deviceTypeCombo.apply {
      renderer = object: SimpleListCellRenderer<MicroPythonDeviceProvider>() {
        override fun customize(list: JList<out MicroPythonDeviceProvider>, value: MicroPythonDeviceProvider?,
                               index: Int, selected: Boolean, hasFocus: Boolean) {
          text = value?.presentableName ?: return
        }
      }
      addActionListener {
        docsHyperlink.apply {
          setHyperlinkTarget(selectedProvider.documentationURL)
          setHyperlinkText("Learn more about setting up ${selectedProvider.presentableName} devices")
          repaint()
        }
      }
    }<commit_msg>Switched from deprecated API to ActionLink for documentation URL<commit_after>/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * 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.jetbrains.micropython.settings

import com.intellij.ide.BrowserUtil
import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.module.Module
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.SimpleListCellRenderer
import com.intellij.ui.components.ActionLink
import com.intellij.ui.components.CheckBox
import com.intellij.util.text.nullize
import com.intellij.util.ui.FormBuilder
import com.intellij.util.ui.UIUtil
import com.jetbrains.micropython.devices.MicroPythonDeviceProvider
import java.awt.BorderLayout
import javax.swing.JButton
import javax.swing.JList
import javax.swing.JPanel

/**
 * @author vlan
 */
class MicroPythonSettingsPanel(private val module: Module) : JPanel() {
  private val deviceTypeCombo = ComboBox(MicroPythonDeviceProvider.providers.toTypedArray())
  private var docsHyperlink = object : ActionLink() {
    var url = ""

    init {
      addActionListener {
        BrowserUtil.browse(url)
      }
    }
  }
  private val devicePath = TextFieldWithBrowseButton()
  private val autoDetectDevicePath = CheckBox("Auto-detect device path").apply {
    addActionListener {
      update()
    }
  }

  private val devicePathPanel: JPanel by lazy {
    FormBuilder.createFormBuilder()
        .addLabeledComponent("Device path:", JPanel(BorderLayout()).apply {
          add(devicePath, BorderLayout.CENTER)
          add(JButton("Detect").apply {
            addActionListener {
              devicePath.text = module.microPythonFacet?.detectDevicePathSynchronously(selectedProvider) ?: ""
            }
          }, BorderLayout.EAST)
        })
        .panel
  }

  init {
    layout = BorderLayout()
    border = IdeBorderFactory.createEmptyBorder(UIUtil.PANEL_SMALL_INSETS)

    val contentPanel = FormBuilder.createFormBuilder()
        .addLabeledComponent("Device type:", deviceTypeCombo)
        .addComponent(autoDetectDevicePath)
        .addComponent(devicePathPanel)
        .addComponent(docsHyperlink)
        .panel

    add(contentPanel, BorderLayout.NORTH)

    deviceTypeCombo.apply {
      renderer = object: SimpleListCellRenderer<MicroPythonDeviceProvider>() {
        override fun customize(list: JList<out MicroPythonDeviceProvider>, value: MicroPythonDeviceProvider?,
                               index: Int, selected: Boolean, hasFocus: Boolean) {
          text = value?.presentableName ?: return
        }
      }
      addActionListener {
        docsHyperlink.apply {
          url = selectedProvider.documentationURL
          text = "Learn more about setting up ${selectedProvider.presentableName} devices"
          repaint()
        }
      }
    }
{'fraction_non_alphanumeric': 0.08525517036155333, 'fraction_numerical': 0.0035708971879184646, 'mean_word_length': 3.505361930294906, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6892273', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1825, 'n_words': 440}