|
| 1 | +package extensions.jose.han; |
| 2 | + |
| 3 | +import com.intellij.ide.BrowserUtil |
| 4 | +import com.intellij.ui.layout.panel |
| 5 | +import extensions.Extension |
| 6 | +import wu.seal.jsontokotlin.classscodestruct.Annotation |
| 7 | +import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass |
| 8 | +import javax.swing.JCheckBox |
| 9 | +import javax.swing.JPanel |
| 10 | + |
| 11 | +/** |
| 12 | + * @author jose.han |
| 13 | + * @Date 2019/7/27Ø |
| 14 | + */ |
| 15 | +object ParcelableAnnotationSupport : Extension() { |
| 16 | + |
| 17 | + val configKey = "jose.han.add_parcelable_annotatioin_enable" |
| 18 | + |
| 19 | + override fun createUI(): JPanel { |
| 20 | + |
| 21 | + |
| 22 | + val checkBox = JCheckBox("Enable Parcelable Support ").apply { |
| 23 | + isSelected = ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean() |
| 24 | + addActionListener { |
| 25 | + ParcelableAnnotationSupport.setConfig(ParcelableAnnotationSupport.configKey, isSelected.toString()) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return panel { |
| 30 | + row { |
| 31 | + |
| 32 | + checkBox() |
| 33 | + right { |
| 34 | + link("Need Some Config") { |
| 35 | + BrowserUtil.browse("https://github.com/wuseal/JsonToKotlinClass/blob/master/parceable_support_tip.md.md") |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass { |
| 43 | + return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) { |
| 44 | + |
| 45 | + val classAnnotationString1 = "@SuppressLint(\"ParcelCreator\")" |
| 46 | + val classAnnotationString2 = "@Parcelize" |
| 47 | + |
| 48 | + val classAnnotation1 = Annotation.fromAnnotationString(classAnnotationString1) |
| 49 | + val classAnnotation2 = Annotation.fromAnnotationString(classAnnotationString2) |
| 50 | + |
| 51 | + return kotlinDataClass.copy(annotations = listOf(classAnnotation1,classAnnotation2),parentClassTemplate = "Parcelable") |
| 52 | + } else { |
| 53 | + kotlinDataClass |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + override fun intercept(originClassImportDeclaration: String): String { |
| 58 | + |
| 59 | + val classAnnotationImportClassString = "import kotlinx.android.parcel.Parcelize".append("import android.os.Parcelable") |
| 60 | + |
| 61 | + return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) { |
| 62 | + originClassImportDeclaration.append(classAnnotationImportClassString) |
| 63 | + } else { |
| 64 | + originClassImportDeclaration |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments