/** @see IDialogPage#createControl(Composite) */
  @Override
  public void createControl(Composite parent) {

    // Set up the composite to hold all the information
    sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
    sc.setLayout(new FillLayout());

    final Composite composite = new Composite(sc, SWT.NULL);
    composite.setLocation(-708, 1);
    composite.setLayout(new GridLayout(2, false));

    // Specify the expansion Adapter
    expansionAdapter =
        new ExpansionAdapter() {
          @Override
          public void expansionStateChanged(ExpansionEvent e) {
            // advanced options expanded, resize
            composite.layout();
            sc.notifyListeners(SWT.Resize, null);

            // force shell resize
            Point size;
            if (e.getState()) size = getShell().computeSize(550, 920);
            else size = getShell().computeSize(550, 400);

            getShell().setSize(size);
          }
        };

    Label lblNewLabel = new Label(composite, SWT.NONE);
    lblNewLabel.setText("&ICAT site ID");

    icatIDCombo = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
    GridData gd_icatIDCombo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_icatIDCombo.widthHint = 269;
    icatIDCombo.setLayoutData(gd_icatIDCombo);
    icatIDCombo.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            logger.debug("selection changed: " + icatIDCombo.getText());

            // change remaining parameters
            int index = icatIDCombo.getSelectionIndex();
            icatSiteNameText.setText(
                getToken(index, preferenceStore.getString("ICAT_NAME_PREF"), DELIMITER));
            txtSftpServer.setText(
                getToken(index, preferenceStore.getString("ICAT_SFTPSERVER_PREF"), DELIMITER));
            txtDirectory.setText(
                getToken(index, preferenceStore.getString("ICAT_DOWNLOADDIR_PREF"), DELIMITER));
          }
        });

    /*
     * populate wizard with current ICAT preferences
     */
    icatIDCombo.removeAll();

    String[] tokens = preferenceStore.getDefaultString("ICAT_ID_PREF").split(DELIMITER);

    for (int count = 0; count < tokens.length; count++) {
      icatIDCombo.add(tokens[count]);
    }
    icatIDCombo.select(0);

    int index = icatIDCombo.getSelectionIndex();

    Label lblicatDatabase = new Label(composite, SWT.NONE);
    lblicatDatabase.setText("&ICAT site name:");

    icatSiteNameText = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
    GridData gd_icatSiteNameText = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_icatSiteNameText.widthHint = 260;
    icatSiteNameText.setLayoutData(gd_icatSiteNameText);
    icatSiteNameText.setEditable(false);
    icatSiteNameText.setText(
        getToken(index, preferenceStore.getString("ICAT_NAME_PREF"), DELIMITER));
    icatSiteNameText.setBounds(113, 53, 212, 27);

    Label fedidLbl = new Label(composite, SWT.NONE);
    fedidLbl.setText("&FedId:");

    txtFedid = new Text(composite, SWT.BORDER);
    GridData gd_txtFedid = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_txtFedid.widthHint = 260;
    txtFedid.setLayoutData(gd_txtFedid);
    txtFedid.setText(initFedid);
    txtFedid.addKeyListener(this);

    Label passwordLbl = new Label(composite, SWT.NONE);
    passwordLbl.setBounds(4, 139, 64, 13);
    passwordLbl.setText("&Password:"******"&Project name:");
    txtProject = new Text(composite, SWT.BORDER);
    GridData gd_txtProject = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_txtProject.widthHint = 260;
    txtProject.setLayoutData(gd_txtProject);
    txtProject.setBounds(113, 173, 212, 27);
    txtProject.setText(initProject);
    txtProject.addKeyListener(this);
    sc.setContent(composite);
    sc.setExpandVertical(true);
    sc.setExpandHorizontal(true);

    new Label(composite, SWT.NONE);
    new Label(composite, SWT.NONE);

    /** set-up advanced options GUI elements */
    advancedOptionsExpander = new ExpandableComposite(composite, SWT.NONE);
    GridData gd_advancedOptionsExpander = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
    gd_advancedOptionsExpander.widthHint = 242;
    advancedOptionsExpander.setLayoutData(gd_advancedOptionsExpander);
    advancedOptionsExpander.setLayout(new GridLayout(2, false));
    advancedOptionsExpander.setText("Advanced Options");
    advancedOptionsExpander.setExpanded(false);

    Composite optionsComposite = new Composite(advancedOptionsExpander, SWT.NONE);
    optionsComposite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
    optionsComposite.setLayout(new GridLayout(3, false));

    Label sftpServerLbl = new Label(optionsComposite, SWT.NONE);
    sftpServerLbl.setText("&SFTP server:");

    txtSftpServer = new Text(optionsComposite, SWT.BORDER);
    GridData gd_txtSftpServer = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_txtSftpServer.widthHint = 193;
    txtSftpServer.setLayoutData(gd_txtSftpServer);
    txtSftpServer.setEditable(true);
    txtSftpServer.setText(
        getToken(index, preferenceStore.getString("ICAT_SFTPSERVER_PREF"), DELIMITER));

    final Button btnSftpTest = new Button(optionsComposite, SWT.NONE);
    GridData gd_btnSftpTest = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_btnSftpTest.widthHint = 100;
    btnSftpTest.setLayoutData(gd_btnSftpTest);
    btnSftpTest.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            /*
             * test server using ping
             */
            if (NetworkUtils.isReachable(txtSftpServer.getText())) {
              Image okImage = (ResourceManager.getPluginImage(ICAT_PLUGIN_ID, "icons/ok.png"));
              ;
              btnSftpTest.setImage(okImage);
            } else {
              Image noImage = (ResourceManager.getPluginImage(ICAT_PLUGIN_ID, "icons/no.png"));
              ;
              btnSftpTest.setImage(noImage);
            }
          }
        });
    btnSftpTest.setText("Test");

    txtSftpServer.addModifyListener(
        new ModifyListener() {
          @Override
          public void modifyText(ModifyEvent e) {
            btnSftpTest.setImage(null);
          }
        });

    Label lbldownloadDirectory = new Label(optionsComposite, SWT.NULL);
    lbldownloadDirectory.setText("&Download directory:");

    txtDirectory = new Text(optionsComposite, SWT.BORDER);
    GridData gd_txtDirectory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_txtDirectory.widthHint = 193;
    txtDirectory.setLayoutData(gd_txtDirectory);
    txtDirectory.setEditable(true);
    txtDirectory.setEnabled(true);

    txtDirectory.setText(
        getToken(index, preferenceStore.getString("ICAT_DOWNLOADDIR_PREF"), DELIMITER));

    Button BtnBrowseDirectory = new Button(optionsComposite, SWT.PUSH);
    GridData gd_BtnBrowseDirectory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_BtnBrowseDirectory.widthHint = 100;
    BtnBrowseDirectory.setLayoutData(gd_BtnBrowseDirectory);
    BtnBrowseDirectory.setText("Browse...");
    BtnBrowseDirectory.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            handleDownloadDirBrowse();
          }
        });

    Label lbltruststorePath = new Label(optionsComposite, SWT.NONE);
    lbltruststorePath.setBounds(10, 131, 103, 19);
    lbltruststorePath.setText("&Truststore path:");

    txtTruststore = new Text(optionsComposite, SWT.BORDER);
    txtTruststore.setText(
        getToken(index, preferenceStore.getString("ICAT_TRUSTSTORE_PATH_PREF"), DELIMITER));
    GridData gd_txtTruststore = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_txtTruststore.widthHint = 193;
    txtTruststore.setLayoutData(gd_txtTruststore);
    txtTruststore.setEnabled(true);
    txtTruststore.setEditable(true);

    Button BtnBrowseTruststore = new Button(optionsComposite, SWT.PUSH);
    GridData gd_BtnBrowseTruststore = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_BtnBrowseTruststore.widthHint = 100;
    BtnBrowseTruststore.setLayoutData(gd_BtnBrowseTruststore);
    BtnBrowseTruststore.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            handleTruststoreBrowse();
          }
        });
    BtnBrowseTruststore.setText("Browse...");

    Label lbltruststorePassword = new Label(optionsComposite, SWT.NONE);
    lbltruststorePassword.setText("&Truststore password:"******"ICAT_TRUSTSTORE_PASS_PREF"), DELIMITER));
    GridData gd_txtTruststorePassword = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_txtTruststorePassword.widthHint = 193;
    txtTruststorePassword.setLayoutData(gd_txtTruststorePassword);
    txtTruststorePassword.setEnabled(true);
    txtTruststorePassword.addKeyListener(this);

    final Button btnShowPassword = new Button(optionsComposite, SWT.CHECK);
    btnShowPassword.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            if (btnShowPassword.getSelection()) {
              txtTruststorePassword.setEchoChar((char) 0);
            } else {
              txtTruststorePassword.setEchoChar('*');
            }
          }
        });
    btnShowPassword.setText("Show password");

    // set default FROM date to current date
    Calendar calA = Calendar.getInstance();

    Label lblFrom = new Label(optionsComposite, SWT.TOP);
    lblFrom.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
    lblFrom.setText("Visit StartDate from:");
    dateFrom = new DateTime(optionsComposite, SWT.NONE | SWT.CALENDAR | SWT.DROP_DOWN);
    dateFrom.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
    dateFrom.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
    GridData gd_dateFrom = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
    gd_dateFrom.widthHint = 193;
    dateFrom.setLayoutData(gd_dateFrom);
    dateFrom.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            // set from date
            setSqlFromDate(dateFrom);
          }
        });

    dateFrom.setYear(2006); // calA.get(Calendar.YEAR));
    dateFrom.setMonth(calA.get(Calendar.MONTH));
    dateFrom.setDay(calA.get(Calendar.DAY_OF_YEAR));
    setSqlFromDate(dateFrom);

    Label lblTo = new Label(optionsComposite, SWT.TOP);
    lblTo.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
    lblTo.setText("Visit StartDate to:");
    dateTo = new DateTime(optionsComposite, SWT.NONE | SWT.CALENDAR | SWT.DROP_DOWN);
    dateTo.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
    dateTo.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
    GridData gd_dateTo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
    gd_dateTo.widthHint = 193;
    dateTo.setLayoutData(gd_dateTo);
    dateTo.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            // set to date
            setSqlToDate(dateTo);
          }
        });
    // end with current date + 1 month
    dateTo.setYear(calA.get(Calendar.YEAR));
    dateTo.setMonth(calA.get(Calendar.MONTH) + 1);
    dateTo.setDay(calA.get(Calendar.DAY_OF_YEAR));
    setSqlToDate(dateTo);

    advancedOptionsExpander.setClient(optionsComposite);
    new Label(optionsComposite, SWT.NONE);
    new Label(optionsComposite, SWT.NONE);
    new Label(optionsComposite, SWT.NONE);
    advancedOptionsExpander.addExpansionListener(expansionAdapter);
    setControl(composite);

    dialogChanged();

    new Label(composite, SWT.NONE);
    new Label(composite, SWT.NONE);
  }
  @SuppressWarnings("unchecked")
  public void prepareForOpen() {
    Point contentsSize = contents.getSize();
    Point titleSize = new Point(0, 0);

    boolean showTitle = ((style & (SWT.CLOSE | SWT.TITLE)) != 0);
    if (showTitle) {
      if (titleLabel == null) {
        titleLabel = new Label(shell, SWT.NONE);
        titleLabel.setBackground(shell.getBackground());
        titleLabel.setForeground(shell.getForeground());
        FontData[] fds = shell.getFont().getFontData();
        for (int i = 0; i < fds.length; i++) {
          fds[i].setStyle(fds[i].getStyle() | SWT.BOLD);
        }
        final Font font = new Font(shell.getDisplay(), fds);
        titleLabel.addListener(
            SWT.Dispose,
            new Listener() {
              @Override
              public void handleEvent(Event event) {
                font.dispose();
              }
            });
        titleLabel.setFont(font);
        selectionControls.add(titleLabel);
      }
      String titleText = shell.getText();
      titleLabel.setText(titleText == null ? "" : titleText);
      titleLabel.pack();
      titleSize = titleLabel.getSize();

      final Image titleImage = shell.getImage();
      if (titleImageLabel == null && shell.getImage() != null) {
        titleImageLabel = new Canvas(shell, SWT.NONE);
        titleImageLabel.setBackground(shell.getBackground());
        titleImageLabel.setBounds(titleImage.getBounds());
        titleImageLabel.addListener(
            SWT.Paint,
            new Listener() {
              @Override
              public void handleEvent(Event event) {
                event.gc.drawImage(titleImage, 0, 0);
              }
            });
        Point tilSize = titleImageLabel.getSize();
        titleSize.x += tilSize.x + titleWidgetSpacing;
        if (tilSize.y > titleSize.y) titleSize.y = tilSize.y;
        selectionControls.add(titleImageLabel);
      }

      if (systemControlsBar == null && (style & SWT.CLOSE) != 0) {
        // Color closeFG = shell.getForeground(), closeBG = shell.getBackground();
        // Color closeFG = shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY), closeBG =
        // shell.getBackground();
        Color closeFG = shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND),
            closeBG = shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
        final Image closeImage = createCloseImage(shell.getDisplay(), closeBG, closeFG);
        shell.addListener(
            SWT.Dispose,
            new Listener() {
              @Override
              public void handleEvent(Event event) {
                closeImage.dispose();
              }
            });
        systemControlsBar = new ToolBar(shell, SWT.FLAT);
        systemControlsBar.setBackground(closeBG);
        systemControlsBar.setForeground(closeFG);
        ToolItem closeItem = new ToolItem(systemControlsBar, SWT.PUSH);
        closeItem.setImage(closeImage);
        closeItem.addListener(
            SWT.Selection,
            new Listener() {
              @Override
              public void handleEvent(Event event) {
                shell.close();
              }
            });
        systemControlsBar.pack();
        Point closeSize = systemControlsBar.getSize();
        titleSize.x += closeSize.x + titleWidgetSpacing;
        if (closeSize.y > titleSize.y) titleSize.y = closeSize.y;
      }

      titleSize.y += titleSpacing;
      if (titleSize.x > contentsSize.x) {
        contentsSize.x = titleSize.x;
        contents.setSize(contentsSize.x, contentsSize.y);
      }
      contentsSize.y += titleSize.y;
    }

    Rectangle screen = shell.getDisplay().getClientArea();

    int anchor = preferredAnchor;
    if (anchor != SWT.NONE && autoAnchor && locX != Integer.MIN_VALUE) {
      if ((anchor & SWT.LEFT) != 0) {
        if (locX + contentsSize.x + marginLeft + marginRight - 16 >= screen.x + screen.width)
          anchor = anchor - SWT.LEFT + SWT.RIGHT;
      } else // RIGHT
      {
        if (locX - contentsSize.x - marginLeft - marginRight + 16 < screen.x)
          anchor = anchor - SWT.RIGHT + SWT.LEFT;
      }
      if ((anchor & SWT.TOP) != 0) {
        if (locY + contentsSize.y + 20 + marginTop + marginBottom >= screen.y + screen.height)
          anchor = anchor - SWT.TOP + SWT.BOTTOM;
      } else // BOTTOM
      {
        if (locY - contentsSize.y - 20 - marginTop - marginBottom < screen.y)
          anchor = anchor - SWT.BOTTOM + SWT.TOP;
      }
    }

    final Point shellSize =
        (anchor == SWT.NONE)
            ? new Point(
                contentsSize.x + marginLeft + marginRight,
                contentsSize.y + marginTop + marginBottom)
            : new Point(
                contentsSize.x + marginLeft + marginRight,
                contentsSize.y + marginTop + marginBottom + 20);

    if (shellSize.x < 54 + marginLeft + marginRight) shellSize.x = 54 + marginLeft + marginRight;
    if (anchor == SWT.NONE) {
      if (shellSize.y < 10 + marginTop + marginBottom) shellSize.y = 10 + marginTop + marginBottom;
    } else {
      if (shellSize.y < 30 + marginTop + marginBottom) shellSize.y = 30 + marginTop + marginBottom;
    }

    shell.setSize(shellSize);
    int titleLocY = marginTop + (((anchor & SWT.TOP) != 0) ? 20 : 0);
    contents.setLocation(marginLeft, titleSize.y + titleLocY);
    if (showTitle) {
      int realTitleHeight = titleSize.y - titleSpacing;
      if (titleImageLabel != null) {
        titleImageLabel.setLocation(
            marginLeft, titleLocY + (realTitleHeight - titleImageLabel.getSize().y) / 2);
        titleLabel.setLocation(
            marginLeft + titleImageLabel.getSize().x + titleWidgetSpacing,
            titleLocY + (realTitleHeight - titleLabel.getSize().y) / 2);
      } else
        titleLabel.setLocation(
            marginLeft, titleLocY + (realTitleHeight - titleLabel.getSize().y) / 2);
      if (systemControlsBar != null)
        systemControlsBar.setLocation(
            shellSize.x - marginRight - systemControlsBar.getSize().x,
            titleLocY + (realTitleHeight - systemControlsBar.getSize().y) / 2);
    }

    final Region region = new Region();
    region.add(createOutline(shellSize, anchor, true));

    shell.setRegion(region);
    shell.addListener(
        SWT.Dispose,
        new Listener() {
          @Override
          public void handleEvent(Event event) {
            region.dispose();
          }
        });

    final int[] outline = createOutline(shellSize, anchor, false);
    shell.addListener(
        SWT.Paint,
        new Listener() {
          @Override
          public void handleEvent(Event event) {
            event.gc.drawPolygon(outline);
          }
        });

    if (locX != Integer.MIN_VALUE) {
      Point shellLoc = new Point(locX, locY);
      if ((anchor & SWT.BOTTOM) != 0) shellLoc.y = shellLoc.y - shellSize.y + 1;
      if ((anchor & SWT.LEFT) != 0) shellLoc.x -= 15;
      else if ((anchor & SWT.RIGHT) != 0) shellLoc.x = shellLoc.x - shellSize.x + 16;

      if (autoAnchor) {
        if (shellLoc.x < screen.x) shellLoc.x = screen.x;
        else if (shellLoc.x > screen.x + screen.width - shellSize.x)
          shellLoc.x = screen.x + screen.width - shellSize.x;

        if (anchor == SWT.NONE) {
          if (shellLoc.y < screen.y) shellLoc.y = screen.y;
          else if (shellLoc.y > screen.y + screen.height - shellSize.y)
            shellLoc.y = screen.y + screen.height - shellSize.y;
        }
      }

      shell.setLocation(shellLoc);
    }
  }
