@Override
 public void onWindowShown() {
   if (!Driver.CurrentUser.getAdministrator()) {
     ManagementButton.SetParent(null);
   } else {
     ManagementButton.SetParent(leftPanel);
     for (Frame i : ScrollList) {
       i.GetChild("Divider").SetPosition(new DPair(1, -99, 0, 2));
       i.GetChild("TopText").SetSize(new DPair(1, -105, .5, -3));
       i.GetChild("BottomText").SetSize(i.GetChild("TopText").GetSize());
       i.GetChild("Action2").SetVisible(true);
     }
   }
   AccountCredit.SetText(String.format("Credit: $%.2f", Driver.CurrentUser.getCredit()));
   LastCurrentListValue = -1;
   SetFrames();
 }
 @Override
 public void ButtonClicked(GuiObject button, int x, int y) {
   switch (button.GetName()) {
     case "LibraryButton":
       CurrentList = 0;
       break;
     case "StoreButton":
       CurrentList = 4;
       break;
     case "ViewAlbums":
       CurrentList = 1;
       break;
     case "ViewAudiobooks":
       CurrentList = 2;
       break;
     case "ViewFilms":
       CurrentList = 3;
       break;
     case "ManagementButton":
       currentUser = Driver.CurrentUser;
       if (currentUser.getAdministrator()) {
         Driver.SetFrame("Management");
       } else {
         JOptionPane.showMessageDialog(
             null, "Access denied - Admin only", "", JOptionPane.WARNING_MESSAGE);
       }
       break;
     case "Action":
       // "Buy" or "Play" button pressed.
       if (button instanceof TextButton
           && ((TextButton) button).GetText().equals("Buy")
           && CurrentList >= 1
           && CurrentList <= 3) {
         int id = GetItemIdOfFrame((Frame) button.GetParent());
         System.out.println("Pressed buy for item: " + id);
         boolean success = Driver.CurrentUser.purchaseItem(id);
         if (success) {
           ((TextButton) button).SetText("Play");
         } else {
           System.out.println("Buy failed.");
         }
       } else if (button instanceof TextButton
           && ((TextButton) button).GetText().equals("Play")
           && CurrentList == 0) {
         int id = GetItemIdOfFrame((Frame) button.GetParent());
         if (CurrentPlayingItem != null) {
           CurrentPlayingItem.stopAudio();
         }
         CurrentPlayingItem = DataLoader.getItemById(id);
         CurrentPlayingItem.playAudioFull();
       } else if (button instanceof TextButton
           && ((TextButton) button).GetText().equals("Stop")
           && CurrentPlayingItem != null) {
         CurrentPlayingItem.stopAudio();
         CurrentPlayingItem = null;
       } else if (button instanceof TextButton
           && ((TextButton) button).GetText().equals("Play")
           && CurrentList >= 1
           && CurrentList <= 3) {
         int id = GetItemIdOfFrame((Frame) button.GetParent());
         CurrentPlayingItem = DataLoader.getItemById(id);
         CurrentPlayingItem.playAudioPreview();
       }
       AccountCredit.SetText(String.format("Credit: $%.2f", Driver.CurrentUser.getCredit()));
       break;
     case "AccountCredit":
       MainFrame.AddChild(CreditAdd);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "CreditAdd":
       double amount = 0;
       try {
         amount = Double.parseDouble(((TextBox) CreditAdd.GetChild("Input")).GetText());
       } catch (Exception e) {
       }
       Driver.CurrentUser.grantCredit(amount);
       MainFrame.RemoveChild(CreditAdd);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "Input":
       if (button instanceof TextBox && ((TextBox) button).GetText().equals("Credit")) {
         ((TextBox) button).SetText("");
       }
       break;
     case "Action2":
       if (button instanceof TextButton
           && ((TextButton) button).GetText().equals("Edit")
           && CurrentList >= 0
           && CurrentList <= 4) {
         int id = GetItemIdOfFrame((Frame) button.GetParent());
         SetModify(id);
       } else if (button instanceof TextButton
           && ((TextButton) button).GetText().equals("Rate")
           && CurrentList == 0) {
         CurrentRatingId = GetItemIdOfFrame((Frame) button.GetParent());
         RatingWindow.SetParent(MainFrame);
         RatingWindow.SetPosition(new DPair(0, x - RatingWindow.GetSize().xOffset, 0, y));
         Driver.GetGuiMain().GetTextBoxes();
       }
       break;
     case "HiddenInput":
       ((CheckBox) button).SetText(((CheckBox) button).GetSelected() ? "true" : "false");
       break;
     case "SubmitChanges":
       Item current = DataLoader.getItemById(CurrentModifyWindowId);
       if (current != null) {
         current.setCreator(((TextBox) ModifyItem.GetChild("ArtistInput")).GetText());
         current.setName(((TextBox) ModifyItem.GetChild("TitleInput")).GetText());
         try {
           current.setYearOfRelease(
               Integer.parseInt(((TextBox) ModifyItem.GetChild("YearInput")).GetText()));
         } catch (Exception e) {
         }
         current.setGenre(((TextBox) ModifyItem.GetChild("GenreInput")).GetText());
         try {
           current.setPrice(
               Double.parseDouble(((TextBox) ModifyItem.GetChild("CostInput")).GetText()));
         } catch (Exception e) {
         }
         int d = 0;
         try {
           d +=
               Integer.parseInt(((TextBox) ModifyItem.GetChild("DurationInputH")).GetText())
                   * 3600;
         } catch (Exception e) {
         }
         try {
           d += Integer.parseInt(((TextBox) ModifyItem.GetChild("DurationInputM")).GetText()) * 60;
         } catch (Exception e) {
         }
         try {
           d += Integer.parseInt(((TextBox) ModifyItem.GetChild("DurationInputS")).GetText());
         } catch (Exception e) {
         }
         current.setDuration(d);
         current.setPreview(((TextBox) ModifyItem.GetChild("PreviewInput")).GetText());
         current.setHidden(((CheckBox) ModifyItem.GetChild("HiddenInput")).GetSelected());
       }
       ModifyItem.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "CloseModifyWindow":
       ModifyItem.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "CloseHelpWindow":
       Help.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "Help":
       Help.SetParent(MainFrame);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "1star":
       System.out.println("CurrentRatingId: " + CurrentRatingId);
       Driver.CurrentUser.rateItem(CurrentRatingId, 1);
       RatingWindow.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "2star":
       Driver.CurrentUser.rateItem(CurrentRatingId, 2);
       RatingWindow.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "3star":
       Driver.CurrentUser.rateItem(CurrentRatingId, 3);
       RatingWindow.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "4star":
       Driver.CurrentUser.rateItem(CurrentRatingId, 4);
       RatingWindow.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "5star":
       Driver.CurrentUser.rateItem(CurrentRatingId, 5);
       RatingWindow.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
     case "0star":
       Driver.CurrentUser.rateItem(CurrentRatingId, 0);
       RatingWindow.SetParent(null);
       Driver.GetGuiMain().GetTextBoxes();
       break;
   }
   SetFrames();
 }
  private void SetFrames() {
    AccountCredit.SetText(String.format("Credit: $%.2f", Driver.CurrentUser.getCredit()));
    Frame current;
    TextLabel rowOne;
    TextLabel rowTwo;
    TextButton commandButton;
    if (LastCurrentListValue != CurrentList) {
      ArrayList temporaryList = null;
      ActiveList.clear();
      if (CurrentList == 0) {
        temporaryList = Driver.CurrentUser.getPurchaseHistory();
      } else if (CurrentList == 1) {
        temporaryList = DataLoader.getAlbums();
      } else if (CurrentList == 2) {
        temporaryList = DataLoader.getAudiobooks();
      } else if (CurrentList == 3) {
        temporaryList = DataLoader.getFilm();
      }
      if (CurrentList >= 1 && CurrentList <= 3) {
        // if the use is looking at a store page, weed out all invisible songs/albums *unless* it is
        // a administrator.
        for (int i = 0; i < temporaryList.size(); i++) {
          if (temporaryList.get(i) instanceof Item
              && (!((Item) temporaryList.get(i)).isVisible()
                  || Driver.CurrentUser.getAdministrator())) {
            ActiveList.add(temporaryList.get(i));
          }
        }
      } else if (CurrentList == 0) {
        for (int i = 0; i < temporaryList.size(); i++) {
          ActiveList.add(temporaryList.get(i));
        }
      } else if (CurrentList == 4) {
        temporaryList = DataLoader.getAlbums();
        for (int i = 0; i < temporaryList.size(); i++) {
          if (temporaryList.get(i) instanceof Item
              && (!((Item) temporaryList.get(i)).isVisible()
                  || Driver.CurrentUser.getAdministrator())) {
            ActiveList.add(temporaryList.get(i));
          }
        }

        temporaryList = DataLoader.getAudiobooks();
        for (int i = 0; i < temporaryList.size(); i++) {
          if (temporaryList.get(i) instanceof Item
              && (!((Item) temporaryList.get(i)).isVisible()
                  || Driver.CurrentUser.getAdministrator())) {
            ActiveList.add(temporaryList.get(i));
          }
        }

        temporaryList = DataLoader.getFilm();
        for (int i = 0; i < temporaryList.size(); i++) {
          if (temporaryList.get(i) instanceof Item
              && (!((Item) temporaryList.get(i)).isVisible()
                  || Driver.CurrentUser.getAdministrator())) {
            ActiveList.add(temporaryList.get(i));
          }
        }
      }
      LastCurrentListValue = CurrentList;
    }
    // this should never happen, but just in case it does, break the function before an error is
    // thrown.
    if (ActiveList == null) {
      return;
    }
    int MaximumIndex = ActiveList.size();
    LibScroll.SetMax(
        Math.max(
            0,
            MaximumIndex * (LIST_ELEMENT_HEIGHT + LIST_ELEMENT_SPACING)
                - Driver.GetGuiMain().GetWindow().getHeight()
                + 50));
    PixelOffset = (int) LibScroll.GetValue();
    ViewIndex =
        (int) Math.floor((double) PixelOffset / (LIST_ELEMENT_HEIGHT + LIST_ELEMENT_SPACING));
    for (int i = 0; i < ScrollList.size(); i++) {
      // Position the frame appropriately.
      current = ScrollList.get(i);
      current.GetPosition().yOffset =
          i * (LIST_ELEMENT_HEIGHT + LIST_ELEMENT_SPACING)
              - PixelOffset % (LIST_ELEMENT_HEIGHT + LIST_ELEMENT_SPACING);
      rowOne = (TextLabel) current.GetChild("TopText");
      rowTwo = (TextLabel) current.GetChild("BottomText");
      commandButton = (TextButton) current.GetChild("Action");

      // If the index we're looking for is out of bounds, don't display the frame.
      if (i + ViewIndex >= MaximumIndex) {
        CenterScrollFrame.RemoveChild(current);
      } else {
        CenterScrollFrame.AddChild(current);
        // Fill the text labels with content based on what view option is selected.
        if (CurrentList == 0) {
          if (!current.GetChild("Action2").GetVisible() && !Driver.CurrentUser.getAdministrator()) {
            current.GetChild("Divider").GetPosition().xOffset = -99;
            current.GetChild("TopText").GetSize().xOffset = -105;
            current.GetChild("BottomText").SetSize(current.GetChild("TopText").GetSize());
            current.GetChild("Action2").SetVisible(true);
            ((TextButton) current.GetChild("Action2")).SetText("Rate");
            Driver.GetGuiMain().GetTextBoxes();
          }
          Item currentItem =
              DataLoader.getItemById(((Integer) ActiveList.get(i + ViewIndex)).intValue());
          SetListText(rowOne, rowTwo, currentItem);
          if (CurrentPlayingItem == currentItem) {
            commandButton.SetText("Stop");
          } else {
            commandButton.SetText("Play");
          }
        } else if (CurrentList >= 1 && CurrentList <= 4) {
          if (current.GetChild("Action2").GetVisible() && !Driver.CurrentUser.getAdministrator()) {
            current.GetChild("Divider").GetPosition().xOffset = -52;
            current.GetChild("TopText").GetSize().xOffset = -58;
            current.GetChild("BottomText").SetSize(current.GetChild("TopText").GetSize());
            current.GetChild("Action2").SetVisible(false);
          }
          Item currentItem = (Item) ActiveList.get(i + ViewIndex);
          SetListText(rowOne, rowTwo, currentItem);
          if (currentItem.isVisible()) {
            rowTwo.SetText(rowTwo.GetText() + "; HIDDEN");
          }
          if (Driver.CurrentUser.getPurchaseHistory().contains(currentItem.getId())) {
            if (CurrentPlayingItem == currentItem) {
              commandButton.SetText("Stop");
            } else {
              commandButton.SetText("Play");
            }
          } else {
            commandButton.SetText("Buy");
          }
        }
      }
    }
  }