1 11 package org.eclipse.ui.internal.commands.ws; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Set ; 17 import java.util.SortedSet ; 18 import java.util.TreeSet ; 19 20 import org.eclipse.jface.viewers.IStructuredContentProvider; 21 import org.eclipse.jface.viewers.ITableLabelProvider; 22 import org.eclipse.jface.viewers.LabelProvider; 23 import org.eclipse.jface.viewers.TableViewer; 24 import org.eclipse.jface.viewers.Viewer; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.graphics.Image; 27 import org.eclipse.swt.layout.GridData; 28 import org.eclipse.swt.layout.GridLayout; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Table; 31 import org.eclipse.swt.widgets.TableColumn; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.commands.CommandEvent; 34 import org.eclipse.ui.commands.CommandManagerEvent; 35 import org.eclipse.ui.commands.ICommand; 36 import org.eclipse.ui.commands.ICommandListener; 37 import org.eclipse.ui.commands.ICommandManager; 38 import org.eclipse.ui.commands.ICommandManagerListener; 39 import org.eclipse.ui.commands.IKeySequenceBinding; 40 import org.eclipse.ui.commands.NotDefinedException; 41 import org.eclipse.ui.keys.KeySequence; 42 import org.eclipse.ui.part.ViewPart; 43 44 public class CommandsView extends ViewPart { 45 46 private final class CommandContentProvider implements 47 IStructuredContentProvider { 48 49 private SortedSet commands = new TreeSet (); 50 51 public void dispose() { 52 for (Iterator iterator = commands.iterator(); iterator.hasNext();) { 53 ICommand command = (ICommand) iterator.next(); 54 command.removeCommandListener(commandListener); 55 } 56 57 commands.clear(); 58 } 59 60 public Object [] getElements(Object inputElement) { 61 Set definedCommandIds = new HashSet (commandManager.getDefinedCommandIds()); 62 63 for (Iterator iterator = commands.iterator(); iterator.hasNext();) { 64 ICommand command = (ICommand) iterator.next(); 65 66 if (!definedCommandIds.remove(command.getId())) { 67 command.removeCommandListener(commandListener); 68 commands.remove(command); 69 } 70 } 71 72 for (Iterator iterator = definedCommandIds.iterator(); iterator.hasNext();) { 73 String commandId = (String ) iterator.next(); 74 ICommand command = commandManager.getCommand(commandId); 75 command.addCommandListener(commandListener); 76 commands.add(command); 77 } 78 79 return commands.toArray(); 80 } 81 82 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 83 } 84 } 85 86 private final class CommandLabelProvider extends LabelProvider 87 implements ITableLabelProvider { 88 89 public Image getColumnImage(Object element, int columnIndex) { 90 return null; 91 } 92 93 public String getColumnText(Object element, int columnIndex) { 94 ICommand command = (ICommand) element; 95 96 if (columnIndex == 0) 97 return command.getId(); 98 else if (columnIndex == 1) { 99 try { 100 return command.getName(); 101 } catch (NotDefinedException eNotDefined) { 102 return "<not defined>"; } 104 105 } else if (columnIndex == 2) { 106 StringBuffer stringBuffer = new StringBuffer (); 107 List keySequenceBindings = command.getKeySequenceBindings(); 108 109 for (int i = 0; i < keySequenceBindings.size(); i++) { 110 IKeySequenceBinding keySequenceBinding = (IKeySequenceBinding) keySequenceBindings.get(i); 111 KeySequence keySequence = keySequenceBinding.getKeySequence(); 112 113 if (i >= 1) 114 stringBuffer.append(", "); 116 stringBuffer.append(keySequence.format()); 117 } 118 119 return stringBuffer.toString(); 120 } 121 122 return null; 123 } 124 } 125 126 private ICommandListener commandListener = new ICommandListener() { 127 public void commandChanged(CommandEvent commandEvent) { 128 tableViewer.refresh(); 129 } 130 }; 131 private ICommandManager commandManager; 132 private ICommandManagerListener commandManagerListener = new ICommandManagerListener() { 133 public void commandManagerChanged(CommandManagerEvent commandManagerEvent) { 134 tableViewer.refresh(); 135 } 136 }; 137 138 private Table table; 139 private TableViewer tableViewer; 140 141 public void dispose() { 142 commandManager.removeCommandManagerListener(commandManagerListener); 143 table.dispose(); 144 } 145 146 public void createPartControl(Composite parent) { 147 GridLayout gridLayout = new GridLayout(); 148 gridLayout.marginHeight = 0; 149 gridLayout.marginWidth = 0; 150 parent.setLayout(gridLayout); 151 table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL); 152 table.setHeaderVisible(true); 153 GridData gridData = new GridData(GridData.FILL_BOTH); 154 gridData.heightHint = 200; 155 table.setLayoutData(gridData); 156 TableColumn tableColumnId = new TableColumn(table, SWT.NULL, 0); 157 tableColumnId.setResizable(true); 158 tableColumnId.setText("ID"); tableColumnId.setWidth(200); 160 TableColumn tableColumnName = new TableColumn(table, SWT.NULL, 1); 161 tableColumnName.setResizable(true); 162 tableColumnName.setText("Name"); tableColumnName.setWidth(200); 164 TableColumn tableColumnKeySequences = new TableColumn(table, SWT.NULL, 2); 165 tableColumnKeySequences.setResizable(true); 166 tableColumnKeySequences.setText("Key Sequences"); tableColumnKeySequences.setWidth(200); 168 commandManager = PlatformUI.getWorkbench().getCommandSupport().getCommandManager(); 169 tableViewer = new TableViewer(table); 170 tableViewer.setContentProvider(new CommandContentProvider()); 171 tableViewer.setLabelProvider(new CommandLabelProvider()); 172 tableViewer.setInput(new Object ()); 173 commandManager.addCommandManagerListener(commandManagerListener); 174 tableViewer.refresh(); 175 } 176 177 public void setFocus() { 178 if (table != null && !table.isDisposed()) 179 table.setFocus(); 180 } 181 } | Popular Tags |