Exemple #3
0
  protected void createContents() {
    shlPonyTrivia = new Shell();
    shlPonyTrivia.setBackground(SWTResourceManager.getColor(255, 240, 245));
    shlPonyTrivia.setSize(800, 600);
    shlPonyTrivia.setText("Pony Trivia");
    shlPonyTrivia.setLayout(new FormLayout());
    shlPonyTrivia.setMinimumSize(shlPonyTrivia.getSize());

    final Label lblFlower = new Label(shlPonyTrivia, SWT.NONE);
    lblFlower.setBackground(SWTResourceManager.getColor(255, 240, 245));
    FormData fd_lblFlower = new FormData();
    fd_lblFlower.left = new FormAttachment(0, 10);
    lblFlower.setLayoutData(fd_lblFlower);
    lblFlower.setImage(
        SWTResourceManager.getImage(GameScreen.class, "/ponytrivia/gui/res/flower.gif"));

    final Label lblDevil = new Label(shlPonyTrivia, SWT.NONE);
    lblDevil.setBackground(SWTResourceManager.getColor(255, 240, 245));
    FormData fd_lblDevil = new FormData();
    fd_lblDevil.right = new FormAttachment(100, -10);
    fd_lblDevil.top = new FormAttachment(lblFlower, 0, SWT.TOP);
    lblDevil.setLayoutData(fd_lblDevil);
    lblDevil.setImage(
        SWTResourceManager.getImage(GameScreen.class, "/ponytrivia/gui/res/hell_boy.gif"));

    Composite composite = new Composite(shlPonyTrivia, SWT.NONE);
    composite.setBackground(SWTResourceManager.getColor(255, 240, 245));
    composite.setLocation(10, -227);
    composite.setLayout(new FormLayout());
    FormData fd_composite = new FormData();
    fd_composite.bottom = new FormAttachment(100, -10);
    fd_composite.left = new FormAttachment(0, 10);
    fd_composite.right = new FormAttachment(100, -10);
    composite.setLayoutData(fd_composite);

    final Button btnNext = new Button(composite, SWT.NONE);
    btnNext.setEnabled(false);
    FormData fd_btnNext = new FormData();
    fd_btnNext.right = new FormAttachment(100, -10);
    btnNext.setLayoutData(fd_btnNext);
    btnNext.setText("Next");

    final Button btnFiftyFifty = new Button(composite, SWT.NONE);
    fd_btnNext.left = new FormAttachment(btnFiftyFifty, 0, SWT.LEFT);
    btnFiftyFifty.setToolTipText("");
    FormData fd_btnFiftyFifty = new FormData();
    fd_btnFiftyFifty.right = new FormAttachment(100, -10);
    btnFiftyFifty.setLayoutData(fd_btnFiftyFifty);
    btnFiftyFifty.setImage(
        SWTResourceManager.getImage(GameScreen.class, "/ponytrivia/gui/res/lifebelt.gif"));

    Composite composite_1 = new Composite(composite, SWT.NONE);
    composite_1.setBackground(SWTResourceManager.getColor(255, 240, 245));
    fd_btnNext.top = new FormAttachment(composite_1, 4);
    fd_btnFiftyFifty.bottom = new FormAttachment(composite_1, -6);
    composite_1.setLayout(new FillLayout(SWT.VERTICAL));
    FormData fd_composite_1 = new FormData();
    fd_composite_1.left = new FormAttachment(0, 10);
    fd_composite_1.right = new FormAttachment(100, -10);
    fd_composite_1.top = new FormAttachment(0, 90);
    fd_composite_1.bottom = new FormAttachment(100, -44);
    composite_1.setLayoutData(fd_composite_1);

    SelectionAdapter enableNext =
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent arg0) {
            if (shlPonyTrivia.isDisposed()) {
              return;
            }
            btnNext.setEnabled(true);
          }
        };

    final Button btnAnswer_1 = new Button(composite_1, SWT.RADIO);
    btnAnswer_1.addSelectionListener(enableNext);
    btnAnswer_1.setText("Answer 1");
    btnAnswer_1.setBackground(SWTResourceManager.getColor(255, 240, 245));

    final Button btnAnswer_2 = new Button(composite_1, SWT.RADIO);
    btnAnswer_2.addSelectionListener(enableNext);
    btnAnswer_2.setText("Answer 2");
    btnAnswer_2.setBackground(SWTResourceManager.getColor(255, 240, 245));

    final Button btnAnswer_3 = new Button(composite_1, SWT.RADIO);
    btnAnswer_3.addSelectionListener(enableNext);
    btnAnswer_3.setText("Answer 3");
    btnAnswer_3.setBackground(SWTResourceManager.getColor(255, 240, 245));

    final Button btnAnswer_4 = new Button(composite_1, SWT.RADIO);
    btnAnswer_4.addSelectionListener(enableNext);
    btnAnswer_4.setText("Answer 4");
    btnAnswer_4.setBackground(SWTResourceManager.getColor(255, 240, 245));

    Composite composite_2 = new Composite(composite, SWT.NONE);
    FormData fd_composite_2 = new FormData();
    fd_composite_2.right = new FormAttachment(btnFiftyFifty, -6);
    fd_composite_2.bottom = new FormAttachment(0, 84);
    fd_composite_2.top = new FormAttachment(0, 10);
    fd_composite_2.left = new FormAttachment(0, 10);
    composite_2.setLayoutData(fd_composite_2);
    composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));

    final Label lblQuestionText = new Label(composite_2, SWT.NONE);
    lblQuestionText.setBackground(SWTResourceManager.getColor(255, 240, 245));
    lblQuestionText.setText("Question Text");

    Composite composite_3 = new Composite(shlPonyTrivia, SWT.NONE);
    composite_3.setBackground(SWTResourceManager.getColor(255, 240, 245));
    fd_lblFlower.top = new FormAttachment(composite_3, 37);
    composite_3.setLayout(new FormLayout());
    FormData fd_composite_3 = new FormData();
    fd_composite_3.left = new FormAttachment(0);
    fd_composite_3.right = new FormAttachment(100);
    fd_composite_3.top = new FormAttachment(0, 10);
    fd_composite_3.bottom = new FormAttachment(0, 51);
    composite_3.setLayoutData(fd_composite_3);

    final Label lblTime = new Label(composite_3, SWT.NONE);
    lblTime.setBackground(SWTResourceManager.getColor(255, 240, 245));
    FormData fd_lblTime = new FormData();
    fd_lblTime.top = new FormAttachment(0, 10);
    fd_lblTime.left = new FormAttachment(0, 10);
    lblTime.setLayoutData(fd_lblTime);
    lblTime.setText("Remaining Time: 30");

    final Label lblScore = new Label(composite_3, SWT.NONE);
    lblScore.setBackground(SWTResourceManager.getColor(255, 240, 245));
    FormData fd_lblScore = new FormData();
    fd_lblScore.left = new FormAttachment(100, -89);
    fd_lblScore.top = new FormAttachment(lblTime, 0, SWT.TOP);
    fd_lblScore.right = new FormAttachment(100, -10);
    lblScore.setLayoutData(fd_lblScore);
    lblScore.setText("Score: 0");

    final Composite composite_4 = new Composite(shlPonyTrivia, SWT.NONE);
    composite_4.setBackground(SWTResourceManager.getColor(255, 240, 245));
    fd_composite.top = new FormAttachment(0, 222);
    composite_4.setLayout(null);
    FormData fd_composite_4 = new FormData();
    fd_composite_4.bottom = new FormAttachment(composite, -6);
    fd_composite_4.top = new FormAttachment(composite_3, 6);
    fd_composite_4.right = new FormAttachment(lblDevil, -6);
    fd_composite_4.left = new FormAttachment(lblFlower, 6);
    composite_4.setLayoutData(fd_composite_4);

    final Label lblGrass = new Label(composite_4, SWT.NONE);
    lblGrass.setBounds(10, 129, 589, 20);

    final Label lblPony = new Label(composite_4, SWT.NONE);
    lblPony.setBackground(SWTResourceManager.getColor(255, 240, 245));
    lblPony.setBounds(260, 37, 84, 86);
    lblPony.setAlignment(SWT.CENTER);
    lblPony.setImage(imgKitty1);
    lblGrass.setBackground(SWTResourceManager.getColor(0, 204, 102));

    ////////////////////////////////////////////////////////////////////////////////// 0/////////

    final Button answerButtons[] =
        new Button[] {btnAnswer_1, btnAnswer_2, btnAnswer_3, btnAnswer_4};

    composite_4.addListener(
        SWT.Resize,
        new Listener() {
          public void handleEvent(Event e) {
            if (shlPonyTrivia.isDisposed()) {
              return;
            }

            Rectangle r = lblGrass.getBounds();
            r.width = composite_4.getBounds().width - 6;
            lblGrass.setBounds(r);

            int x =
                (int)
                    ((lblGrass.getBounds().width / 2)
                        * (1 - ((double) gameState.pony_pos) / config.questions_to_win));
            lblPony.setLocation(x - lblPony.getBounds().width / 2, lblPony.getLocation().y);
          }
        });

    final Runnable updateTimeLabel =
        new Runnable() {
          private Color original = lblTime.getForeground();

          @Override
          public void run() {
            if (shlPonyTrivia.isDisposed()) {
              return;
            }
            lblTime.setText("Remaining time: " + Math.max(0, gameState.remaining_time));
            if (gameState.remaining_time <= config.alotted_time / 3) {
              lblTime.setForeground(display.getSystemColor(SWT.COLOR_RED));
            } else {
              lblTime.setForeground(original);
            }
          }
        };
    updateTimeLabel.run();

    final Color origButtonColor = btnAnswer_1.getBackground();

    final Runnable updateQuestion =
        new Runnable() {
          @Override
          public void run() {
            if (shlPonyTrivia.isDisposed()) {
              return;
            }
            QuestionInfo qi;
            qi = questionRegistry.getQuestion();
            lblQuestionText.setText(qi.questionText);
            for (int i = 0; i < answerButtons.length; i++) {
              Button btn = answerButtons[i];
              btn.setText(qi.answers.get(i));
              btn.setEnabled(true);
              btn.setBackground(origButtonColor);
              btn.setSelection(false);
            }
            gameState.correctAnswerIndex = qi.correctAnswerIndex;
          }
        };
    updateQuestion.run();

    final SelectionAdapter answerQuestion =
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent arg0) {
            if (shlPonyTrivia.isDisposed()) {
              return;
            }

            int delta = lblGrass.getBounds().width / (2 * config.questions_to_win);
            int timeout = 1000;
            gameState.enabled = false;
            btnNext.setEnabled(false);

            final Button correct = answerButtons[gameState.correctAnswerIndex];

            if (correct.getSelection()) {
              gameState.total_score +=
                  10 + (gameState.remaining_time < 0 ? 0 : gameState.remaining_time);
              delta = -delta;
              correct.setBackground(new Color(display, 150, 250, 150));
              lblPony.setImage(imgKitty1);
              gameState.pony_pos += 1;
            } else {
              gameState.total_score -= 10;
              correct.setBackground(new Color(display, 250, 150, 150));
              timeout = 1500;
              lblPony.setImage(imgKitty2);
              gameState.pony_pos -= 1;
            }
            if (gameState.total_score < 0) {
              gameState.total_score = 0;
            }
            lblScore.setText("Score: " + gameState.total_score);
            gameState.question_number += 1;

            gameState.turnsBeforeEnablingFiftyFifty -= 1;

            class AnimatePony implements Runnable {
              private int delta;
              private int cnt;
              private int timeout;
              private final int steps = 20;
              private Point origPony;
              private Point origFlower;
              private Point origDevil;

              public AnimatePony(int delta, int timeout) {
                this.delta = delta;
                this.timeout = timeout;
                cnt = 0;
                origPony = lblPony.getLocation();
                origFlower = lblFlower.getLocation();
                origDevil = lblDevil.getLocation();
              }

              @Override
              public void run() {
                cnt++;
                Point p = lblPony.getLocation();
                double height = Math.abs(Math.sin(((double) cnt / steps) * 3 * Math.PI));
                lblPony.setLocation(p.x + delta / steps, origPony.y - (int) (10 * height));

                if (delta < 0) {
                  lblFlower.setLocation(
                      origFlower.x,
                      origFlower.y - (int) (10 * Math.sin((double) cnt / steps * Math.PI)));
                } else {
                  lblDevil.setLocation(
                      origDevil.x,
                      origDevil.y - (int) (10 * Math.sin((double) cnt / steps * Math.PI)));
                }

                if (cnt < steps) {
                  display.timerExec(timeout / steps, this);
                }
              }
            }

            display.timerExec(0, new AnimatePony(delta, (int) (timeout * 0.9)));

            display.timerExec(
                timeout,
                new Runnable() {
                  @Override
                  public void run() {
                    if (gameState.pony_pos >= config.questions_to_win) {
                      shlPonyTrivia.setEnabled(false);

                      try {
                        insertHighscore.insert(config.playerId, gameState.total_score);
                        schema.commit();
                      } catch (SQLException e) {
                        e.printStackTrace();
                      }

                      WinScreen.run(display);
                      shlPonyTrivia.close();
                      return;
                    }
                    if (gameState.pony_pos <= -config.questions_to_win) {
                      shlPonyTrivia.setEnabled(false);

                      try {
                        insertHighscore.insert(config.playerId, gameState.total_score);
                        schema.commit();
                      } catch (SQLException e) {
                        e.printStackTrace();
                      }

                      LoseScreen.run(display);
                      shlPonyTrivia.close();
                      return;
                    }
                    if (gameState.turnsBeforeEnablingFiftyFifty <= 0) {
                      gameState.turnsBeforeEnablingFiftyFifty = 0;
                      btnFiftyFifty.setEnabled(true);
                    }
                    updateTimeLabel.run();
                    updateQuestion.run();
                    gameState.remaining_time = config.alotted_time;
                    gameState.enabled = true;
                  }
                });
          }
        };

    btnFiftyFifty.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent arg0) {
            if (shlPonyTrivia.isDisposed()) {
              return;
            }
            btnFiftyFifty.setEnabled(false);
            gameState.turnsBeforeEnablingFiftyFifty = config.initalTurnsForFiftyFifty;
            ArrayList<Integer> discarded = new ArrayList<Integer>();
            discarded.add(0);
            discarded.add(1);
            discarded.add(2);
            discarded.add(3);
            discarded.remove(gameState.correctAnswerIndex);
            Collections.shuffle(discarded);
            discarded.remove(2);
            for (int i : discarded) {
              Button btn = answerButtons[i];
              btn.setEnabled(false);
              btn.setSelection(false);
              btn.setBackground(new Color(display, 50, 50, 50));
            }
          }
        });

    final Runnable timer =
        new Runnable() {
          public void run() {
            if (shlPonyTrivia.isDisposed()) {
              return;
            }
            gameState.remaining_time -= 1;
            display.timerExec(1000, this);
            updateTimeLabel.run();
            if (gameState.remaining_time <= 0 && gameState.enabled) {
              for (Button btn : answerButtons) {
                btn.setSelection(false);
              }
              answerQuestion.widgetSelected(null);
            }
          }
        };
    display.timerExec(1000, timer);

    btnNext.addSelectionListener(answerQuestion);

    bgMusicThread.setDaemon(true);
    bgMusicThread.start();
  }