Skip to content

Commit

Permalink
Avoid less useful auto-configuration project variants
Browse files Browse the repository at this point in the history
 #KT-14052 Fixed
  • Loading branch information
zarechenskiy committed Sep 29, 2016
1 parent 11b539c commit 66709bc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class KotlinClasspathContainer(val javaProject: IJavaProject) : IClasspat
val CONTAINER_ENTRY: IClasspathEntry = JavaCore.newContainerEntry(runtimeContainerId)
val LIB_RUNTIME_NAME = "kotlin-runtime"
val LIB_RUNTIME_SRC_NAME = "kotlin-runtime-sources"
private val LIB_REFLECT_NAME = "kotlin-reflect"
val LIB_REFLECT_NAME = "kotlin-reflect"

@JvmStatic
public fun getPathToLightClassesFolder(javaProject: IJavaProject): IPath {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private class UpdatePluginNotification(
}
}

private fun gridData(
fun gridData(
horizontalAlignment: Int = SWT.BEGINNING,
verticalAlignment: Int = SWT.CENTER,
grabExcessHorizontalSpace: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
package org.jetbrains.kotlin.ui.launch

import org.eclipse.core.resources.IProject
import org.eclipse.jface.dialogs.MessageDialogWithToggle
import org.eclipse.mylyn.commons.ui.dialogs.AbstractNotificationPopup
import org.eclipse.swt.SWT
import org.eclipse.swt.layout.GridLayout
import org.eclipse.swt.widgets.Composite
import org.eclipse.swt.widgets.Display
import org.eclipse.swt.widgets.Label
import org.jetbrains.kotlin.core.utils.ProjectUtils
import org.jetbrains.kotlin.eclipse.ui.utils.ProjectScopedPreferenceUtils
import org.jetbrains.kotlin.ui.gridData
import org.eclipse.swt.custom.StyledText
import org.jetbrains.kotlin.core.KotlinClasspathContainer
import org.jetbrains.kotlin.ui.gridData
import org.eclipse.swt.custom.StyleRange

class KotlinRuntimeConfigurator(private val project: IProject) : Runnable {
companion object {
private val SUGGESTION = "the Kotlin runtime library"
private val MESSAGE_DIALOG_TITLE = "Add $SUGGESTION"
private val MESSAGE_DIALOG_TEXT_FORMAT = "Would you like to add %s to the project \'%s\'?"
private val MESSAGE_DIALOG_TOOGLE_TEXT = "Don't ask again for this project"
private val PREFERENCE_KEY = "suggest.Configure.Runtime"

@JvmStatic fun suggestForProject(project: IProject) {
Display.getDefault().asyncExec(KotlinRuntimeConfigurator(project))
}
Expand All @@ -38,22 +40,46 @@ class KotlinRuntimeConfigurator(private val project: IProject) : Runnable {
override fun run() {
if (ProjectUtils.hasKotlinRuntime(project)) return

if (ProjectScopedPreferenceUtils.getBooleanPreference(project, PREFERENCE_KEY, true)) {
val dialogWithToogle = MessageDialogWithToggle.openYesNoQuestion(
Display.getDefault().getActiveShell(),
MESSAGE_DIALOG_TITLE,
String.format(MESSAGE_DIALOG_TEXT_FORMAT, SUGGESTION, project.getName()),
MESSAGE_DIALOG_TOOGLE_TEXT,
false,
null,
null)
if (dialogWithToogle.returnCode == 2) {
ProjectUtils.addKotlinRuntime(project)
}
ProjectUtils.addKotlinRuntime(project)

RuntimeNotificationPopup(Display.getDefault()).open()
}
}

private class RuntimeNotificationPopup(display: Display) : AbstractNotificationPopup(display) {
companion object {
private val RUNTIME_JAR = KotlinClasspathContainer.LIB_RUNTIME_NAME.toJar()
private val REFLECT_JAR = KotlinClasspathContainer.LIB_REFLECT_NAME.toJar()

private fun String.toJar() = "$this.jar"
}

init {
setDelayClose(0)
}

override fun createContentArea(parent: Composite) {
val parentLayout = GridLayout(1, true)

parent.setLayout(parentLayout)
parent.setLayoutData(gridData())

StyledText(parent, SWT.LEFT).apply {
setText("$RUNTIME_JAR, $REFLECT_JAR were added to the project classpath.")
makeBold(RUNTIME_JAR, REFLECT_JAR)
}
}

override fun getPopupShellTitle(): String = "Configure Kotlin in Project"

private fun StyledText.makeBold(vararg strs: String) {
val styleRanges = strs.mapNotNull { str ->
val start = text.indexOf(str)
if (start < 0) return@mapNotNull null

if (dialogWithToogle.getToggleState()) {
ProjectScopedPreferenceUtils.putBooleanPreference(project, PREFERENCE_KEY, false)
}
StyleRange(start, str.length, foreground, background, SWT.BOLD)
}

setStyleRanges(styleRanges.toTypedArray())
}
}

0 comments on commit 66709bc

Please sign in to comment.