Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created March 30, 2016 11:47
Show Gist options
  • Save zaltoprofen/e6355df7103066328a09466f3e368b5b to your computer and use it in GitHub Desktop.
Save zaltoprofen/e6355df7103066328a09466f3e368b5b to your computer and use it in GitHub Desktop.
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import java.awt.*
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextField
class HogeWindow: ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val layout = GridBagLayout()
val component = toolWindow.component.jPanel(layout) {
jTextField {
preferredSize = Dimension(150, 30)
val cs = GridBagConstraints().apply {
gridx = 1
gridy = 0
}
layout.setConstraints(this, cs)
}
jLabel("HogeHoge") {
val cs = GridBagConstraints().apply {
gridx = 0
gridy = 0
}
layout.setConstraints(this, cs)
}
}
}
fun <T: JComponent> T.jPanel(
layoutManager: LayoutManager = GridLayout(),
isDoubleBuffered: Boolean = false,
initializer: (JPanel.() -> Unit)? = null): T {
val panel = JPanel(layoutManager, isDoubleBuffered)
initializer?.invoke(panel)
add(panel)
return this
}
fun <T: JComponent> T.jLabel(text: String, initializer: (JLabel.() -> Unit)? = null): T {
val label = JLabel(text)
initializer?.invoke(label)
add(label)
return this
}
fun <T: JComponent> T.jTextField(initializer: (JTextField.() -> Unit)? = null): T {
val textField = JTextField()
initializer?.invoke(textField)
add(textField)
return this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment