KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > ws > CommandView


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

11 package org.eclipse.ui.internal.commands.ws;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.SortedSet JavaDoc;
18 import java.util.TreeSet JavaDoc;
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 CommandView extends ViewPart {
45
46     private final class CommandContentProvider implements
47             IStructuredContentProvider {
48
49         private SortedSet JavaDoc commands = new TreeSet JavaDoc();
50         
51         public void dispose() {
52             for (Iterator JavaDoc 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 JavaDoc[] getElements(Object JavaDoc inputElement) {
61             Set JavaDoc definedCommandIds = new HashSet JavaDoc(commandManager.getDefinedCommandIds());
62             
63             for (Iterator JavaDoc 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 JavaDoc iterator = definedCommandIds.iterator(); iterator.hasNext();) {
73                 String JavaDoc commandId = (String JavaDoc) 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 JavaDoc oldInput, Object JavaDoc newInput) {
83         }
84     }
85
86     private final class CommandLabelProvider extends LabelProvider
87             implements ITableLabelProvider {
88
89         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
90             return null;
91         }
92
93         public String JavaDoc getColumnText(Object JavaDoc 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>"; //$NON-NLS-1$
103
}
104                 
105             } else if (columnIndex == 2) {
106                 StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
107                 List JavaDoc 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(", "); //$NON-NLS-1$
115

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"); //$NON-NLS-1$
159
tableColumnId.setWidth(200);
160         TableColumn tableColumnName = new TableColumn(table, SWT.NULL, 1);
161         tableColumnName.setResizable(true);
162         tableColumnName.setText("Name"); //$NON-NLS-1$
163
tableColumnName.setWidth(200);
164         TableColumn tableColumnKeySequences = new TableColumn(table, SWT.NULL, 2);
165         tableColumnKeySequences.setResizable(true);
166         tableColumnKeySequences.setText("Key Sequences"); //$NON-NLS-1$
167
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 JavaDoc());
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