summaryrefslogtreecommitdiff
path: root/java/idea-ui/src/com/intellij/ide/starters/remote/wizard/WebStarterInitialStep.kt
blob: 60793e9f6a18696b53374e8e70c595f27ca00415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright 2000-2021 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.ide.starters.remote.wizard

import com.intellij.icons.AllIcons
import com.intellij.ide.BrowserUtil
import com.intellij.ide.starters.JavaStartersBundle
import com.intellij.ide.starters.local.StarterModuleBuilder
import com.intellij.ide.starters.remote.*
import com.intellij.ide.starters.shared.*
import com.intellij.ide.starters.shared.ValidationFunctions.*
import com.intellij.ide.util.PropertiesComponent
import com.intellij.ide.wizard.AbstractWizard
import com.intellij.ide.wizard.withVisualPadding
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.observable.properties.GraphProperty
import com.intellij.openapi.observable.util.trim
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.roots.ui.configuration.validateJavaVersion
import com.intellij.openapi.ui.DialogPanel
import com.intellij.openapi.ui.InputValidator
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.ui.popup.IconButton
import com.intellij.openapi.util.Condition
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.Ref
import com.intellij.openapi.util.io.FileUtil
import com.intellij.ui.InplaceButton
import com.intellij.ui.SimpleListCellRenderer
import com.intellij.ui.components.ActionLink
import com.intellij.ui.dsl.builder.*
import com.intellij.util.concurrency.Semaphore
import com.intellij.util.ui.AsyncProcessIcon
import com.intellij.util.ui.UIUtil
import java.awt.event.ActionListener
import java.io.IOException
import java.net.MalformedURLException
import java.net.URL
import java.util.concurrent.Future
import javax.swing.DefaultComboBoxModel
import javax.swing.JComponent
import javax.swing.SwingUtilities

