1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.DefaultListModel ; 27 import java.util.*; 28 30 38 public class HistoryModel extends DefaultListModel 39 implements MutableListModel 40 { 41 46 public HistoryModel(String name) 47 { 48 this.name = name; 49 } 51 57 public void addItem(String text) 58 { 59 if(text == null || text.length() == 0) 60 return; 61 62 int index = indexOf(text); 63 if(index != -1) 64 removeElementAt(index); 65 66 insertElementAt(text,0); 67 68 while(getSize() > max) 69 removeElementAt(getSize() - 1); 70 } 72 public void insertElementAt(Object obj, int index) 74 { 75 modified = true; 76 super.insertElementAt(obj,index); 77 } 79 84 public String getItem(int index) 85 { 86 return (String )elementAt(index); 87 } 89 public boolean removeElement(Object obj) 91 { 92 modified = true; 93 return super.removeElement(obj); 94 } 96 100 public void clear() 101 { 102 removeAllElements(); 103 } 105 public void removeAllElements() 107 { 108 modified = true; 109 super.removeAllElements(); 110 } 112 117 public String getName() 118 { 119 return name; 120 } 122 128 public static HistoryModel getModel(String name) 129 { 130 if(models == null) 131 models = Collections.synchronizedMap(new HashMap<String , HistoryModel>()); 132 133 HistoryModel model = models.get(name); 134 if(model == null) 135 { 136 model = new HistoryModel(name); 137 models.put(name,model); 138 } 139 140 return model; 141 } 143 public static void loadHistory() 145 { 146 if (saver != null) 147 models = saver.load(models); 148 } 150 public static void saveHistory() 152 { 153 if (saver != null && modified && saver.save(models)) 154 modified = false; 155 } 157 public static void setMax(int max) 159 { 160 HistoryModel.max = max; 161 } 163 public static void setSaver(HistoryModelSaver saver) 165 { 166 HistoryModel.saver = saver; 167 } 169 private static int max; 171 172 private String name; 173 private static Map<String , HistoryModel> models; 174 175 private static boolean modified; 176 private static HistoryModelSaver saver; 177 } 179 | Popular Tags |