1 11 package org.eclipse.pde.internal.ui.commands; 12 13 import java.util.ArrayList ; 14 import java.util.Comparator ; 15 import java.util.TreeMap ; 16 17 import org.eclipse.core.commands.Category; 18 import org.eclipse.core.commands.Command; 19 import org.eclipse.core.commands.common.NotDefinedException; 20 import org.eclipse.jface.viewers.ITreeContentProvider; 21 import org.eclipse.jface.viewers.Viewer; 22 import org.eclipse.ui.commands.ICommandService; 23 24 public class CommandTreeContentProvider implements ITreeContentProvider { 25 26 protected final int F_CAT_CONTENT = 0; protected final int F_CON_CONTENT = 1; 29 private ICommandService fComServ; 30 private TreeMap fCatMap; private TreeMap fConMap; private Viewer fViewer; 33 private int fCurContent = F_CAT_CONTENT; 34 35 public CommandTreeContentProvider(ICommandService comServ) { 36 fComServ = comServ; 37 init(); 38 } 39 40 private void init() { 41 fCatMap = new TreeMap (new Comparator () { 42 public int compare(Object arg0, Object arg1) { 43 String comA = CommandList.getText(arg0); 44 String comB = CommandList.getText(arg1); 45 if (comA != null) 46 return comA.compareTo(comB); 47 return +1; } 49 }); 50 fConMap = new TreeMap (); 51 Command[] commands = fComServ.getDefinedCommands(); 52 for (int i = 0; i < commands.length; i++) { 53 56 if (commands[i].getId().startsWith("AUTOGEN:::")) continue; 59 try { 61 Category cat = commands[i].getCategory(); 62 ArrayList list = (ArrayList )fCatMap.get(cat); 63 if (list == null) 64 fCatMap.put(cat, list = new ArrayList ()); 65 list.add(commands[i]); 66 } catch (NotDefinedException e) { 67 continue; 68 } 69 } 72 } 73 74 public Object getParent(Object element) { 75 if (element instanceof Command) 76 try { 77 return ((Command)element).getCategory(); 78 } catch (NotDefinedException e) { 79 } 82 return null; 83 } 84 85 public void dispose() { 86 fCatMap.clear(); 87 fConMap.clear(); 88 } 89 90 public Object [] getChildren(Object parentElement) { 91 if (parentElement instanceof Category) { 92 ArrayList list = (ArrayList ) fCatMap.get(parentElement); 93 if (list != null) 94 return list.toArray(new Command[list.size()]); 95 } 96 return null; 97 } 98 99 public boolean hasChildren(Object element) { 100 if (element instanceof Category) { 101 ArrayList list = (ArrayList ) fCatMap.get(element); 102 if (list != null) 103 return list.size() > 0; 104 } 105 return false; 106 } 107 108 public Object [] getElements(Object inputElement) { 109 switch (fCurContent) { 110 case F_CAT_CONTENT: 111 return fCatMap.keySet().toArray(); 112 case F_CON_CONTENT: 113 return new Object [0]; 114 } 115 return null; 116 } 117 118 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 119 fViewer = viewer; 120 } 121 122 public void refreshWithCategoryContent() { 123 fCurContent = F_CAT_CONTENT; 124 if (fViewer != null) 125 fViewer.refresh(); 126 } 127 128 public void refreshWithContextContent() { 129 fCurContent = F_CON_CONTENT; 130 if (fViewer != null) 131 fViewer.refresh(); 132 } 133 134 } | Popular Tags |