-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Ghidra configuration strategy and facet UI
- Loading branch information
1 parent
d457bd7
commit a4378e5
Showing
12 changed files
with
255 additions
and
154 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/codingmates/ghidra/intellij/ide/GhidraBundle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.codingmates.ghidra.intellij.ide | ||
|
||
import com.intellij.DynamicBundle | ||
import org.jetbrains.annotations.Nls | ||
import org.jetbrains.annotations.PropertyKey | ||
import java.util.function.Supplier | ||
|
||
object GhidraBundle : DynamicBundle("messages.GhidraBundle") { | ||
fun message(key: @PropertyKey(resourceBundle = "messages.GhidraBundle") String, vararg params: Any): @Nls String { | ||
return getMessage(key, *params) | ||
} | ||
|
||
fun messagePointer( | ||
key: @PropertyKey(resourceBundle = "messages.GhidraBundle") String, | ||
vararg params: Any | ||
): Supplier<String> { | ||
return getLazyMessage(key, *params) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 81 additions & 47 deletions
128
src/main/kotlin/com/codingmates/ghidra/intellij/ide/facet/GhidraFacetConfigurationEditor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,104 @@ | ||
package com.codingmates.ghidra.intellij.ide.facet | ||
|
||
import com.intellij.facet.ui.FacetEditorContext | ||
import com.intellij.facet.ui.FacetEditorTab | ||
import com.intellij.facet.ui.FacetValidatorsManager | ||
import com.intellij.openapi.fileChooser.FileChooser | ||
import com.codingmates.ghidra.intellij.ide.GhidraBundle | ||
import com.codingmates.ghidra.intellij.ide.facet.model.isGhidraInstallationPath | ||
import com.codingmates.ghidra.intellij.ide.facet.model.isGhidraSourcesPath | ||
import com.intellij.facet.ui.* | ||
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory | ||
import com.intellij.openapi.observable.properties.PropertyGraph | ||
import com.intellij.openapi.observable.util.toUiPathProperty | ||
import com.intellij.openapi.options.ConfigurationException | ||
import com.intellij.openapi.util.text.StringUtil | ||
import com.intellij.openapi.ui.BrowseFolderDescriptor.Companion.withPathToTextConvertor | ||
import com.intellij.openapi.ui.BrowseFolderDescriptor.Companion.withTextToPathConvertor | ||
import com.intellij.openapi.ui.getCanonicalPath | ||
import com.intellij.openapi.ui.getPresentablePath | ||
import com.intellij.openapi.ui.setEmptyState | ||
import com.intellij.ui.dsl.builder.AlignX | ||
import com.intellij.ui.dsl.builder.bindText | ||
import com.intellij.ui.dsl.builder.panel | ||
import org.jetbrains.annotations.Nls | ||
import java.awt.BorderLayout | ||
import javax.swing.* | ||
|
||
|
||
class GhidraFacetConfigurationEditor( | ||
private val state: GhidraFacetState, | ||
private val state: GhidraFacetSettings, | ||
private val context: FacetEditorContext, | ||
private val _validator: FacetValidatorsManager | ||
private val validator: FacetValidatorsManager | ||
) : FacetEditorTab() { | ||
|
||
private val installationPathEditor = JTextField(state.installationPath) | ||
|
||
override fun createComponent(): JComponent { | ||
val top = JPanel(BorderLayout()) | ||
top.add(JLabel("Path to Ghidra installation: "), BorderLayout.WEST) | ||
top.add(installationPathEditor) | ||
val chooserButton = JButton("...") | ||
top.add(chooserButton, BorderLayout.EAST) | ||
val facetPanel = JPanel(BorderLayout()) | ||
facetPanel.add(top, BorderLayout.NORTH) | ||
chooserButton.addActionListener { | ||
FileChooser.chooseFile( | ||
FileChooserDescriptorFactory.createSingleFolderDescriptor(), | ||
context.project, null | ||
) { virtualFile -> | ||
virtualFile.canonicalPath?.let { path -> | ||
installationPathEditor.text = path | ||
} | ||
private val propertyGraph = PropertyGraph() | ||
private val installationDir = propertyGraph.property(state.installationPath) | ||
private val settingsDir = propertyGraph.property(state.settingsPath?.toString() ?: "") | ||
private val version = propertyGraph.property(state.version ?: "") | ||
private val applied = propertyGraph.property(false) | ||
|
||
init { | ||
validator.registerValidator(GhidraInstallationValidator()) | ||
propertyGraph.afterPropagation { validator.validate() } | ||
} | ||
|
||
override fun createComponent() = panel { | ||
group("Ghidra Settings") { | ||
row(GhidraBundle.message("ghidra.facet.editor.installation")) { | ||
val title = GhidraBundle.message("ghidra.facet.editor.installation.dialog.title") | ||
val fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor() | ||
.withPathToTextConvertor(::getPresentablePath).withTextToPathConvertor(::getCanonicalPath) | ||
|
||
textFieldWithBrowseButton(title, context.project, fileChooserDescriptor) | ||
.bindText(installationDir.toUiPathProperty()) | ||
.applyToComponent { setEmptyState(GhidraBundle.message("ghidra.facet.editor.installation.empty")) } | ||
.align(AlignX.FILL) | ||
} | ||
} | ||
return facetPanel | ||
} | ||
|
||
/** | ||
* @return the name of this facet for display in this editor tab. | ||
*/ | ||
@Nls(capitalization = Nls.Capitalization.Title) | ||
override fun getDisplayName(): String { | ||
return GhidraFacetType.FACET_NAME | ||
group("Ghidra Installation Details") { | ||
row("Version") { | ||
textField() | ||
.bindText(version) | ||
.enabled(false) | ||
.align(AlignX.FILL) | ||
} | ||
|
||
row("Settings") { | ||
textField() | ||
.bindText(settingsDir) | ||
.enabled(false) | ||
.align(AlignX.FILL) | ||
|
||
} | ||
}.visibleIf(applied) | ||
} | ||
|
||
inner class GhidraInstallationValidator : FacetEditorValidator(), SlowFacetEditorValidator { | ||
override fun check(): ValidationResult { | ||
val ghidraInstallation = installationDir.get() | ||
|
||
override fun isModified(): Boolean { | ||
return !StringUtil.equals(state.installationPath, installationPathEditor.text.trim()) | ||
if (!isGhidraInstallationPath(ghidraInstallation)) { | ||
return ValidationResult(GhidraBundle.message("ghidra.facet.editor.installation.error.no-properties")) | ||
} | ||
|
||
if (isGhidraSourcesPath(ghidraInstallation)) { | ||
return ValidationResult(GhidraBundle.message("ghidra.facet.editor.installation.error.sources")) | ||
} | ||
|
||
return ValidationResult.OK | ||
} | ||
} | ||
|
||
@Nls(capitalization = Nls.Capitalization.Title) | ||
override fun getDisplayName() = GhidraFacetType.FACET_NAME | ||
|
||
override fun isModified() = state.installationPath != installationDir.get() | ||
|
||
@Throws(ConfigurationException::class) | ||
override fun apply() { | ||
// Not much to go wrong here, but fulfill the contract | ||
try { | ||
val newTextContent: String = installationPathEditor.text | ||
state.installationPath = newTextContent | ||
} catch (e: Exception) { | ||
throw ConfigurationException(e.toString()) | ||
} | ||
} | ||
state.installationPath = installationDir.get() | ||
state.resolve() | ||
|
||
override fun reset() { | ||
installationPathEditor.text = state.installationPath | ||
settingsDir.set(state.settingsPath.toString()) | ||
version.set(state.version!!) | ||
applied.set(true) | ||
} catch (e: ConfigurationException) { | ||
throw ConfigurationException(e.localizedMessage) | ||
} | ||
} | ||
} |
Oops, something went wrong.