KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > commands > CommandTreeContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.commands;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.TreeMap JavaDoc;
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; // category grouped content
27
protected final int F_CON_CONTENT = 1; // context grouped content
28

29     private ICommandService fComServ;
30     private TreeMap JavaDoc fCatMap; // mapping of commands to category
31
private TreeMap JavaDoc fConMap; // mapping of commands to context
32
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 JavaDoc(new Comparator JavaDoc() {
42             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
43                 String JavaDoc comA = CommandList.getText(arg0);
44                 String JavaDoc comB = CommandList.getText(arg1);
45                 if (comA != null)
46                     return comA.compareTo(comB);
47                 return +1; // undefined ids should go last
48
}
49         });
50         fConMap = new TreeMap JavaDoc();
51         Command[] commands = fComServ.getDefinedCommands();
52         for (int i = 0; i < commands.length; i++) {
53             /*
54              * IWorkbenchRegistryConstants.AUTOGENERATED_PREFIX = "AUTOGEN:::"
55              */

56             // skip commands with autogenerated id's
57
if (commands[i].getId().startsWith("AUTOGEN:::")) //$NON-NLS-1$
58
continue;
59             // populate category map
60
try {
61                 Category cat = commands[i].getCategory();
62                 ArrayList JavaDoc list = (ArrayList JavaDoc)fCatMap.get(cat);
63                 if (list == null)
64                     fCatMap.put(cat, list = new ArrayList JavaDoc());
65                 list.add(commands[i]);
66             } catch (NotDefinedException e) {
67                 continue;
68             }
69             // TODO: populate context map
70
// can we easily group commands by context?
71
}
72     }
73
74     public Object JavaDoc getParent(Object JavaDoc element) {
75         if (element instanceof Command)
76             try {
77                 return ((Command)element).getCategory();
78             } catch (NotDefinedException e) {
79                 // undefined category - should never hit this as these commands
80
// will not be listed
81
}
82         return null;
83     }
84
85     public void dispose() {
86         fCatMap.clear();
87         fConMap.clear();
88     }
89
90     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
91         if (parentElement instanceof Category) {
92             ArrayList JavaDoc list = (ArrayList JavaDoc) 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 JavaDoc element) {
100         if (element instanceof Category) {
101             ArrayList JavaDoc list = (ArrayList JavaDoc) fCatMap.get(element);
102             if (list != null)
103                 return list.size() > 0;
104         }
105         return false;
106     }
107
108     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
109         switch (fCurContent) {
110         case F_CAT_CONTENT:
111             return fCatMap.keySet().toArray();
112         case F_CON_CONTENT:
113             return new Object JavaDoc[0];
114         }
115         return null;
116     }
117
118     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc 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