1 11 package org.eclipse.pde.internal.ui.commands; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.regex.Pattern ; 16 import java.util.regex.PatternSyntaxException ; 17 18 import org.eclipse.core.commands.Category; 19 import org.eclipse.core.commands.Command; 20 import org.eclipse.core.commands.common.NotDefinedException; 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.action.ToolBarManager; 24 import org.eclipse.jface.resource.ImageDescriptor; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.ISelectionChangedListener; 27 import org.eclipse.jface.viewers.ITreeContentProvider; 28 import org.eclipse.jface.viewers.LabelProvider; 29 import org.eclipse.jface.viewers.StructuredSelection; 30 import org.eclipse.jface.viewers.TreeViewer; 31 import org.eclipse.jface.viewers.Viewer; 32 import org.eclipse.jface.viewers.ViewerComparator; 33 import org.eclipse.jface.viewers.ViewerFilter; 34 import org.eclipse.pde.internal.core.util.PatternConstructor; 35 import org.eclipse.pde.internal.ui.PDEPluginImages; 36 import org.eclipse.pde.internal.ui.PDEUIMessages; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.events.DisposeEvent; 39 import org.eclipse.swt.events.DisposeListener; 40 import org.eclipse.swt.events.KeyAdapter; 41 import org.eclipse.swt.events.KeyEvent; 42 import org.eclipse.swt.events.ModifyEvent; 43 import org.eclipse.swt.events.ModifyListener; 44 import org.eclipse.swt.graphics.Cursor; 45 import org.eclipse.swt.graphics.Image; 46 import org.eclipse.swt.layout.GridData; 47 import org.eclipse.swt.layout.GridLayout; 48 import org.eclipse.swt.widgets.Composite; 49 import org.eclipse.swt.widgets.Display; 50 import org.eclipse.swt.widgets.Text; 51 import org.eclipse.swt.widgets.ToolBar; 52 import org.eclipse.swt.widgets.Tree; 53 import org.eclipse.ui.PlatformUI; 54 import org.eclipse.ui.forms.events.HyperlinkAdapter; 55 import org.eclipse.ui.forms.events.HyperlinkEvent; 56 import org.eclipse.ui.forms.widgets.ExpandableComposite; 57 import org.eclipse.ui.forms.widgets.FormToolkit; 58 import org.eclipse.ui.forms.widgets.ImageHyperlink; 59 import org.eclipse.ui.forms.widgets.Section; 60 import org.eclipse.ui.internal.commands.ICommandImageService; 61 62 public class CommandList { 63 64 protected class CommandTreeLabelProvider extends LabelProvider { 65 private HashMap fImgMap = new HashMap (); 66 private Image fDefaultImage; 67 public String getText(Object element) { 68 if (element instanceof Category) 69 return CommandList.getText(element); 70 else if (element instanceof Command) 71 return CommandList.getText(element); 72 return null; 73 } 74 public Image getImage(Object element) { 75 Image img = (Image)fImgMap.get(element); 76 if (img != null) 77 return img; 78 79 if (element instanceof Category) 80 img = PDEPluginImages.DESC_COMGROUP_OBJ.createImage(); 81 else if (element instanceof Command) { 82 ImageDescriptor desc = fComImgServ.getImageDescriptor(((Command)element).getId()); 83 if (desc == null) { 84 if (fDefaultImage == null) 85 fDefaultImage = PDEPluginImages.DESC_BUILD_VAR_OBJ.createImage(); 86 return fDefaultImage; 87 } 88 img = desc.createImage(); 89 } 90 91 if (img != null) 92 fImgMap.put(element, img); 93 94 return img; 95 } 96 public void dispose() { 97 for (Iterator i = fImgMap.keySet().iterator(); i.hasNext();) 98 ((Image)fImgMap.get(i.next())).dispose(); 99 if (fDefaultImage != null) 100 fDefaultImage.dispose(); 101 super.dispose(); 102 } 103 } 104 105 protected class CommandTreeComparator extends ViewerComparator { 106 public int compare(Viewer viewer, Object e1, Object e2) { 107 return getText(e1).compareTo(getText(e2)); 108 } 109 } 110 111 protected class WildcardFilter extends ViewerFilter { 112 public boolean select(Viewer viewer, Object parentElement, Object element) { 113 String filterText = fFilterText.getText(); 114 if (filterText.length() == 0) 115 return true; 116 String wMatch = '*' + filterText + '*'; 117 Pattern pattern = null; 118 try { 119 pattern = PatternConstructor.createPattern(wMatch, false); 120 } catch (PatternSyntaxException e) { 121 return false; 122 } 123 if (element instanceof Category) { 124 ITreeContentProvider prov = (ITreeContentProvider)fTreeViewer.getContentProvider(); 125 Command[] commands = (Command[])prov.getChildren(element); 126 for (int i = 0; i < commands.length; i++) { 127 String text = getText(commands[i]); 128 if (pattern.matcher(text.subSequence(0, text.length())).matches()) 129 return true; 130 } 131 return false; 132 } 133 String text = getText(element); 134 return pattern.matcher(text.subSequence(0, text.length())).matches(); 135 } 136 } 137 138 protected static String getText(Object obj) { 139 if (obj instanceof Command) { 140 Command com = (Command)obj; 141 try { 142 return com.getName(); 143 } catch (NotDefinedException e) { 144 return com.getId(); 145 } 146 } else if (obj instanceof Category) { 147 Category cat = (Category)obj; 148 try { 149 return cat.getName(); 150 } catch (NotDefinedException e) { 151 return cat.getId(); 152 } 153 } 154 return new String (); 155 } 156 157 private class CollapseAction extends Action { 158 public CollapseAction() { 159 super(PDEUIMessages.CommandList_collapseAll0, IAction.AS_PUSH_BUTTON); 160 setImageDescriptor(PDEPluginImages.DESC_COLLAPSE_ALL); 161 setToolTipText(PDEUIMessages.CommandList_collapseAll0); 162 } 163 164 } 165 166 private CommandComposerPart fCCP; 167 private FormToolkit fToolkit; 168 private Text fFilterText; 169 private TreeViewer fTreeViewer; 170 private CommandTreeContentProvider fContentProvider; 171 private ICommandImageService fComImgServ; 172 173 public CommandList(CommandComposerPart cv, Composite parent) { 174 fCCP = cv; 175 fToolkit = cv.getToolkit(); 176 createTree(parent); 177 fComImgServ = (ICommandImageService) PlatformUI.getWorkbench().getAdapter(ICommandImageService.class); 178 } 179 private void createTree(Composite parent) { 180 Composite c = fCCP.createComposite(parent, GridData.FILL_BOTH, 1, true, 5); 181 182 Section section = fToolkit.createSection(c, ExpandableComposite.TITLE_BAR); 183 section.setText(PDEUIMessages.CommandList_groupName); 184 section.setLayoutData(new GridData(GridData.FILL_BOTH)); 185 186 Composite comp = fCCP.createComposite(section); 187 188 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT); 189 ToolBar toolbar = toolBarManager.createControl(section); 190 final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); 191 toolbar.setCursor(handCursor); 192 toolBarManager.add(new CollapseAction() { 193 public void run() { 194 fTreeViewer.collapseAll(); 195 } 196 }); 197 toolbar.addDisposeListener(new DisposeListener() { 198 public void widgetDisposed(DisposeEvent e) { 199 handCursor.dispose(); 200 } 201 }); 202 fToolkit.adapt(toolbar, true, true); 203 toolbar.setBackground(null); 204 section.setTextClient(toolbar); 205 toolBarManager.update(true); 206 207 createFilterText(comp); 208 209 Tree tree = fToolkit.createTree(comp, SWT.V_SCROLL | SWT.H_SCROLL); 210 tree.setLayout(new GridLayout()); 211 GridData gd = new GridData(GridData.FILL_BOTH); 212 gd.widthHint = 200; 213 tree.setLayoutData(gd); 214 fTreeViewer = new TreeViewer(tree); 215 fContentProvider = new CommandTreeContentProvider(fCCP.getCommandService()); 216 fTreeViewer.setContentProvider(fContentProvider); 217 fTreeViewer.setLabelProvider(new CommandTreeLabelProvider()); 218 fTreeViewer.setComparator(new CommandTreeComparator()); 219 fTreeViewer.addFilter(new WildcardFilter()); 220 fTreeViewer.setInput(new Object ()); 221 fTreeViewer.addSelectionChangedListener(fCCP); 222 223 section.setClient(comp); 224 } 225 226 protected void addTreeSelectionListener(ISelectionChangedListener listener) { 227 if (listener != null) 228 fTreeViewer.addSelectionChangedListener(listener); 229 } 230 231 private void createFilterText(Composite parent) { 232 Composite c = fCCP.createComposite(parent, GridData.FILL_HORIZONTAL, 3, false, 0); 233 fFilterText = fToolkit.createText(c, "", SWT.BORDER); fFilterText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 235 fFilterText.addKeyListener(new KeyAdapter() { 236 public void keyPressed(KeyEvent e) { 237 if (e.keyCode == SWT.ARROW_DOWN) fTreeViewer.getControl().setFocus(); 238 } 239 }); 240 241 final ImageHyperlink clearButton = fToolkit.createImageHyperlink(c, SWT.NONE); 242 final Image hoverImg = PDEPluginImages.DESC_CLEAR.createImage(); 243 clearButton.setImage(hoverImg); 244 clearButton.setToolTipText(PDEUIMessages.CommandList_clearTooltip); 245 clearButton.addDisposeListener(new DisposeListener() { 246 public void widgetDisposed(DisposeEvent e) { 247 hoverImg.dispose(); 248 } 249 }); 250 clearButton.addHyperlinkListener(new HyperlinkAdapter() { 251 public void linkActivated(HyperlinkEvent e) { 252 fFilterText.setText(""); } 254 }); 255 clearButton.setVisible(false); 256 257 fFilterText.addModifyListener(new ModifyListener() { 258 public void modifyText(ModifyEvent e) { 259 fTreeViewer.refresh(); 260 clearButton.setVisible(fFilterText.getText().length() > 0); 261 } 262 }); 263 } 264 265 public void setFocus() { 266 fFilterText.setFocus(); 267 } 268 269 protected void setSelection(Object object) { 270 if (fTreeViewer != null && object != null) 271 fTreeViewer.setSelection(new StructuredSelection(object)); 272 } 273 274 277 public ISelection getSelection() { 278 return fTreeViewer.getSelection(); 279 } 280 281 } 282 | Popular Tags |