001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.runtime.IProgressMonitor;
013: import org.eclipse.jface.action.IMenuManager;
014: import org.eclipse.jface.action.MenuManager;
015: import org.eclipse.jface.action.Separator;
016: import org.eclipse.swt.custom.StyledText;
017: import org.eclipse.swt.dnd.DND;
018: import org.eclipse.swt.dnd.DropTargetEvent;
019: import org.eclipse.swt.dnd.DropTargetListener;
020: import org.eclipse.swt.dnd.TextTransfer;
021: import org.eclipse.swt.dnd.Transfer;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.ui.IEditorInput;
024: import org.eclipse.ui.IFileEditorInput;
025: import org.eclipse.ui.dnd.IDragAndDropService;
026: import org.eclipse.ui.editors.text.TextEditor;
027: import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
028:
029: /**
030: * This class implements the Readme editor. Since the readme
031: * editor is mostly just a text editor, there is very little
032: * implemented in this actual class. It can be regarded as
033: * simply decorating the text editor with a content outline.
034: */
035: public class ReadmeEditor extends TextEditor {
036: protected ReadmeContentOutlinePage page;
037:
038: /**
039: * Creates a new ReadmeEditor.
040: */
041: public ReadmeEditor() {
042: super ();
043: }
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.ui.texteditor.StatusTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
047: */
048: public void createPartControl(Composite parent) {
049: super .createPartControl(parent);
050:
051: StyledText tw = getSourceViewer().getTextWidget();
052:
053: // Add a 'TextTransfer' drop target to the editor
054: int ops = DND.DROP_DEFAULT | DND.DROP_COPY;
055: Transfer[] transfers = { TextTransfer.getInstance() };
056: DropTargetListener editorListener = new DropTargetListener() {
057:
058: public void dragEnter(DropTargetEvent event) {
059: event.detail = DND.DROP_COPY;
060: }
061:
062: public void dragLeave(DropTargetEvent event) {
063: }
064:
065: public void dragOperationChanged(DropTargetEvent event) {
066: event.detail = DND.DROP_COPY;
067: }
068:
069: public void dragOver(DropTargetEvent event) {
070: event.feedback = DND.FEEDBACK_SCROLL
071: | DND.FEEDBACK_SELECT;
072: }
073:
074: public void drop(DropTargetEvent event) {
075: if (TextTransfer.getInstance().isSupportedType(
076: event.currentDataType)) {
077: String text = (String) event.data;
078: getSourceViewer().getTextWidget().insert(text);
079: }
080: }
081:
082: public void dropAccept(DropTargetEvent event) {
083: }
084:
085: };
086:
087: IDragAndDropService dtSvc = (IDragAndDropService) getSite()
088: .getService(IDragAndDropService.class);
089: dtSvc.addMergedDropTarget(tw, ops, transfers, editorListener);
090: }
091:
092: /** (non-Javadoc)
093: * Method declared on IEditorPart
094: */
095: public void doSave(IProgressMonitor monitor) {
096: super .doSave(monitor);
097: if (page != null)
098: page.update();
099: }
100:
101: /** (non-Javadoc)
102: * Method declared on IAdaptable
103: */
104: public Object getAdapter(Class key) {
105: if (key.equals(IContentOutlinePage.class)) {
106: IEditorInput input = getEditorInput();
107: if (input instanceof IFileEditorInput) {
108: page = new ReadmeContentOutlinePage(
109: ((IFileEditorInput) input).getFile());
110: return page;
111: }
112: }
113: return super .getAdapter(key);
114: }
115:
116: /** (non-Javadoc)
117: * Method declared on AbstractTextEditor
118: */
119: protected void editorContextMenuAboutToShow(IMenuManager parentMenu) {
120: super .editorContextMenuAboutToShow(parentMenu);
121: parentMenu.add(new Separator());
122: IMenuManager subMenu = new MenuManager(MessageUtil
123: .getString("Add")); //$NON-NLS-1$
124: parentMenu.add(subMenu);
125: if (subMenu != null) {
126: // Add readme actions with various attributes
127: Object[][] att = new Object[][] { {
128: IReadmeConstants.MARKER_ATT_ID, new Integer(1234) } };
129: subMenu
130: .add(new AddReadmeMarkerAction(
131: this ,
132: MessageUtil
133: .getString("Add_readme_marker_action_label") + "1", //$NON-NLS-1$ //$NON-NLS-2$
134: att,
135: MessageUtil
136: .getString("Readme_marker_message_example") + " id=1234")); //$NON-NLS-1$ //$NON-NLS-2$
137:
138: att = new Object[][] { { IReadmeConstants.MARKER_ATT_LEVEL,
139: new Integer(7) } };
140: subMenu
141: .add(new AddReadmeMarkerAction(
142: this ,
143: MessageUtil
144: .getString("Add_readme_marker_action_label") + "2", //$NON-NLS-1$ //$NON-NLS-2$
145: att,
146: MessageUtil
147: .getString("Readme_marker_message_example") + " level=7")); //$NON-NLS-1$ //$NON-NLS-2$
148:
149: att = new Object[][] {
150: { IReadmeConstants.MARKER_ATT_LEVEL, new Integer(7) },
151: { IReadmeConstants.MARKER_ATT_DEPT, "infra" } }; //$NON-NLS-1$
152: subMenu
153: .add(new AddReadmeMarkerAction(
154: this ,
155: MessageUtil
156: .getString("Add_readme_marker_action_label") + "3", //$NON-NLS-1$ //$NON-NLS-2$
157: att,
158: MessageUtil
159: .getString("Readme_marker_message_example") + " level=7, department=infra")); //$NON-NLS-1$ //$NON-NLS-2$
160:
161: att = new Object[][] { { IReadmeConstants.MARKER_ATT_CODE,
162: "red" } }; //$NON-NLS-1$
163: subMenu
164: .add(new AddReadmeMarkerAction(
165: this ,
166: MessageUtil
167: .getString("Add_readme_marker_action_label") + "4", //$NON-NLS-1$ //$NON-NLS-2$
168: att,
169: MessageUtil
170: .getString("Readme_marker_message_example") + " code=red")); //$NON-NLS-1$ //$NON-NLS-2$
171:
172: att = new Object[][] { { IReadmeConstants.MARKER_ATT_LANG,
173: "english" } }; //$NON-NLS-1$
174: subMenu
175: .add(new AddReadmeMarkerAction(
176: this ,
177: MessageUtil
178: .getString("Add_readme_marker_action_label") + "5", //$NON-NLS-1$ //$NON-NLS-2$
179: att,
180: MessageUtil
181: .getString("Readme_marker_message_example") + " language=english")); //$NON-NLS-1$ //$NON-NLS-2$
182:
183: att = new Object[][] {
184: { IReadmeConstants.MARKER_ATT_ID, new Integer(1234) },
185: { IReadmeConstants.MARKER_ATT_LEVEL, new Integer(7) },
186: { IReadmeConstants.MARKER_ATT_DEPT, "infra" }, //$NON-NLS-1$
187: { IReadmeConstants.MARKER_ATT_CODE, "red" }, //$NON-NLS-1$
188: { IReadmeConstants.MARKER_ATT_LANG, "english" } }; //$NON-NLS-1$
189: subMenu
190: .add(new AddReadmeMarkerAction(
191: this ,
192: MessageUtil
193: .getString("Add_readme_marker_action_label") + "6", //$NON-NLS-1$ //$NON-NLS-2$
194: att,
195: MessageUtil
196: .getString("Readme_marker_message_example") + //$NON-NLS-1$
197: " id=1234, level=7, department=infra, code=red, language=english")); //$NON-NLS-1$
198:
199: att = new Object[0][0];
200: subMenu
201: .add(new AddReadmeMarkerAction(
202: this ,
203: MessageUtil
204: .getString("Add_readme_marker_action_label") + "7", //$NON-NLS-1$ //$NON-NLS-2$
205: att,
206: MessageUtil
207: .getString("Readme_marker_message_example") + " No attributes specified")); //$NON-NLS-1$ //$NON-NLS-2$
208: }
209: }
210:
211: }
|