View Javadoc

1   package org.lsst.ccs.plugin.jas3.trending.timeselection;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   import java.util.Properties;
7   import javax.swing.table.AbstractTableModel;
8   import org.freehep.application.PropertyUtilities;
9   import org.freehep.application.studio.Studio;
10  
11  /**
12   * Maintains a list of time window presets known to the application.
13   *
14   * @author onoprien
15   */
16  public class PresetList extends AbstractTableModel {
17  
18  // -- Private parts : ----------------------------------------------------------
19      
20      private final String KEY = "org.lsst.trending.custom";
21      private final Studio app;
22      
23      private ArrayList<TimeSelection> data;
24  
25  // -- Construction and initialization : ----------------------------------------
26      
27      public PresetList(Studio application) {
28          app = application;
29          restore();
30      }
31      
32  
33  // -- Getters and setters : ----------------------------------------------------
34      
35      /** Returns a list of <tt>maxSize</tt> most recently used time windows ordered by name. */
36      public List<TimeSelection> getRecent(int maxSize) {
37          List<TimeSelection> out = new ArrayList<>(data);
38          Collections.sort(out, TimeSelection.compareByTime());
39          if (maxSize < out.size()) out = out.subList(0, maxSize);
40          Collections.sort(out, TimeSelection.compareByName());
41          return out;
42      }
43      
44      /** Returns preset with the specified index. */
45      public TimeSelection get(int index) {
46          return data.get(index);
47      }
48      
49      /** Returns the index of the specified preset in this list, or -1 if this list does not contain the preset. */
50      public int indexOf(TimeSelection timeWindow) {
51          return data.indexOf(timeWindow);
52      }
53      
54      
55  // -- Modifying the list : -----------------------------------------------------
56      
57      /**
58       * Adds time window to this list, removing any identically named elements.
59       * If the specified element is already in this list, it is removed and re-added.
60       */
61      public void insert(TimeSelection timeWindow) {
62          
63          data.remove(timeWindow);
64          
65          String name = timeWindow.name;
66          int nElements = data.size();
67          boolean needSave = timeWindow.isPersistent();
68              
69          int i = 0;
70          for (; i < nElements; i++) {
71              TimeSelection e = data.get(i);
72              int out = name.compareTo(e.name);
73              if (out == 0) {
74                  needSave = needSave || e.isPersistent();
75                  data.set(i, timeWindow);
76                  break;
77              } else if (out < 0) {
78                  data.add(i, timeWindow);
79                  break;
80              }
81          }
82          if (i == nElements) {
83              data.add(timeWindow);
84          }
85  
86          if (needSave) save();
87          fireTableStructureChanged();
88      }
89      
90      /**
91       * Removes the specified window from this list.
92       * Updates application properties if necessary.
93       */
94      public void delete(TimeSelection timeWindow) {
95          if (data.remove(timeWindow) && timeWindow.isPersistent()) {
96              save();
97          }
98          fireTableStructureChanged();
99      }
100     
101 
102 // -- Implementing TableModel : ------------------------------------------------
103 
104     @Override
105     public int getRowCount() {
106         return data.size();
107     }
108 
109     @Override
110     public int getColumnCount() {
111         return 4;
112     }
113 
114     @Override
115     public Object getValueAt(int rowIndex, int columnIndex) {
116         switch (columnIndex) {
117             case 0: return data.get(rowIndex).name;
118             case 1: return data.get(rowIndex).lowerEdgeString;
119             case 2: return data.get(rowIndex).upperEdgeString;
120             case 3: return data.get(rowIndex).isPersistent();
121             default: throw new IllegalArgumentException();
122         }
123     }
124 
125     @Override
126     public Class<?> getColumnClass(int columnIndex) {
127         return columnIndex == 3 ? Boolean.class : String.class;
128     }
129 
130     @Override
131     public String getColumnName(int column) {
132         switch (column) {
133             case 0: return "Name";
134             case 1: return "Start";
135             case 2: return "End";
136             case 3: return "Saved";
137             default: throw new IllegalArgumentException();
138         }
139     }
140     
141     
142 // -- Saving to / restoring from properties : ----------------------------------
143     
144     /**
145      * Updates information on the specified time window in application properties.
146      * @param timeWindow  Time window that needs to be saved or removed.
147      * @param keep  If true, the data is saved in the properties;
148      *              if false, the data is removed.
149      */
150     private void save() {
151         Properties prop = app.getUserProperties();
152         if (prop != null) {
153             int n = data.size();
154             ArrayList<String> savedData = null;
155             if (n != 0) {
156                 savedData = new ArrayList<>(n);
157                 for (int i=0; i<n; i++) {
158                     savedData.add(data.get(i).toCompressedString());
159                 }
160             }
161             PropertyUtilities.setStringCollection(prop, KEY, savedData);
162         }
163     }
164     
165     /** Restore persistent time selections from application properties. */
166     private void restore() {
167         Properties prop = app.getUserProperties();
168         if (prop == null) {
169             data = new ArrayList<>(0);
170         } else {
171             String[] savedData = PropertyUtilities.getStringArray(prop, KEY, new String[0]);
172             data = new ArrayList<>(savedData.length);
173             for (String s : savedData) {
174                 try {
175                     data.add(TimeSelection.parseCompressedString(s));
176                 } catch (IllegalArgumentException x) {
177                 }
178             }
179             Collections.sort(data, TimeSelection.compareByName());
180         }
181     }
182     
183 }