1 6 package com.nightlabs.ipanema.person.preferences; 7 8 import java.util.Collections ; 9 import java.util.Comparator ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.LinkedList ; 13 import java.util.List ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import org.eclipse.jface.viewers.IStructuredContentProvider; 18 import org.eclipse.jface.viewers.Viewer; 19 20 23 public class BlockOrderListContentProvider implements IStructuredContentProvider 24 { 25 public BlockOrderListContentProvider() { 26 } 27 28 public Map structBlockDisplayOrder; 29 private List structBlockDisplayOrderEntryList; 30 31 public class Entry implements Map.Entry { 32 33 private Map.Entry entry; 34 public Entry(Map.Entry entry) { 35 this.entry = entry; 36 } 37 40 public Object getKey() { 41 return entry.getKey(); 42 } 43 44 47 public Object getValue() { 48 if (internalValue == null) 49 return entry.getValue(); 50 else 51 return internalValue; 52 } 53 54 55 private Object internalValue; 56 59 public Object setValue(Object value) { 60 return internalValue = value; 61 } 62 63 } 64 65 protected void refreshOrder() { 66 Set entrySet = structBlockDisplayOrder.entrySet(); 67 structBlockDisplayOrderEntryList = new LinkedList (); 68 for (Iterator iter = entrySet.iterator(); iter.hasNext();) { 70 Map.Entry entry = (Map.Entry ) iter.next(); 71 structBlockDisplayOrderEntryList.add(new Entry(entry)); 72 } 73 sortList(); 74 } 75 76 protected void sortList() { 77 Collections.sort(structBlockDisplayOrderEntryList,new Comparator () { 79 public int compare(Object o1, Object o2) { 80 if ((o1 instanceof Map.Entry ) && (o2 instanceof Map.Entry )) { 81 int p1, p2; 82 p1 = ((Integer ) ((Map.Entry )o1).getValue() ).intValue(); 83 p2 = ((Integer ) ((Map.Entry )o2).getValue() ).intValue(); 84 if (p1 < p2) 85 return -1; 86 else if (p1 > p2) 87 return 1; 88 else 89 return 0; 90 } 91 return 0; 92 } 93 }); 94 95 } 96 99 public Object [] getElements(Object inputElement) { 100 if (inputElement != structBlockDisplayOrder) { 101 this.structBlockDisplayOrder = (Map )inputElement; 102 refreshOrder(); 104 } 105 return structBlockDisplayOrderEntryList.toArray(); 107 } 109 110 113 public void dispose() { 114 } 115 116 119 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 120 if (structBlockDisplayOrderEntryList != null) 121 sortList(); 122 } 124 125 126 public void moveUp(int priority) { 127 structBlockDisplayOrderEntryList.hashCode(); 128 for (Iterator iter = structBlockDisplayOrderEntryList.iterator(); iter.hasNext();) { 129 Map.Entry entry = (Map.Entry ) iter.next(); 130 int swapPriority = ((Integer )entry.getValue()).intValue(); 131 if (swapPriority == priority) 132 entry.setValue(new Integer (priority-1)); 133 else if (swapPriority == (priority-1)) 134 entry.setValue(new Integer (priority)); 135 } 136 sortList(); 137 } 138 139 public void moveDown(int priority) { 140 structBlockDisplayOrderEntryList.hashCode(); 141 for (Iterator iter = structBlockDisplayOrderEntryList.iterator(); iter.hasNext();) { 142 Map.Entry entry = (Map.Entry ) iter.next(); 143 int swapPriority = ((Integer )entry.getValue()).intValue(); 144 if (swapPriority == priority) 145 entry.setValue(new Integer (priority+1)); 146 else if (swapPriority == (priority+1)) 147 entry.setValue(new Integer (priority)); 148 } 149 sortList(); 150 } 151 152 public Map getStructBlockOrder() { 153 Map result = new HashMap (); 154 for (Iterator iter = structBlockDisplayOrderEntryList.iterator(); iter.hasNext();) { 155 Map.Entry entry = (Map.Entry ) iter.next(); 156 result.put(entry.getKey(),entry.getValue()); 157 } 158 return result; 159 } 160 161 } 162 | Popular Tags |