1 19 20 package org.netbeans.modules.form.editors2; 21 22 import javax.swing.*; 23 import org.netbeans.modules.form.editors.StringArrayEditor; 24 import org.openide.util.NbBundle; 25 26 30 31 public class ListModelEditor extends StringArrayEditor { 32 33 private ListModel listModel = null; 34 35 public void setValue(Object val) { 36 if (val instanceof ListModel) { 37 listModel = (ListModel) val; 38 super.setValue(getDataFromModel(listModel)); 39 } 40 else if (val instanceof String []) { 41 listModel = getModelForData((String [])val); 42 super.setValue(val); 43 } 44 else { 45 listModel = getModelForData(new String [0]); 46 super.setValue(null); 47 } 48 } 49 50 public Object getValue() { 51 return listModel; 52 } 53 54 public void setStringArray(String [] value) { 55 listModel = getModelForData(value); 56 super.setValue(value); 57 } 58 59 public String [] getStringArray () { 60 return (String [])super.getValue (); 61 } 62 63 public String getJavaInitializationString() { 64 if (getStrings(true).equals("")) 65 return null; 66 StringBuffer buf = new StringBuffer ("new javax.swing.AbstractListModel() {\n"); buf.append("String[] strings = { "); buf.append(getStrings(true)); 69 buf.append(" };\n"); buf.append("public int getSize() { return strings.length; }\n"); buf.append("public Object getElementAt(int i) { return strings[i]; }\n"); buf.append("}"); 74 return buf.toString(); 75 } 76 77 static String [] getDataFromModel(ListModel model) { 78 String [] data = new String [model.getSize()]; 79 for (int i=0; i < data.length; i++) { 80 Object obj = model.getElementAt(i); 81 data[i] = obj instanceof String ? (String ) obj : ""; } 83 return data; 84 } 85 86 static ListModel getModelForData(String [] data) { 87 DefaultListModel model = new DefaultListModel(); 88 for (int i=0; i < data.length; i++) 89 model.addElement(data[i]); 90 return model; 91 } 92 93 public String getDisplayName() { 95 return NbBundle.getBundle(getClass()).getString("CTL_ListModelEditor_DisplayName"); } 97 98 } 99 | Popular Tags |