diff src/name/blackcap/clipman/SearchDialog.kt @ 28:f1fcc1281dad

Use BorderFactory; clean up Find dialog.
author David Barts <n5jrn@me.com>
date Wed, 29 Jan 2020 13:39:14 -0800
parents 8aa2dfac27eb
children c4f53bc01732
line wrap: on
line diff
--- a/src/name/blackcap/clipman/SearchDialog.kt	Wed Jan 29 10:50:07 2020 -0800
+++ b/src/name/blackcap/clipman/SearchDialog.kt	Wed Jan 29 13:39:14 2020 -0800
@@ -14,8 +14,8 @@
 
 class SearchDialog: JDialog(frame.v), ActionListener, DocumentListener {
     /* the search term */
-    private val _searchFor = JTextField(50).also {
-        it.border = LineBorder(Color.GRAY, 1)
+    private val _searchFor = JTextField(25).also {
+        it.border = BorderFactory.createLineBorder(Color.GRAY, 1)
         it.horizontalAlignment = JTextField.LEFT
         it.alignmentX = JTextField.LEFT_ALIGNMENT
         it.text = ""
@@ -58,6 +58,10 @@
         throw RuntimeException("impossible button state!")
     }
 
+    /* standard spacing between elements (18 pixels = 1/4") and half that */
+    private val BW = 9
+    private val BW2 = 2 * BW
+
     /* where to begin searching from. unlike the other properties, this
        one is read/write. null means to start from the beginning on
        forward searches, and from the end on backward searches (i.e.
@@ -69,38 +73,69 @@
         it.addActionListener(this)
     }
 
+    private val _cancel = JButton("Cancel").also {
+        it.actionCommand = "Cancel"
+        it.addActionListener(this)
+    }
+
     /* initializer */
     init {
         title = "Find"
         contentPane.apply {
             add(Box(BoxLayout.Y_AXIS).apply {
                 add(Box(BoxLayout.Y_AXIS).apply {
-                    add(JLabel("Search for").apply {
+                    add(JLabel("Search for:").apply {
                         horizontalAlignment = JLabel.LEFT
                         alignmentX = JLabel.LEFT_ALIGNMENT
                     })
                     add(_searchFor)
+                    alignmentX = Box.CENTER_ALIGNMENT
+                    border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2)
                 })
                 add(Box(BoxLayout.X_AXIS).apply {
+                    add(Box.createGlue())
                     add(Box(BoxLayout.Y_AXIS).apply {
+                        add(JLabel("Settings:").apply {
+                            horizontalAlignment = JLabel.CENTER
+                            alignmentX = JLabel.LEFT_ALIGNMENT
+                        })
                         add(_ignoreCase)
                         add(_autoWrap)
                     })
+                    add(Box.createGlue())
                     add(Box(BoxLayout.Y_AXIS).apply {
+                        add(JLabel("Direction:").apply {
+                            horizontalAlignment = JLabel.CENTER
+                            alignmentX = JLabel.LEFT_ALIGNMENT
+                        })
                         add(_forwards)
                         add(_backwards)
                     })
+                    add(Box.createGlue())
+                    alignmentX = Box.CENTER_ALIGNMENT
+                    border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2)
                 })
-                add(_find)
+                add(Box(BoxLayout.X_AXIS).apply {
+                    add(Box.createGlue())
+                    add(_cancel)
+                    add(Box.createGlue())
+                    add(_find)
+                    add(Box.createGlue())
+                    border = BorderFactory.createEmptyBorder(BW, BW2, BW2, BW2)
+                })
             })
         }
         rootPane.setDefaultButton(_find)
+        pack()
     }
 
     override fun actionPerformed(e: ActionEvent) {
-        if (e.actionCommand == "Find") {
-            setVisible(false)
-            find()
+        when (e.actionCommand) {
+            "Find" -> {
+                setVisible(false)
+                find()
+            }
+            "Cancel" -> setVisible(false)
         }
     }
 
@@ -115,6 +150,11 @@
     }
 
     fun find(): Unit {
+        if (searchFor.isEmpty()) {
+            Toolkit.getDefaultToolkit().beep()
+            origin = null
+            return
+        }
         fun doFind(o: PasteboardQueue.Offset?) = queue.v.find(searchFor,
             direction = direction, foldCase = ignoreCase, origin = o)
         var result = doFind(origin)