PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/platform-impl/src/com/intellij/ui/layout/testPanels.kt

http://github.com/JetBrains/intellij-community
Kotlin | 441 lines | 389 code | 42 blank | 10 comment | 0 complexity | 9ef095158df1b6f572cfdbe2f652f286 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
  2. @file:Suppress("HardCodedStringLiteral")
  3. package com.intellij.ui.layout
  4. import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
  5. import com.intellij.openapi.ui.ComboBox
  6. import com.intellij.ui.JBIntSpinner
  7. import com.intellij.ui.UIBundle
  8. import com.intellij.ui.components.*
  9. import java.awt.Dimension
  10. import java.awt.GridLayout
  11. import javax.swing.*
  12. /**
  13. * See [ShowcaseUiDslAction]
  14. */
  15. fun labelRowShouldNotGrow(): JPanel {
  16. return panel {
  17. row("Create Android module") { CheckBox("FooBar module name foo")() }
  18. row("Android module name:") { JTextField("input")() }
  19. }
  20. }
  21. fun secondColumnSmallerPanel(): JPanel {
  22. val selectForkButton = JButton("Select Other Fork")
  23. val branchCombobox = ComboBox<String>()
  24. val diffButton = JButton("Show Diff")
  25. val titleTextField = JTextField()
  26. val panel = panel {
  27. row("Base fork:") {
  28. JComboBox<String>(arrayOf())(growX, CCFlags.pushX)
  29. selectForkButton(growX)
  30. }
  31. row("Base branch:") {
  32. branchCombobox(growX, pushX)
  33. diffButton(growX)
  34. }
  35. row("Title:") { titleTextField() }
  36. row("Description:") {
  37. scrollPane(JTextArea())
  38. }
  39. }
  40. // test scrollPane
  41. panel.preferredSize = Dimension(512, 256)
  42. return panel
  43. }
  44. @Suppress("unused")
  45. fun visualPaddingsPanelOnlyComboBox(): JPanel {
  46. return panel {
  47. row("Combobox:") { JComboBox<String>(arrayOf("one", "two"))(growX) }
  48. row("Combobox Editable:") {
  49. val field = JComboBox<String>(arrayOf("one", "two"))
  50. field.isEditable = true
  51. field(growX)
  52. }
  53. }
  54. }
  55. @Suppress("unused")
  56. fun visualPaddingsPanelOnlyButton(): JPanel {
  57. return panel {
  58. row("Button:") { button("label") {}.constraints(growX) }
  59. }
  60. }
  61. @Suppress("unused")
  62. fun visualPaddingsPanelOnlyLabeledScrollPane(): JPanel {
  63. return panel {
  64. row("Description:") {
  65. scrollPane(JTextArea())
  66. }
  67. }
  68. }
  69. @Suppress("unused")
  70. fun visualPaddingsPanelOnlyTextField(): JPanel {
  71. return panel {
  72. row("Text field:") { JTextField("text")() }
  73. }
  74. }
  75. fun visualPaddingsPanel(): JPanel {
  76. // we use growX to test right border
  77. return panel {
  78. row("Text field:") { JTextField("text")() }
  79. row("Password:") { JPasswordField("secret")() }
  80. row("Combobox:") { JComboBox<String>(arrayOf("one", "two"))(growX) }
  81. row("Combobox Editable:") {
  82. val field = JComboBox<String>(arrayOf("one", "two"))
  83. field.isEditable = true
  84. field(growX)
  85. }
  86. row("Button:") { button("label") {}.constraints(growX) }
  87. row("CheckBox:") { CheckBox("enabled")() }
  88. row("RadioButton:") { JRadioButton("label")() }
  89. row("Spinner:") { JBIntSpinner(0, 0, 7)() }
  90. row("Text with browse:") { textFieldWithBrowseButton("File") }
  91. // test text baseline alignment
  92. row("All:") {
  93. cell {
  94. JTextField("t")()
  95. JPasswordField("secret")()
  96. JComboBox<String>(arrayOf("c1", "c2"))(growX)
  97. button("b") {}
  98. CheckBox("c")()
  99. JRadioButton("rb")()
  100. }
  101. }
  102. row("Scroll pane:") {
  103. scrollPane(JTextArea("first line baseline equals to label"))
  104. }
  105. }
  106. }
  107. fun fieldWithGear(): JPanel {
  108. return panel {
  109. row("Database:") {
  110. JTextField()()
  111. gearButton()
  112. }
  113. row("Master Password:") {
  114. JBPasswordField()()
  115. }
  116. }
  117. }
  118. fun fieldWithGearWithIndent(): JPanel {
  119. return panel {
  120. row {
  121. row("Database:") {
  122. JTextField()()
  123. gearButton()
  124. }
  125. row("Master Password:") {
  126. JBPasswordField()()
  127. }
  128. }
  129. }
  130. }
  131. fun alignFieldsInTheNestedGrid(): JPanel {
  132. return panel {
  133. buttonGroup {
  134. row {
  135. RadioButton("In KeePass")()
  136. row("Database:") {
  137. JTextField()()
  138. gearButton()
  139. }
  140. row("Master Password:") {
  141. JBPasswordField()(comment = "Stored using weak encryption.")
  142. }
  143. }
  144. }
  145. }
  146. }
  147. fun noteRowInTheDialog(): JPanel {
  148. val passwordField = JPasswordField()
  149. return panel {
  150. noteRow("Profiler requires access to the kernel-level API.\nEnter the sudo password to allow this. ")
  151. row("Sudo password:") { passwordField() }
  152. row { CheckBox(UIBundle.message("auth.remember.cb"), true)() }
  153. noteRow("Should be an empty row above as a gap. <a href=''>Click me</a>.") {
  154. System.out.println("Hello")
  155. }
  156. }
  157. }
  158. fun jbTextField(): JPanel {
  159. val passwordField = JBPasswordField()
  160. return panel {
  161. noteRow("Enter credentials for bitbucket.org")
  162. row("Username:") { JTextField("develar")() }
  163. row("Password:") { passwordField() }
  164. row {
  165. JBCheckBox(UIBundle.message("auth.remember.cb"), true)()
  166. }
  167. }
  168. }
  169. fun cellPanel(): JPanel {
  170. return panel {
  171. row("Repository:") {
  172. cell {
  173. ComboBox<String>()(comment = "Use File -> Settings Repository... to configure")
  174. JButton("Delete")()
  175. }
  176. }
  177. row {
  178. // need some pushx/grow component to test label cell grow policy if there is cell with several components
  179. scrollPane(JTextArea())
  180. }
  181. }
  182. }
  183. fun commentAndPanel(): JPanel {
  184. return panel {
  185. row("Repository:") {
  186. cell {
  187. checkBox("Auto Sync", comment = "Use File -> Settings Repository... to configure")
  188. }
  189. }
  190. row {
  191. panel("Foo", JScrollPane(JTextArea()))
  192. }
  193. }
  194. }
  195. fun createLafTestPanel(): JPanel {
  196. val spacing = createIntelliJSpacingConfiguration()
  197. val panel = JPanel(GridLayout(0, 1, spacing.horizontalGap, spacing.verticalGap))
  198. panel.add(JTextField("text"))
  199. panel.add(JPasswordField("secret"))
  200. panel.add(ComboBox<String>(arrayOf("one", "two")))
  201. val field = ComboBox<String>(arrayOf("one", "two"))
  202. field.isEditable = true
  203. panel.add(field)
  204. panel.add(JButton("label"))
  205. panel.add(CheckBox("enabled"))
  206. panel.add(JRadioButton("label"))
  207. panel.add(JBIntSpinner(0, 0, 7))
  208. panel.add(textFieldWithHistoryWithBrowseButton(null, "File", FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor()))
  209. return panel
  210. }
  211. fun withVerticalButtons(): JPanel {
  212. return panel {
  213. row {
  214. label("<html>Merging branch <b>foo</b> into <b>bar</b>")
  215. }
  216. row {
  217. scrollPane(JTextArea()).constraints(pushX)
  218. cell(isVerticalFlow = true) {
  219. button("Accept Yours") {}.constraints(growX)
  220. button("Accept Theirs") {}.constraints(growX)
  221. button("Merge ...") {}.constraints(growX)
  222. }
  223. }
  224. }
  225. }
  226. fun titledRows(): JPanel {
  227. return panel {
  228. titledRow("Async Profiler") {
  229. row { browserLink("Async profiler README.md", "https://github.com/jvm-profiling-tools/async-profiler") }
  230. row("Agent path:") { textFieldWithBrowseButton("").comment("If field is empty bundled agent will be used") }
  231. row("Agent options:") { textFieldWithBrowseButton("").comment("Don't add output format (collapsed is used) or output file options") }
  232. }
  233. titledRow("Java Flight Recorder") {
  234. row("JRE home:") {
  235. textFieldWithBrowseButton("").comment("At least OracleJRE 9 or OpenJRE 11 is required to import dump")
  236. }
  237. }
  238. }
  239. }
  240. fun hideableRow(): JPanel {
  241. val dummyTextBinding = PropertyBinding({ "" }, {})
  242. return panel {
  243. row("Foo") {
  244. textField(dummyTextBinding)
  245. }
  246. hideableRow("Bar") {
  247. textField(dummyTextBinding)
  248. }
  249. }
  250. }
  251. fun spannedCheckbox(): JPanel {
  252. return panel {
  253. buttonGroup {
  254. row {
  255. RadioButton("In KeePass")()
  256. row("Database:") {
  257. // comment can lead to broken layout, so, test it
  258. JTextField("test")(comment = "Stored using weak encryption. It is recommended to store on encrypted volume for additional security.")
  259. }
  260. row {
  261. cell {
  262. checkBox("Protect master password using PGP key")
  263. val comboBox = ComboBox(arrayOf("Foo", "Bar"))
  264. comboBox.isVisible = false
  265. comboBox(growPolicy = GrowPolicy.MEDIUM_TEXT)
  266. }
  267. }
  268. }
  269. row {
  270. RadioButton("Do not save, forget passwords after restart")()
  271. }
  272. }
  273. }
  274. }
  275. fun checkboxRowsWithBigComponents(): JPanel {
  276. return panel {
  277. row {
  278. CheckBox("Sample checkbox label")()
  279. }
  280. row {
  281. CheckBox("Sample checkbox label")()
  282. }
  283. row {
  284. CheckBox("Sample checkbox label")()
  285. ComboBox(DefaultComboBoxModel(arrayOf("asd", "asd")))()
  286. }
  287. row {
  288. CheckBox("Sample checkbox label")()
  289. }
  290. row {
  291. CheckBox("Sample checkbox label")()
  292. ComboBox(DefaultComboBoxModel(arrayOf("asd", "asd")))()
  293. }
  294. row {
  295. CheckBox("Sample checkbox label")()
  296. ComboBox(DefaultComboBoxModel(arrayOf("asd", "asd")))()
  297. }
  298. row {
  299. CheckBox("Sample checkbox label")()
  300. JBTextField()()
  301. }
  302. row {
  303. cell(isFullWidth = true) {
  304. CheckBox("Sample checkbox label")()
  305. }
  306. }
  307. row {
  308. cell(isFullWidth = true) {
  309. CheckBox("Sample checkbox label")()
  310. JBTextField()()
  311. }
  312. }
  313. row {
  314. cell(isFullWidth = true) {
  315. CheckBox("Sample checkbox label")()
  316. comment("commentary")
  317. }
  318. }
  319. }
  320. }
  321. // titledRows is not enough to test because component align depends on comment components, so, pure titledRow must be tested
  322. fun titledRow(): JPanel {
  323. return panel {
  324. titledRow("Remote settings") {
  325. row("Default notebook name:") { JTextField("")() }
  326. row("Spark version:") { JTextField("")() }
  327. }
  328. }
  329. }
  330. fun sampleConfigurablePanel(): JPanel {
  331. return panel {
  332. titledRow("Settings") {
  333. row { checkBox("Some test option") }
  334. row { checkBox("Another test option") }
  335. }
  336. titledRow("Options") {
  337. row { checkBox("Some test option") }
  338. row {
  339. buttonGroup("Radio group") {
  340. row { radioButton("Option 1") }
  341. row { radioButton("Option 2") }
  342. }
  343. }
  344. row {
  345. buttonGroup("Radio group") {
  346. row { radioButton("Option 1", comment = "Comment for the Option 1") }
  347. row { radioButton("Option 2") }
  348. }
  349. }
  350. }
  351. titledRow("Test") {
  352. row("Header") { JTextField()() }
  353. row("Longer Header") { checkBox("Some long description", comment = "Comment for the checkbox with longer header.") }
  354. row("Header") { JPasswordField()() }
  355. row("Header") { comboBox(DefaultComboBoxModel(arrayOf("Option 1", "Option 2")), { null }, {}) }
  356. }
  357. }
  358. }
  359. private data class TestOptions(var threadDumpDelay: Int, var enableLargeIndexing: Boolean, var largeIndexFilesCount: Int)
  360. fun checkBoxFollowedBySpinner(): JPanel {
  361. val testOptions = TestOptions(50, true, 100)
  362. return panel {
  363. row(label = "Thread dump capture delay (ms):") {
  364. spinner(testOptions::threadDumpDelay, 50, 5000, 50)
  365. }
  366. row {
  367. val c = checkBox("Create", testOptions::enableLargeIndexing).actsAsLabel()
  368. spinner(testOptions::largeIndexFilesCount, 100, 1_000_000, 1_000)
  369. .enableIf(c.selected)
  370. label("files to start background indexing")
  371. }
  372. }
  373. }
  374. fun separatorAndComment() : JPanel {
  375. return panel {
  376. row("Label", separated = true) {
  377. textField({ "abc" }, {}).comment("comment")
  378. }
  379. }
  380. }
  381. fun rowWithIndent(): JPanel {
  382. return panel {
  383. row("Zero") {
  384. subRowIndent = 0
  385. row("Bar 0") {
  386. }
  387. }
  388. row("One") {
  389. subRowIndent = 1
  390. row("Bar 1") {
  391. }
  392. }
  393. row("Two") {
  394. subRowIndent = 2
  395. row("Bar 2") {
  396. }
  397. }
  398. }
  399. }