Changeset 1677 in josm for trunk/src/org/openstreetmap/josm/gui/historycombobox/ComboBoxHistory.java
- Timestamp:
- 2009-06-17T10:04:22+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/historycombobox/ComboBoxHistory.java
r1575 r1677 1 1 /* Copyright (c) 2008, Henrik Niehaus 2 2 * All rights reserved. 3 * 3 * 4 4 * Redistribution and use in source and binary forms, with or without 5 5 * modification, are permitted provided that the following conditions are met: 6 * 6 * 7 7 * 1. Redistributions of source code must retain the above copyright notice, 8 8 * this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 9 * 2. Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 11 11 * and/or other materials provided with the distribution. 12 * 3. Neither the name of the project nor the names of its 13 * contributors may be used to endorse or promote products derived from this 12 * 3. Neither the name of the project nor the names of its 13 * contributors may be used to endorse or promote products derived from this 14 14 * software without specific prior written permission. 15 * 15 * 16 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE … … 37 37 public class ComboBoxHistory extends DefaultComboBoxModel implements Iterable<String> { 38 38 39 private int maxSize = 10; 40 41 private List<HistoryChangedListener> listeners = new ArrayList<HistoryChangedListener>(); 42 43 public ComboBoxHistory(int size) { 44 maxSize = size; 45 } 46 47 /** 48 * Adds or moves an element to the top of the history 49 */ 50 public void addElement(Object o) { 51 String newEntry = (String)o; 52 53 // if history contains this object already, delete it, 54 // so that it looks like a move to the top 55 for (int i = 0; i < getSize(); i++) { 56 String oldEntry = (String) getElementAt(i); 57 if(oldEntry.equals(newEntry)) { 58 removeElementAt(i); 59 } 60 } 61 62 // insert element at the top 63 insertElementAt(o, 0); 64 65 // remove an element, if the history gets too large 66 if(getSize()> maxSize) { 67 removeElementAt(getSize()-1); 68 } 69 70 // set selected item to the one just added 71 setSelectedItem(o); 72 73 fireHistoryChanged(); 74 } 75 76 public Iterator<String> iterator() { 77 return new Iterator<String>() { 39 private int maxSize = 10; 78 40 79 private int position = -1; 80 81 public void remove() { 82 removeElementAt(position); 83 } 41 private List<HistoryChangedListener> listeners = new ArrayList<HistoryChangedListener>(); 84 42 85 public boolean hasNext() { 86 if(position < getSize()-1 && getSize()>0) { 87 return true; 88 } 89 return false; 90 } 43 public ComboBoxHistory(int size) { 44 maxSize = size; 45 } 91 46 92 public String next() { 93 position++; 94 return getElementAt(position).toString(); 95 } 96 97 }; 98 } 47 /** 48 * Adds or moves an element to the top of the history 49 */ 50 public void addElement(Object o) { 51 String newEntry = (String)o; 99 52 100 public void setItems(List<String> items) { 101 removeAllElements(); 102 Collections.reverse(items); 103 for (String item : items) { 53 // if history contains this object already, delete it, 54 // so that it looks like a move to the top 55 for (int i = 0; i < getSize(); i++) { 56 String oldEntry = (String) getElementAt(i); 57 if(oldEntry.equals(newEntry)) { 58 removeElementAt(i); 59 } 60 } 61 62 // insert element at the top 63 insertElementAt(o, 0); 64 65 // remove an element, if the history gets too large 66 if(getSize()> maxSize) { 67 removeElementAt(getSize()-1); 68 } 69 70 // set selected item to the one just added 71 setSelectedItem(o); 72 73 fireHistoryChanged(); 74 } 75 76 public Iterator<String> iterator() { 77 return new Iterator<String>() { 78 79 private int position = -1; 80 81 public void remove() { 82 removeElementAt(position); 83 } 84 85 public boolean hasNext() { 86 if(position < getSize()-1 && getSize()>0) { 87 return true; 88 } 89 return false; 90 } 91 92 public String next() { 93 position++; 94 return getElementAt(position).toString(); 95 } 96 97 }; 98 } 99 100 public void setItems(List<String> items) { 101 removeAllElements(); 102 Collections.reverse(items); 103 for (String item : items) { 104 104 addElement(item); 105 105 } 106 107 108 106 Collections.reverse(items); 107 } 108 109 109 public List<String> asList() { 110 110 List<String> list = new ArrayList<String>(maxSize); … … 114 114 return list; 115 115 } 116 116 117 117 public void addHistoryChangedListener(HistoryChangedListener l) { 118 118 listeners.add(l); 119 119 } 120 120 121 121 public void removeHistoryChangedListener(HistoryChangedListener l) { 122 122 listeners.remove(l); 123 123 } 124 124 125 125 private void fireHistoryChanged() { 126 126 for (HistoryChangedListener l : listeners) {
Note:
See TracChangeset
for help on using the changeset viewer.