Desktop tool for browsing account info from EVE-Online
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.pages;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.ActionListener;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.concurrent.ExecutionException;
  29. import javax.swing.JLabel;
  30. import javax.swing.JSeparator;
  31. import javax.swing.SwingWorker;
  32. import net.miginfocom.swing.MigLayout;
  33. import uk.co.md87.evetool.AccountManager;
  34. import uk.co.md87.evetool.ApiFactory;
  35. import uk.co.md87.evetool.ui.ContentPanel.Page;
  36. import uk.co.md87.evetool.ui.ContextPanel;
  37. import uk.co.md87.evetool.ui.MainWindow;
  38. import uk.co.md87.evetool.ui.components.FilterButton;
  39. import uk.co.md87.evetool.ui.components.HeaderPanel;
  40. import uk.co.md87.evetool.ui.components.ListablePanel;
  41. import uk.co.md87.evetool.ui.dialogs.listableconfig.ListableConfigDialog;
  42. import uk.co.md87.evetool.ui.listable.Listable;
  43. import uk.co.md87.evetool.ui.listable.ListableComparator;
  44. import uk.co.md87.evetool.ui.listable.ListableConfig;
  45. import uk.co.md87.evetool.ui.listable.ListableConfig.CompoundConfigElement;
  46. import uk.co.md87.evetool.ui.listable.ListableParser;
  47. /**
  48. *
  49. * TODO: Document ListablePage
  50. * @author chris
  51. */
  52. public abstract class ListablePage<T extends Listable> extends Page implements ActionListener {
  53. /**
  54. * A version number for this class. It should be changed whenever the class
  55. * structure is changed (or anything else that would prevent serialized
  56. * objects being unserialized with the new class).
  57. */
  58. private static final long serialVersionUID = 10;
  59. protected final MainWindow window;
  60. protected final ApiFactory factory;
  61. protected final AccountManager manager;
  62. protected ListableConfig config;
  63. public ListablePage(final MainWindow window, final AccountManager manager,
  64. final ApiFactory factory) {
  65. this.window = window;
  66. this.factory = factory;
  67. this.manager = manager;
  68. setLayout(new MigLayout("fillx, wrap 1"));
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public void activated(final ContextPanel context) {
  73. final FilterButton button = new FilterButton();
  74. button.addActionListener(this);
  75. context.add(button, "growy, al right");
  76. updatePage();
  77. }
  78. public void setConfig(final ListableConfig config) {
  79. this.config = config;
  80. updatePage();
  81. }
  82. protected void updatePage() {
  83. removeAll();
  84. add(new JLabel("Loading..."));
  85. new UpdateWorker().execute();
  86. }
  87. protected abstract List<T> getListables();
  88. /** {@inheritDoc} */
  89. @Override
  90. public void actionPerformed(final ActionEvent e) {
  91. new ListableConfigDialog(window, this, config, getListables().get(0)).setVisible(true);
  92. }
  93. protected class UpdateResult {
  94. private final List<T> list;
  95. private final ListableParser parser;
  96. public UpdateResult(List<T> list, ListableParser parser) {
  97. this.list = list;
  98. this.parser = parser;
  99. }
  100. public List<T> getList() {
  101. return list;
  102. }
  103. public ListableParser getParser() {
  104. return parser;
  105. }
  106. }
  107. protected class UpdateWorker extends SwingWorker<UpdateResult, Void> {
  108. @Override
  109. protected UpdateResult doInBackground() throws Exception {
  110. final List<T> list = new ArrayList<T>(getListables());
  111. final ListableParser parser = new ListableParser(list.get(0).getClass());
  112. if (config.sortOrder != null) {
  113. Collections.sort(list, new ListableComparator(config.sortOrder, parser));
  114. }
  115. return new UpdateResult(list, parser);
  116. }
  117. @Override
  118. protected void done() {
  119. try {
  120. final UpdateResult res = get();
  121. removeAll();
  122. String lastGroup = null;
  123. boolean first = true;
  124. for (T listable : res.getList()) {
  125. if (config.group != null &&
  126. (!(config.group instanceof CompoundConfigElement)
  127. || ((CompoundConfigElement) config.group)
  128. .getElements().length > 0)) {
  129. final String thisGroup = config.group.getValue(listable,
  130. res.getParser());
  131. if (lastGroup == null || !thisGroup.equals(lastGroup)) {
  132. first = true;
  133. lastGroup = thisGroup;
  134. add(new HeaderPanel(lastGroup), "growx, pushx");
  135. }
  136. }
  137. if (first) {
  138. first = false;
  139. } else {
  140. add(new JSeparator(), "growx, pushx");
  141. }
  142. add(new ListablePanel(listable, res.getParser(), config), "growx, pushx");
  143. }
  144. revalidate();
  145. } catch (ExecutionException ex) {
  146. } catch (InterruptedException ex) {
  147. }
  148. }
  149. }
  150. }