Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

PijulPushTargetPanel.kt
/**
 * Dracon - An IntelliJ-Pijul integration.
 * Copyright 2021 JonathanxD <jhrldev@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.github.jonathanxd.dracon.push

import com.github.jonathanxd.dracon.i18n.DraconBundle
import com.github.jonathanxd.dracon.repository.PijulRepository
import com.intellij.dvcs.DvcsUtil
import com.intellij.dvcs.push.PushTargetPanel
import com.intellij.dvcs.push.VcsError
import com.intellij.dvcs.push.ui.PushTargetEditorListener
import com.intellij.dvcs.push.ui.PushTargetTextField
import com.intellij.dvcs.push.ui.VcsEditableTextComponent
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.text.HtmlChunk
import com.intellij.openapi.util.text.StringUtil
import com.intellij.ui.ColoredTreeCellRenderer
import com.intellij.util.textCompletion.TextFieldWithCompletion
import com.intellij.ui.SimpleTextAttributes
import java.awt.BorderLayout

class PijulPushTargetPanel(repository: PijulRepository, source: PijulPushSource, defaultTarget: PijulTarget?) :
    PushTargetPanel<PijulTarget>() {
    private val myRepository: PijulRepository
    private val myBranchName: @NlsSafe String?
    private val myDestTargetPanel: TextFieldWithCompletion
    private val myTargetRenderedComponent: VcsEditableTextComponent

    override fun render(
        renderer: ColoredTreeCellRenderer,
        isSelected: Boolean,
        isActive: Boolean,
        forceRenderedText: String?
    ) {
        if (forceRenderedText != null) {
            myDestTargetPanel.text = forceRenderedText
            renderer.append(forceRenderedText)
            return
        }
        val targetText = myDestTargetPanel.text
        if (StringUtil.isEmptyOrSpaces(targetText)) {
            renderer.append(
                DraconBundle.message("action.Pijul.push.enter.remote"),
                SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES,
                myTargetRenderedComponent
            )
        }
        myTargetRenderedComponent.setSelected(isSelected)
        myTargetRenderedComponent.setTransparent(!isActive)
        myTargetRenderedComponent.render(renderer)
    }

    override fun getValue(): PijulTarget {
        return createValidPushTarget()
    }

    private fun createValidPushTarget(): PijulTarget {
        return PijulTarget(myDestTargetPanel.text, myBranchName!!)
    }

    override fun fireOnCancel() {
        myDestTargetPanel.text = myTargetRenderedComponent.text
    }

    override fun fireOnChange() {
        myTargetRenderedComponent.updateLinkText(myDestTargetPanel.text)
    }

    override fun verify(): ValidationInfo? {
        return if (StringUtil.isEmptyOrSpaces(myDestTargetPanel.text)) {
            ValidationInfo(VcsError.createEmptyTargetError(DvcsUtil.getShortRepositoryName(myRepository)).text, this)
        } else null
    }

    override fun setFireOnChangeAction(action: Runnable) {
        // no extra changing components => ignore
    }

    override fun addTargetEditorListener(listener: PushTargetEditorListener) {
        myDestTargetPanel.addDocumentListener(object : DocumentListener {
            override fun documentChanged(e: DocumentEvent) {
                listener.onTargetInEditModeChanged(myDestTargetPanel.text)
            }
        })
    }

    init {
        layout = BorderLayout()
        isOpaque = false
        myRepository = repository
        myBranchName = source.channel
        val targetVariants: List<String> = repository.pijulConfig.remotes.keys.toList()
        val defaultText = defaultTarget?.presentation ?: ""
        myTargetRenderedComponent = VcsEditableTextComponent(HtmlChunk.link("", defaultText).toString(), null)
        myDestTargetPanel = PushTargetTextField(repository.project, targetVariants, defaultText)
        add(myDestTargetPanel, BorderLayout.CENTER)
    }
}