Skip to content

Commit 455afdf

Browse files
committed
refactor: prefer always using IDEA's file chooser
While the native chooser respects the host OS, it is also more difficult to use.
1 parent ea47f32 commit 455afdf

File tree

5 files changed

+42
-37
lines changed

5 files changed

+42
-37
lines changed

src/main/java/com/github/lppedd/cc/configuration/CCMainConfigurableGui.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private void finishUpComponents(
216216
final var gc = new GridConstraints();
217217
gc.setFill(FILL_BOTH);
218218
gc.setHSizePolicy(SIZEPOLICY_CAN_SHRINK | SIZEPOLICY_CAN_GROW | SIZEPOLICY_WANT_GROW);
219-
defaultsPanel.add(JBUI.Borders.empty(0, 1, 16, 0).wrap(new DefaultTokensFileExportPanel()), gc);
219+
defaultsPanel.add(JBUI.Borders.empty(0, 1, 16, 0).wrap(new DefaultTokensFileExportPanel(project)), gc);
220220

221221
gc.setRow(1);
222222
defaultTokensFilePickerPanel = new DefaultTokensFilePickerPanel(project, disposable);
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.github.lppedd.cc.configuration.component
22

3+
import com.github.lppedd.cc.findRootDir
34
import com.github.lppedd.cc.setName
45
import com.intellij.openapi.fileChooser.PathChooserDialog
56
import com.intellij.openapi.fileChooser.impl.FileChooserFactoryImpl
67
import com.intellij.openapi.project.Project
78
import com.intellij.openapi.ui.TextBrowseFolderListener
9+
import com.intellij.openapi.vfs.VirtualFile
810
import com.intellij.openapi.wm.WindowManager
911

1012
/**
@@ -14,37 +16,37 @@ import com.intellij.openapi.wm.WindowManager
1416
*/
1517
@Suppress("UnstableApiUsage")
1618
internal class CCTextBrowseFolderListener(
17-
private val fileChooserDescriptor: CCFileChooserDescriptor,
18-
project: Project? = null,
19-
) : TextBrowseFolderListener(fileChooserDescriptor, project) {
19+
descriptor: CCFileChooserDescriptor,
20+
project: Project?,
21+
) : TextBrowseFolderListener(descriptor, project) {
2022
override fun run() {
2123
val dialog = getPathChooserDialog()
22-
dialog.choose(initialFile) { chosenFiles ->
23-
onFileChosen(chosenFiles[0])
24-
}
24+
dialog.choose(initialFile) { onFileChosen(it[0]) }
2525
}
2626

27+
override fun getInitialFile(): VirtualFile? =
28+
project?.findRootDir() ?: super.getInitialFile()
29+
2730
private fun getPathChooserDialog(): PathChooserDialog {
31+
val descriptor = myFileChooserDescriptor as CCFileChooserDescriptor
2832
val parentComponent = myTextComponent ?: WindowManager.getInstance().suggestParentWindow(project)
29-
val nativePathChooser = FileChooserFactoryImpl.createNativePathChooserIfEnabled(
30-
myFileChooserDescriptor,
31-
project,
32-
parentComponent,
33-
)
34-
35-
if (nativePathChooser != null) {
36-
return nativePathChooser
33+
34+
if (!descriptor.isForcedToUseIdeaFileChooser) {
35+
val nativeChooser = FileChooserFactoryImpl.createNativePathChooserIfEnabled(descriptor, project, parentComponent)
36+
37+
if (nativeChooser != null) {
38+
return nativeChooser
39+
}
3740
}
3841

39-
val chooser = if (parentComponent != null) {
40-
CCFileChooserDialogImpl(myFileChooserDescriptor, parentComponent, project)
42+
val ideaChooser = if (parentComponent != null) {
43+
CCFileChooserDialogImpl(descriptor, parentComponent, project)
4144
} else {
42-
CCFileChooserDialogImpl(myFileChooserDescriptor, project)
45+
CCFileChooserDialogImpl(descriptor, project)
4346
}
4447

45-
fileChooserDescriptor.okActionName?.let(chooser.okAction::setName)
46-
fileChooserDescriptor.cancelActionName?.let(chooser.cancelAction::setName)
47-
48-
return chooser
48+
descriptor.okActionName?.let(ideaChooser.okAction::setName)
49+
descriptor.cancelActionName?.let(ideaChooser.cancelAction::setName)
50+
return ideaChooser
4951
}
5052
}

src/main/kotlin/com/github/lppedd/cc/configuration/component/CoAuthorsFilePickerPanel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ internal class CoAuthorsFilePickerPanel(
164164
withFileFilter(validFileTest)
165165
withTitle(CCBundle["cc.config.coAuthors.customFile.dialog.title"])
166166
withDescription(CCBundle["cc.config.coAuthors.customFile.dialog.description"])
167+
isForcedToUseIdeaFileChooser = true
167168
}
168169
}
169170
}

src/main/kotlin/com/github/lppedd/cc/configuration/component/DefaultTokensFileExportPanel.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package com.github.lppedd.cc.configuration.component
22

3-
import com.github.lppedd.cc.CC
4-
import com.github.lppedd.cc.CCBundle
5-
import com.github.lppedd.cc.getResourceAsStream
6-
import com.github.lppedd.cc.scaled
3+
import com.github.lppedd.cc.*
74
import com.intellij.ide.plugins.PluginManagerCore
85
import com.intellij.openapi.application.WriteAction
96
import com.intellij.openapi.extensions.PluginId
107
import com.intellij.openapi.fileChooser.FileChooserFactory
118
import com.intellij.openapi.fileChooser.FileSaverDescriptor
9+
import com.intellij.openapi.project.Project
1210
import com.intellij.openapi.util.text.StringUtil
13-
import com.intellij.openapi.vfs.VirtualFile
1411
import com.intellij.openapi.vfs.VirtualFileWrapper
1512
import com.intellij.ui.JBColor
1613
import com.intellij.ui.components.JBLabel
@@ -26,8 +23,8 @@ import javax.swing.JPanel
2623
/**
2724
* @author Edoardo Luppi
2825
*/
29-
internal class DefaultTokensFileExportPanel
30-
: JPanel(GridLayoutManager(1, 2, JBUI.emptyInsets(), 20.scaled, 0)),
26+
internal class DefaultTokensFileExportPanel(private val project: Project) :
27+
JPanel(GridLayoutManager(1, 2, JBUI.emptyInsets(), 20.scaled, 0)),
3128
LinkListener<Any?> {
3229
private val exportAction = ActionLinkLabel(CCBundle["cc.config.defaults.exportToPath"], this)
3330
private val exportInfo = JBLabel()
@@ -44,26 +41,30 @@ internal class DefaultTokensFileExportPanel
4441
}
4542

4643
override fun linkSelected(aSource: LinkLabel<Any?>, aLinkData: Any?) {
47-
val virtualFileWrapper = FileChooserFactory.getInstance()
48-
.createSaveFileDialog(FileSaverDescriptor(CCBundle["cc.config.exportDialog.title"], ""), null)
49-
.save(null as VirtualFile?, CC.File.Defaults)
44+
val descriptor = FileSaverDescriptor(CCBundle["cc.config.exportDialog.title"], "").also {
45+
it.isForcedToUseIdeaFileChooser = true
46+
}
47+
48+
val fileWrapper = FileChooserFactory.getInstance()
49+
.createSaveFileDialog(descriptor, project)
50+
.save(project.findRootDir(), CC.File.Defaults)
5051

5152
try {
52-
writeFile(virtualFileWrapper)
53+
writeFile(fileWrapper)
5354
} catch (e: Exception) {
5455
exportInfo.foreground = JBColor.RED
5556
exportInfo.text = "${CCBundle["cc.config.defaults.exportToPath.error"]} - ${e.message}"
5657
}
5758
}
5859

59-
private fun writeFile(virtualFileWrapper: VirtualFileWrapper?) {
60-
val file = virtualFileWrapper?.file ?: return
60+
private fun writeFile(fileWrapper: VirtualFileWrapper?) {
61+
val file = fileWrapper?.file ?: return
6162

6263
if (!file.exists()) {
6364
file.createNewFile()
6465
}
6566

66-
val virtualFile = virtualFileWrapper.virtualFile
67+
val virtualFile = fileWrapper.virtualFile
6768

6869
if (virtualFile == null || !virtualFile.isWritable) {
6970
exportInfo.foreground = JBColor.RED

src/main/kotlin/com/github/lppedd/cc/configuration/component/DefaultTokensFilePickerPanel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal class DefaultTokensFilePickerPanel(
4242

4343
private val customFile = TextFieldWithBrowseButton().also {
4444
it.isEnabled = false
45-
it.addBrowseFolderListener(CCTextBrowseFolderListener(MyFileChooserDescriptor))
45+
it.addBrowseFolderListener(CCTextBrowseFolderListener(MyFileChooserDescriptor, null))
4646
}
4747

4848
var isComponentValid = true
@@ -174,6 +174,7 @@ internal class DefaultTokensFilePickerPanel(
174174
withFileFilter(validFileTest)
175175
withTitle(CCBundle["cc.config.fileDialog.title"])
176176
withDescription(CCBundle["cc.config.fileDialog.description"])
177+
isForcedToUseIdeaFileChooser = true
177178
}
178179
}
179180
}

0 commit comments

Comments
 (0)