View Javadoc

1   package ca.uhn.hl7v2.testpanel.ui.editor;
2   
3   import java.beans.PropertyChangeEvent;
4   import java.beans.PropertyChangeListener;
5   
6   import javax.swing.DefaultComboBoxModel;
7   
8   import ca.uhn.hl7v2.testpanel.controller.Controller;
9   import ca.uhn.hl7v2.testpanel.model.conf.TableFile;
10  import ca.uhn.hl7v2.testpanel.model.conf.TableFileList;
11  import ca.uhn.hl7v2.testpanel.ui.IDestroyable;
12  
13  public class TablesComboModel extends DefaultComboBoxModel implements IDestroyable {
14  
15  	public static final String NONE_DEFINED = "None Defined";
16  	public static final String NONE = "None";
17  
18  	private static PropertyChangeListener myFilesListener;
19  	private static TableFileList myTableFileList;
20  
21  	/**
22  	 * Constructor
23  	 */
24  	public TablesComboModel(Controller theController) {
25  
26  		myTableFileList = theController.getTableFileList();
27  		myFilesListener = new PropertyChangeListener() {
28  			public void propertyChange(PropertyChangeEvent theEvt) {
29  				update();
30  			}
31  		};
32  		myTableFileList.addPropertyChangeListener(TableFileList.PROP_FILES, myFilesListener);
33  		update();
34  	}
35  
36  	private void update() {
37  		int i = 0;
38  
39  		if (myTableFileList.getTableFiles().size() > 0) {
40  			if (getSize() == 0) {
41  				addElement(NONE);
42  			} else if (getElementAt(0) != NONE) {
43  				insertElementAt(NONE, 0);
44  			}
45  		}
46  
47  		for (TableFile next : myTableFileList.getTableFiles()) {
48  			String nextName = next.getName();
49  			if (getSize() <= (i + 1) || !getElementAt(i).equals(nextName)) {
50  				insertElementAt(nextName, i + 1);
51  			}
52  
53  			i++;
54  		}
55  
56  		if (this.getSize() == 0) {
57  			addElement(NONE_DEFINED);
58  		}
59  
60  		while (Math.min(1, this.getSize()) > myTableFileList.getTableFiles().size()) {
61  			removeElementAt(this.getSize() - 1);
62  		}
63  	}
64  
65  	public void destroy() {
66  		myTableFileList.removePropertyChangeListener(TableFileList.PROP_FILES, myFilesListener);
67  	}
68  
69  }