View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.misc;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.swing.AbstractListModel;
22  
23  import p3j.pppm.parameters.ParameterInstance;
24  import p3j.pppm.sets.SetType;
25  
26  /**
27   * List model for a list of parameters.
28   * 
29   * Created on February 4, 2007
30   * 
31   * @author Christina Bohk
32   * @author Roland Ewald
33   * 
34   */
35  public class ParameterListModel extends AbstractListModel<ParameterInstance> {
36  
37    /** Serialization ID. */
38    private static final long serialVersionUID = 5605809577849847910L;
39  
40    /** List of parameters. */
41    private List<ParameterInstance> parameters;
42  
43    /** An empty list. */
44    private static final List<ParameterInstance> EMPTY_LIST = new ArrayList<ParameterInstance>();
45  
46    @Override
47    public ParameterInstance getElementAt(int index) {
48      if (parameters == null) {
49        return null;
50      }
51      return parameters.get(index);
52    }
53  
54    @Override
55    public int getSize() {
56      if (parameters == null) {
57        return 0;
58      }
59      return parameters.size();
60    }
61  
62    /**
63     * Updates with content from a new Settype.
64     * 
65     * @param newSetType
66     *          the new Settype
67     */
68    public void updateSetType(SetType newSetType) {
69      int oldIndex = this.parameters == null ? 0 : this.parameters.size();
70      this.parameters = newSetType == null ? EMPTY_LIST : newSetType
71          .getDefinedParameters();
72      this.fireContentsChanged(this, 0, Math.max(oldIndex, getSize()));
73    }
74  
75    /**
76     * Refresh the list.
77     */
78    public void refresh() {
79      this.fireContentsChanged(this, 0, getSize());
80    }
81  
82  }