open class WebStarterInitialStep(contextProvider: WebStarterContextProvider) : CommonStarterInitialStep(
  contextProvider.wizardContext,
  contextProvider.starterContext,
  contextProvider.moduleBuilder,
  contextProvider.parentDisposable,
  contextProvider.settings
) {
  protected val moduleBuilder: WebStarterModuleBuilder = contextProvider.moduleBuilder
  protected val starterContext: WebStarterContext = contextProvider.starterContext

  private val packageNameProperty: GraphProperty<String> = propertyGraph.lazyProperty { starterContext.packageName }

  private val packagingProperty: GraphProperty<StarterAppPackaging> = propertyGraph.lazyProperty {
    starterContext.packaging ?: StarterAppPackaging("unknown", "")
  }
  private val languageLevelProperty: GraphProperty<StarterLanguageLevel> = propertyGraph.lazyProperty {
    starterContext.languageLevel ?: StarterLanguageLevel("unknown", "", "")
  }

  private var languageLevel: StarterLanguageLevel by languageLevelProperty
  private var packageName: String by packageNameProperty.trim()

  private val contentPanel: DialogPanel by lazy { createComponent() }
  private val progressIcon: AsyncProcessIcon by lazy { AsyncProcessIcon(moduleBuilder.builderId + "ServerOptions") }
  private val serverUrlLink: ActionLink by lazy { createServerUrlLink() }
  private val retryButton: InplaceButton by lazy { createRetryButton() }

  private val languageLevelsModel: DefaultComboBoxModel<StarterLanguageLevel> = DefaultComboBoxModel<StarterLanguageLevel>()
  private val applicationTypesModel: DefaultComboBoxModel<StarterAppType> = DefaultComboBoxModel<StarterAppType>()

  private lateinit var projectTypesSelector: SegmentedButton<StarterProjectType>
  private lateinit var packagingTypesSelector: SegmentedButton<StarterAppPackaging>
  private lateinit var languagesSelector: SegmentedButton<StarterLanguage>

  private var languages: List<StarterLanguage> = starterSettings.languages
  private var applicationTypes: List<StarterAppType> = starterSettings.applicationTypes
  private var projectTypes: List<StarterProjectType> = starterSettings.projectTypes
  private var packagingTypes: List<StarterAppPackaging> = starterSettings.packagingTypes

  @Volatile
  private var serverOptions: WebStarterServerOptions? = null

  @Volatile
  private var currentRequest: Future<*>? = null

  @Volatile
  private var isDisposed: Boolean = false

  private val serverOptionsLoadingSemaphore: Semaphore = Semaphore()
  private val serverSettingsButton: InplaceButton = InplaceButton(
    IconButton(JavaStartersBundle.message("button.tooltip.configure"), AllIcons.General.Gear, AllIcons.General.GearHover),
    ActionListener {
      configureServer()
    }
  )

  init {
    Disposer.register(parentDisposable, Disposable {
      isDisposed = true
      currentRequest?.cancel(true)
    })
  }

  override fun getComponent(): JComponent = contentPanel

  override fun getHelpId(): String? = moduleBuilder.getHelpId()

  override fun updateDataModel() {
    starterContext.serverOptions = this.serverOptions!!

    starterContext.projectType = projectTypeProperty.get()
    starterContext.language = languageProperty.get()
    starterContext.group = groupId
    starterContext.artifact = artifactId
    starterContext.name = entityName
    starterContext.packageName = packageName
    starterContext.packaging = packagingProperty.get()
    starterContext.languageLevel = languageLevel
    starterContext.testFramework = testFrameworkProperty.get()
    starterContext.applicationType = applicationTypeProperty.get()
    starterContext.includeExamples = exampleCodeProperty.get()
    starterContext.gitIntegration = gitProperty.get()

    wizardContext.projectName = entityName
    wizardContext.setProjectFileDirectory(FileUtil.join(location, entityName))

    val sdk = sdkProperty.get()
    if (wizardContext.project == null) {
      wizardContext.projectJdk = sdk
    }
    else {
      moduleBuilder.moduleJdk = sdk
    }
  }

  private fun suggestPackageName(): String {
    return StarterModuleBuilder.suggestPackageName(groupId, artifactId)
  }

  private fun createComponent(): DialogPanel {
    entityNameProperty.dependsOn(artifactIdProperty) { artifactId }
    artifactIdProperty.dependsOn(entityNameProperty) { entityName }

    packageNameProperty.dependsOn(artifactIdProperty, ::suggestPackageName)
    packageNameProperty.dependsOn(groupIdProperty, ::suggestPackageName)

    progressIcon.toolTipText = JavaStartersBundle.message("message.state.connecting.and.retrieving.options")

    return panel {
      row {
        label(JavaStartersBundle.message("title.project.server.url.label"))

        cell(serverUrlLink)
        cell(serverSettingsButton)
        cell(retryButton)
        cell(progressIcon)
      }.bottomGap(BottomGap.SMALL)

      addProjectLocationUi()

      addFieldsBefore(this)

      if (starterSettings.languages.size > 1) {
        row(JavaStartersBundle.message("title.project.language.label")) {
          languagesSelector = segmentedButton(starterSettings.languages, StarterLanguage::title)
            .bind(languageProperty)

          bottomGap(BottomGap.SMALL)
        }
      }

      if (starterSettings.projectTypes.isNotEmpty()) {
        val messages = starterSettings.customizedMessages
        row(messages?.projectTypeLabel ?: JavaStartersBundle.message("title.project.type.label")) {
          projectTypesSelector = segmentedButton(starterSettings.projectTypes, StarterProjectType::title)
            .bind(projectTypeProperty)

          bottomGap(BottomGap.SMALL)
        }
      }

      if (starterSettings.testFrameworks.isNotEmpty()) {
        row(JavaStartersBundle.message("title.project.test.framework.label")) {
          segmentedButton(starterSettings.testFrameworks, StarterTestRunner::title)
            .bind(testFrameworkProperty)
        }.bottomGap(BottomGap.SMALL)
      }

      addGroupArtifactUi()

      if (starterSettings.isPackageNameEditable) {
        row(JavaStartersBundle.message("title.project.package.label")) {
          textField()
            .bindText(packageNameProperty)
            .columns(COLUMNS_MEDIUM)
            .withSpecialValidation(CHECK_NOT_EMPTY, CHECK_NO_WHITESPACES, CHECK_NO_RESERVED_WORDS, CHECK_PACKAGE_NAME)
        }.bottomGap(BottomGap.SMALL)
      }

      if (starterSettings.applicationTypes.isNotEmpty()) {
        row(JavaStartersBundle.message("title.project.app.type.label")) {
          applicationTypesModel.addAll(starterSettings.applicationTypes)
          comboBox(applicationTypesModel, SimpleListCellRenderer.create("") { it?.title ?: "" })
            .bindItem(applicationTypeProperty)
            .columns(COLUMNS_MEDIUM)
        }.bottomGap(BottomGap.SMALL)
      }

      addSdkUi()

      if (starterSettings.languageLevels.isNotEmpty()) {
        row(JavaStartersBundle.message("title.project.java.version.label")) {
          languageLevelsModel.addAll(starterSettings.languageLevels)
          comboBox(languageLevelsModel, SimpleListCellRenderer.create("") { it?.title ?: "" })
            .bindItem(languageLevelProperty)
        }.bottomGap(BottomGap.SMALL)
      }

      if (starterSettings.packagingTypes.isNotEmpty()) {
        row(JavaStartersBundle.message("title.project.packaging.label")) {
          packagingTypesSelector = segmentedButton(starterSettings.packagingTypes, StarterAppPackaging::title)
            .bind(packagingProperty)
        }.bottomGap(BottomGap.SMALL)
      }

      addSampleCodeUi()

      addFieldsAfter(this)
    }.withVisualPadding()
  }

  private fun createServerUrlLink(): ActionLink {
    val result = ActionLink(urlPreview(starterContext.serverUrl)) {
      BrowserUtil.browse(starterContext.serverUrl)
    }
    UIUtil.applyStyle(UIUtil.ComponentStyle.REGULAR, result)
    return result
  }

  private fun createRetryButton(): InplaceButton {
    return InplaceButton(IconButton(JavaStartersBundle.message("button.tooltip.retry"),
                                    AllIcons.Nodes.ErrorIntroduction, AllIcons.Actions.ForceRefresh), ActionListener {
      requestServerOptions()
    }).apply {
      isVisible = false
    }
  }

  @NlsSafe
  private fun urlPreview(serverUrl: String): String {
    val url = serverUrl
      .removePrefix("https://")
      .removeSuffix("/")

    if (url.length > 35) {
      return url.take(30) + "..."
    }
    return url
  }

  override fun getPreferredFocusedComponent(): JComponent? {
    return contentPanel.preferredFocusedComponent
  }

  override fun validate(): Boolean {
    if (!validateFormFields(component, contentPanel, validatedTextComponents)) {
      return false
    }
    val passTechnologyName = if (starterSettings.languageLevels.size > 1) null else moduleBuilder.presentableName
    if (languageLevel.javaVersion.isNotBlank() &&
        !validateJavaVersion(sdkProperty, languageLevel.javaVersion, passTechnologyName)) {
      return false
    }

    return checkServerOptionsLoaded()
  }

  private fun checkServerOptionsLoaded(): Boolean {
    val request = currentRequest
    if (serverOptions != null && request == null) {
      return true
    }

    if (request == null) {
      // failure? retry server options loading
      requestServerOptions()
    }

    val newOptionsRef: Ref<WebStarterServerOptions> = Ref.create()

    ProgressManager.getInstance().runProcessWithProgressSynchronously(Runnable {
      val progressIndicator = ProgressManager.getInstance().progressIndicator
      progressIndicator.isIndeterminate = true

      for (i in 0 until 30) {
        progressIndicator.checkCanceled()
        if (serverOptionsLoadingSemaphore.waitFor(500)) {
          serverOptions?.let {
            newOptionsRef.set(it)
          }
          return@Runnable
        }
      }
    }, JavaStartersBundle.message("message.state.connecting.and.retrieving.options"), true, wizardContext.project)

    if (!newOptionsRef.isNull) {
      updatePropertiesWithServerOptions(newOptionsRef.get())
    }

    return serverOptions != null
  }

  override fun _init() {
    super._init()

    if (serverOptions == null && currentRequest == null) {
      @Suppress("HardCodedStringLiteral")
      val serverUrlFromSettings = PropertiesComponent.getInstance().getValue(getServerUrlPropertyName())
      if (serverUrlFromSettings != null) {
        setServerUrl(serverUrlFromSettings)
      }

      // required on dialog opening to get correct modality state
      SwingUtilities.invokeLater(this::requestServerOptions)
    }
  }

  private fun setServerUrl(@NlsSafe url: String) {
    starterContext.serverUrl = url
    serverUrlLink.text = urlPreview(url)
    serverUrlLink.toolTipText = url
  }

  private fun requestServerOptions() {
    progressIcon.isVisible = true
    retryButton.isVisible = false
    progressIcon.resume()

    serverOptionsLoadingSemaphore.down()

    currentRequest = ApplicationManager.getApplication().executeOnPooledThread {
      val readyServerOptions = try {
        moduleBuilder.getServerOptions(starterContext.serverUrl)
      }
      catch (e: Exception) {
        if (e is IOException || e is IllegalStateException) {
          logger<WebStarterInitialStep>().info("Unable to get server options for " + moduleBuilder.builderId, e)
        }
        else {
          logger<WebStarterInitialStep>().error("Unable to get server options for " + moduleBuilder.builderId, e)
        }

        ApplicationManager.getApplication().invokeLater(
          {
            if (component.isShowing) {
              // only if the wizard is visible
              Messages.showErrorDialog(
                JavaStartersBundle.message("message.no.connection.with.error.content", starterContext.serverUrl, e.message),
                JavaStartersBundle.message("message.title.error"))
            }
          }, getModalityState())

        null
      }

      setServerOptions(readyServerOptions)
    }
  }

  private fun setServerOptions(serverOptions: WebStarterServerOptions?) {
    this.serverOptions = serverOptions
    this.currentRequest = null
    this.serverOptionsLoadingSemaphore.up()

    ApplicationManager.getApplication().invokeLater(Runnable {
      progressIcon.suspend()
      progressIcon.isVisible = false
      retryButton.isVisible = serverOptions == null

      if (serverOptions != null) {
        updatePropertiesWithServerOptions(serverOptions)
      }
    }, getModalityState(), getDisposed())
  }

  private fun getModalityState(): ModalityState {
    return ModalityState.stateForComponent(wizardContext.getUserData(AbstractWizard.KEY)!!.contentComponent)
  }

  private fun getDisposed(): Condition<Any> = Condition<Any> { isDisposed }

  private fun configureServer() {
    val currentServerUrl = starterContext.serverUrl
    val serverUrlTitle = starterSettings.customizedMessages?.serverUrlDialogTitle
                         ?: JavaStartersBundle.message("title.server.url.dialog")

    val newUrl = Messages.showInputDialog(component, null, serverUrlTitle, null, currentServerUrl, object : InputValidator {
      override fun canClose(inputString: String?): Boolean = checkInput(inputString)

      override fun checkInput(inputString: String?): Boolean {
        try {
          URL(inputString)
        }
        catch (e: MalformedURLException) {
          return false
        }
        return true
      }
    })

    // update
    if (newUrl != null && starterContext.serverUrl != newUrl) {
      setServerUrl(newUrl)

      PropertiesComponent.getInstance().setValue(getServerUrlPropertyName(), newUrl)

      requestServerOptions()
    }
  }

  private fun getServerUrlPropertyName(): String {
    return moduleBuilder.builderId + ".service.url.last"
  }

  protected open fun updatePropertiesWithServerOptions(serverOptions: WebStarterServerOptions) {
    starterContext.frameworkVersion = serverOptions.frameworkVersions.find { it.isDefault }
                                      ?: serverOptions.frameworkVersions.firstOrNull()

    val currentPackageName = packageName // remember package name before applying group and artifact

    serverOptions.extractOption(SERVER_NAME_KEY) {
      if (entityName == suggestName(DEFAULT_MODULE_NAME)) {
        val newName = suggestName(it)
        if (entityName != newName) {
          entityNameProperty.set(newName)
        }
      }
    }
    serverOptions.extractOption(SERVER_GROUP_KEY) {
      if (groupId == DEFAULT_MODULE_GROUP && groupId != it) {
        groupIdProperty.set(it)
      }
    }
    serverOptions.extractOption(SERVER_ARTIFACT_KEY) {
      if (artifactId == DEFAULT_MODULE_ARTIFACT && artifactId != it) {
        artifactIdProperty.set(it)
      }
    }
    serverOptions.extractOption(SERVER_PACKAGE_NAME_KEY) {
      if (currentPackageName == DEFAULT_PACKAGE_NAME && currentPackageName != it) {
        packageNameProperty.set(it)
      }
    }
    serverOptions.extractOption(SERVER_VERSION_KEY) {
      starterContext.version = it
    }
    serverOptions.extractOption(SERVER_LANGUAGE_LEVELS_KEY) { levels ->
      val selectedItem = languageLevelsModel.selectedItem
      languageLevelsModel.removeAllElements()
      languageLevelsModel.addAll(levels)

      if (levels.contains(selectedItem)) {
        languageLevelsModel.selectedItem = selectedItem
      }
      else {
        languageLevel = levels.firstOrNull() ?: StarterLanguageLevel("unknown", "", "")
        languageLevelsModel.selectedItem = languageLevel
      }
    }
    serverOptions.extractOption(SERVER_LANGUAGE_LEVEL_KEY) { level ->
      if (languageLevel == starterSettings.defaultLanguageLevel && languageLevel != level) {
        languageLevelProperty.set(level)
      }
    }
    serverOptions.extractOption(SERVER_PROJECT_TYPES) { types ->
      if (types.isNotEmpty() && types != this.projectTypes && ::projectTypesSelector.isInitialized) {
        val correspondingOption = types.find { it.id == projectTypeProperty.get().id }
        projectTypeProperty.set(correspondingOption ?: types.first())
        projectTypesSelector.items(types)
        this.projectTypes = types
      }
    }
    serverOptions.extractOption(SERVER_APPLICATION_TYPES) { types ->
      if (types.isNotEmpty() && types != applicationTypes) {
        applicationTypesModel.removeAllElements()
        applicationTypesModel.addAll(types)
        applicationTypesModel.selectedItem = types.firstOrNull()
        this.applicationTypes = types
      }
    }
    serverOptions.extractOption(SERVER_PACKAGING_TYPES) { types ->
      if (types.isNotEmpty() && types != this.packagingTypes && ::packagingTypesSelector.isInitialized) {
        val correspondingOption = types.find { it.id == packagingProperty.get().id }
        packagingProperty.set(correspondingOption ?: types.first())
        packagingTypesSelector.items(types)
        this.packagingTypes = types
      }
    }
    serverOptions.extractOption(SERVER_LANGUAGES) { languages ->
      if (languages.isNotEmpty() && languages != this.languages && ::languagesSelector.isInitialized) {
        val correspondingOption = languages.find { it.id == languageProperty.get().id }
        languageProperty.set(correspondingOption ?: languages.first())
        languagesSelector.items(languages)
        this.languages = languages
      }
    }

    contentPanel.revalidate()
  }
}