From ae960bad050428369c439ed9650066ef3351654f Mon Sep 17 00:00:00 2001 From: cdcsgit Date: Thu, 23 Dec 2021 18:46:43 +0900 Subject: [PATCH] Separate "include" and "exclude" items in the filter and show them in the tooltip --- .../kotlinstudy/lognote/ColorComboBox.kt | 21 --- .../kotlinstudy/lognote/FilterComboBox.kt | 143 ++++++++++++++++++ .../blogspot/kotlinstudy/lognote/MainUI.kt | 49 +++--- 3 files changed, 175 insertions(+), 38 deletions(-) create mode 100644 src/com/blogspot/kotlinstudy/lognote/FilterComboBox.kt diff --git a/src/com/blogspot/kotlinstudy/lognote/ColorComboBox.kt b/src/com/blogspot/kotlinstudy/lognote/ColorComboBox.kt index 8e1a0a80..b00d93d1 100644 --- a/src/com/blogspot/kotlinstudy/lognote/ColorComboBox.kt +++ b/src/com/blogspot/kotlinstudy/lognote/ColorComboBox.kt @@ -18,27 +18,6 @@ class ColorComboBox : JComboBox() { textField.border = BorderFactory.createLineBorder(Color.black) } - fun isExistItem(item:String) : Boolean { - var isExist = false - for (idx in 0 until itemCount) { - if (getItemAt(idx).toString() == item) { - isExist = true - break - } - } - return isExist - } - - fun setEnabledFilter(enabled: Boolean) { - isEnabled = enabled - if (!enabled && editor.item.toString().isEmpty()) { - isVisible = false - } - else { - isVisible = true - } - } - internal class ComboBoxRenderer : BasicComboBoxRenderer() { override fun getListCellRendererComponent( list: JList<*>, value: Any, diff --git a/src/com/blogspot/kotlinstudy/lognote/FilterComboBox.kt b/src/com/blogspot/kotlinstudy/lognote/FilterComboBox.kt new file mode 100644 index 00000000..893116d1 --- /dev/null +++ b/src/com/blogspot/kotlinstudy/lognote/FilterComboBox.kt @@ -0,0 +1,143 @@ +package com.blogspot.kotlinstudy.lognote + +import java.awt.Color +import java.awt.Component +import java.awt.event.KeyAdapter +import java.awt.event.KeyEvent +import java.awt.event.MouseEvent +import javax.swing.* +import javax.swing.plaf.basic.BasicComboBoxRenderer +import javax.swing.plaf.basic.BasicComboBoxUI + +class FilterComboBox : JComboBox() { + var mTf: JTextField? = null + var mEnabledTfTooltip = true + + init { + ui = BasicComboBoxUI() + ui.installUI(this) + + mTf = editor.editorComponent as JTextField + mTf!!.border = BorderFactory.createLineBorder(Color.black) + mTf!!.toolTipText = toolTipText + mTf!!.addKeyListener(KeyHandler()) + } + + fun setEnabledFilter(enabled: Boolean) { + isEnabled = enabled + if (!enabled && editor.item.toString().isEmpty()) { + isVisible = false + } + else { + isVisible = true + } + } + + fun isExistItem(item:String) : Boolean { + var isExist = false + for (idx in 0 until itemCount) { + if (getItemAt(idx).toString() == item) { + isExist = true + break + } + } + return isExist + } + + private fun parsePattern(pattern: String) : Array { + val patterns: Array = Array(2) { "" } + + val patternSplit = pattern.split("|") + var prevPatternIdx = -1 + + for (item in patternSplit) { + if (prevPatternIdx != -1) { + patterns[prevPatternIdx] += "|" + patterns[prevPatternIdx] += item + + if (!item.substring(item.length - 1).equals("\\")) { + prevPatternIdx = -1 + } + continue + } + + if (item.isNotEmpty()) { + if (item[0] != '-') { + if (patterns[0].isNotEmpty()) { + patterns[0] += "|" + patterns[0] += item + } else { + patterns[0] = item + } + if (item.substring(item.length - 1).equals("\\")) { + prevPatternIdx = 0 + } + } else { + if (patterns[1].isNotEmpty()) { + patterns[1] += "|" + patterns[1] += item.substring(1) + } else { + patterns[1] = item.substring(1) + } + if (item.substring(item.length - 1).equals("\\")) { + prevPatternIdx = 1 + } + } + } + } + + return patterns + } + + fun updateTooltip() { + if (!mEnabledTfTooltip) { + return; + } + val patterns = parsePattern(mTf!!.text) + var includeStr = patterns[0] + var excludeStr = patterns[1] + + if (includeStr.isNotEmpty()) { + includeStr = includeStr.replace("|", "|") + } + + if (excludeStr.isNotEmpty()) { + excludeStr = excludeStr.replace("|", "|") + } + + var tooltip = "$toolTipText
" + tooltip += "INCLUDE : $includeStr
" + tooltip += "EXCLUDE : $excludeStr
" + tooltip += "" + mTf!!.toolTipText = tooltip + } + + internal inner class KeyHandler : KeyAdapter() { + override fun keyReleased(e: KeyEvent) { + updateTooltip() + ToolTipManager.sharedInstance().mouseMoved(MouseEvent(mTf, 0, 0, 0, 0, preferredSize.height / 3, 0, false)) + super.keyReleased(e) + } + } + + internal class ComboBoxRenderer : BasicComboBoxRenderer() { + override fun getListCellRendererComponent( + list: JList<*>, value: Any, + index: Int, isSelected: Boolean, cellHasFocus: Boolean + ): Component { + if (isSelected) { + background = list.selectionBackground + foreground = list.selectionForeground + if (-1 < index) { + list.toolTipText = list.selectedValue.toString() + } + } else { + background = list.background + foreground = list.foreground + } + font = list.font + text = value.toString() + return this + } + } +} \ No newline at end of file diff --git a/src/com/blogspot/kotlinstudy/lognote/MainUI.kt b/src/com/blogspot/kotlinstudy/lognote/MainUI.kt index 90557882..69898771 100644 --- a/src/com/blogspot/kotlinstudy/lognote/MainUI.kt +++ b/src/com/blogspot/kotlinstudy/lognote/MainUI.kt @@ -56,27 +56,27 @@ class MainUI(title: String) : JFrame() { private lateinit var mLogPanel: JPanel private lateinit var mShowLogPanel: JPanel private lateinit var mMatchCaseBtn: ColorToggleButton - private lateinit var mShowLogCombo: ColorComboBox + private lateinit var mShowLogCombo: FilterComboBox private lateinit var mShowLogToggle: ColorToggleButton private lateinit var mShowLogTogglePanel: JPanel private lateinit var mBoldLogPanel: JPanel - private lateinit var mBoldLogCombo: ColorComboBox + private lateinit var mBoldLogCombo: FilterComboBox private lateinit var mBoldLogToggle: ColorToggleButton private lateinit var mBoldLogTogglePanel: JPanel private lateinit var mShowTagPanel: JPanel - private lateinit var mShowTagCombo: ColorComboBox + private lateinit var mShowTagCombo: FilterComboBox private lateinit var mShowTagToggle: ColorToggleButton private lateinit var mShowTagTogglePanel: JPanel private lateinit var mShowPidPanel: JPanel - private lateinit var mShowPidCombo: ColorComboBox + private lateinit var mShowPidCombo: FilterComboBox private lateinit var mShowPidToggle: ColorToggleButton private lateinit var mShowPidTogglePanel: JPanel private lateinit var mShowTidPanel: JPanel - private lateinit var mShowTidCombo: ColorComboBox + private lateinit var mShowTidCombo: FilterComboBox private lateinit var mShowTidToggle: ColorToggleButton private lateinit var mShowTidTogglePanel: JPanel @@ -422,10 +422,10 @@ class MainUI(title: String) : JFrame() { mLogPanel = JPanel() mShowLogPanel = JPanel() - mShowLogCombo = ColorComboBox() + mShowLogCombo = FilterComboBox() mShowLogCombo.toolTipText = TooltipStrings.LOG_COMBO mShowLogCombo.isEditable = true - mShowLogCombo.renderer = ColorComboBox.ComboBoxRenderer() + mShowLogCombo.renderer = FilterComboBox.ComboBoxRenderer() mShowLogCombo.editor.editorComponent.addKeyListener(mKeyHandler) mShowLogCombo.addItemListener(mItemHandler) mShowLogCombo.addPopupMenuListener(mPopupMenuHandler) @@ -439,10 +439,11 @@ class MainUI(title: String) : JFrame() { mShowLogToggle.addItemListener(mItemHandler) mBoldLogPanel = JPanel() - mBoldLogCombo = ColorComboBox() + mBoldLogCombo = FilterComboBox() mBoldLogCombo.toolTipText = TooltipStrings.BOLD_COMBO + mBoldLogCombo.mEnabledTfTooltip = false mBoldLogCombo.isEditable = true - mBoldLogCombo.renderer = ColorComboBox.ComboBoxRenderer() + mBoldLogCombo.renderer = FilterComboBox.ComboBoxRenderer() mBoldLogCombo.editor.editorComponent.addKeyListener(mKeyHandler) mBoldLogCombo.addItemListener(mItemHandler) mBoldLogCombo.editor.editorComponent.addMouseListener(mMouseHandler) @@ -455,10 +456,10 @@ class MainUI(title: String) : JFrame() { mBoldLogToggle.addItemListener(mItemHandler) mShowTagPanel = JPanel() - mShowTagCombo = ColorComboBox() + mShowTagCombo = FilterComboBox() mShowTagCombo.toolTipText = TooltipStrings.TAG_COMBO mShowTagCombo.isEditable = true - mShowTagCombo.renderer = ColorComboBox.ComboBoxRenderer() + mShowTagCombo.renderer = FilterComboBox.ComboBoxRenderer() mShowTagCombo.editor.editorComponent.addKeyListener(mKeyHandler) mShowTagCombo.addItemListener(mItemHandler) mShowTagCombo.editor.editorComponent.addMouseListener(mMouseHandler) @@ -471,10 +472,10 @@ class MainUI(title: String) : JFrame() { mShowTagToggle.addItemListener(mItemHandler) mShowPidPanel = JPanel() - mShowPidCombo = ColorComboBox() + mShowPidCombo = FilterComboBox() mShowPidCombo.toolTipText = TooltipStrings.PID_COMBO mShowPidCombo.isEditable = true - mShowPidCombo.renderer = ColorComboBox.ComboBoxRenderer() + mShowPidCombo.renderer = FilterComboBox.ComboBoxRenderer() mShowPidCombo.editor.editorComponent.addKeyListener(mKeyHandler) mShowPidCombo.addItemListener(mItemHandler) mShowPidCombo.editor.editorComponent.addMouseListener(mMouseHandler) @@ -487,10 +488,10 @@ class MainUI(title: String) : JFrame() { mShowPidToggle.addItemListener(mItemHandler) mShowTidPanel = JPanel() - mShowTidCombo = ColorComboBox() + mShowTidCombo = FilterComboBox() mShowTidCombo.toolTipText = TooltipStrings.TID_COMBO mShowTidCombo.isEditable = true - mShowTidCombo.renderer = ColorComboBox.ComboBoxRenderer() + mShowTidCombo.renderer = FilterComboBox.ComboBoxRenderer() mShowTidCombo.editor.editorComponent.addKeyListener(mKeyHandler) mShowTidCombo.addItemListener(mItemHandler) mShowTidCombo.editor.editorComponent.addMouseListener(mMouseHandler) @@ -694,6 +695,9 @@ class MainUI(title: String) : JFrame() { mShowLogCombo.addItem(item) } } + + mShowLogCombo.updateTooltip() + if (mShowLogCombo.itemCount > 0) { mShowLogCombo.selectedIndex = 0 } @@ -716,6 +720,9 @@ class MainUI(title: String) : JFrame() { mShowTagCombo.selectedIndex = 0 } } + + mShowTagCombo.updateTooltip() + check = mConfigManager.mProperties.get(mConfigManager.ITEM_SHOW_TAG_CHECK) as? String if (!check.isNullOrEmpty()) { mShowTagToggle.isSelected = check.toBoolean() @@ -750,6 +757,9 @@ class MainUI(title: String) : JFrame() { mBoldLogCombo.selectedIndex = 0 } } + + mBoldLogCombo.updateTooltip() + check = mConfigManager.mProperties.get(mConfigManager.ITEM_HIGHLIGHT_LOG_CHECK) as? String if (!check.isNullOrEmpty()) { mBoldLogToggle.isSelected = check.toBoolean() @@ -1750,6 +1760,7 @@ class MainUI(title: String) : JFrame() { } resetComboItem(combo, item) mFilteredTableModel.mFilterLog = item + combo.updateTooltip() } else if (p0?.source == mBoldLogCombo) { if (mBoldLogCombo.selectedIndex < 0) { return @@ -1758,6 +1769,7 @@ class MainUI(title: String) : JFrame() { val item = combo.selectedItem!!.toString() resetComboItem(combo, item) mFilteredTableModel.mFilterHighlightLog = item + combo.updateTooltip() } else if (p0?.source == mShowTagCombo) { if (mShowTagCombo.selectedIndex < 0) { return @@ -1766,6 +1778,7 @@ class MainUI(title: String) : JFrame() { val item = combo.selectedItem!!.toString() resetComboItem(combo, item) mFilteredTableModel.mFilterTag = item + combo.updateTooltip() } else if (p0?.source == mShowPidCombo) { if (mShowPidCombo.selectedIndex < 0) { return @@ -1774,6 +1787,7 @@ class MainUI(title: String) : JFrame() { val item = combo.selectedItem!!.toString() resetComboItem(combo, item) mFilteredTableModel.mFilterPid = item + combo.updateTooltip() } else if (p0?.source == mShowTidCombo) { if (mShowTidCombo.selectedIndex < 0) { return @@ -1782,6 +1796,7 @@ class MainUI(title: String) : JFrame() { val item = combo.selectedItem!!.toString() resetComboItem(combo, item) mFilteredTableModel.mFilterTid = item + combo.updateTooltip() } } @@ -1806,7 +1821,7 @@ class MainUI(title: String) : JFrame() { } } - fun resetComboItem(combo: ColorComboBox, item: String) { + fun resetComboItem(combo: FilterComboBox, item: String) { if (combo.isExistItem(item)) { if (combo.selectedIndex == 0) { return @@ -1819,7 +1834,7 @@ class MainUI(title: String) : JFrame() { } fun goToLine(line: Int) { - println("Line : " + line) + println("Line : $line") if (line < 0) { return }