Skip to content

Commit 92c8aea

Browse files
committed
chore: clean up, maybe resolve "Project is already disposed"
1 parent 899aefc commit 92c8aea

File tree

11 files changed

+53
-57
lines changed

11 files changed

+53
-57
lines changed

src/main/kotlin/net/allape/App.kt

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package net.allape
22

3-
import com.intellij.openapi.diagnostic.Logger
43
import com.intellij.openapi.project.DumbAware
54
import com.intellij.openapi.project.Project
65
import com.intellij.openapi.wm.ToolWindow
76
import com.intellij.openapi.wm.ToolWindowFactory
87
import com.intellij.openapi.wm.ex.ToolWindowEx
98
import com.intellij.ui.content.ContentFactory
10-
import com.intellij.ui.content.ContentManagerEvent
11-
import com.intellij.ui.content.ContentManagerListener
129
import net.allape.action.NewWindowAction
1310
import net.allape.common.XFTPManager
1411
import net.allape.xftp.XFTP
@@ -22,7 +19,7 @@ class App: ToolWindowFactory, DumbAware {
2219
val window = XFTP(project, toolWindow)
2320

2421
//获取内容工厂的实例
25-
val contentFactory = ContentFactory.SERVICE.getInstance()
22+
val contentFactory = ContentFactory.getInstance()
2623

2724
//获取用于toolWindow显示的内容
2825
val content = contentFactory.createContent(window.getUI(), XFTPManager.DEFAULT_NAME, false)
@@ -36,31 +33,11 @@ class App: ToolWindowFactory, DumbAware {
3633

3734
window.bindContext(content)
3835

39-
// 放入缓存
40-
XFTPManager.windows[content] = window
41-
4236
return window
4337
}
4438
}
4539

