|
54 | 54 |
|
55 | 55 | import java.util.Calendar; |
56 | 56 | import java.util.Date; |
| 57 | +import java.util.ArrayList; |
| 58 | +import java.util.Collections; |
| 59 | +import java.util.List; |
57 | 60 | import java.util.ListIterator; |
58 | 61 |
|
59 | 62 | import static com.codename1.ui.ComponentSelector.$; |
@@ -115,6 +118,29 @@ public class Picker extends Button { |
115 | 118 | private boolean useLightweightPopup; |
116 | 119 | private Runnable stopEditingCallback; |
117 | 120 | private boolean suppressPaint; |
| 121 | + private final ArrayList<LightweightPopupButton> lightweightPopupButtons = new ArrayList<LightweightPopupButton>(); |
| 122 | + |
| 123 | + /// Placement options for custom lightweight popup buttons. |
| 124 | + public static final class LightweightPopupButtonPlacement { |
| 125 | + /// Place the custom button in the top button row between the `Cancel` and `Done` groups. |
| 126 | + public static final int BETWEEN_CANCEL_AND_DONE = 0; |
| 127 | + /// Place the custom button row directly above the spinner wheels. |
| 128 | + public static final int ABOVE_SPINNER = 1; |
| 129 | + /// Place the custom button row directly below the spinner wheels. |
| 130 | + public static final int BELOW_SPINNER = 2; |
| 131 | + } |
| 132 | + |
| 133 | + private static final class LightweightPopupButton { |
| 134 | + private final String text; |
| 135 | + private final Runnable action; |
| 136 | + private final int placement; |
| 137 | + |
| 138 | + private LightweightPopupButton(String text, Runnable action, int placement) { |
| 139 | + this.text = text; |
| 140 | + this.action = action; |
| 141 | + this.placement = placement; |
| 142 | + } |
| 143 | + } |
118 | 144 |
|
119 | 145 | /// Default constructor |
120 | 146 | public Picker() { |
@@ -559,8 +585,21 @@ protected void deinitialize() { |
559 | 585 | .setBgTransparency(0) |
560 | 586 | .setMargin(0) |
561 | 587 | .setPaddingMillimeters(3f, 0); |
562 | | - //wrapper.add(BorderLayout.CENTER, spinnerC); |
563 | | - dlg.getContentPane().add(BorderLayout.CENTER, wrapper); |
| 588 | + Container topCustomButtons = createLightweightPopupButtonRow(spinner, LightweightPopupButtonPlacement.ABOVE_SPINNER, isTablet); |
| 589 | + Container bottomCustomButtons = createLightweightPopupButtonRow(spinner, LightweightPopupButtonPlacement.BELOW_SPINNER, isTablet); |
| 590 | + if (topCustomButtons != null || bottomCustomButtons != null) { |
| 591 | + Container spinnerSection = new Container(new BorderLayout()); |
| 592 | + spinnerSection.add(BorderLayout.CENTER, wrapper); |
| 593 | + if (topCustomButtons != null) { |
| 594 | + spinnerSection.add(BorderLayout.NORTH, topCustomButtons); |
| 595 | + } |
| 596 | + if (bottomCustomButtons != null) { |
| 597 | + spinnerSection.add(BorderLayout.SOUTH, bottomCustomButtons); |
| 598 | + } |
| 599 | + dlg.getContentPane().add(BorderLayout.CENTER, spinnerSection); |
| 600 | + } else { |
| 601 | + dlg.getContentPane().add(BorderLayout.CENTER, wrapper); |
| 602 | + } |
564 | 603 |
|
565 | 604 |
|
566 | 605 | Button doneButton = new Button("Done", isTablet ? "PickerButtonTablet" : "PickerButton"); |
@@ -643,7 +682,8 @@ public void actionPerformed(ActionEvent evt) { |
643 | 682 | west.add(nextButton); |
644 | 683 | } |
645 | 684 |
|
646 | | - Container buttonBar = BorderLayout.centerEastWest(null, doneButton, west); |
| 685 | + Container centerButtons = createLightweightPopupButtonRow(spinner, LightweightPopupButtonPlacement.BETWEEN_CANCEL_AND_DONE, isTablet); |
| 686 | + Container buttonBar = BorderLayout.centerEastWest(centerButtons, doneButton, west); |
647 | 687 | buttonBar.setUIID(isTablet ? "PickerButtonBarTablet" : "PickerButtonBar"); |
648 | 688 | dlg.getContentPane().add(BorderLayout.NORTH, buttonBar); |
649 | 689 |
|
@@ -716,6 +756,73 @@ public void actionPerformed(ActionEvent evt) { |
716 | 756 | updateValue(); |
717 | 757 | } |
718 | 758 |
|
| 759 | + private Container createLightweightPopupButtonRow(final InternalPickerWidget spinner, int placement, boolean isTablet) { |
| 760 | + Container row = null; |
| 761 | + for (LightweightPopupButton entry : lightweightPopupButtons) { |
| 762 | + if (entry.placement != placement) { |
| 763 | + continue; |
| 764 | + } |
| 765 | + if (row == null) { |
| 766 | + row = new Container(BoxLayout.x()); |
| 767 | + row.setUIID(isTablet ? "PickerButtonBarTablet" : "PickerButtonBar"); |
| 768 | + $(row).selectAllStyles().setMargin(0).setPadding(0).setBorder(Border.createEmpty()).setBgTransparency(0); |
| 769 | + } |
| 770 | + final LightweightPopupButton popupButton = entry; |
| 771 | + Button button = new Button(popupButton.text, isTablet ? "PickerButtonTablet" : "PickerButton"); |
| 772 | + button.addActionListener(new ActionListener() { |
| 773 | + @Override |
| 774 | + public void actionPerformed(ActionEvent evt) { |
| 775 | + if (popupButton.action != null) { |
| 776 | + popupButton.action.run(); |
| 777 | + } |
| 778 | + spinner.setValue(value); |
| 779 | + updateValue(); |
| 780 | + } |
| 781 | + }); |
| 782 | + row.add(button); |
| 783 | + } |
| 784 | + return row; |
| 785 | + } |
| 786 | + |
| 787 | + /// Adds a custom button to the lightweight picker popup in the default placement |
| 788 | + /// between the `Cancel` and `Done` areas. |
| 789 | + /// |
| 790 | + /// #### Parameters |
| 791 | + /// |
| 792 | + /// - `text`: Button label. |
| 793 | + /// - `action`: Action to run when the button is pressed. |
| 794 | + public void addLightweightPopupButton(String text, Runnable action) { |
| 795 | + addLightweightPopupButton(text, action, LightweightPopupButtonPlacement.BETWEEN_CANCEL_AND_DONE); |
| 796 | + } |
| 797 | + |
| 798 | + /// Adds a custom button to the lightweight picker popup. |
| 799 | + /// |
| 800 | + /// #### Parameters |
| 801 | + /// |
| 802 | + /// - `text`: Button label. |
| 803 | + /// - `action`: Action to run when the button is pressed. |
| 804 | + /// - `placement`: One of `LightweightPopupButtonPlacement#BETWEEN_CANCEL_AND_DONE`, |
| 805 | + /// `LightweightPopupButtonPlacement#ABOVE_SPINNER`, or `LightweightPopupButtonPlacement#BELOW_SPINNER`. |
| 806 | + public void addLightweightPopupButton(String text, Runnable action, int placement) { |
| 807 | + lightweightPopupButtons.add(new LightweightPopupButton(text, action, placement)); |
| 808 | + } |
| 809 | + |
| 810 | + /// Removes all custom lightweight popup buttons that were previously added with |
| 811 | + /// `#addLightweightPopupButton`. |
| 812 | + public void clearLightweightPopupButtons() { |
| 813 | + lightweightPopupButtons.clear(); |
| 814 | + } |
| 815 | + |
| 816 | + /// Returns an immutable list of custom button labels currently configured for |
| 817 | + /// the lightweight popup. |
| 818 | + public List<String> getLightweightPopupButtonLabels() { |
| 819 | + ArrayList<String> out = new ArrayList<String>(); |
| 820 | + for (LightweightPopupButton b : lightweightPopupButtons) { |
| 821 | + out.add(b.text); |
| 822 | + } |
| 823 | + return Collections.unmodifiableList(out); |
| 824 | + } |
| 825 | + |
719 | 826 | /// Whether useLightweightPopup should default to true, this can be set via |
720 | 827 | /// the theme constant `lightweightPickerBool` |
721 | 828 | /// |
|
0 commit comments