Skip to content

Commit

Permalink
feat(marketplace): refactor table component and add install action #86
Browse files Browse the repository at this point in the history
Refactored the MarketplaceToolWindowFactory and ShirePackageModel classes to improve the table component in the marketplace. The table now uses a ListTableModel and TableView for better data handling. Added an install action button to each row in the table, allowing users to install packages directly from the table. Updated related resource messages to support these changes.
  • Loading branch information
phodal committed Sep 13, 2024
1 parent 7088492 commit fc18040
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.phodal.shire.marketplace

import com.intellij.ide.IdeBundle.message
import com.intellij.openapi.Disposable
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.StoragePathMacros
import com.intellij.openapi.options.UnnamedConfigurable
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.FileColorManager
import com.intellij.ui.LayeredIcon
import com.intellij.ui.ToolbarDecorator.createDecorator
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.hover.TableHoverListener
import com.intellij.ui.table.JBTable
import com.intellij.util.ui.EditableModel
import com.intellij.ui.table.TableView
import com.intellij.util.ui.ColumnInfo
import com.intellij.util.ui.ListTableModel
import com.phodal.shire.ShireMainBundle
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.table.AbstractTableModel
import java.awt.BorderLayout
import java.awt.Component
import javax.swing.*
import javax.swing.table.TableCellEditor
import javax.swing.table.TableCellRenderer

class MarketplaceToolWindowFactory : ToolWindowFactory, DumbAware {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
Expand All @@ -32,20 +31,15 @@ class MarketplaceToolWindowFactory : ToolWindowFactory, DumbAware {
}

@State(name = "MarketPlaceView", storages = [Storage(StoragePathMacros.PRODUCT_WORKSPACE_FILE)])
class MarketPlaceView(project: Project) : Disposable {
private val myProject = project
class MarketPlaceView(val project: Project) : Disposable {
private var myToolWindowPanel: JPanel? = null
private val packageModel = ShirePackageModel()

private val shirePackageTableComponent = ShirePackageTableComponent()

init {
myToolWindowPanel = panel {
row {
cell(packageModel.createComponent())
.align(Align.FILL)
.comment(ShireMainBundle.message("marketplace.table.comment"))
.onIsModified { packageModel.isModified }
.onReset { packageModel.reset() }
.onApply { packageModel.apply() }
cell(shirePackageTableComponent.mainPanel).align(Align.FILL)
}.resizableRow()
}
}
Expand All @@ -61,46 +55,103 @@ class MarketPlaceView(project: Project) : Disposable {
}
}

private class Column(private val key: String, val type: Class<*>, val editable: Boolean) {
val name: String
get() = ShireMainBundle.message(key)
}

private val columns = arrayOf(
Column("marketplace.column.name", String::class.java, false),
Column("marketplace.column.description", String::class.java, true),
Column("marketplace.column.version", String::class.java, true)
)
class ShirePackageTableComponent {
val mainPanel: JPanel = JPanel(BorderLayout())

private class ShirePackageModel() : AbstractTableModel(), UnnamedConfigurable {
override fun getColumnCount() = columns.size
// Create a list to store the row data
val dataList = listOf(
ShirePackage("Plugin 1", "A useful plugin", "1.0", "Author A"),
ShirePackage("Plugin 2", "Another great plugin", "2.1", "Author B"),
ShirePackage("Plugin 3", "Yet another plugin", "3.0", "Author C")
)

override fun getColumnName(column: Int) = columns[column].name
init {
val columns = arrayOf(
object : ColumnInfo<ShirePackage, String>(ShireMainBundle.message("marketplace.column.name")) {
override fun valueOf(data: ShirePackage): String = data.name
},
object : ColumnInfo<ShirePackage, String>(ShireMainBundle.message("marketplace.column.description")) {
override fun valueOf(data: ShirePackage): String = data.description
},
object : ColumnInfo<ShirePackage, String>(ShireMainBundle.message("marketplace.column.version")) {
override fun valueOf(data: ShirePackage): String = data.version
},
object : ColumnInfo<ShirePackage, String>(ShireMainBundle.message("marketplace.column.author")) {
override fun valueOf(data: ShirePackage): String = data.author
},
object : ColumnInfo<ShirePackage, JButton>(ShireMainBundle.message("marketplace.column.action")) {
override fun valueOf(data: ShirePackage): JButton {
val installButton = JButton(ShireMainBundle.message("marketplace.action.install"))
installButton.addActionListener {
onInstallClicked(data)
}
return installButton
}

override fun getRenderer(item: ShirePackage?): TableCellRenderer? = ButtonRenderer()

override fun getEditor(item: ShirePackage?): TableCellEditor? {
return ButtonEditor()
}

override fun isCellEditable(item: ShirePackage): Boolean = true

override fun getColumnClass(): Class<*> = JButton::class.java
}
)

val model = ListTableModel(columns, dataList)
val tableView = TableView(model)
val scrollPane = JBScrollPane(tableView)
mainPanel.add(scrollPane, BorderLayout.CENTER)
}

override fun getColumnClass(column: Int) = columns[column].type
// Action when the Install button is clicked
private fun onInstallClicked(data: ShirePackage) {
Messages.showMessageDialog(
ShireMainBundle.message("marketplace.action.installing", data.name),
ShireMainBundle.message("marketplace.dialog.installAction"),
Messages.getInformationIcon()
)
}
}

override fun isCellEditable(row: Int, column: Int) = columns[column].editable
data class ShirePackage(val name: String, val description: String, val version: String, val author: String)

override fun getRowCount() = 0

override fun getValueAt(row: Int, column: Int): Any? {
return null
class ButtonRenderer : JButton(), TableCellRenderer {
init {
isOpaque = true
}

override fun createComponent(): JComponent {
val table = JBTable(this)
table.setShowGrid(false)
table.emptyText.text = ShireMainBundle.message("marketplace.empty.text")
override fun getTableCellRendererComponent(
table: JTable, value: Any?, isSelected: Boolean,
hasFocus: Boolean, row: Int, column: Int
): Component {
text = (value as JButton).text
return this
}
}

class ButtonEditor : DefaultCellEditor(JCheckBox()) {
private val button = JButton()

return createDecorator(table)
.createPanel()
init {
button.addActionListener {
fireEditingStopped()
}
}

override fun isModified(): Boolean {
return false
override fun getTableCellEditorComponent(
table: JTable, value: Any?, isSelected: Boolean,
row: Int, column: Int
): Component {
button.text = (value as JButton).text
return button
}

override fun apply() {
// TODO("Not yet implemented")
override fun getCellEditorValue(): Any {
return button
}
}
}
7 changes: 6 additions & 1 deletion src/main/resources/messages/ShireMainBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ marketplace.column.name=Name
marketplace.column.description=Description
marketplace.column.version=Version
marketplace.empty.text=No Shire packages found
marketplace.table.comment=Install your packages
marketplace.table.comment=Install your packages
marketplace.action.installing=Install
marketplace.dialog.installAction=Install Action
marketplace.column.author=Author
marketplace.column.action=Action
marketplace.action.install=Install

0 comments on commit fc18040

Please sign in to comment.