For lines 805-812: To avoid code duplication with lines 365-375, I suggest to move the lines 365-375 into a function and call that function from createItem() and runPaletteComponentAction().

This will also ease bug fixing as with this version of code it will be very easy to fix something only in one place instead of two.