vcs: Convert "CommitOptionsPanel" to kotlin

This commit is contained in:
Konstantin Kolosovsky
2018-06-07 15:06:34 +03:00
parent 97f210d829
commit df2d2103dc
2 changed files with 103 additions and 150 deletions
@@ -369,7 +369,8 @@ public class CommitChangeListDialog extends DialogWrapper implements CheckinProj
setComment(initialSelection, comment);
}
myCommitOptions = new CommitOptionsPanel(this, myHandlers, getAffectedVcses());
//noinspection unchecked
myCommitOptions = new CommitOptionsPanel(this, myHandlers, (Set)getAffectedVcses());
restoreState();
myWarningLabel = new JBLabel();
@@ -1,193 +1,145 @@
/*
* 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.intellij.openapi.vcs.changes.ui;
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.vcs.changes.ui
import com.intellij.openapi.Disposable;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.CheckinProjectPanel;
import com.intellij.openapi.vcs.changes.ChangesUtil;
import com.intellij.openapi.vcs.changes.LocalChangeList;
import com.intellij.openapi.vcs.changes.PseudoMap;
import com.intellij.openapi.vcs.checkin.CheckinChangeListSpecificComponent;
import com.intellij.openapi.vcs.checkin.CheckinEnvironment;
import com.intellij.openapi.vcs.checkin.CheckinHandler;
import com.intellij.openapi.vcs.ui.RefreshableOnComponent;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.components.BorderLayoutPanel;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.Disposable
import com.intellij.openapi.vcs.AbstractVcs
import com.intellij.openapi.vcs.CheckinProjectPanel
import com.intellij.openapi.vcs.VcsBundle.message
import com.intellij.openapi.vcs.changes.ChangesUtil.getAffectedVcses
import com.intellij.openapi.vcs.changes.ChangesUtil.getAffectedVcsesForFiles
import com.intellij.openapi.vcs.changes.LocalChangeList
import com.intellij.openapi.vcs.changes.PseudoMap
import com.intellij.openapi.vcs.checkin.CheckinChangeListSpecificComponent
import com.intellij.openapi.vcs.checkin.CheckinHandler
import com.intellij.openapi.vcs.ui.RefreshableOnComponent
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.ScrollPaneFactory
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import java.awt.BorderLayout
import java.util.Collections.unmodifiableList
import javax.swing.Box
import javax.swing.JPanel
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
private val VCS_COMPARATOR = compareBy<AbstractVcs<*>, String>(String.CASE_INSENSITIVE_ORDER) { it.keyInstanceMethod.name }
import static com.intellij.openapi.vcs.VcsBundle.message;
import static com.intellij.util.containers.ContainerUtil.*;
import static java.util.Collections.unmodifiableList;
class CommitOptionsPanel(private val myCommitPanel: CheckinProjectPanel,
private val myHandlers: Collection<CheckinHandler>,
vcses: Collection<AbstractVcs<*>>) : BorderLayoutPanel(), Disposable {
private val myPerVcsOptionsPanels = mutableMapOf<AbstractVcs<*>, JPanel>()
private val myAdditionalComponents = mutableListOf<RefreshableOnComponent>()
private val myCheckinChangeListSpecificComponents = mutableSetOf<CheckinChangeListSpecificComponent>()
val additionalData = PseudoMap<Any, Any>()
val isEmpty = init(vcses)
public class CommitOptionsPanel extends BorderLayoutPanel implements Disposable {
val additionalComponents: List<RefreshableOnComponent> get() = unmodifiableList(myAdditionalComponents)
private static final Comparator<AbstractVcs> VCS_COMPARATOR =
Comparator.comparing(it -> it.getKeyInstanceMethod().getName(), String::compareToIgnoreCase);
fun saveState() = myAdditionalComponents.forEach { it.saveState() }
@NotNull private final CheckinProjectPanel myCommitPanel;
@NotNull private final Collection<CheckinHandler> myHandlers;
@NotNull private final Map<AbstractVcs, JPanel> myPerVcsOptionsPanels = newHashMap();
@NotNull private final List<RefreshableOnComponent> myAdditionalComponents = newArrayList();
@NotNull private final Set<CheckinChangeListSpecificComponent> myCheckinChangeListSpecificComponents = newHashSet();
@NotNull private final PseudoMap<Object, Object> myAdditionalData = new PseudoMap<>();
private final boolean myEmpty;
fun restoreState() = myAdditionalComponents.forEach { it.restoreState() }
public CommitOptionsPanel(@NotNull CheckinProjectPanel panel,
@NotNull Collection<CheckinHandler> handlers,
@NotNull Collection<AbstractVcs> vcses) {
myCommitPanel = panel;
myHandlers = handlers;
myEmpty = init(vcses);
}
fun refresh() = myAdditionalComponents.forEach { it.refresh() }
public boolean isEmpty() {
return myEmpty;
}
@NotNull
public List<RefreshableOnComponent> getAdditionalComponents() {
return unmodifiableList(myAdditionalComponents);
}
@NotNull
public PseudoMap<Object, Object> getAdditionalData() {
return myAdditionalData;
}
public void saveState() {
myAdditionalComponents.forEach(RefreshableOnComponent::saveState);
}
public void restoreState() {
myAdditionalComponents.forEach(RefreshableOnComponent::restoreState);
}
public void refresh() {
myAdditionalComponents.forEach(RefreshableOnComponent::refresh);
}
public void onChangeListSelected(@NotNull LocalChangeList changeList, List<VirtualFile> unversionedFiles) {
Set<AbstractVcs> affectedVcses = union(
ChangesUtil.getAffectedVcses(changeList.getChanges(), myCommitPanel.getProject()),
ChangesUtil.getAffectedVcsesForFiles(unversionedFiles, myCommitPanel.getProject()));
for (Map.Entry<AbstractVcs, JPanel> entry : myPerVcsOptionsPanels.entrySet()) {
entry.getValue().setVisible(affectedVcses.contains(entry.getKey()));
fun onChangeListSelected(changeList: LocalChangeList, unversionedFiles: List<VirtualFile>) {
val affectedVcses =
getAffectedVcses(changeList.changes, myCommitPanel.project) + getAffectedVcsesForFiles(unversionedFiles, myCommitPanel.project)
for ((vcs, panel) in myPerVcsOptionsPanels) {
panel.isVisible = affectedVcses.contains(vcs)
}
myCheckinChangeListSpecificComponents.forEach(component -> component.onChangeListSelected(changeList));
myCheckinChangeListSpecificComponents.forEach { it.onChangeListSelected(changeList) }
}
public void saveChangeListComponentsState() {
myCheckinChangeListSpecificComponents.forEach(CheckinChangeListSpecificComponent::saveState);
fun saveChangeListComponentsState() = myCheckinChangeListSpecificComponents.forEach { it.saveState() }
override fun dispose() {
}
@Override
public void dispose() {
}
private boolean init(@NotNull Collection<AbstractVcs> vcses) {
String borderTitleName = myCommitPanel.getCommitActionName().replace("_", "").replace("&", "");
boolean hasVcsOptions = false;
Box vcsCommitOptions = Box.createVerticalBox();
for (AbstractVcs vcs : sorted(vcses, VCS_COMPARATOR)) {
CheckinEnvironment checkinEnvironment = vcs.getCheckinEnvironment();
private fun init(vcses: Collection<AbstractVcs<*>>): Boolean {
val borderTitleName = myCommitPanel.commitActionName.replace("_", "").replace("&", "")
var hasVcsOptions = false
val vcsCommitOptions = Box.createVerticalBox()
for (vcs in vcses.sortedWith(VCS_COMPARATOR)) {
val checkinEnvironment = vcs.checkinEnvironment
if (checkinEnvironment != null) {
RefreshableOnComponent options = checkinEnvironment.createAdditionalOptionsPanel(myCommitPanel, myAdditionalData);
val options = checkinEnvironment.createAdditionalOptionsPanel(myCommitPanel, additionalData)
if (options != null) {
JPanel vcsOptions = new JPanel(new BorderLayout());
vcsOptions.add(options.getComponent(), BorderLayout.CENTER);
vcsOptions.setBorder(IdeBorderFactory.createTitledBorder(vcs.getDisplayName(), true));
vcsCommitOptions.add(vcsOptions);
myPerVcsOptionsPanels.put(vcs, vcsOptions);
myAdditionalComponents.add(options);
if (options instanceof CheckinChangeListSpecificComponent) {
myCheckinChangeListSpecificComponents.add((CheckinChangeListSpecificComponent)options);
val vcsOptions = JPanel(BorderLayout())
vcsOptions.add(options.component, BorderLayout.CENTER)
vcsOptions.border = IdeBorderFactory.createTitledBorder(vcs.displayName, true)
vcsCommitOptions.add(vcsOptions)
myPerVcsOptionsPanels[vcs] = vcsOptions
myAdditionalComponents.add(options)
if (options is CheckinChangeListSpecificComponent) {
myCheckinChangeListSpecificComponents.add(options)
}
hasVcsOptions = true;
hasVcsOptions = true
}
}
}
boolean beforeVisible = false;
boolean afterVisible = false;
Box beforeBox = Box.createVerticalBox();
Box afterBox = Box.createVerticalBox();
for (CheckinHandler handler : myHandlers) {
RefreshableOnComponent beforePanel = handler.getBeforeCheckinConfigurationPanel();
var beforeVisible = false
var afterVisible = false
val beforeBox = Box.createVerticalBox()
val afterBox = Box.createVerticalBox()
for (handler in myHandlers) {
val beforePanel = handler.beforeCheckinConfigurationPanel
if (beforePanel != null) {
beforeVisible = true;
addCheckinHandlerComponent(beforePanel, beforeBox);
beforeVisible = true
addCheckinHandlerComponent(beforePanel, beforeBox)
}
RefreshableOnComponent afterPanel = handler.getAfterCheckinConfigurationPanel(this);
val afterPanel = handler.getAfterCheckinConfigurationPanel(this)
if (afterPanel != null) {
afterVisible = true;
addCheckinHandlerComponent(afterPanel, afterBox);
afterVisible = true
addCheckinHandlerComponent(afterPanel, afterBox)
}
}
if (!hasVcsOptions && !beforeVisible && !afterVisible) return true;
if (!hasVcsOptions && !beforeVisible && !afterVisible) return true
Box optionsBox = Box.createVerticalBox();
val optionsBox = Box.createVerticalBox()
if (hasVcsOptions) {
vcsCommitOptions.add(Box.createVerticalGlue());
optionsBox.add(vcsCommitOptions);
vcsCommitOptions.add(Box.createVerticalGlue())
optionsBox.add(vcsCommitOptions)
}
if (beforeVisible) {
beforeBox.add(Box.createVerticalGlue());
JPanel beforePanel = new JPanel(new BorderLayout());
beforePanel.add(beforeBox);
beforePanel.setBorder(IdeBorderFactory.createTitledBorder(
message("border.standard.checkin.options.group", borderTitleName), true));
optionsBox.add(beforePanel);
beforeBox.add(Box.createVerticalGlue())
val beforePanel = JPanel(BorderLayout())
beforePanel.add(beforeBox)
beforePanel.border = IdeBorderFactory.createTitledBorder(
message("border.standard.checkin.options.group", borderTitleName), true)
optionsBox.add(beforePanel)
}
if (afterVisible) {
afterBox.add(Box.createVerticalGlue());
JPanel afterPanel = new JPanel(new BorderLayout());
afterPanel.add(afterBox);
afterPanel.setBorder(IdeBorderFactory.createTitledBorder(
message("border.standard.after.checkin.options.group", borderTitleName), true));
optionsBox.add(afterPanel);
afterBox.add(Box.createVerticalGlue())
val afterPanel = JPanel(BorderLayout())
afterPanel.add(afterBox)
afterPanel.border = IdeBorderFactory.createTitledBorder(
message("border.standard.after.checkin.options.group", borderTitleName), true)
optionsBox.add(afterPanel)
}
optionsBox.add(Box.createVerticalGlue());
JPanel additionalOptionsPanel = new JPanel(new BorderLayout());
additionalOptionsPanel.add(optionsBox, BorderLayout.NORTH);
optionsBox.add(Box.createVerticalGlue())
val additionalOptionsPanel = JPanel(BorderLayout())
additionalOptionsPanel.add(optionsBox, BorderLayout.NORTH)
JScrollPane optionsPane = ScrollPaneFactory.createScrollPane(additionalOptionsPanel, true);
addToCenter(optionsPane).withBorder(JBUI.Borders.emptyLeft(10));
return false;
val optionsPane = ScrollPaneFactory.createScrollPane(additionalOptionsPanel, true)
addToCenter(optionsPane).withBorder(JBUI.Borders.emptyLeft(10))
return false
}
private void addCheckinHandlerComponent(@NotNull RefreshableOnComponent component, @NotNull Box container) {
container.add(component.getComponent());
myAdditionalComponents.add(component);
if (component instanceof CheckinChangeListSpecificComponent) {
myCheckinChangeListSpecificComponents.add((CheckinChangeListSpecificComponent)component);
private fun addCheckinHandlerComponent(component: RefreshableOnComponent, container: Box) {
container.add(component.component)
myAdditionalComponents.add(component)
if (component is CheckinChangeListSpecificComponent) {
myCheckinChangeListSpecificComponents.add(component)
}
}
}