comparison src/name/blackcap/clipman/Main.kt @ 47:19d9da731c43

Recoded; cleaned up root namespace, removed race conditions.
author David Barts <n5jrn@me.com>
date Sun, 12 Apr 2020 14:31:06 -0700
parents 339e2da5bf83
children 7a75c743f973
comparison
equal deleted inserted replaced
46:88066346f129 47:19d9da731c43
32 val PANEL_BORDER = 9 32 val PANEL_BORDER = 9
33 33
34 /* default font sizes in the text-display panes */ 34 /* default font sizes in the text-display panes */
35 val MONO_SIZE = 14 35 val MONO_SIZE = 14
36 val PROP_SIZE = 16 36 val PROP_SIZE = 16
37
38 /* the queue of data we deal with and the main application frame */
39 val queue = LateInit<PasteboardQueue>()
40 val frame = LateInit<JFrame>()
41 37
42 /* kills the updating thread (and does a system exit) when needed */ 38 /* kills the updating thread (and does a system exit) when needed */
43 class KillIt() : WindowListener { 39 class KillIt() : WindowListener {
44 // events we don't care about 40 // events we don't care about
45 override fun windowActivated(e: WindowEvent) {} 41 override fun windowActivated(e: WindowEvent) {}
89 text = dhtml 85 text = dhtml
90 resize() 86 resize()
91 }) 87 })
92 } 88 }
93 piv.searchable.addMouseListener(this) 89 piv.searchable.addMouseListener(this)
94 queue.v.add(QueueItem(contents, piv)) 90 Application.queue.add(QueueItem(contents, piv))
95 oldContents = contents 91 oldContents = contents
96 } 92 }
97 } 93 }
98 if (Thread.interrupted()) { 94 if (Thread.interrupted()) {
99 return 95 return
129 override fun mouseClicked(e: MouseEvent) { 125 override fun mouseClicked(e: MouseEvent) {
130 val source = e.getSource() as? ClipText 126 val source = e.getSource() as? ClipText
131 if (source == null) { 127 if (source == null) {
132 return 128 return
133 } 129 }
134 queue.v.deselectAll() 130 Application.queue.deselectAll()
135 source.selected = true 131 source.selected = true
136 source.validate() 132 source.validate()
137 anyRequired.enable() 133 Application.anyRequired.enable()
138 source.basedOn.let { 134 source.basedOn.let {
139 if (it is PasteboardItem.HTML || it is PasteboardItem.RTF) { 135 if (it is PasteboardItem.HTML || it is PasteboardItem.RTF) {
140 styledRequired.enable() 136 Application.styledRequired.enable()
141 } else { 137 } else {
142 styledRequired.disable() 138 Application.styledRequired.disable()
143 } 139 }
144 } 140 }
145 } 141 }
146 142
147 override fun mousePressed(e: MouseEvent) { 143 override fun mousePressed(e: MouseEvent) {
152 maybeShowPopup(e) 148 maybeShowPopup(e)
153 } 149 }
154 150
155 private fun maybeShowPopup(e: MouseEvent) { 151 private fun maybeShowPopup(e: MouseEvent) {
156 if (e.isPopupTrigger()) { 152 if (e.isPopupTrigger()) {
157 popupMenu.show(e.component, e.x, e.y) 153 Application.popupMenu.show(e.component, e.x, e.y)
158 } 154 }
159 } 155 }
160 156
161 override fun mouseEntered(e: MouseEvent) { } 157 override fun mouseEntered(e: MouseEvent) { }
162 override fun mouseExited(e: MouseEvent) { } 158 override fun mouseExited(e: MouseEvent) { }
163 } 159 }
160
161 object Application {
162 /* name we call ourselves */
163 val MYNAME = "ExifWasher"
164
165 /* global UI objects, must be created on the Swing thread */
166 var queue: PasteboardQueue by setOnce()
167 var frame: JFrame by setOnce()
168 var coerceDialog: CoerceDialog by setOnce()
169 var menuItemListener: MenuItemListener by setOnce()
170 var popupMenu: MyPopupMenu by setOnce()
171 var searchDialog: SearchDialog by setOnce()
172 var settingsDialog: SettingsDialog by setOnce()
173
174 /* used by the menus, but not themselves Swing objects */
175 val anyRequired = SelectionRequired()
176 val styledRequired = SelectionRequired()
177
178 fun initialize() {
179 /* make ourselves look more native */
180 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
181
182 /* initialize reusable GUI objects */
183 frame = JFrame(MYNAME) /* must init this gui object first */
184 coerceDialog = CoerceDialog()
185 menuItemListener = MenuItemListener() /* must init before menus */
186 popupMenu = MyPopupMenu()
187 searchDialog = SearchDialog()
188 settingsDialog = SettingsDialog()
189
190 /* set up the main frame */
191 val con = JPanel().apply {
192 layout = BoxLayout(this, BoxLayout.Y_AXIS)
193 border = BorderFactory.createEmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER)
194 background = frame.background
195 }
196 frame.apply {
197 jMenuBar = MyMenuBar()
198 contentPane.add(
199 JScrollPane(con).apply {
200 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
201 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
202 preferredSize = Dimension(CPWIDTH, CPHEIGHT)
203 background = frame.background
204 }, BorderLayout.CENTER)
205 pack()
206 setVisible(true)
207 addWindowListener(KillIt())
208 }
209 setMacMenus()
210
211 /* launch the updating thread */
212 queue = PasteboardQueue(con, settingsDialog.qLength)
213 UpdateIt(1000).apply { start() }
214 }
215 }
216
164 217
165 /* entry point */ 218 /* entry point */
166 fun main(args: Array<String>) { 219 fun main(args: Array<String>) {
167 LOGGER.log(Level.INFO, "beginning execution") 220 LOGGER.log(Level.INFO, "beginning execution")
168 if (OS.type == OS.MAC) { 221 if (OS.type == OS.MAC) {
169 System.setProperty("apple.laf.useScreenMenuBar", "true") 222 System.setProperty("apple.laf.useScreenMenuBar", "true")
170 } 223 }
171 lateinit var con: JPanel 224 inSwingThread {
172 inSynSwingThread { 225 Application.initialize()
173 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 226 }
174 frame.v = JFrame(MYNAME) 227 }
175 con = JPanel().apply {
176 layout = BoxLayout(this, BoxLayout.Y_AXIS)
177 border = BorderFactory.createEmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER)
178 background = frame.v.background
179 }
180 frame.v.jMenuBar = menuBar
181 frame.v.apply {
182 contentPane.add(
183 JScrollPane(con).apply {
184 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
185 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
186 preferredSize = Dimension(CPWIDTH, CPHEIGHT)
187 background = frame.v.background
188 }, BorderLayout.CENTER)
189 pack()
190 setVisible(true)
191 addWindowListener(KillIt())
192 }
193 setMacMenus()
194 }
195 queue.v = PasteboardQueue(con, settingsDialog.qLength)
196 UpdateIt(1000).apply { start() }
197 }