blob: 65bcba39a5b4097b9d9c36790c12bd2683fa6a01 [file] [log] [blame]
/*
* Copyright (c) 2010-2020 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
package org.eclipse.scout.sdk.s2i.ui
import java.awt.Component
import java.awt.Container
import java.awt.FocusTraversalPolicy
import java.util.*
/**
* A [FocusTraversalPolicy] that allows to apply a tab index to a component.
*/
open class IndexedFocusTraversalPolicy : FocusTraversalPolicy() {
private val m_components = TreeMap<Int, Component>()
fun addComponent(component: Component) {
val highest = if (m_components.isEmpty()) -1 else m_components.lastKey()
addComponent(component, highest + 1)
}
fun addComponent(component: Component, position: Int) {
m_components[position] = component
}
override fun getComponentAfter(aContainer: Container?, aComponent: Component?): Component? = advanceToNextVisibleComponent(aComponent, true)
override fun getComponentBefore(aContainer: Container?, aComponent: Component?): Component? = advanceToNextVisibleComponent(aComponent, false)
private fun advanceToNextVisibleComponent(aComponent: Component?, forward: Boolean): Component? {
val start = m_components.entries
.filter { it.value == aComponent }
.map { it.key }.firstOrNull() ?: return null
var candidate: Component?
var position: Int = start
do {
var entry = if (forward) m_components.higherEntry(position) else m_components.lowerEntry(position)
if (entry == null) {
entry = if (forward) m_components.firstEntry() else m_components.lastEntry()
}
candidate = entry?.value
position = entry.key
} while (candidate != null && candidate != aComponent && (!candidate.isShowing || !candidate.isEnabled))
return candidate
}
override fun getDefaultComponent(aContainer: Container?): Component? = getFirstComponent(aContainer)
override fun getLastComponent(aContainer: Container?): Component? = m_components.lastEntry().value
override fun getFirstComponent(aContainer: Container?): Component? = m_components.firstEntry().value
}