Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccountUpdateWorker.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2009 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.evetool.ui.workers;
  23. import java.awt.Font;
  24. import java.awt.event.MouseAdapter;
  25. import java.awt.event.MouseEvent;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javax.swing.JLabel;
  29. import javax.swing.JPanel;
  30. import javax.swing.JSeparator;
  31. import javax.swing.SwingWorker;
  32. import uk.co.md87.evetool.api.ApiResponse;
  33. import uk.co.md87.evetool.api.EveApi;
  34. import uk.co.md87.evetool.api.wrappers.CharacterList;
  35. import uk.co.md87.evetool.api.wrappers.data.BasicCharInfo;
  36. import uk.co.md87.evetool.ui.MainWindow;
  37. import uk.co.md87.evetool.ui.data.AccountChar;
  38. import uk.co.md87.evetool.ui.pages.OverviewPage;
  39. /**
  40. *
  41. * TODO: Document AccountUpdateWorker
  42. * @author chris
  43. */
  44. public class AccountUpdateWorker extends SwingWorker<ApiResponse<CharacterList>, Object> {
  45. /**
  46. * A version number for this class. It should be changed whenever the class
  47. * structure is changed (or anything else that would prevent serialized
  48. * objects being unserialized with the new class).
  49. */
  50. private static final long serialVersionUID = 10;
  51. private final OverviewPage page;
  52. private final EveApi api;
  53. private final JPanel target;
  54. private final MainWindow window;
  55. private final int selectChar;
  56. public AccountUpdateWorker(final OverviewPage page, final MainWindow window,
  57. final EveApi api, final JPanel panel, final int selectChar) {
  58. super();
  59. this.page = page;
  60. this.api = api;
  61. this.window = window;
  62. this.target = panel;
  63. this.selectChar = selectChar;
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. protected ApiResponse<CharacterList> doInBackground() throws Exception {
  68. return api.getCharacterList();
  69. }
  70. @Override
  71. protected void done() {
  72. try {
  73. final ApiResponse<CharacterList> res = get();
  74. if (res.wasSuccessful()) {
  75. target.removeAll();
  76. boolean first = true;
  77. for (BasicCharInfo character : res.getResult()) {
  78. if (first) {
  79. first = false;
  80. } else {
  81. target.add(new JSeparator(),
  82. "span, growx, pushx, gaptop 5, gapbottom 5");
  83. }
  84. final JLabel portrait = new JLabel("Loading...");
  85. final JLabel nameLabel = new JLabel(character.getName());
  86. final JLabel skillLabel = new JLabel("Loading...");
  87. final JLabel iskLabel = new JLabel("Loading...", JLabel.RIGHT);
  88. nameLabel.setFont(nameLabel.getFont().deriveFont(Font.BOLD));
  89. final AccountChar ac = new AccountChar(character, portrait,
  90. nameLabel, iskLabel, skillLabel);
  91. page.addChar(ac);
  92. target.add(portrait, "spany 2, height 64!, width 64!");
  93. target.add(nameLabel, "height 20!");
  94. target.add(new JLabel(character.getCorp().getName(), JLabel.RIGHT), "wrap");
  95. target.add(skillLabel, "span, split, gaptop 20, height 20!");
  96. target.add(iskLabel, "wrap");
  97. final EveApi newApi = api.clone();
  98. newApi.setCharID(character.getId());
  99. new PortraitLoaderWorker(character.getId(), portrait, 64).execute();
  100. new CharacterBalanceUpdateWorker(newApi, ac).execute();
  101. new CharacterSkillUpdateWorker(newApi, ac).execute();
  102. portrait.addMouseListener(new PortraitMouseListener(page, ac));
  103. if (character.getId() == selectChar) {
  104. window.setSelectedChar(ac);
  105. }
  106. }
  107. } else {
  108. target.removeAll();
  109. target.add(new JLabel("Error: " + res.getError()));
  110. }
  111. target.revalidate();
  112. } catch (Exception ex) {
  113. Logger.getLogger(OverviewPage.class.getName()).log(Level.SEVERE, null, ex);
  114. }
  115. }
  116. private static class PortraitMouseListener extends MouseAdapter {
  117. private final OverviewPage page;
  118. private final AccountChar ac;
  119. public PortraitMouseListener(OverviewPage page, AccountChar ac) {
  120. this.page = page;
  121. this.ac = ac;
  122. }
  123. @Override
  124. public void mouseClicked(MouseEvent e) {
  125. page.setSelectedChar(ac);
  126. }
  127. }
  128. }