001: /*******************************************************************************
002: * Copyright (c) 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.ui.examples.contributions.view;
011:
012: import java.util.Collections;
013: import java.util.List;
014:
015: import org.eclipse.core.commands.AbstractHandler;
016: import org.eclipse.core.commands.ExecutionEvent;
017: import org.eclipse.core.commands.ExecutionException;
018: import org.eclipse.core.commands.IHandler;
019: import org.eclipse.jface.action.GroupMarker;
020: import org.eclipse.jface.action.IMenuListener;
021: import org.eclipse.jface.action.IMenuManager;
022: import org.eclipse.jface.action.MenuManager;
023: import org.eclipse.jface.dialogs.MessageDialog;
024: import org.eclipse.jface.viewers.IStructuredContentProvider;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.jface.viewers.ListViewer;
027: import org.eclipse.jface.viewers.Viewer;
028: import org.eclipse.osgi.util.NLS;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Menu;
032: import org.eclipse.ui.IWorkbenchActionConstants;
033: import org.eclipse.ui.contexts.IContextService;
034: import org.eclipse.ui.examples.contributions.Activator;
035: import org.eclipse.ui.examples.contributions.ContributionMessages;
036: import org.eclipse.ui.examples.contributions.model.Person;
037: import org.eclipse.ui.handlers.IHandlerService;
038: import org.eclipse.ui.part.ViewPart;
039:
040: /**
041: * Our example view.
042: *
043: * @since 3.3
044: */
045: public class InfoView extends ViewPart {
046:
047: private static final String VIEW_COUNT_ID = "org.eclipse.ui.examples.contributions.view.count"; //$NON-NLS-1$
048: private static final String VIEW_CONTEXT_ID = "org.eclipse.ui.examples.contributions.view.context"; //$NON-NLS-1$
049: private ListViewer viewer;
050: private IHandler countHandler;
051:
052: private static class ContentProvider implements
053: IStructuredContentProvider {
054:
055: /*
056: * (non-Javadoc)
057: *
058: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
059: */
060: public void dispose() {
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
067: * java.lang.Object, java.lang.Object)
068: */
069: public void inputChanged(Viewer viewer, Object oldInput,
070: Object newInput) {
071: // nothing to do here
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
078: */
079: public Object[] getElements(Object inputElement) {
080: if (inputElement instanceof List) {
081: return ((List) inputElement).toArray();
082: }
083: return null;
084: }
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
091: */
092: public void createPartControl(Composite parent) {
093: viewer = new ListViewer(parent);
094: viewer.setContentProvider(new ContentProvider());
095: viewer.setLabelProvider(new LabelProvider());
096: viewer.setInput(Activator.getModel());
097: getSite().setSelectionProvider(viewer);
098:
099: MenuManager contextMenu = new MenuManager();
100: contextMenu.setRemoveAllWhenShown(true);
101:
102: // this is to work around complaints about missing standard groups.
103: contextMenu.addMenuListener(new IMenuListener() {
104: public void menuAboutToShow(IMenuManager manager) {
105: manager.add(new GroupMarker(
106: IWorkbenchActionConstants.MB_ADDITIONS));
107: }
108: });
109:
110: getSite().registerContextMenu(contextMenu, viewer);
111: Control control = viewer.getControl();
112: Menu menu = contextMenu.createContextMenu(control);
113: control.setMenu(menu);
114:
115: activateContext();
116: createHandlers();
117: }
118:
119: /*
120: * (non-Javadoc)
121: *
122: * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
123: */
124: public void setFocus() {
125: viewer.getControl().setFocus();
126: }
127:
128: /**
129: * Activate a context that this view uses. It will be tied to this view
130: * activation events and will be removed when the view is disposed.
131: */
132: private void activateContext() {
133: IContextService contextService = (IContextService) getSite()
134: .getService(IContextService.class);
135: contextService.activateContext(VIEW_CONTEXT_ID);
136: }
137:
138: /**
139: * Instantiate any handlers specific to this view and activate them.
140: */
141: private void createHandlers() {
142: IHandlerService handlerService = (IHandlerService) getSite()
143: .getService(IHandlerService.class);
144: countHandler = new AbstractHandler() {
145: public Object execute(ExecutionEvent event)
146: throws ExecutionException {
147: List elements = (List) viewer.getInput();
148: MessageDialog
149: .openInformation(
150: getSite().getShell(),
151: ContributionMessages.SampleHandler_plugin_name,
152: NLS
153: .bind(
154: ContributionMessages.InfoView_countElements,
155: new Integer(elements
156: .size())));
157: return null;
158: }
159: };
160: handlerService.activateHandler(VIEW_COUNT_ID, countHandler);
161: }
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see org.eclipse.ui.part.WorkbenchPart#dispose()
167: */
168: public void dispose() {
169: if (countHandler != null) {
170: // we must dispose our handlers, although in this case it will
171: // be a no-op
172: countHandler.dispose();
173: countHandler = null;
174: }
175: super .dispose();
176: }
177:
178: /**
179: * Swap the 2 given elements from the model.
180: *
181: * @param p1
182: * @param p2
183: */
184: public void swap(Person p1, Person p2) {
185: List elements = Activator.getModel();
186: int i1 = elements.indexOf(p1);
187: int i2 = elements.indexOf(p2);
188: Collections.swap(elements, i1, i2);
189: viewer.refresh();
190: }
191:
192: /**
193: * Refresh the viewer from the model.
194: */
195: public void refresh() {
196: viewer.refresh();
197: }
198: }
|