001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.commands;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.regex.Pattern;
015: import java.util.regex.PatternSyntaxException;
016:
017: import org.eclipse.core.commands.Category;
018: import org.eclipse.core.commands.Command;
019: import org.eclipse.core.commands.common.NotDefinedException;
020: import org.eclipse.jface.action.Action;
021: import org.eclipse.jface.action.IAction;
022: import org.eclipse.jface.action.ToolBarManager;
023: import org.eclipse.jface.resource.ImageDescriptor;
024: import org.eclipse.jface.viewers.ISelection;
025: import org.eclipse.jface.viewers.ISelectionChangedListener;
026: import org.eclipse.jface.viewers.ITreeContentProvider;
027: import org.eclipse.jface.viewers.LabelProvider;
028: import org.eclipse.jface.viewers.StructuredSelection;
029: import org.eclipse.jface.viewers.TreeViewer;
030: import org.eclipse.jface.viewers.Viewer;
031: import org.eclipse.jface.viewers.ViewerComparator;
032: import org.eclipse.jface.viewers.ViewerFilter;
033: import org.eclipse.pde.internal.core.util.PatternConstructor;
034: import org.eclipse.pde.internal.ui.PDEPluginImages;
035: import org.eclipse.pde.internal.ui.PDEUIMessages;
036: import org.eclipse.swt.SWT;
037: import org.eclipse.swt.events.DisposeEvent;
038: import org.eclipse.swt.events.DisposeListener;
039: import org.eclipse.swt.events.KeyAdapter;
040: import org.eclipse.swt.events.KeyEvent;
041: import org.eclipse.swt.events.ModifyEvent;
042: import org.eclipse.swt.events.ModifyListener;
043: import org.eclipse.swt.graphics.Cursor;
044: import org.eclipse.swt.graphics.Image;
045: import org.eclipse.swt.layout.GridData;
046: import org.eclipse.swt.layout.GridLayout;
047: import org.eclipse.swt.widgets.Composite;
048: import org.eclipse.swt.widgets.Display;
049: import org.eclipse.swt.widgets.Text;
050: import org.eclipse.swt.widgets.ToolBar;
051: import org.eclipse.swt.widgets.Tree;
052: import org.eclipse.ui.PlatformUI;
053: import org.eclipse.ui.forms.events.HyperlinkAdapter;
054: import org.eclipse.ui.forms.events.HyperlinkEvent;
055: import org.eclipse.ui.forms.widgets.ExpandableComposite;
056: import org.eclipse.ui.forms.widgets.FormToolkit;
057: import org.eclipse.ui.forms.widgets.ImageHyperlink;
058: import org.eclipse.ui.forms.widgets.Section;
059: import org.eclipse.ui.internal.commands.ICommandImageService;
060:
061: public class CommandList {
062:
063: protected class CommandTreeLabelProvider extends LabelProvider {
064: private HashMap fImgMap = new HashMap();
065: private Image fDefaultImage;
066:
067: public String getText(Object element) {
068: if (element instanceof Category)
069: return CommandList.getText(element);
070: else if (element instanceof Command)
071: return CommandList.getText(element);
072: return null;
073: }
074:
075: public Image getImage(Object element) {
076: Image img = (Image) fImgMap.get(element);
077: if (img != null)
078: return img;
079:
080: if (element instanceof Category)
081: img = PDEPluginImages.DESC_COMGROUP_OBJ.createImage();
082: else if (element instanceof Command) {
083: ImageDescriptor desc = fComImgServ
084: .getImageDescriptor(((Command) element).getId());
085: if (desc == null) {
086: if (fDefaultImage == null)
087: fDefaultImage = PDEPluginImages.DESC_BUILD_VAR_OBJ
088: .createImage();
089: return fDefaultImage;
090: }
091: img = desc.createImage();
092: }
093:
094: if (img != null)
095: fImgMap.put(element, img);
096:
097: return img;
098: }
099:
100: public void dispose() {
101: for (Iterator i = fImgMap.keySet().iterator(); i.hasNext();)
102: ((Image) fImgMap.get(i.next())).dispose();
103: if (fDefaultImage != null)
104: fDefaultImage.dispose();
105: super .dispose();
106: }
107: }
108:
109: protected class CommandTreeComparator extends ViewerComparator {
110: public int compare(Viewer viewer, Object e1, Object e2) {
111: return getText(e1).compareTo(getText(e2));
112: }
113: }
114:
115: protected class WildcardFilter extends ViewerFilter {
116: public boolean select(Viewer viewer, Object parentElement,
117: Object element) {
118: String filterText = fFilterText.getText();
119: if (filterText.length() == 0)
120: return true;
121: String wMatch = '*' + filterText + '*';
122: Pattern pattern = null;
123: try {
124: pattern = PatternConstructor.createPattern(wMatch,
125: false);
126: } catch (PatternSyntaxException e) {
127: return false;
128: }
129: if (element instanceof Category) {
130: ITreeContentProvider prov = (ITreeContentProvider) fTreeViewer
131: .getContentProvider();
132: Command[] commands = (Command[]) prov
133: .getChildren(element);
134: for (int i = 0; i < commands.length; i++) {
135: String text = getText(commands[i]);
136: if (pattern.matcher(
137: text.subSequence(0, text.length()))
138: .matches())
139: return true;
140: }
141: return false;
142: }
143: String text = getText(element);
144: return pattern.matcher(text.subSequence(0, text.length()))
145: .matches();
146: }
147: }
148:
149: protected static String getText(Object obj) {
150: if (obj instanceof Command) {
151: Command com = (Command) obj;
152: try {
153: return com.getName();
154: } catch (NotDefinedException e) {
155: return com.getId();
156: }
157: } else if (obj instanceof Category) {
158: Category cat = (Category) obj;
159: try {
160: return cat.getName();
161: } catch (NotDefinedException e) {
162: return cat.getId();
163: }
164: }
165: return new String();
166: }
167:
168: private class CollapseAction extends Action {
169: public CollapseAction() {
170: super (PDEUIMessages.CommandList_collapseAll0,
171: IAction.AS_PUSH_BUTTON);
172: setImageDescriptor(PDEPluginImages.DESC_COLLAPSE_ALL);
173: setToolTipText(PDEUIMessages.CommandList_collapseAll0);
174: }
175:
176: }
177:
178: private CommandComposerPart fCCP;
179: private FormToolkit fToolkit;
180: private Text fFilterText;
181: private TreeViewer fTreeViewer;
182: private CommandTreeContentProvider fContentProvider;
183: private ICommandImageService fComImgServ;
184:
185: public CommandList(CommandComposerPart cv, Composite parent) {
186: fCCP = cv;
187: fToolkit = cv.getToolkit();
188: createTree(parent);
189: fComImgServ = (ICommandImageService) PlatformUI.getWorkbench()
190: .getAdapter(ICommandImageService.class);
191: }
192:
193: private void createTree(Composite parent) {
194: Composite c = fCCP.createComposite(parent, GridData.FILL_BOTH,
195: 1, true, 5);
196:
197: Section section = fToolkit.createSection(c,
198: ExpandableComposite.TITLE_BAR);
199: section.setText(PDEUIMessages.CommandList_groupName);
200: section.setLayoutData(new GridData(GridData.FILL_BOTH));
201:
202: Composite comp = fCCP.createComposite(section);
203:
204: ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
205: ToolBar toolbar = toolBarManager.createControl(section);
206: final Cursor handCursor = new Cursor(Display.getCurrent(),
207: SWT.CURSOR_HAND);
208: toolbar.setCursor(handCursor);
209: toolBarManager.add(new CollapseAction() {
210: public void run() {
211: fTreeViewer.collapseAll();
212: }
213: });
214: toolbar.addDisposeListener(new DisposeListener() {
215: public void widgetDisposed(DisposeEvent e) {
216: handCursor.dispose();
217: }
218: });
219: fToolkit.adapt(toolbar, true, true);
220: toolbar.setBackground(null);
221: section.setTextClient(toolbar);
222: toolBarManager.update(true);
223:
224: createFilterText(comp);
225:
226: Tree tree = fToolkit.createTree(comp, SWT.V_SCROLL
227: | SWT.H_SCROLL);
228: tree.setLayout(new GridLayout());
229: GridData gd = new GridData(GridData.FILL_BOTH);
230: gd.widthHint = 200;
231: tree.setLayoutData(gd);
232: fTreeViewer = new TreeViewer(tree);
233: fContentProvider = new CommandTreeContentProvider(fCCP
234: .getCommandService());
235: fTreeViewer.setContentProvider(fContentProvider);
236: fTreeViewer.setLabelProvider(new CommandTreeLabelProvider());
237: fTreeViewer.setComparator(new CommandTreeComparator());
238: fTreeViewer.addFilter(new WildcardFilter());
239: fTreeViewer.setInput(new Object());
240: fTreeViewer.addSelectionChangedListener(fCCP);
241:
242: section.setClient(comp);
243: }
244:
245: protected void addTreeSelectionListener(
246: ISelectionChangedListener listener) {
247: if (listener != null)
248: fTreeViewer.addSelectionChangedListener(listener);
249: }
250:
251: private void createFilterText(Composite parent) {
252: Composite c = fCCP.createComposite(parent,
253: GridData.FILL_HORIZONTAL, 3, false, 0);
254: fFilterText = fToolkit.createText(c, "", SWT.BORDER); //$NON-NLS-1$
255: fFilterText
256: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
257: fFilterText.addKeyListener(new KeyAdapter() {
258: public void keyPressed(KeyEvent e) {
259: if (e.keyCode == SWT.ARROW_DOWN)
260: fTreeViewer.getControl().setFocus();
261: }
262: });
263:
264: final ImageHyperlink clearButton = fToolkit
265: .createImageHyperlink(c, SWT.NONE);
266: final Image hoverImg = PDEPluginImages.DESC_CLEAR.createImage();
267: clearButton.setImage(hoverImg);
268: clearButton
269: .setToolTipText(PDEUIMessages.CommandList_clearTooltip);
270: clearButton.addDisposeListener(new DisposeListener() {
271: public void widgetDisposed(DisposeEvent e) {
272: hoverImg.dispose();
273: }
274: });
275: clearButton.addHyperlinkListener(new HyperlinkAdapter() {
276: public void linkActivated(HyperlinkEvent e) {
277: fFilterText.setText(""); //$NON-NLS-1$
278: }
279: });
280: clearButton.setVisible(false);
281:
282: fFilterText.addModifyListener(new ModifyListener() {
283: public void modifyText(ModifyEvent e) {
284: fTreeViewer.refresh();
285: clearButton
286: .setVisible(fFilterText.getText().length() > 0);
287: }
288: });
289: }
290:
291: public void setFocus() {
292: fFilterText.setFocus();
293: }
294:
295: protected void setSelection(Object object) {
296: if (fTreeViewer != null && object != null)
297: fTreeViewer.setSelection(new StructuredSelection(object));
298: }
299:
300: /**
301: * @return
302: */
303: public ISelection getSelection() {
304: return fTreeViewer.getSelection();
305: }
306:
307: }
|