001: /*******************************************************************************
002: * Copyright (c) 2006, 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.pde.internal.ui.editor.toc;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IWorkspaceRoot;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.jdt.ui.JavaUI;
022: import org.eclipse.jface.action.ControlContribution;
023: import org.eclipse.jface.action.IToolBarManager;
024: import org.eclipse.jface.viewers.ISelection;
025: import org.eclipse.jface.viewers.IStructuredSelection;
026: import org.eclipse.jface.viewers.StructuredSelection;
027: import org.eclipse.jface.window.Window;
028: import org.eclipse.jface.wizard.WizardDialog;
029: import org.eclipse.pde.core.IModel;
030: import org.eclipse.pde.internal.core.text.toc.TocModel;
031: import org.eclipse.pde.internal.core.text.toc.TocObject;
032: import org.eclipse.pde.internal.ui.IPDEUIConstants;
033: import org.eclipse.pde.internal.ui.PDEPlugin;
034: import org.eclipse.pde.internal.ui.PDEUIMessages;
035: import org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage;
036: import org.eclipse.pde.internal.ui.editor.MultiSourceEditor;
037: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
038: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
039: import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
040: import org.eclipse.pde.internal.ui.editor.context.InputContext;
041: import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
042: import org.eclipse.pde.internal.ui.wizards.toc.RegisterTocWizard;
043: import org.eclipse.swt.SWT;
044: import org.eclipse.swt.widgets.Composite;
045: import org.eclipse.swt.widgets.Control;
046: import org.eclipse.ui.IEditorInput;
047: import org.eclipse.ui.IFileEditorInput;
048: import org.eclipse.ui.IPageLayout;
049: import org.eclipse.ui.IStorageEditorInput;
050: import org.eclipse.ui.PartInitException;
051: import org.eclipse.ui.PlatformUI;
052: import org.eclipse.ui.forms.editor.IFormPage;
053: import org.eclipse.ui.forms.events.HyperlinkEvent;
054: import org.eclipse.ui.forms.events.IHyperlinkListener;
055: import org.eclipse.ui.forms.widgets.ImageHyperlink;
056: import org.eclipse.ui.part.IShowInSource;
057: import org.eclipse.ui.part.IShowInTargetList;
058: import org.eclipse.ui.part.ShowInContext;
059:
060: /**
061: * TocEditor
062: *
063: */
064: public class TocEditor extends MultiSourceEditor {
065:
066: private ImageHyperlink fImageHyperlinkRegisterTOC;
067:
068: /**
069: *
070: */
071: public TocEditor() {
072: super ();
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getEditorID()
077: */
078: protected String getEditorID() {
079: return IPDEUIConstants.TABLE_OF_CONTENTS_EDITOR_ID;
080: }
081:
082: public Object getAdapter(Class adapter) {
083: if (isShowInApplicable()) {
084: if (adapter == IShowInSource.class) {
085: return getShowInSource();
086: } else if (adapter == IShowInTargetList.class) {
087: return getShowInTargetList();
088: }
089: }
090:
091: return super .getAdapter(adapter);
092: }
093:
094: private boolean isShowInApplicable() {
095: if (getSelection().isEmpty()) {
096: return false;
097: }
098:
099: if (getSelection() instanceof IStructuredSelection) {
100: IStructuredSelection selection = (IStructuredSelection) getSelection();
101: for (Iterator iter = selection.iterator(); iter.hasNext();) {
102: Object obj = iter.next();
103: if (!(obj instanceof TocObject))
104: return false;
105: if (((TocObject) obj).getPath() == null)
106: return false;
107: }
108:
109: return true;
110: }
111:
112: return false;
113: }
114:
115: /**
116: * Returns the <code>IShowInSource</code> for this section.
117: * @return the <code>IShowInSource</code>
118: */
119: private IShowInSource getShowInSource() {
120: return new IShowInSource() {
121: public ShowInContext getShowInContext() {
122: ArrayList resourceList = new ArrayList();
123: IStructuredSelection selection = (IStructuredSelection) getSelection();
124: IStructuredSelection resources;
125: if (selection.isEmpty()) {
126: resources = null;
127: } else {
128: IWorkspaceRoot root = PDEPlugin.getWorkspace()
129: .getRoot();
130:
131: for (Iterator iter = selection.iterator(); iter
132: .hasNext();) {
133: Object obj = iter.next();
134: if (obj instanceof TocObject
135: && ((TocObject) obj).getPath() != null) {
136: Path resourcePath = new Path(
137: ((TocObject) obj).getPath());
138:
139: if (!resourcePath.isEmpty()) {
140: TocModel model = (TocModel) getAggregateModel();
141:
142: IPath pluginPath = model
143: .getUnderlyingResource()
144: .getProject().getFullPath();
145: IResource resource = root
146: .findMember(pluginPath
147: .append(resourcePath));
148:
149: if (resource != null) {
150: resourceList.add(resource);
151: }
152: }
153: }
154: }
155:
156: resources = new StructuredSelection(resourceList);
157: }
158:
159: return new ShowInContext(null, resources);
160: }
161: };
162: }
163:
164: /**
165: * Returns the <code>IShowInTargetList</code> for this section.
166: * @return the <code>IShowInTargetList</code>
167: */
168: private IShowInTargetList getShowInTargetList() {
169: return new IShowInTargetList() {
170: public String[] getShowInTargetIds() {
171: return new String[] { JavaUI.ID_PACKAGES,
172: IPageLayout.ID_RES_NAV };
173: }
174: };
175: }
176:
177: /* (non-Javadoc)
178: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#isSaveAsAllowed()
179: */
180: public boolean isSaveAsAllowed() {
181: return true;
182: }
183:
184: /* (non-Javadoc)
185: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getContextIDForSaveAs()
186: */
187: public String getContextIDForSaveAs() {
188: return TocInputContext.CONTEXT_ID;
189: }
190:
191: /* (non-Javadoc)
192: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#addEditorPages()
193: */
194: protected void addEditorPages() {
195: try {
196: addPage(new TocPage(this ));
197: } catch (PartInitException e) {
198: PDEPlugin.logException(e);
199: }
200: // Add source page
201: addSourcePage(TocInputContext.CONTEXT_ID);
202: }
203:
204: /* (non-Javadoc)
205: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#createContentOutline()
206: */
207: protected ISortableContentOutlinePage createContentOutline() {
208: return new TocFormOutlinePage(this );
209: }
210:
211: /* (non-Javadoc)
212: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#createInputContextManager()
213: */
214: protected InputContextManager createInputContextManager() {
215: return new TocInputContextManager(this );
216: }
217:
218: /* (non-Javadoc)
219: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#createResourceContexts(org.eclipse.pde.internal.ui.editor.context.InputContextManager, org.eclipse.ui.IFileEditorInput)
220: */
221: protected void createResourceContexts(InputContextManager contexts,
222: IFileEditorInput input) {
223: contexts.putContext(input, new TocInputContext(this , input,
224: true));
225: contexts.monitorFile(input.getFile());
226: }
227:
228: /* (non-Javadoc)
229: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#createStorageContexts(org.eclipse.pde.internal.ui.editor.context.InputContextManager, org.eclipse.ui.IStorageEditorInput)
230: */
231: protected void createStorageContexts(InputContextManager contexts,
232: IStorageEditorInput input) {
233: contexts.putContext(input, new TocInputContext(this , input,
234: true));
235: }
236:
237: /* (non-Javadoc)
238: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#createSystemFileContexts(org.eclipse.pde.internal.ui.editor.context.InputContextManager, org.eclipse.pde.internal.ui.editor.SystemFileEditorInput)
239: */
240: protected void createSystemFileContexts(
241: InputContextManager contexts, SystemFileEditorInput input) {
242: File file = (File) input.getAdapter(File.class);
243: if (file != null) {
244: IEditorInput in = new SystemFileEditorInput(file);
245: contexts
246: .putContext(in, new TocInputContext(this , in, true));
247: }
248: }
249:
250: /* (non-Javadoc)
251: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#editorContextAdded(org.eclipse.pde.internal.ui.editor.context.InputContext)
252: */
253: public void editorContextAdded(InputContext context) {
254: // Add the source page
255: addSourcePage(context.getId());
256: }
257:
258: /* (non-Javadoc)
259: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getInputContext(java.lang.Object)
260: */
261: protected InputContext getInputContext(Object object) {
262: return fInputContextManager
263: .findContext(TocInputContext.CONTEXT_ID);
264: }
265:
266: /* (non-Javadoc)
267: * @see org.eclipse.pde.internal.ui.editor.context.IInputContextListener#contextRemoved(org.eclipse.pde.internal.ui.editor.context.InputContext)
268: */
269: public void contextRemoved(InputContext context) {
270: close(false);
271: }
272:
273: /* (non-Javadoc)
274: * @see org.eclipse.pde.internal.ui.editor.context.IInputContextListener#monitoredFileAdded(org.eclipse.core.resources.IFile)
275: */
276: public void monitoredFileAdded(IFile monitoredFile) {
277: // NO-OP
278: }
279:
280: /* (non-Javadoc)
281: * @see org.eclipse.pde.internal.ui.editor.context.IInputContextListener#monitoredFileRemoved(org.eclipse.core.resources.IFile)
282: */
283: public boolean monitoredFileRemoved(IFile monitoredFile) {
284: return true;
285: }
286:
287: /* (non-Javadoc)
288: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getSelection()
289: */
290: public ISelection getSelection() {
291: IFormPage formPage = getActivePageInstance();
292: if ((formPage != null) && (formPage instanceof TocPage)) {
293: // Synchronizes the selection made in the master tree view with the
294: // selection in the outline view when the link with editor button
295: // is toggled on
296: return ((TocPage) formPage).getSelection();
297: }
298:
299: return super .getSelection();
300: }
301:
302: public boolean canCut(ISelection selection) {
303: if (selection instanceof IStructuredSelection) {
304: IStructuredSelection sel = (IStructuredSelection) selection;
305: for (Iterator iter = sel.iterator(); iter.hasNext();) {
306: Object obj = iter.next();
307: if (obj instanceof TocObject
308: && ((TocObject) obj).canBeRemoved()) {
309: return canCopy(selection);
310: }
311: }
312: }
313:
314: return false;
315: }
316:
317: /* (non-Javadoc)
318: * @see org.eclipse.pde.internal.ui.editor.MultiSourceEditor#createSourcePage(org.eclipse.pde.internal.ui.editor.PDEFormEditor, java.lang.String, java.lang.String, java.lang.String)
319: */
320: protected PDESourcePage createSourcePage(PDEFormEditor editor,
321: String title, String name, String contextId) {
322: return new TocSourcePage(editor, title, name);
323: }
324:
325: public void contributeToToolbar(IToolBarManager manager) {
326: // Add the register cheat sheet link to the form title area
327: if (getAggregateModel().isEditable())
328: manager.add(createUIControlConRegisterCS());
329: }
330:
331: /**
332: * @return
333: */
334: private ControlContribution createUIControlConRegisterCS() {
335: return new ControlContribution("Register") { //$NON-NLS-1$
336: protected Control createControl(Composite parent) {
337: // Create UI
338: createUIImageHyperlinkRegisterToc(parent);
339: // Create Listener
340: createUIListenerImageHyperlinkRegisterToc();
341: return fImageHyperlinkRegisterTOC;
342: }
343: };
344: }
345:
346: /**
347: * @param parent
348: */
349: private void createUIImageHyperlinkRegisterToc(Composite parent) {
350: fImageHyperlinkRegisterTOC = new ImageHyperlink(parent,
351: SWT.NONE);
352: fImageHyperlinkRegisterTOC
353: .setText(PDEUIMessages.TocPage_msgRegisterThisTOC);
354: fImageHyperlinkRegisterTOC.setUnderlined(true);
355: fImageHyperlinkRegisterTOC.setForeground(getToolkit()
356: .getHyperlinkGroup().getForeground());
357: }
358:
359: /**
360: *
361: */
362: private void createUIListenerImageHyperlinkRegisterToc() {
363: fImageHyperlinkRegisterTOC
364: .addHyperlinkListener(new IHyperlinkListener() {
365: public void linkActivated(HyperlinkEvent e) {
366: handleLinkActivatedRegisterTOC();
367: }
368:
369: public void linkEntered(HyperlinkEvent e) {
370: handleLinkEnteredRegisterTOC(e.getLabel());
371: }
372:
373: public void linkExited(HyperlinkEvent e) {
374: handleLinkExitedRegisterTOC();
375: }
376: });
377: }
378:
379: /**
380: * @param message
381: */
382: private void handleLinkEnteredRegisterTOC(String message) {
383: // Update colour
384: fImageHyperlinkRegisterTOC.setForeground(getToolkit()
385: .getHyperlinkGroup().getActiveForeground());
386: // Update IDE status line
387: getEditorSite().getActionBars().getStatusLineManager()
388: .setMessage(message);
389: }
390:
391: /**
392: *
393: */
394: private void handleLinkExitedRegisterTOC() {
395: // Update colour
396: fImageHyperlinkRegisterTOC.setForeground(getToolkit()
397: .getHyperlinkGroup().getForeground());
398: // Update IDE status line
399: getEditorSite().getActionBars().getStatusLineManager()
400: .setMessage(null);
401: }
402:
403: /**
404: *
405: */
406: private void handleLinkActivatedRegisterTOC() {
407: RegisterTocWizard wizard = new RegisterTocWizard(
408: (IModel) getAggregateModel());
409: // Initialize the wizard
410: wizard.init(PlatformUI.getWorkbench(), null);
411: // Create the dialog for the wizard
412: WizardDialog dialog = new WizardDialog(PDEPlugin
413: .getActiveWorkbenchShell(), wizard);
414: dialog.create();
415: dialog.getShell().setSize(400, 250);
416: // Check the result
417: if (dialog.open() == Window.OK) {
418: // NO-OP
419: }
420: }
421: }
|