PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/beam-ui/src/main/java/org/esa/beam/framework/ui/product/InputListModel.java

http://github.com/bcdev/beam
Java | 179 lines | 142 code | 19 blank | 18 comment | 28 complexity | dd85d672a5e5e808bd94095bc0f9a9f4 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de)
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version.
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, see http://www.gnu.org/licenses/
  15. */
  16. package org.esa.beam.framework.ui.product;
  17. import com.bc.ceres.binding.Property;
  18. import com.bc.ceres.binding.ValidationException;
  19. import org.esa.beam.framework.datamodel.Product;
  20. import org.esa.beam.util.logging.BeamLogManager;
  21. import javax.swing.AbstractListModel;
  22. import java.beans.PropertyChangeEvent;
  23. import java.beans.PropertyChangeListener;
  24. import java.io.File;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.logging.Level;
  28. /**
  29. * @author Thomas Storm
  30. */
  31. class InputListModel extends AbstractListModel<Object> {
  32. private final List<Object> list = new ArrayList<>();
  33. private List<Product> sourceProducts = new ArrayList<>();
  34. private Property sourceProductPaths;
  35. private boolean internalPropertyChange;
  36. @Override
  37. public Object getElementAt(int index) {
  38. return list.get(index);
  39. }
  40. @Override
  41. public int getSize() {
  42. return list.size();
  43. }
  44. Product[] getSourceProducts() {
  45. return sourceProducts.toArray(new Product[sourceProducts.size()]);
  46. }
  47. void setElements(String[] elements) throws ValidationException {
  48. if (!list.isEmpty()) {
  49. final int endIndex = list.size() - 1;
  50. list.clear();
  51. fireIntervalRemoved(this, 0, endIndex);
  52. }
  53. final File[] files = new File[elements.length];
  54. for (int i = 0; i < files.length; i++) {
  55. files[i] = new File(elements[i]);
  56. }
  57. addElements(files);
  58. }
  59. void addElements(Object... elements) throws ValidationException {
  60. final int startIndex = list.size();
  61. for (Object element : elements) {
  62. if (!(element instanceof File || element instanceof Product)) {
  63. throw new IllegalStateException(
  64. "Only java.io.File or org.esa.beam.framework.datamodel.Product allowed.");
  65. }
  66. if (mayAdd(element)) {
  67. list.add(element);
  68. }
  69. }
  70. updateProperty();
  71. fireIntervalAdded(this, startIndex, list.size() - 1);
  72. }
  73. void clear() {
  74. if (!list.isEmpty()) {
  75. final int endIndex = list.size() - 1;
  76. list.clear();
  77. try {
  78. updateProperty();
  79. } catch (ValidationException ignored) {
  80. }
  81. fireIntervalRemoved(this, 0, endIndex);
  82. }
  83. }
  84. void removeElementsAt(int[] selectedIndices) {
  85. List<Object> toRemove = new ArrayList<>();
  86. int startIndex = Integer.MAX_VALUE;
  87. int endIndex = Integer.MIN_VALUE;
  88. for (int selectedIndex : selectedIndices) {
  89. startIndex = Math.min(startIndex, selectedIndex);
  90. endIndex = Math.max(endIndex, selectedIndex);
  91. toRemove.add(list.get(selectedIndex));
  92. }
  93. if (list.removeAll(toRemove)) {
  94. try {
  95. updateProperty();
  96. } catch (ValidationException ignored) {
  97. }
  98. fireIntervalRemoved(this, startIndex, endIndex);
  99. }
  100. }
  101. private void updateProperty() throws ValidationException {
  102. final List<String> files = new ArrayList<>();
  103. final List<Product> products = new ArrayList<>();
  104. for (Object element : list) {
  105. if (element instanceof File) {
  106. files.add(((File) element).getPath());
  107. } else if (element instanceof Product) {
  108. products.add((Product) element);
  109. }
  110. }
  111. internalPropertyChange = true;
  112. sourceProductPaths.setValue(files.toArray(new String[files.size()]));
  113. internalPropertyChange = false;
  114. sourceProducts = products;
  115. }
  116. private boolean mayAdd(Object element) {
  117. if (list.contains(element)) {
  118. return false;
  119. }
  120. if (element instanceof Product) {
  121. return true;
  122. }
  123. File file = (File) element;
  124. return file.isDirectory() || !alreadyContained(file);
  125. }
  126. private boolean alreadyContained(File file) {
  127. for (Product sourceProduct : sourceProducts) {
  128. File fileLocation = sourceProduct.getFileLocation();
  129. if (fileLocation != null && fileLocation.getAbsolutePath().equals(file.getAbsolutePath())) {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. public void setProperty(Property property) {
  136. this.sourceProductPaths = property;
  137. if (sourceProductPaths != null && sourceProductPaths.getContainer() != null) {
  138. sourceProductPaths.addPropertyChangeListener(new PropertyChangeListener() {
  139. @Override
  140. public void propertyChange(PropertyChangeEvent evt) {
  141. if (!internalPropertyChange) {
  142. Object newValue = evt.getNewValue();
  143. try {
  144. if (newValue == null) {
  145. clear();
  146. } else {
  147. setElements((String[]) newValue);
  148. }
  149. } catch (ValidationException e) {
  150. BeamLogManager.getSystemLogger().log(Level.SEVERE, "Problems at setElements.", e);
  151. }
  152. }
  153. }
  154. });
  155. }
  156. }
  157. public Property getProperty() {
  158. return sourceProductPaths;
  159. }
  160. }