1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details; 13 14 import java.util.Iterator ; 15 16 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.ISimpleCSCommandKeyListener; 17 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.NewCommandKeyEvent; 18 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSCommandManager; 19 import org.eclipse.pde.internal.ui.parts.ComboPart; 20 import org.eclipse.swt.custom.CCombo; 21 import org.eclipse.swt.events.DisposeEvent; 22 import org.eclipse.swt.events.DisposeListener; 23 import org.eclipse.swt.widgets.Combo; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.ui.forms.widgets.FormToolkit; 26 27 31 public class SimpleCSCommandComboPart extends ComboPart implements 32 ISimpleCSCommandKeyListener, DisposeListener { 33 34 private int fNewCommandKeyIndex; 35 36 private int fComboEntryLimit; 37 38 41 public SimpleCSCommandComboPart() { 42 super(); 43 fNewCommandKeyIndex = -1; 44 fComboEntryLimit = -1; 45 } 46 47 50 public void addDisposeListener(DisposeListener listener) { 51 if (combo == null) { 52 return; 53 } else if (combo instanceof Combo) { 54 ((Combo) combo).addDisposeListener(listener); 55 } else { 56 ((CCombo) combo).addDisposeListener(listener); 57 } 58 } 59 60 63 public void createControl(Composite parent, FormToolkit toolkit, int style) { 64 super.createControl(parent, toolkit, style); 65 SimpleCSCommandManager.Instance().addCommandKeyListener(this); 67 addDisposeListener(this); 70 } 71 72 75 public void newCommandKey(NewCommandKeyEvent event) { 76 String key = event.getKey(); 78 putValueInCombo(key, fNewCommandKeyIndex); 80 } 81 82 85 private void putValueInCombo(String key, int index) { 86 if (indexOf(key) != -1) { 88 return; 89 } 90 if (getItemCount() >= fComboEntryLimit) { 93 removeLeastRecentEntry(); 94 } 95 if (index < 0) { 97 add(key); 99 } else { 100 add(key, index); 102 } 103 } 104 105 108 private void removeLeastRecentEntry() { 109 int entryCount = getItemCount(); 112 if (entryCount <= 1) { 114 return; 115 } 116 int lastEntry = entryCount - 1; 118 if (lastEntry != getSelectionIndex()) { 125 remove(lastEntry); 126 return; 127 } 128 int secondlastEntry = lastEntry - 1; 130 remove(secondlastEntry); 131 } 132 133 136 public void setComboEntryLimit(int limit) { 137 fComboEntryLimit = limit; 138 } 139 140 143 public int getComboEntryLimit() { 144 return fComboEntryLimit; 145 } 146 147 153 public void setNewCommandKeyIndex(int newCommandKeyIndex) { 154 fNewCommandKeyIndex = newCommandKeyIndex; 155 } 156 157 160 public int getNewCommandKeyIndex() { 161 return fNewCommandKeyIndex; 162 } 163 164 167 public void widgetDisposed(DisposeEvent e) { 168 SimpleCSCommandManager.Instance().removeCommandKeyListener(this); 170 } 171 172 175 public void populate() { 176 Iterator iterator = SimpleCSCommandManager.Instance().getKeys().iterator(); 178 while (iterator.hasNext()) { 179 String key = (String )iterator.next(); 180 add(key); 181 } 182 } 183 184 188 public void putValue(String key, String value) { 189 putValue(key, value, -1); 190 } 191 192 196 public void putValue(String key, String value, int index) { 197 SimpleCSCommandManager manager = SimpleCSCommandManager.Instance(); 200 putValueInCombo(key, index); 201 manager.put(key, value); 204 } 205 206 210 public String getValue(String key) { 211 return SimpleCSCommandManager.Instance().get(key); 212 } 213 214 } 215 | Popular Tags |