001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.readmetool;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.runtime.IAdaptable;
014: import org.eclipse.core.runtime.IProgressMonitor;
015: import org.eclipse.jface.action.Action;
016: import org.eclipse.jface.action.MenuManager;
017: import org.eclipse.jface.action.Separator;
018: import org.eclipse.jface.dialogs.MessageDialog;
019: import org.eclipse.jface.viewers.ISelectionChangedListener;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.TreeViewer;
022: import org.eclipse.swt.dnd.DND;
023: import org.eclipse.swt.dnd.TextTransfer;
024: import org.eclipse.swt.dnd.Transfer;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Menu;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.ui.IWorkbenchActionConstants;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.model.WorkbenchContentProvider;
031: import org.eclipse.ui.model.WorkbenchLabelProvider;
032: import org.eclipse.ui.part.PluginTransfer;
033: import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
034:
035: /**
036: * Content outline page for the readme editor.
037: */
038: public class ReadmeContentOutlinePage extends ContentOutlinePage {
039: protected IFile input;
040:
041: class OutlineAction extends Action {
042: private Shell shell;
043:
044: public OutlineAction(String label) {
045: super (label);
046: getTreeViewer().addSelectionChangedListener(
047: new ISelectionChangedListener() {
048: public void selectionChanged(
049: SelectionChangedEvent event) {
050: setEnabled(!event.getSelection().isEmpty());
051: }
052: });
053: }
054:
055: public void setShell(Shell shell) {
056: this .shell = shell;
057: }
058:
059: public void run() {
060: MessageDialog.openInformation(shell, MessageUtil
061: .getString("Readme_Outline"), //$NON-NLS-1$
062: MessageUtil
063: .getString("ReadmeOutlineActionExecuted")); //$NON-NLS-1$
064: }
065: }
066:
067: /**
068: * Creates a new ReadmeContentOutlinePage.
069: */
070: public ReadmeContentOutlinePage(IFile input) {
071: super ();
072: this .input = input;
073: }
074:
075: /**
076: * Creates the control and registers the popup menu for this page
077: * Menu id "org.eclipse.ui.examples.readmetool.outline"
078: */
079: public void createControl(Composite parent) {
080: super .createControl(parent);
081:
082: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
083: IReadmeConstants.CONTENT_OUTLINE_PAGE_CONTEXT);
084:
085: TreeViewer viewer = getTreeViewer();
086: viewer.setContentProvider(new WorkbenchContentProvider());
087: viewer.setLabelProvider(new WorkbenchLabelProvider());
088: viewer.setInput(getContentOutline(input));
089: initDragAndDrop();
090:
091: // Configure the context menu.
092: MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
093: menuMgr.add(new Separator(
094: IWorkbenchActionConstants.MB_ADDITIONS));
095: menuMgr.add(new Separator(
096: IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
097:
098: Menu menu = menuMgr.createContextMenu(viewer.getTree());
099: viewer.getTree().setMenu(menu);
100: // Be sure to register it so that other plug-ins can add actions.
101: getSite()
102: .registerContextMenu(
103: "org.eclipse.ui.examples.readmetool.outline", menuMgr, viewer); //$NON-NLS-1$
104:
105: getSite().getActionBars().setGlobalActionHandler(
106: IReadmeConstants.RETARGET2,
107: new OutlineAction(MessageUtil
108: .getString("Outline_Action2"))); //$NON-NLS-1$
109:
110: OutlineAction action = new OutlineAction(MessageUtil
111: .getString("Outline_Action3")); //$NON-NLS-1$
112: action.setToolTipText(MessageUtil
113: .getString("Readme_Outline_Action3")); //$NON-NLS-1$
114: getSite().getActionBars().setGlobalActionHandler(
115: IReadmeConstants.LABELRETARGET3, action);
116: action = new OutlineAction(MessageUtil
117: .getString("Outline_Action4")); //$NON-NLS-1$
118: getSite().getActionBars().setGlobalActionHandler(
119: IReadmeConstants.ACTION_SET_RETARGET4, action);
120: action = new OutlineAction(MessageUtil
121: .getString("Outline_Action5")); //$NON-NLS-1$
122: action.setToolTipText(MessageUtil
123: .getString("Readme_Outline_Action5")); //$NON-NLS-1$
124: getSite().getActionBars().setGlobalActionHandler(
125: IReadmeConstants.ACTION_SET_LABELRETARGET5, action);
126: }
127:
128: /**
129: * Gets the content outline for a given input element.
130: * Returns the outline (a list of MarkElements), or null
131: * if the outline could not be generated.
132: */
133: private IAdaptable getContentOutline(IAdaptable input) {
134: return ReadmeModelFactory.getInstance()
135: .getContentOutline(input);
136: }
137:
138: /**
139: * Initializes drag and drop for this content outline page.
140: */
141: private void initDragAndDrop() {
142: int ops = DND.DROP_COPY | DND.DROP_MOVE;
143: Transfer[] transfers = new Transfer[] {
144: TextTransfer.getInstance(),
145: PluginTransfer.getInstance() };
146: getTreeViewer().addDragSupport(ops, transfers,
147: new ReadmeContentOutlineDragListener(this ));
148: }
149:
150: /**
151: * Forces the page to update its contents.
152: *
153: * @see ReadmeEditor#doSave(IProgressMonitor)
154: */
155: public void update() {
156: getControl().setRedraw(false);
157: getTreeViewer().setInput(getContentOutline(input));
158: getTreeViewer().expandAll();
159: getControl().setRedraw(true);
160: }
161: }
|