001package polaris.sdk.ui.controls; 002import polaris.sdk.ui.*; 003import java.time.*; 004import java.time.format.*; 005 006/** Time editor composed of three equally spaced, frameless horizontal steppers. */ 007public class TimePicker extends Button { 008 private LocalTime value=LocalTime.of(12,0); 009 public TimePicker(){refresh();} 010 public TimePicker value(LocalTime time){if(time==null)throw new NullPointerException("time");value=time.withNano(0);refresh();return this;} 011 public LocalTime value(){return value;} 012 private void refresh(){text=value.format(DateTimeFormatter.ofPattern("HH:mm:ss"))+" ▾";} 013 @Override public void activate(){if(ui!=null)ui.show(new TimeEditor());} 014 private final class TimeEditor extends Modal { 015 private final Spinner hour=new Spinner(value.getHour()).range(0,23); 016 private final Spinner minute=new Spinner(value.getMinute()).range(0,59); 017 private final Spinner second=new Spinner(value.getSecond()).range(0,59); 018 private final Label[] labels={new CenterLabel("小时"),new CenterLabel("分钟"),new CenterLabel("秒")}; 019 private final Spinner[] fields={hour,minute,second}; 020 private final Button apply=new Button("确定").primary(true); 021 TimeEditor(){ 022 super("选择时间");w=404;h=HEADER_HEIGHT+Spacing.GAP+Spacing.LABEL_HEIGHT+Spacing.GAP+40+Spacing.GAP+Spacing.CONTROL_HEIGHT+Spacing.INSET; 023 id("timeEditor");hour.id("timeHour");minute.id("timeMinute");second.id("timeSecond");apply.id("timeApply"); 024 for(int i=0;i<3;i++){add(labels[i]);add(fields[i]);} 025 apply.onClick(w->{value(LocalTime.of((int)hour.number(),(int)minute.number(),(int)second.number()));TimePicker.this.changed();close();});add(apply); 026 } 027 @Override protected void layoutChildren(){ 028 float inset=Spacing.INSET,gap=Spacing.GAP,cell=Spacing.columnWidth(w,inset,gap,3),top=HEADER_HEIGHT+gap; 029 for(int i=0;i<3;i++){ 030 float x=inset+i*(cell+gap); 031 labels[i].bounds(x,top,cell,Spacing.LABEL_HEIGHT); 032 fields[i].bounds(x,top+Spacing.LABEL_HEIGHT+gap,cell,40); 033 } 034 apply.bounds(w-inset-cell,h-inset-Spacing.CONTROL_HEIGHT,cell,Spacing.CONTROL_HEIGHT); 035 } 036 } 037 private static final class CenterLabel extends Label { 038 CenterLabel(String text){super(text);size(13);} 039 @Override protected void paint(Painter p){p.text(text,ax+(w-p.textWidth(text,size))/2,ay+(h-size)/2,size,theme().muted);} 040 } 041}