Guest User

{passwords : ["rectangle", "circle"]}

a guest
Nov 23rd, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.57 KB | None | 0 0
  1. package com.example.LoginStuff;
  2.  
  3. import javax.servlet.annotation.WebServlet;
  4.  
  5. import com.vaadin.annotations.Theme;
  6. import com.vaadin.annotations.VaadinServletConfiguration;
  7. import com.vaadin.data.Property;
  8. import com.vaadin.data.Property.ValueChangeEvent;
  9. import com.vaadin.server.FontAwesome;
  10. import com.vaadin.server.VaadinRequest;
  11. import com.vaadin.server.VaadinServlet;
  12. import com.vaadin.shared.ui.label.ContentMode;
  13. import com.vaadin.shared.ui.slider.SliderOrientation;
  14. import com.vaadin.ui.Alignment;
  15. import com.vaadin.ui.Button;
  16. import com.vaadin.ui.Button.ClickEvent;
  17. import com.vaadin.ui.Button.ClickListener;
  18. import com.vaadin.ui.Component;
  19. import com.vaadin.ui.CssLayout;
  20. import com.vaadin.ui.HorizontalLayout;
  21. import com.vaadin.ui.Label;
  22. import com.vaadin.ui.PopupView;
  23. import com.vaadin.ui.ProgressBar;
  24. import com.vaadin.ui.Slider;
  25. import com.vaadin.ui.TextField;
  26. import com.vaadin.ui.UI;
  27. import com.vaadin.ui.VerticalLayout;
  28.  
  29.  
  30. @Theme("mytheme")
  31. public class MyUI extends UI {
  32.  
  33.     @Override
  34.     protected void init(VaadinRequest vaadinRequest) {
  35.         /*I start off defining the layouts */
  36.         final VerticalLayout layout = new VerticalLayout();
  37.         final CssLayout cssForBtns = new CssLayout(){
  38.             protected String getCss(Component c){
  39.                 if(c instanceof Button){
  40.                 return "color: blue; font-size:35px;align:center;text-align:center;padding:10px;margin:20px;margin-left:60px;";
  41.                 }
  42.                 else return "";
  43.             }
  44.            
  45.         };
  46.         cssForBtns.setWidth("300px");
  47.         cssForBtns.setHeight("150px"); /*
  48.         here end the layouts. Begin the form.
  49.         */
  50.         final TextField userName = new TextField();
  51.         userName.setCaption("Username:");
  52.         final TextField password = new TextField();
  53.         password.setCaption("Password:");/* here ends the form */
  54.        
  55.         /* button starts here */
  56.         password.setIcon(FontAwesome.LOCK);
  57.         userName.setIcon(FontAwesome.USER);
  58.         Button logInBtn = new Button("Log in");
  59.         logInBtn.setIcon(FontAwesome.LAPTOP);
  60.         logInBtn.setWidth("60%");
  61.         logInBtn.setHeight("50%");
  62.         logInBtn.addClickListener( e -> {
  63.             /* Here I'd make a method that actually checks whether the user is valid and stuff. So far, it only returns a warning that it's not. */
  64.             logInBtn.setEnabled(false);
  65.            
  66.             if(password.getValue().equals("rectangle")){
  67.                 layout.removeAllComponents();
  68.                 CssLayout lay = new CssLayout(){
  69.                     protected String getCss(Component c){
  70.                         return "background:green";
  71.                     }
  72.                
  73.                 };
  74.                 int w = 400;
  75.                 int h = 400;
  76.                 Label rect = new Label(" ",ContentMode.HTML);
  77.                 rect.setWidth(Integer.toString(w)+"px");
  78.                 rect.setHeight(Integer.toString(h)+"px");
  79.                 lay.addComponent(rect);
  80.                 Slider vertSlider = new Slider(1,500);
  81.                 vertSlider.setOrientation(SliderOrientation.VERTICAL);
  82.  
  83.                 vertSlider.addValueChangeListener(
  84.                         new Property.ValueChangeListener() {
  85.                         public void valueChange(ValueChangeEvent event) {
  86.                             double value = (Double) vertSlider.getValue();
  87.                           rect.setHeight(Double.toString(value)+"px");
  88.                            
  89.                         }
  90.                     });
  91.                
  92.                 Slider horiSlider = new Slider(1,500);
  93.                 horiSlider.addValueChangeListener(
  94.                         new Property.ValueChangeListener() {
  95.                         public void valueChange(ValueChangeEvent event) {
  96.                             double value = (Double) horiSlider.getValue();
  97.                           rect.setWidth(Double.toString(value)+"px");
  98.                            
  99.                         }
  100.                     });
  101.                 vertSlider.setHeight("400px");
  102.                 horiSlider.setWidth("400px");
  103.                 HorizontalLayout horiLay = new HorizontalLayout();
  104.                 horiLay.addComponents(lay,vertSlider);
  105.                 layout.addComponents(horiLay,horiSlider);
  106.                 layout.setComponentAlignment(horiLay, Alignment.TOP_CENTER);
  107.                 layout.setComponentAlignment(horiSlider, Alignment.TOP_CENTER);
  108.                
  109.             }
  110.             else if(password.getValue().equals("circle")){
  111.                 layout.removeAllComponents();
  112.                 TextField colorString = new TextField("","red");
  113.                 colorString.setCaption("enter a color and press enter:");
  114.                
  115.                 /*
  116.                  * checkbox1.addValueChangeListener(event -> // Java 8
  117.                     checkbox2.setValue(! checkbox1.getValue()));
  118.                  *
  119.                  * */
  120.                 Label someLabel = new Label(" ",ContentMode.HTML);
  121.                 someLabel.setWidth("400px");
  122.                 someLabel.setHeight("400px");
  123.                 String defaultColor = "#00FAFA";
  124.                 CssLayout forCircle = new CssLayout(){
  125.                     protected String getCss(Component c){
  126.                        
  127.                         return "border-radius:100%;background:"+defaultColor+";background:"+colorString.getValue()+";";
  128.                     }
  129.                    
  130.                 };
  131.                 forCircle.addComponents(someLabel);
  132.                 colorString.addValueChangeListener(event -> {//it HAS to be called event.
  133.                     forCircle.removeAllComponents();
  134.                     forCircle.addComponent(someLabel);
  135.                 });
  136.                
  137.                 VerticalLayout vertiLay = new VerticalLayout();
  138.                 vertiLay.addComponents(colorString,new Label("<br/><br/>",ContentMode.HTML),forCircle);
  139.                 vertiLay.setComponentAlignment(forCircle, Alignment.BOTTOM_CENTER);
  140.                 vertiLay.setComponentAlignment(colorString, Alignment.BOTTOM_CENTER);
  141.                 layout.addComponent(vertiLay);
  142.             }
  143.             else {
  144.                 layout.removeAllComponents();
  145.                 ProgressBar bar = new ProgressBar(0.0f);
  146.                 layout.addComponent(bar);
  147.                 layout.setComponentAlignment(bar, Alignment.TOP_CENTER);
  148.                 layout.addComponent(new Button("Next",
  149.                         new ClickListener() {
  150.                         @Override
  151.                         public void buttonClick(ClickEvent event) {
  152.                             float current = bar.getValue();
  153.                             if (current < 1.0f)
  154.                                 {bar.setValue(current + 0.34f);}
  155.                             if(bar.getValue()>= 1.0f) {/* No use of else so that the click that fills the bar activates this sequence */
  156.                                 layout.removeAllComponents();
  157.                                 Label warningSign = new Label("<br/><div style=\"color:red;text-align:center;\">We're sorry, your username doesn't exist.</div>",ContentMode.HTML);
  158.                                 layout.addComponent(warningSign);
  159.                                 layout.setComponentAlignment(warningSign, Alignment.BOTTOM_CENTER);
  160.                             }
  161.                         }
  162.                     }));
  163.                 layout.setComponentAlignment(layout.getComponent(1), Alignment.BOTTOM_CENTER);
  164.                 }
  165.            
  166.         });
  167.         cssForBtns.addComponent(logInBtn);
  168.         /* the button ends here */
  169.                
  170.         /* I start adding the stuff to the layout here. */
  171.         layout.addComponents(userName,password, cssForBtns);
  172.         layout.setComponentAlignment(userName, Alignment.MIDDLE_CENTER);
  173.         layout.setComponentAlignment(password, Alignment.MIDDLE_CENTER);
  174.         layout.setComponentAlignment(cssForBtns, Alignment.BOTTOM_CENTER);
  175.         setContent(layout);
  176.     }
  177.  
  178.     @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
  179.     @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
  180.     public static class MyUIServlet extends VaadinServlet {
  181.     }
  182. }
Add Comment
Please, Sign In to add comment