Skip to content

Latest commit

 

History

History
executable file
·
164 lines (131 loc) · 4.83 KB

component-ktx.md

File metadata and controls

executable file
·
164 lines (131 loc) · 4.83 KB

Kotlin DSL for Radiance components

Maven Central radiance-component-ktx for build instructions of the latest stable release.

In your Java app, this is how you would configure a toggle command, add it to a command group, and project the group to create a command button strip:

Command styleBold = Command.builder()
      .setIconFactory(Format_text_bold.factory())
      .setAction(commandActionEvent -> System.out.println("Bold toggled"))
      .setToggleSelected(true)
      .setActionRichTooltip(RichTooltip.builder()
              .setTitle(resourceBundle.getString("FontBold.tooltip.textActionTitle"))
              .addDescriptionSection(resourceBundle.getString("FontBold.tooltip.textActionParagraph1"))
              .build())
      .build();

Map<Command, CommandButtonPresentationModel.Overlay> styleOverlays = new HashMap<>();
styleOverlays.put(styleBold, CommandButtonPresentationModel.overlay().setActionKeyTip("1"));

CommandStripProjection styleStripProjection = new CommandStripProjection(
      new CommandGroup(styleBold),
      CommandStripPresentationModel.withDefaults());
styleStripProjection.setCommandOverlays(styleOverlays);

JCommandButtonStrip buttonStrip = styleStripProjection.buildComponent();

And here is how the same code would look like in Kotlin using the Radiance-provided DSL:

commandButtonStrip {
    command {
        iconFactory = Format_text_bold.factory()
        action = { println("Bold toggled") }
        isToggleSelected = true
        actionRichTooltip {
            title = resourceBundle["FontBold.tooltip.textActionTitle"]
            description = resourceBundle["FontBold.tooltip.textActionParagraph1"]
        }
        actionKeyTip = "1"
    }
}

DSL constructs for the following Radiance components are available:

  • JCommandButton
  • JCommandButtonPanel
  • JCommandButtonStrip
  • JCommandPopupMenu
  • JRibbon and all its parts

For example, here is a function that constructs and returns a ribbon band:

fun getFindBand(): KRibbonBand {
    return ribbonBand {
        title = resourceBundle["Find.textBandTitle"]
        iconFactory = Edit_find.factory()
        collapsedStateKeyTip = "ZY"

        command(RibbonElementPriority.TOP, actionKeyTip = "FD") {
            title = resourceBundle["Search.text"]
            iconFactory = System_search.factory()
            action = { println("Search activated") }
        }

        command(RibbonElementPriority.MEDIUM) {
            title = resourceBundle["Find.text"]
            iconFactory = Edit_find.factory()
            action = { println("Find activated") }
        }

        command(RibbonElementPriority.MEDIUM) {
            title = resourceBundle["FindReplace.text"]
            iconFactory = Edit_find_replace.factory()
            action = { println("Find Replace activated") }
            isActionEnabled = false
        }

        command(RibbonElementPriority.MEDIUM) {
          title = resourceBundle["SelectAll.text"]
            iconFactory = Edit_select_all.factory()
            action = { println("Select All activated") }
        }

        resizePolicies = { ribbonBand ->
            listOf(CoreRibbonResizePolicies.Mirror(ribbonBand),
         CoreRibbonResizePolicies.IconRibbonBandResizePolicy(ribbonBand))
        }
    }
}

You can then construct a ribbon task out of one or more bands:

fun getPageLayoutTask(): KRibbonTask {
    return ribbonTask {
      title = resourceBundle["PageLayout.textTaskTitle"]
        keyTip = "P"

        bands {
            +getClipboardBand()
            +getQuickStylesBand()
            +getFontBand()
            +getDocumentBand()
            +getFindBand()
        }
    }
}

And a ribbon frame out of one or more ribbon tasks and other elements:

val ribbonFrame = ribbonFrame {
    title = builder.resourceBundle["Frame.title"]
    applicationIcon = Applications_internet.factory()

    tasks {
        +builder.getPageLayoutTask()
        +builder.getWriteTask()
        +builder.getAnimationsTask()
        +builder.getWrappedTask()
    }

    contextualTaskGroups {
        // ...
    }

    anchored {
        // ...
    }

    taskbar {
        // ...
    }

    applicationMenu {
        // ...
    }

    onTaskSelectionChange = { ... }

    keyboardActions {
        // ...
    }
}

// This returns a JRibbonFrame object
val javaRibbonFrame = ribbonFrame.asRibbonFrame()

// Which we can then configure and display
javaRibbonFrame.preferredSize = Dimension(...)
javaRibbonFrame.minimumSize = Dimension(...)
javaRibbonFrame.pack()
javaRibbonFrame.setLocation(...)
javaRibbonFrame.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE

javaRibbonFrame.setVisible(true)