Skip to content

Commit 6c723d0

Browse files
authored
Add copyTextMenuItem to PopupMenu (#83)
1 parent 1f1c77a commit 6c723d0

5 files changed

Lines changed: 84 additions & 46 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package no.spk.fiskeoye.plugin.component.menu
2+
3+
import com.intellij.openapi.ui.JBMenuItem
4+
import com.intellij.ui.table.JBTable
5+
import no.spk.fiskeoye.plugin.component.LabelIcon
6+
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.Copy
7+
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.CopyLink
8+
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.CopyLinkForJira
9+
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.CopyLinkForMarkdown
10+
import no.spk.fiskeoye.plugin.util.copy
11+
import no.spk.fiskeoye.plugin.util.htmlToText
12+
import javax.swing.Icon
13+
14+
internal abstract class FiskeoyeMenuItem(open val table: JBTable, title: String, icon: Icon) : JBMenuItem(title, icon) {
15+
private fun selectedRow(): Int = table.selectedRow
16+
private fun selectedHtml(): String = (table.model.getValueAt(selectedRow(), 0) as LabelIcon).text
17+
protected fun selectedUrl(): String = table.model.getValueAt(selectedRow(), 1).toString()
18+
protected fun selectedText(): String = table.model.getValueAt(selectedRow(), 2).toString()
19+
protected fun selectedFullText(): String = htmlToText(selectedHtml()).trim()
20+
}
21+
22+
internal class CopyTextMenuItem(override val table: JBTable) : FiskeoyeMenuItem(table, "Copy", Copy) {
23+
init {
24+
this.addActionListener {
25+
copy(selectedFullText())
26+
}
27+
}
28+
}
29+
30+
internal class CopyLinkMenuItem(override val table: JBTable) : FiskeoyeMenuItem(table, "Copy Link", CopyLink) {
31+
init {
32+
this.addActionListener {
33+
copy(selectedUrl())
34+
}
35+
}
36+
}
37+
38+
internal class CopyLinkForMarkdownMenuItem(override val table: JBTable) : FiskeoyeMenuItem(table, "Copy Link for Markdown", CopyLinkForMarkdown) {
39+
init {
40+
this.addActionListener {
41+
copy("[${selectedText()}](${selectedUrl()})")
42+
}
43+
}
44+
}
45+
46+
internal class CopyLinkForJiraMenuItem(override val table: JBTable) : FiskeoyeMenuItem(table, "Copy Link for Jira", CopyLinkForJira) {
47+
init {
48+
this.addActionListener {
49+
copy("[${selectedText()}|${selectedUrl()}]")
50+
}
51+
}
52+
}

src/main/kotlin/no/spk/fiskeoye/plugin/icons/FiskeoyeIcons.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ internal object FiskeoyeIcons {
3939
@JvmField
4040
val ScrollUp = IconLoader.getIcon("/icons/scrollUp.svg", javaClass)
4141

42+
@JvmField
43+
val Copy = IconLoader.getIcon("/icons/copy.svg", javaClass)
44+
4245
@JvmField
4346
val CopyLink = IconLoader.getIcon("/icons/link.svg", javaClass)
4447

src/main/kotlin/no/spk/fiskeoye/plugin/ui/FiskeoyePanel.kt

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.intellij.openapi.actionSystem.ActionGroup
55
import com.intellij.openapi.actionSystem.ActionManager
66
import com.intellij.openapi.actionSystem.ActionToolbar
77
import com.intellij.openapi.actionSystem.DefaultActionGroup
8+
import com.intellij.openapi.diagnostic.Logger
89
import com.intellij.openapi.project.DumbAware
910
import com.intellij.openapi.ui.JBMenuItem
1011
import com.intellij.openapi.ui.JBPopupMenu
@@ -22,16 +23,16 @@ import no.spk.fiskeoye.plugin.actions.window.ScrollToTopAction
2223
import no.spk.fiskeoye.plugin.actions.window.SettingAction
2324
import no.spk.fiskeoye.plugin.component.LabelIcon
2425
import no.spk.fiskeoye.plugin.component.LabelIconRenderer
26+
import no.spk.fiskeoye.plugin.component.menu.CopyLinkForJiraMenuItem
27+
import no.spk.fiskeoye.plugin.component.menu.CopyLinkForMarkdownMenuItem
28+
import no.spk.fiskeoye.plugin.component.menu.CopyLinkMenuItem
29+
import no.spk.fiskeoye.plugin.component.menu.CopyTextMenuItem
2530
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons
26-
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.CopyLink
27-
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.CopyLinkForJira
28-
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.CopyLinkForMarkdown
2931
import no.spk.fiskeoye.plugin.listeners.button.FiskeoyeActionListener
3032
import no.spk.fiskeoye.plugin.listeners.table.TableCellKeyListener
3133
import no.spk.fiskeoye.plugin.listeners.table.TableCellMouseListener
3234
import no.spk.fiskeoye.plugin.listeners.toggle.ToggleKeyListener
3335
import no.spk.fiskeoye.plugin.settings.FiskeoyeState
34-
import no.spk.fiskeoye.plugin.util.copy
3536
import java.awt.Dimension
3637
import java.awt.Font
3738
import java.awt.event.KeyListener
@@ -45,6 +46,8 @@ import javax.swing.event.PopupMenuEvent
4546

4647
internal abstract class FiskeoyePanel : SimpleToolWindowPanel(true, true), DumbAware {
4748

49+
private val logger: Logger = Logger.getInstance(FiskeoyePanel::class.java)
50+
4851
protected fun buildToolbar(place: String, actionGroup: ActionGroup, horizontal: Boolean = false): ActionToolbar {
4952
return ActionManager.getInstance().createActionToolbar(place, actionGroup, horizontal).apply {
5053
setShowSeparatorTitles(true)
@@ -108,58 +111,22 @@ internal abstract class FiskeoyePanel : SimpleToolWindowPanel(true, true), DumbA
108111
val isNotNull = table.model.getValueAt(table.selectedRow, 1) != null
109112
isNotNull && hasData && isNotEmpty
110113
} catch (e: Exception) {
114+
logger.error(e)
111115
false
112116
}
113117
if (!isValid) {
114-
val nothingHere = JBMenuItem("Nothing here")
115-
add(nothingHere)
118+
add(JBMenuItem("Nothing here"))
116119
return
117120
}
118-
val copyLink = buildCopyLink(table)
119-
add(copyLink)
120-
val copyLinkForMarkdown = buildCopyLinkForMarkdown(table)
121-
add(copyLinkForMarkdown)
122-
val copyLinkForJira = buildCopyLinkForJira(table)
123-
add(copyLinkForJira)
121+
add(CopyTextMenuItem(table))
122+
add(CopyLinkMenuItem(table))
123+
add(CopyLinkForMarkdownMenuItem(table))
124+
add(CopyLinkForJiraMenuItem(table))
124125
}
125126
})
126127
}
127128
}
128129

129-
private fun buildCopyLink(table: JBTable): JBMenuItem {
130-
return JBMenuItem("Copy Link", CopyLink).apply {
131-
this.addActionListener {
132-
val selectedRow = table.selectedRow
133-
val url = table.model.getValueAt(selectedRow, 1).toString()
134-
copy(url)
135-
}
136-
}
137-
}
138-
139-
private fun buildCopyLinkForMarkdown(table: JBTable): JBMenuItem {
140-
return JBMenuItem("Copy Link for Markdown", CopyLinkForMarkdown).apply {
141-
this.addActionListener {
142-
val selectedRow = table.selectedRow
143-
val url = table.model.getValueAt(selectedRow, 1).toString()
144-
val text = table.model.getValueAt(selectedRow, 2).toString()
145-
val markdown = "[$text]($url)"
146-
copy(markdown)
147-
}
148-
}
149-
}
150-
151-
private fun buildCopyLinkForJira(table: JBTable): JBMenuItem {
152-
return JBMenuItem("Copy Link for Jira", CopyLinkForJira).apply {
153-
this.addActionListener {
154-
val selectedRow = table.selectedRow
155-
val url = table.model.getValueAt(selectedRow, 1).toString()
156-
val text = table.model.getValueAt(selectedRow, 2).toString()
157-
val markdown = "[$text|$url]"
158-
copy(markdown)
159-
}
160-
}
161-
}
162-
163130
protected fun buildTextField(colums: Int, keyListener: KeyListener? = null): JBTextField {
164131
return JBTextField().apply {
165132
columns = colums

src/main/resources/icons/copy.svg

Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 8 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)