2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.settingsRepository
18 import com.intellij.notification.NotificationType
19 import com.intellij.openapi.application.ApplicationManager
20 import com.intellij.openapi.project.Project
21 import com.intellij.openapi.ui.Messages
22 import com.intellij.openapi.ui.TextFieldWithBrowseButton
23 import com.intellij.openapi.util.SystemInfo
24 import com.intellij.openapi.util.text.StringUtil
25 import com.intellij.util.ArrayUtil
26 import org.jetbrains.settingsRepository.actions.NOTIFICATION_GROUP
27 import java.awt.Container
28 import java.awt.event.ActionEvent
29 import javax.swing.AbstractAction
30 import javax.swing.Action
32 internal fun checkUrl(url: String?): Boolean {
34 return url != null && url.length > 1 && icsManager.repositoryService.checkUrl(url, false)
36 catch (e: Throwable) {
41 fun updateSyncButtonState(url: String?, syncActions: Array<Action>) {
42 val enabled = checkUrl(url)
43 for (syncAction in syncActions) {
44 syncAction.isEnabled = enabled;
48 fun createMergeActions(project: Project?, urlTextField: TextFieldWithBrowseButton, dialogParent: Container, okAction: (() -> Unit)): Array<Action> {
49 var syncTypes = SyncType.values()
50 if (SystemInfo.isMac) {
51 syncTypes = ArrayUtil.reverseArray(syncTypes)
54 val icsManager = icsManager
57 val syncType = syncTypes[it]
58 object : AbstractAction(icsMessage("action.${if (syncType == SyncType.MERGE) "Merge" else (if (syncType == SyncType.OVERWRITE_LOCAL) "ResetToTheirs" else "ResetToMy")}Settings.text")) {
59 private fun saveRemoteRepositoryUrl(): Boolean {
60 val url = StringUtil.nullize(urlTextField.text)
61 if (url != null && !icsManager.repositoryService.checkUrl(url, true, project)) {
65 val repositoryManager = icsManager.repositoryManager
66 repositoryManager.createRepositoryIfNeed()
67 repositoryManager.setUpstream(url, null)
71 override fun actionPerformed(event: ActionEvent) {
72 val repositoryWillBeCreated = !icsManager.repositoryManager.isRepositoryExists()
73 var upstreamSet = false
75 if (!saveRemoteRepositoryUrl()) {
76 if (repositoryWillBeCreated) {
77 // remove created repository
78 icsManager.repositoryManager.deleteRepository()
84 if (repositoryWillBeCreated && syncType != SyncType.OVERWRITE_LOCAL) {
85 ApplicationManager.getApplication().saveSettings()
87 icsManager.sync(syncType, project, { copyLocalConfig() })
90 icsManager.sync(syncType, project, null)
93 catch (e: Throwable) {
94 if (repositoryWillBeCreated) {
95 // remove created repository
96 icsManager.repositoryManager.deleteRepository()
101 if (!upstreamSet || e is NoRemoteRepositoryException) {
102 Messages.showErrorDialog(dialogParent, icsMessage("set.upstream.failed.message", e.message), icsMessage("set.upstream.failed.title"))
105 Messages.showErrorDialog(dialogParent, StringUtil.notNullize(e.message, "Internal error"), icsMessage(if (e is AuthenticationException) "sync.not.authorized.title" else "sync.rejected.title"))
111 NOTIFICATION_GROUP.createNotification(icsMessage("sync.done.message"), NotificationType.INFORMATION).notify(project)