46-
private val logger = Logger.getInstance(App::class.java)
47-
4840
override fun init(toolWindow: ToolWindow) {
49-
XFTPManager.toolWindow = toolWindow
50-
toolWindow.addContentManagerListener(object : ContentManagerListener {
51-
override fun contentRemoved(event: ContentManagerEvent) {
52-
super.contentRemoved(event)
53-
val window = XFTPManager.windows[event.content]
54-
if (window == null) {
55-
logger.warn("closed an un-cached window: $event")
56-
} else {
57-
XFTPManager.windows.remove(event.content)
58-
}
59-
if (toolWindow.contentManager.contents.isEmpty()) {
60-
toolWindow.hide()
61-
}
62-
}
63-
})
6441
if (toolWindow is ToolWindowEx) {
6542
toolWindow.setTabActions(NewWindowAction())
6643
}

src/main/kotlin/net/allape/action/MakeAConnectAction.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.allape.action
22

33
import com.intellij.icons.AllIcons
4+
import com.intellij.openapi.actionSystem.ActionUpdateThread
45
import com.intellij.openapi.actionSystem.AnActionEvent
56
import com.intellij.openapi.actionSystem.CommonDataKeys
67
import com.intellij.openapi.project.DumbAwareAction
@@ -10,6 +11,10 @@ import net.allape.xftp.XFTP
1011
class MakeAConnectAction: DumbAwareAction(
1112
AllIcons.Webreferences.Server,
1213
) {
14+
override fun getActionUpdateThread(): ActionUpdateThread {
15+
return ActionUpdateThread.EDT
16+
}
17+
1318
override fun update(e: AnActionEvent) {
1419
val window = XFTPManager.getCurrentSelectedWindow()
1520
if (window != null && window.isConnected()) {

src/main/kotlin/net/allape/action/MutableAction.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.allape.action
22

3+
import com.intellij.openapi.actionSystem.ActionUpdateThread
34
import com.intellij.openapi.actionSystem.AnAction
45
import com.intellij.openapi.actionSystem.AnActionEvent
56
import com.intellij.openapi.actionSystem.ShortcutSet
@@ -40,6 +41,9 @@ class SimpleMutableAction(
4041
from?.templatePresentation?.icon,
4142
from?.shortcutSet,
4243
) {
44+
override fun getActionUpdateThread(): ActionUpdateThread {
45+
return ActionUpdateThread.EDT
46+
}
4347

4448
override fun update(e: AnActionEvent) {
4549
super.update(e)

src/main/kotlin/net/allape/common/XFTPManager.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@ package net.allape.common
22

33
import com.intellij.notification.NotificationGroupManager
44
import com.intellij.notification.Notifications
5-
import com.intellij.openapi.project.ProjectManager
5+
import com.intellij.openapi.project.Project
66
import com.intellij.openapi.ui.MessageType
77
import com.intellij.openapi.ui.popup.Balloon
8+
import com.intellij.openapi.wm.IdeFocusManager
89
import com.intellij.openapi.wm.ToolWindow
10+
import com.intellij.openapi.wm.ToolWindowManager
911
import com.intellij.ui.GotItTooltip
10-
import com.intellij.ui.content.Content
1112
import net.allape.xftp.XFTP
13+
import net.allape.xftp.XFTPPanel
1214
import java.awt.Point
1315
import javax.swing.JComponent
1416

1517

1618
class XFTPManager {
1719

1820
companion object {
19-
20-
lateinit var toolWindow: ToolWindow
21+
const val TOOL_WINDOW_ID = "XFTP"
2122

2223
// 默认的窗口名称
2324
const val DEFAULT_NAME = "Explorer"
2425

25-
// 当前打开的窗口
26-
val windows: HashMap<Content, XFTP> = HashMap(10)
27-
2826
/**
2927
* message中用到的group
3028
*/
@@ -49,7 +47,7 @@ class XFTPManager {
4947
fun gotIt(component: JComponent, message: String) {
5048
// JBPopupFactory.getInstance().createComponentPopupBuilder(component, component).setTitle("啊哈").setAdText(message)
5149
// GotItMessage.createMessage("A", message).setShowCallout(false).show(RelativePoint(component, Point(0,0)), Balloon.Position.above)
52-
val tooltips = GotItTooltip(GOT_IT_ID, message, ProjectManager.getInstance().defaultProject)
50+
val tooltips = GotItTooltip(GOT_IT_ID, message, getCurrentSelectedWindow())
5351
tooltips.showCondition = { true }
5452
tooltips
5553
.withTimeout(3000)
@@ -59,15 +57,22 @@ class XFTPManager {
5957
}
6058
}
6159

62-
/**
63-
* 获取当前选中的窗口, 可能为null
64-
*/
6560
fun getCurrentSelectedWindow(): XFTP? {
66-
return toolWindow.contentManager.selectedContent?.let { content ->
67-
windows[content]
61+
return getCurrentProjectToolWindow()?.let { toolWindow ->
62+
val selectedComponent = toolWindow.contentManager.selectedContent?.component as XFTPPanel?
63+
return selectedComponent?.xftp
6864
}
6965
}
7066

67+
fun getCurrentProject(): Project? {
68+
return IdeFocusManager.getGlobalInstance().lastFocusedFrame?.project
69+
}
70+
71+
fun getCurrentProjectToolWindow(): ToolWindow? {
72+
return getCurrentProject()?.let { project ->
73+
return ToolWindowManager.getInstance(project).getToolWindow(TOOL_WINDOW_ID)
74+
}
75+
}
7176
}
7277

7378
}

src/main/kotlin/net/allape/xftp/ExplorerWindowTabCloseListener.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.intellij.openapi.Disposable
55
import com.intellij.openapi.project.Project
66
import com.intellij.openapi.ui.MessageDialogBuilder
77
import com.intellij.ui.content.Content
8-
import net.allape.common.XFTPManager
98

109
class ExplorerWindowTabCloseListener(
1110
private val project: Project,
@@ -15,16 +14,16 @@ class ExplorerWindowTabCloseListener(
1514

1615
override fun disposeContent(content: Content) {
1716
try {
18-
XFTPManager.windows[content]?.dispose()
17+
(content.component as XFTPPanel).xftp.dispose()
1918
} catch (e: Exception) {
2019
e.printStackTrace()
2120
}
2221
}
2322

2423
override fun closeQuery(content: Content, projectClosing: Boolean): Boolean {
2524
if (projectClosing) return true
26-
val window = XFTPManager.windows[content]
27-
return if (window?.sftpClient != null) {
25+
val window = (content.component as XFTPPanel).xftp
26+
return if (window.sftpClient != null) {
2827
MessageDialogBuilder.yesNo("A server is connected", "Do you really want to close this tab?")
2928
.asWarning()
3029
.yesText("Close")

src/main/kotlin/net/allape/xftp/XFTP.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.intellij.openapi.vfs.VirtualFileManager
1212
import com.intellij.openapi.vfs.newvfs.BulkFileListener
1313
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
1414
import com.intellij.openapi.wm.ToolWindow
15+
import com.intellij.openapi.wm.ToolWindowManager
1516
import com.intellij.remote.RemoteCredentials
1617
import com.intellij.ssh.RemoteFileObject
1718
import com.intellij.ssh.connectionBuilder
@@ -58,7 +59,7 @@ class XFTP(
5859
val XFTP_KEY: Key<XFTP> = Key.create(XFTP::javaClass.name)
5960

6061
fun createWindowWithAnActionEvent(project: Project, showToolWindow: Boolean = true, consumer: Consumer<XFTP>? = null) {
61-
XFTPManager.toolWindow.let { toolWindow ->
62+
ToolWindowManager.getInstance(project).getToolWindow(XFTPManager.TOOL_WINDOW_ID)?.let { toolWindow ->
6263
if (showToolWindow) toolWindow.show()
6364
App.createTheToolWindowContent(project, toolWindow).let { window -> consumer?.accept(window) }
6465
}
@@ -90,6 +91,8 @@ class XFTP(
9091
}
9192
})
9293

94+
95+
panelWrapper = XFTPPanel(this)
9396
buildUI()
9497
}
9598

src/main/kotlin/net/allape/xftp/XFTPCore.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ abstract class XFTPCore(
5555
companion object {
5656
// 服务器文件系统分隔符
5757
const val FILE_SEP = "/"
58-
init {
59-
if (FILE_SEP.length != 1) {
60-
throw IllegalArgumentException("FILE_SEP Only accepts one character")
61-
}
62-
}
6358

6459
// 分隔符重复替换正则
6560
val duplicateRegex = Regex("$FILE_SEP$FILE_SEP+")
@@ -72,7 +67,7 @@ abstract class XFTPCore(
7267
const val COLLECTION_SIZE = 100
7368

7469
// 当前用户本地home目录
75-
val USER_HOME: String = System.getProperty("user.home")
70+
// val USER_HOME: String = System.getProperty("user.home")
7671

7772
// 最大可打开文件
7873
const val EDITABLE_FILE_SIZE = (2 * 1024 * 1024).toLong()
@@ -525,7 +520,7 @@ abstract class XFTPCore(
525520
}
526521
try {
527522
val formattedFileName = remoteFile.name()
528-
// 所有点"."开头的文件名可能导致本地fs错误
523+
// 所有点 "." 开头的文件名可能导致本地fs错误
529524
// if (remoteFile.name().startsWith(".")) {
530525
// formattedFileName = remoteFile.name().replaceAll("\\.", "-");
531526
// } else {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.allape.xftp
2+
3+
import java.awt.BorderLayout
4+
import javax.swing.JPanel
5+
6+
class XFTPPanel(val xftp: XFTP): JPanel(BorderLayout())

src/main/kotlin/net/allape/xftp/XFTPWidget.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ abstract class XFTPWidget(
107107

108108
// region UI组件
109109

110-
private var panelWrapper = JPanel(BorderLayout())
110+
protected lateinit var panelWrapper: XFTPPanel
111111
private val splitter: JBSplitter = OnePixelSplitter("xftp-main-window", .5f)
112112

113113
private var localWrapper = JPanel(GridBagLayout())
@@ -151,7 +151,9 @@ abstract class XFTPWidget(
151151
val explore: MutableAction = SimpleMutableAction(globalExplorer) {
152152
if (!isConnected()) {
153153
connect {
154-
if (!XFTPManager.toolWindow.isVisible) XFTPManager.toolWindow.show()
154+
XFTPManager.getCurrentProjectToolWindow()?.let { toolWindow ->
155+
if (!toolWindow.isVisible) toolWindow.show()
156+
}
155157
}
156158
}
157159
}

src/main/kotlin/net/allape/xftp/component/FileNameTextFieldDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class FileNameValidator(
6060
private var errorText: String? = null
6161

6262
override fun checkInput(inputString: String?): Boolean {
63-
if (inputString == null || inputString.isEmpty()) {
63+
if (inputString.isNullOrEmpty()) {
6464
errorText = "$objectName name can NOT be empty"
6565
return false
6666
}

0 commit comments

Comments
 (0)