Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2007 Dakshinamurthy Karra and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Dakshinamurthy Karra - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.ui.texteditor.templates; |
12 |
|
13 |
import java.io.IOException; |
14 |
import java.util.ArrayList; |
15 |
import java.util.Arrays; |
16 |
import java.util.Iterator; |
17 |
import java.util.List; |
18 |
|
19 |
import org.eclipse.jface.action.Action; |
20 |
import org.eclipse.jface.action.IAction; |
21 |
import org.eclipse.jface.action.IMenuListener; |
22 |
import org.eclipse.jface.action.IMenuManager; |
23 |
import org.eclipse.jface.action.IToolBarManager; |
24 |
import org.eclipse.jface.action.MenuManager; |
25 |
import org.eclipse.jface.action.Separator; |
26 |
import org.eclipse.jface.dialogs.MessageDialog; |
27 |
import org.eclipse.jface.layout.TreeColumnLayout; |
28 |
import org.eclipse.jface.preference.IPreferenceStore; |
29 |
import org.eclipse.jface.resource.JFaceResources; |
30 |
import org.eclipse.jface.text.Document; |
31 |
import org.eclipse.jface.text.IDocument; |
32 |
import org.eclipse.jface.text.source.SourceViewer; |
33 |
import org.eclipse.jface.text.source.SourceViewerConfiguration; |
34 |
import org.eclipse.jface.text.templates.ContextTypeRegistry; |
35 |
import org.eclipse.jface.text.templates.Template; |
36 |
import org.eclipse.jface.text.templates.TemplateContextType; |
37 |
import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; |
38 |
import org.eclipse.jface.text.templates.persistence.TemplateStore; |
39 |
import org.eclipse.jface.util.IPropertyChangeListener; |
40 |
import org.eclipse.jface.util.PropertyChangeEvent; |
41 |
import org.eclipse.jface.viewers.ColumnWeightData; |
42 |
import org.eclipse.jface.viewers.DoubleClickEvent; |
43 |
import org.eclipse.jface.viewers.IDoubleClickListener; |
44 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
45 |
import org.eclipse.jface.viewers.IStructuredSelection; |
46 |
import org.eclipse.jface.viewers.ITableLabelProvider; |
47 |
import org.eclipse.jface.viewers.ITreeContentProvider; |
48 |
import org.eclipse.jface.viewers.LabelProvider; |
49 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
50 |
import org.eclipse.jface.viewers.StructuredSelection; |
51 |
import org.eclipse.jface.viewers.TreeViewer; |
52 |
import org.eclipse.jface.viewers.Viewer; |
53 |
import org.eclipse.jface.viewers.ViewerComparator; |
54 |
import org.eclipse.jface.window.Window; |
55 |
import org.eclipse.swt.SWT; |
56 |
import org.eclipse.swt.custom.CLabel; |
57 |
import org.eclipse.swt.custom.SashForm; |
58 |
import org.eclipse.swt.custom.ViewForm; |
59 |
import org.eclipse.swt.dnd.Clipboard; |
60 |
import org.eclipse.swt.dnd.DND; |
61 |
import org.eclipse.swt.dnd.DragSourceAdapter; |
62 |
import org.eclipse.swt.dnd.DragSourceEvent; |
63 |
import org.eclipse.swt.dnd.DropTargetAdapter; |
64 |
import org.eclipse.swt.dnd.DropTargetEvent; |
65 |
import org.eclipse.swt.dnd.TextTransfer; |
66 |
import org.eclipse.swt.dnd.Transfer; |
67 |
import org.eclipse.swt.events.ControlEvent; |
68 |
import org.eclipse.swt.events.ControlListener; |
69 |
import org.eclipse.swt.graphics.Color; |
70 |
import org.eclipse.swt.graphics.Font; |
71 |
import org.eclipse.swt.graphics.FontData; |
72 |
import org.eclipse.swt.graphics.GC; |
73 |
import org.eclipse.swt.graphics.Image; |
74 |
import org.eclipse.swt.layout.GridData; |
75 |
import org.eclipse.swt.widgets.Composite; |
76 |
import org.eclipse.swt.widgets.Control; |
77 |
import org.eclipse.swt.widgets.Menu; |
78 |
import org.eclipse.swt.widgets.Shell; |
79 |
import org.eclipse.swt.widgets.Tree; |
80 |
import org.eclipse.swt.widgets.TreeColumn; |
81 |
import org.eclipse.swt.widgets.TreeItem; |
82 |
import org.eclipse.ui.IActionBars; |
83 |
import org.eclipse.ui.IWorkbenchActionConstants; |
84 |
import org.eclipse.ui.IWorkbenchPartSite; |
85 |
import org.eclipse.ui.actions.ActionFactory; |
86 |
import org.eclipse.ui.dialogs.PreferencesUtil; |
87 |
import org.eclipse.ui.dnd.IDragAndDropService; |
88 |
import org.eclipse.ui.internal.texteditor.TextEditorPlugin; |
89 |
import org.eclipse.ui.part.Page; |
90 |
import org.eclipse.ui.texteditor.templates.TemplatePreferencePage.EditTemplateDialog; |
91 |
|
92 |
/** |
93 |
* An abstract base class for template pages for use with TextEditor and JFace |
94 |
* {@link Template}. |
95 |
* <p> |
96 |
* Clients who are defining an editor may elect to provide a corresponding |
97 |
* templates page. This templates page will be presented to the user via the |
98 |
* Templates View (the user decides whether their workbench window contains this |
99 |
* view) whenever that editor is active. This class should be subclassed. |
100 |
* </p> |
101 |
* <p> |
102 |
* Internally, a TemplatesPage uses the template store to display different |
103 |
* categories. A link to editor mode on the templates page allows to filtering |
104 |
* of the categories to only that are supported in this context. |
105 |
* </p> |
106 |
* @since 3.4 |
107 |
*/ |
108 |
public abstract class TemplatesPage extends Page { |
109 |
|
110 |
/** |
111 |
* Sashform size |
112 |
*/ |
113 |
private static final String SASH_SIZE_PREF_ID = TextEditorPlugin.PLUGIN_ID |
114 |
+ ".templates.templatesPage.sashSize"; //$NON-NLS-1$ |
115 |
/** |
116 |
* Tree columns widths |
117 |
*/ |
118 |
private static final String COLUMN_NAME_WIDTH_PREF_ID = TextEditorPlugin.PLUGIN_ID |
119 |
+ ".templates.templatesPage.nameWidth"; //$NON-NLS-1$ |
120 |
private static final String COLUMN_DESCRIPTION_WIDTH_PREF_ID = TextEditorPlugin.PLUGIN_ID |
121 |
+ ".templates.templatesPage.descriptionWidth"; //$NON-NLS-1$ |
122 |
/** |
123 |
* Link to editor action setting |
124 |
*/ |
125 |
private static final String LINK_ACTION_PREF_ID = TextEditorPlugin.PLUGIN_ID |
126 |
+ ".templates.templatesPage.linkAction"; //$NON-NLS-1$ |
127 |
/** |
128 |
* Show disabled actions setting |
129 |
*/ |
130 |
private static final String SHOW_DISABLED_ACTION_PREF_ID = TextEditorPlugin.PLUGIN_ID |
131 |
+ ".templates.templatesPage.showDisabledAction"; //$NON-NLS-1$ |
132 |
/** |
133 |
* Show new dialog on drop setting |
134 |
*/ |
135 |
private static final String SHOW_NEW_DIALOG_ACTION_PREF_ID = TextEditorPlugin.PLUGIN_ID |
136 |
+ ".templates.templatesPage.showNewDialogAction"; //$NON-NLS-1$; |
137 |
|
138 |
/** |
139 |
* The ID for the popup menu for this templates page |
140 |
*/ |
141 |
private static final String POPUP_MENU_ID = "org.eclipse.ui.texteditor.templates.PopupMenu"; //$NON-NLS-1$ |
142 |
|
143 |
/** |
144 |
* Drop support for the editor linked to this page. When a user drops a |
145 |
* template into the active editor, the template is applied at the drop |
146 |
* position. |
147 |
*/ |
148 |
private final class EditorDropTarget extends DropTargetAdapter { |
149 |
/* |
150 |
* (non-Javadoc) |
151 |
* |
152 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#dragEnter(org.eclipse.swt.dnd.DropTargetEvent) |
153 |
*/ |
154 |
public void dragEnter(DropTargetEvent event) { |
155 |
if (event.detail == DND.DROP_DEFAULT) |
156 |
event.detail = DND.DROP_COPY; |
157 |
} |
158 |
|
159 |
/* |
160 |
* (non-Javadoc) |
161 |
* |
162 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent) |
163 |
*/ |
164 |
public void dragOperationChanged(DropTargetEvent event) { |
165 |
if (event.detail == DND.DROP_DEFAULT) |
166 |
event.detail = DND.DROP_COPY; |
167 |
} |
168 |
|
169 |
/* |
170 |
* (non-Javadoc) |
171 |
* |
172 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#dragOver(org.eclipse.swt.dnd.DropTargetEvent) |
173 |
*/ |
174 |
public void dragOver(DropTargetEvent event) { |
175 |
event.feedback |= DND.FEEDBACK_SCROLL; |
176 |
if (!isEditorModifiable()) { |
177 |
event.detail = DND.DROP_NONE; |
178 |
return; |
179 |
} |
180 |
if (isValidTemplateForPosition(getSelectedTemplate(), event.x, event.y)) { |
181 |
event.detail = DND.DROP_COPY; |
182 |
return; |
183 |
} |
184 |
event.detail = DND.DROP_NONE; |
185 |
} |
186 |
|
187 |
/* |
188 |
* (non-Javadoc) |
189 |
* |
190 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#drop(org.eclipse.swt.dnd.DropTargetEvent) |
191 |
*/ |
192 |
public void drop(DropTargetEvent event) { |
193 |
insertTemplate(getSelectedTemplate()); |
194 |
} |
195 |
} |
196 |
|
197 |
/** |
198 |
* Comparator for the viewer. Sorts the templates by name and then |
199 |
* description and context types by names. |
200 |
*/ |
201 |
private static final class TemplateViewerComparator extends ViewerComparator { |
202 |
/* |
203 |
* (non-Javadoc) |
204 |
* |
205 |
* @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, |
206 |
* java.lang.Object, java.lang.Object) |
207 |
*/ |
208 |
public int compare(Viewer viewer, Object object1, Object object2) { |
209 |
if ((object1 instanceof TemplatePersistenceData) |
210 |
&& (object2 instanceof TemplatePersistenceData)) { |
211 |
Template left = ((TemplatePersistenceData) object1).getTemplate(); |
212 |
Template right = ((TemplatePersistenceData) object2).getTemplate(); |
213 |
int result = left.getName().compareToIgnoreCase(right.getName()); |
214 |
if (result != 0) |
215 |
return result; |
216 |
return left.getDescription().compareToIgnoreCase(right.getDescription()); |
217 |
} |
218 |
if ((object1 instanceof TemplateContextType) |
219 |
&& (object2 instanceof TemplateContextType)) { |
220 |
return ((TemplateContextType) object1).getName().compareToIgnoreCase( |
221 |
((TemplateContextType) object1).getName()); |
222 |
} |
223 |
return super.compare(viewer, object1, object2); |
224 |
} |
225 |
|
226 |
/* |
227 |
* (non-Javadoc) |
228 |
* |
229 |
* @see org.eclipse.jface.viewers.ViewerComparator#isSorterProperty(java.lang.Object, |
230 |
* java.lang.String) |
231 |
*/ |
232 |
public boolean isSorterProperty(Object element, String property) { |
233 |
return true; |
234 |
} |
235 |
} |
236 |
|
237 |
/** |
238 |
* Label provider for templates. |
239 |
*/ |
240 |
private class TemplateLabelProvider extends LabelProvider implements ITableLabelProvider { |
241 |
|
242 |
/* |
243 |
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, |
244 |
* int) |
245 |
*/ |
246 |
public Image getColumnImage(Object element, int columnIndex) { |
247 |
return null; |
248 |
} |
249 |
|
250 |
/* |
251 |
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, |
252 |
* int) |
253 |
*/ |
254 |
public String getColumnText(Object element, int columnIndex) { |
255 |
if (element instanceof TemplatePersistenceData) |
256 |
return getTemplateColumnText(((TemplatePersistenceData) element).getTemplate(), |
257 |
columnIndex); |
258 |
return getContextColumnText((TemplateContextType) element, columnIndex); |
259 |
} |
260 |
|
261 |
/** |
262 |
* Return the column text for a template |
263 |
* |
264 |
* @param template |
265 |
* @param columnIndex |
266 |
* @return the column text |
267 |
*/ |
268 |
private String getTemplateColumnText(Template template, int columnIndex) { |
269 |
switch (columnIndex) { |
270 |
case 0: |
271 |
return template.getName(); |
272 |
case 1: |
273 |
return template.getDescription(); |
274 |
default: |
275 |
return ""; //$NON-NLS-1$ |
276 |
} |
277 |
} |
278 |
|
279 |
/** |
280 |
* Return the column text for a context |
281 |
* |
282 |
* @param contextType |
283 |
* @param columnIndex |
284 |
* @return the column text |
285 |
*/ |
286 |
private String getContextColumnText(TemplateContextType contextType, int columnIndex) { |
287 |
switch (columnIndex) { |
288 |
case 0: |
289 |
return contextType.getName(); |
290 |
case 1: |
291 |
return TextEditorTemplateMessages.TemplatesPage_context_description; |
292 |
default: |
293 |
return ""; //$NON-NLS-1$ |
294 |
} |
295 |
} |
296 |
|
297 |
} |
298 |
|
299 |
/** |
300 |
* Content provider for templates. Provides all the templates defined for |
301 |
* this editor and optionally the disabled ones. |
302 |
*/ |
303 |
private class TemplatesContentProvider implements ITreeContentProvider { |
304 |
/* |
305 |
* (non-Javadoc) |
306 |
* |
307 |
* @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) |
308 |
*/ |
309 |
public Object[] getChildren(Object parentElement) { |
310 |
if (parentElement instanceof TemplatePersistenceData) |
311 |
return new Object[0]; |
312 |
else if (parentElement instanceof TemplateContextType) { |
313 |
TemplateContextType contextType = (TemplateContextType) parentElement; |
314 |
return getTemplates(contextType.getId()); |
315 |
} |
316 |
return null; |
317 |
} |
318 |
|
319 |
/** |
320 |
* Get the templates for a given contextId. if ShowDisabled is selected |
321 |
* the disabled templates are also included. |
322 |
* |
323 |
* @param contextId |
324 |
* @return the templates |
325 |
*/ |
326 |
private TemplatePersistenceData[] getTemplates(String contextId) { |
327 |
List templateList = new ArrayList(); |
328 |
TemplatePersistenceData[] datas = fTemplateStore.getTemplateData(false); |
329 |
for (int i = 0; i < datas.length; i++) { |
330 |
if ((fShowDisabled || datas[i].isEnabled()) |
331 |
&& datas[i].getTemplate().getContextTypeId().equals(contextId)) |
332 |
templateList.add(datas[i]); |
333 |
} |
334 |
return (TemplatePersistenceData[]) templateList |
335 |
.toArray(new TemplatePersistenceData[templateList.size()]); |
336 |
} |
337 |
|
338 |
/* |
339 |
* (non-Javadoc) |
340 |
* |
341 |
* @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) |
342 |
*/ |
343 |
public Object getParent(Object element) { |
344 |
if (element instanceof TemplatePersistenceData) { |
345 |
TemplatePersistenceData templateData = (TemplatePersistenceData) element; |
346 |
return fContextTypeRegistry.getContextType(templateData.getTemplate() |
347 |
.getContextTypeId()); |
348 |
} |
349 |
return null; |
350 |
} |
351 |
|
352 |
/* |
353 |
* (non-Javadoc) |
354 |
* |
355 |
* @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) |
356 |
*/ |
357 |
public boolean hasChildren(Object element) { |
358 |
if (element instanceof TemplateContextType) |
359 |
return true; |
360 |
return false; |
361 |
} |
362 |
|
363 |
/* |
364 |
* (non-Javadoc) |
365 |
* |
366 |
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
367 |
*/ |
368 |
public Object[] getElements(Object inputElement) { |
369 |
List contextTypes = new ArrayList(); |
370 |
|
371 |
for (Iterator iterator = fContextTypeRegistry.contextTypes(); iterator.hasNext();) { |
372 |
TemplateContextType contextType = (TemplateContextType) iterator.next(); |
373 |
if (isActiveContext(contextType) || !fLinkWithEditor) |
374 |
contextTypes.add(contextType); |
375 |
} |
376 |
return contextTypes.toArray(new TemplateContextType[contextTypes.size()]); |
377 |
} |
378 |
|
379 |
/** |
380 |
* Checks whether given context type is active for displaying the |
381 |
* templates |
382 |
* |
383 |
* @param contextType |
384 |
* @return true if context is active |
385 |
*/ |
386 |
private boolean isActiveContext(TemplateContextType contextType) { |
387 |
return fActiveTypes == null || fActiveTypes.contains(contextType.getId()); |
388 |
} |
389 |
|
390 |
/* |
391 |
* (non-Javadoc) |
392 |
* |
393 |
* @see org.eclipse.jface.viewers.IContentProvider#dispose() |
394 |
*/ |
395 |
public void dispose() { |
396 |
} |
397 |
|
398 |
/* |
399 |
* (non-Javadoc) |
400 |
* |
401 |
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, |
402 |
* java.lang.Object, java.lang.Object) |
403 |
*/ |
404 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
405 |
} |
406 |
} |
407 |
|
408 |
/** |
409 |
* Show/Hide new dialog |
410 |
*/ |
411 |
private static final int SHOW_DIALOG = 0; |
412 |
private static final int MAY_HIDE_DIALOG = 1; |
413 |
|
414 |
/** The viewer displays the pattern of selected template. */ |
415 |
private SourceViewer fPatternViewer; |
416 |
/** The store for our templates. */ |
417 |
private TemplateStore fTemplateStore; |
418 |
/** |
419 |
* The template stores preference page. Used to listen to the changes and |
420 |
* update the view. Needed when the user selects Preferences and updates the |
421 |
* templates. |
422 |
*/ |
423 |
private IPreferenceStore fTemplatePreferenceStore; |
424 |
private IPropertyChangeListener fTemplateChangeListener; |
425 |
/** The context type registry. */ |
426 |
private ContextTypeRegistry fContextTypeRegistry; |
427 |
|
428 |
/** The control for this page book view */ |
429 |
private SashForm fControl; |
430 |
|
431 |
/* Actions */ |
432 |
private Action fInsertAction; |
433 |
private Action fAddAction; |
434 |
private Action fEditAction; |
435 |
private Action fRemoveAction; |
436 |
private Action fLinkWithEditorAction; |
437 |
private Action fExpandCollapseAction; |
438 |
private Action fPreferencePageAction; |
439 |
private Action fShowDisabledAction; |
440 |
private Action fShowNewDialogOnDropAction; |
441 |
|
442 |
/* Clipboard actions */ |
443 |
private Action fPasteAction; |
444 |
private Action fCopyAction; |
445 |
|
446 |
/* Options and values saved in preference store */ |
447 |
private boolean fLinkWithEditor ; |
448 |
private boolean fShowDisabled ; |
449 |
private boolean fShowNewDialogOnDrop ; |
450 |
private int fSashSize; |
451 |
private int fNameWidth; |
452 |
private int fDescriptionWidth ; |
453 |
|
454 |
/** Currently selected templates and type */ |
455 |
private ArrayList fSelectedTemplateList = new ArrayList(); |
456 |
private TemplateContextType fSelectedType; |
457 |
|
458 |
/** Current active context types for the editor */ |
459 |
private List fActiveTypes; |
460 |
|
461 |
/* Preference stores */ |
462 |
/** |
463 |
* Preference store to store the dialog setting for this page |
464 |
*/ |
465 |
private IPreferenceStore fPreferenceStore; |
466 |
|
467 |
/* Controls */ |
468 |
private Tree fTemplatesTree; |
469 |
private TreeViewer fTreeViewer; |
470 |
private Menu fContextMenu; |
471 |
|
472 |
/** |
473 |
* Creates a new template view page. |
474 |
* |
475 |
* @param templateStore |
476 |
* @param templatePreferenceStore |
477 |
* @param registry |
478 |
*/ |
479 |
protected TemplatesPage(TemplateStore templateStore, IPreferenceStore templatePreferenceStore, |
480 |
ContextTypeRegistry registry) { |
481 |
super(); |
482 |
setupPreferenceStore(); |
483 |
fTemplateStore = templateStore; |
484 |
fTemplatePreferenceStore = templatePreferenceStore; |
485 |
if (fTemplatePreferenceStore != null) { |
486 |
fTemplateChangeListener = new IPropertyChangeListener() { |
487 |
public void propertyChange(PropertyChangeEvent event) { |
488 |
refresh(); |
489 |
} |
490 |
}; |
491 |
fTemplatePreferenceStore.addPropertyChangeListener(fTemplateChangeListener); |
492 |
} |
493 |
fContextTypeRegistry = registry; |
494 |
} |
495 |
|
496 |
private void setupPreferenceStore() { |
497 |
fPreferenceStore = TextEditorPlugin.getDefault().getPreferenceStore(); |
498 |
fPreferenceStore.setDefault(LINK_ACTION_PREF_ID, true); |
499 |
fPreferenceStore.setDefault(SHOW_DISABLED_ACTION_PREF_ID, false); |
500 |
fPreferenceStore.setDefault(SHOW_NEW_DIALOG_ACTION_PREF_ID, true); |
501 |
fPreferenceStore.setDefault(SASH_SIZE_PREF_ID, 80); |
502 |
fLinkWithEditor = fPreferenceStore.getBoolean(LINK_ACTION_PREF_ID); |
503 |
fShowDisabled = fPreferenceStore.getBoolean(SHOW_DISABLED_ACTION_PREF_ID); |
504 |
fShowNewDialogOnDrop = fPreferenceStore.getBoolean(SHOW_NEW_DIALOG_ACTION_PREF_ID); |
505 |
fSashSize = fPreferenceStore.getInt(SASH_SIZE_PREF_ID); |
506 |
} |
507 |
|
508 |
/** |
509 |
* Check whether the linked editor is modifiable |
510 |
* |
511 |
* @return true if the editor is modifiable |
512 |
*/ |
513 |
protected boolean isEditorModifiable() { |
514 |
return true; |
515 |
} |
516 |
|
517 |
/** |
518 |
* Check whether the template is valid for the current drop position |
519 |
* |
520 |
* @param template |
521 |
* @param x |
522 |
* @param y |
523 |
* @return true if the template is valid |
524 |
*/ |
525 |
protected boolean isValidTemplateForPosition(Template template, int x, int y) { |
526 |
return true; |
527 |
} |
528 |
|
529 |
/* |
530 |
* (non-Javadoc) |
531 |
* |
532 |
* @see org.eclipse.ui.part.Page#createControl(org.eclipse.swt.widgets.Composite) |
533 |
*/ |
534 |
public void createControl(Composite ancestor) { |
535 |
setupActions(); |
536 |
|
537 |
fControl = new SashForm(ancestor, SWT.VERTICAL); |
538 |
|
539 |
createTemplateTree(); |
540 |
createPatternViewer(fControl); |
541 |
|
542 |
hookContextMenu(); |
543 |
initializeDND(); |
544 |
updateButtons(); |
545 |
|
546 |
fControl.setWeights(new int[] { fSashSize, 100 - fSashSize }); |
547 |
} |
548 |
|
549 |
/** |
550 |
* Setup the actions |
551 |
*/ |
552 |
private void setupActions() { |
553 |
createActions(); |
554 |
IActionBars actionBars = getSite().getActionBars(); |
555 |
|
556 |
actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), fPasteAction); |
557 |
actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), fCopyAction); |
558 |
|
559 |
fillToolbar(actionBars); |
560 |
fillMenu(actionBars); |
561 |
} |
562 |
|
563 |
/** |
564 |
* Create actions for this view |
565 |
*/ |
566 |
private void createActions() { |
567 |
fInsertAction = new Action(TextEditorTemplateMessages.TemplatesPage_insert) { |
568 |
public void run() { |
569 |
insertTemplate(getSelectedTemplate()); |
570 |
} |
571 |
}; |
572 |
fInsertAction.setToolTipText(TextEditorTemplateMessages.TemplatesPage_insert_tooltip); |
573 |
|
574 |
fAddAction = new Action(TextEditorTemplateMessages.TemplatesPage_new) { |
575 |
public void run() { |
576 |
addTemplate(); |
577 |
} |
578 |
}; |
579 |
fAddAction.setImageDescriptor(TemplatesViewImages |
580 |
.getImageDescriptor(ITemplateViewImageConstants.IMG_ELCL_TEMPLATE_NEW)); |
581 |
fAddAction.setToolTipText(TextEditorTemplateMessages.TemplatesPage_new_tooltip); |
582 |
|
583 |
fEditAction = new Action(TextEditorTemplateMessages.TemplatesPage_edit) { |
584 |
public void run() { |
585 |
editTemplate(); |
586 |
} |
587 |
}; |
588 |
fEditAction.setImageDescriptor(TemplatesViewImages |
589 |
.getImageDescriptor(ITemplateViewImageConstants.IMG_ELCL_TEMPLATE_EDIT)); |
590 |
fEditAction.setDisabledImageDescriptor(TemplatesViewImages |
591 |
.getImageDescriptor(ITemplateViewImageConstants.IMG_DLCL_TEMPLATE_EDIT)); |
592 |
fEditAction.setToolTipText(TextEditorTemplateMessages.TemplatesPage_edit_tooltip); |
593 |
|
594 |
fRemoveAction = new Action(TextEditorTemplateMessages.TemplatesPage_remove) { |
595 |
public void run() { |
596 |
removeTemplate(); |
597 |
} |
598 |
}; |
599 |
fRemoveAction.setImageDescriptor(TemplatesViewImages |
600 |
.getImageDescriptor(ITemplateViewImageConstants.IMG_DLCL_TEMPLATE_DELETE)); |
601 |
fRemoveAction.setImageDescriptor(TemplatesViewImages |
602 |
.getImageDescriptor(ITemplateViewImageConstants.IMG_ELCL_TEMPLATE_DELETE)); |
603 |
fRemoveAction.setToolTipText(TextEditorTemplateMessages.TemplatesPage_remove_tooltip); |
604 |
|
605 |
fLinkWithEditorAction = new Action(TextEditorTemplateMessages.TemplatesPage_link_to_editor, |
606 |
IAction.AS_CHECK_BOX) { |
607 |
public void run() { |
608 |
fLinkWithEditor = fLinkWithEditorAction.isChecked(); |
609 |
refresh(); |
610 |
} |
611 |
}; |
612 |
fLinkWithEditorAction.setImageDescriptor(TemplatesViewImages |
613 |
.getImageDescriptor(ITemplateViewImageConstants.IMG_ELCL_TEMPLATE_LINK)); |
614 |
fLinkWithEditorAction.setChecked(fPreferenceStore.getBoolean(LINK_ACTION_PREF_ID)); |
615 |
fLinkWithEditorAction.setToolTipText(TextEditorTemplateMessages.TemplatesPage_link_to_editor_tooltip); |
616 |
fExpandCollapseAction = new Action(TextEditorTemplateMessages.TemplatesPage_collapse_expand) { |
617 |
public void run() { |
618 |
Object[] vee = fTreeViewer.getVisibleExpandedElements(); |
619 |
boolean collapse = vee.length != 0; |
620 |
if (collapse) |
621 |
fTreeViewer.collapseAll(); |
622 |
else |
623 |
fTreeViewer.expandAll(); |
624 |
} |
625 |
}; |
626 |
fExpandCollapseAction.setImageDescriptor(TemplatesViewImages |
627 |
.getImageDescriptor(ITemplateViewImageConstants.IMG_ELCL_TEMPLATE_COLLAPSE_EXPAND)); |
628 |
fExpandCollapseAction |
629 |
.setToolTipText(TextEditorTemplateMessages.TemplatesPage_collapse_expand_tooltip); |
630 |
|
631 |
if (getPreferencePageId() != null) { |
632 |
fPreferencePageAction = new Action( |
633 |
TextEditorTemplateMessages.TemplatesPage_preference_page) { |
634 |
public void run() { |
635 |
showPreferencePage(); |
636 |
} |
637 |
}; |
638 |
fPreferencePageAction |
639 |
.setToolTipText(TextEditorTemplateMessages.TemplatesPage_preference_page_tooltip); |
640 |
} |
641 |
|
642 |
fShowDisabledAction = new Action(TextEditorTemplateMessages.TemplatesPage_show_disabled, |
643 |
IAction.AS_CHECK_BOX) { |
644 |
public void run() { |
645 |
fShowDisabled = fShowDisabledAction.isChecked(); |
646 |
refresh(); |
647 |
} |
648 |
}; |
649 |
fShowDisabledAction.setChecked(fPreferenceStore.getBoolean(SHOW_DISABLED_ACTION_PREF_ID)); |
650 |
fShowDisabledAction |
651 |
.setToolTipText(TextEditorTemplateMessages.TemplatesPage_show_disabled_tooltip); |
652 |
|
653 |
fShowNewDialogOnDropAction = new Action( |
654 |
TextEditorTemplateMessages.TemplatesPage_show_new_dialog, IAction.AS_CHECK_BOX) { |
655 |
public void run() { |
656 |
fShowNewDialogOnDrop = fShowNewDialogOnDropAction.isChecked(); |
657 |
} |
658 |
}; |
659 |
fShowNewDialogOnDropAction.setChecked(fPreferenceStore |
660 |
.getBoolean(SHOW_NEW_DIALOG_ACTION_PREF_ID)); |
661 |
fShowNewDialogOnDropAction |
662 |
.setToolTipText(TextEditorTemplateMessages.TemplatesPage_show_new_dialog_tooltip); |
663 |
|
664 |
fPasteAction = new Action() { |
665 |
public void run() { |
666 |
Clipboard clipBoard = new Clipboard(getShell().getDisplay()); |
667 |
final Template template = getTemplateFromCB(clipBoard); |
668 |
if (template == null) |
669 |
return; |
670 |
getShell().getDisplay().asyncExec(new Runnable() { |
671 |
public void run() { |
672 |
addTemplate(template, MAY_HIDE_DIALOG); |
673 |
} |
674 |
}); |
675 |
} |
676 |
|
677 |
private Template getTemplateFromCB(Clipboard clipBoard) { |
678 |
String text = (String) clipBoard.getContents(TextTransfer.getInstance()); |
679 |
if (text == null) |
680 |
return getTemplateFromTemplate(clipBoard); |
681 |
return new Template(TextEditorTemplateMessages.TemplatesPage_snippet, |
682 |
TextEditorTemplateMessages.TemplatesPage_paste_description, |
683 |
getContextTypeId(), text.replaceAll("\\$", "\\$\\$"), true); //$NON-NLS-1$ //$NON-NLS-2$ |
684 |
} |
685 |
|
686 |
private Template getTemplateFromTemplate(Clipboard clipBoard) { |
687 |
Object contents = clipBoard.getContents(TemplateTransfer.getInstance()); |
688 |
if (contents == null) |
689 |
return null; |
690 |
Template t = ((Template[]) contents)[0]; |
691 |
return new Template(t.getName(), t.getDescription(), getContextTypeId(), t |
692 |
.getPattern(), true); |
693 |
} |
694 |
}; |
695 |
|
696 |
fCopyAction = new Action() { |
697 |
public void run() { |
698 |
if (!isSingleTemplateSelected()) |
699 |
return; |
700 |
Template[] templates = new Template[] { getSelectedTemplate() }; |
701 |
Clipboard clipBoard = new Clipboard(getShell().getDisplay()); |
702 |
clipBoard.setContents(new Object[] { templates }, new Transfer[] { TemplateTransfer |
703 |
.getInstance() }); |
704 |
} |
705 |
}; |
706 |
} |
707 |
|
708 |
/** |
709 |
* Fill the menu items |
710 |
* |
711 |
* @param actionBars |
712 |
*/ |
713 |
private void fillMenu(IActionBars actionBars) { |
714 |
IMenuManager menuManager = actionBars.getMenuManager(); |
715 |
|
716 |
if (fPreferencePageAction != null) { |
717 |
menuManager.add(fPreferencePageAction); |
718 |
menuManager.add(new Separator()); |
719 |
} |
720 |
menuManager.add(fShowDisabledAction); |
721 |
menuManager.add(fShowNewDialogOnDropAction); |
722 |
|
723 |
menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
724 |
} |
725 |
|
726 |
/** |
727 |
* Fill items into the toolbar |
728 |
* |
729 |
* @param actionBars |
730 |
*/ |
731 |
private void fillToolbar(IActionBars actionBars) { |
732 |
IToolBarManager toolBarManager = actionBars.getToolBarManager(); |
733 |
toolBarManager.add(fAddAction); |
734 |
toolBarManager.add(fEditAction); |
735 |
toolBarManager.add(fRemoveAction); |
736 |
|
737 |
toolBarManager.add(new Separator()); |
738 |
|
739 |
toolBarManager.add(fLinkWithEditorAction); |
740 |
toolBarManager.add(fExpandCollapseAction); |
741 |
} |
742 |
|
743 |
/** |
744 |
* Setup the context menu for the viewer. |
745 |
*/ |
746 |
private void hookContextMenu() { |
747 |
MenuManager menuMgr = new MenuManager(POPUP_MENU_ID); |
748 |
menuMgr.setRemoveAllWhenShown(true); |
749 |
menuMgr.addMenuListener(new IMenuListener() { |
750 |
public void menuAboutToShow(IMenuManager manager) { |
751 |
fillContextMenu(manager); |
752 |
} |
753 |
}); |
754 |
fContextMenu = menuMgr.createContextMenu(fTreeViewer.getControl()); |
755 |
fTreeViewer.getControl().setMenu(fContextMenu); |
756 |
getSite().registerContextMenu(POPUP_MENU_ID, menuMgr, fTreeViewer); |
757 |
} |
758 |
|
759 |
/** |
760 |
* Fill up the context menu |
761 |
* |
762 |
* @param manager |
763 |
*/ |
764 |
private void fillContextMenu(IMenuManager manager) { |
765 |
manager.add(fInsertAction); |
766 |
manager.add(new Separator()); |
767 |
manager.add(fAddAction); |
768 |
manager.add(fEditAction); |
769 |
manager.add(fRemoveAction); |
770 |
// Other plug-ins can contribute there actions here |
771 |
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
772 |
} |
773 |
|
774 |
/** |
775 |
* Create the tree control to display template information |
776 |
*/ |
777 |
private void createTemplateTree() { |
778 |
Composite treeComposite = new Composite(fControl, SWT.NONE); |
779 |
GridData data = new GridData(GridData.FILL_BOTH); |
780 |
treeComposite.setLayoutData(data); |
781 |
|
782 |
TreeColumnLayout columnLayout = new TreeColumnLayout(); |
783 |
treeComposite.setLayout(columnLayout); |
784 |
fTemplatesTree = new Tree(treeComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI |
785 |
| SWT.FULL_SELECTION); |
786 |
fTemplatesTree.setHeaderVisible(true); |
787 |
fTemplatesTree.setLinesVisible(true); |
788 |
|
789 |
GC gc = new GC(getShell()); |
790 |
gc.setFont(JFaceResources.getDialogFont()); |
791 |
|
792 |
TreeColumn columnName = new TreeColumn(fTemplatesTree, SWT.NONE); |
793 |
columnName.setText(TextEditorTemplateMessages.TemplatesPage_column_name); |
794 |
int minWidth = fPreferenceStore.getInt(COLUMN_NAME_WIDTH_PREF_ID); |
795 |
if (minWidth == 0) |
796 |
minWidth = gc.stringExtent(TextEditorTemplateMessages.TemplatesPage_column_name).x + 10; |
797 |
columnLayout.setColumnData(columnName, new ColumnWeightData(2, minWidth, true)); |
798 |
columnName.addControlListener(new ControlListener() { |
799 |
public void controlMoved(ControlEvent e) { |
800 |
} |
801 |
|
802 |
public void controlResized(ControlEvent e) { |
803 |
fNameWidth = ((TreeColumn)e.getSource()).getWidth(); |
804 |
} |
805 |
}); |
806 |
|
807 |
TreeColumn columnDescription = new TreeColumn(fTemplatesTree, SWT.NONE); |
808 |
columnDescription.setText(TextEditorTemplateMessages.TemplatesPage_column_description); |
809 |
minWidth = fPreferenceStore.getInt(COLUMN_DESCRIPTION_WIDTH_PREF_ID); |
810 |
if (minWidth == 0) |
811 |
minWidth = gc.stringExtent(TextEditorTemplateMessages.TemplatesPage_column_description).x + 10; |
812 |
columnLayout.setColumnData(columnDescription, new ColumnWeightData(3, minWidth, true)); |
813 |
columnDescription.addControlListener(new ControlListener() { |
814 |
public void controlMoved(ControlEvent e) { |
815 |
} |
816 |
|
817 |
public void controlResized(ControlEvent e) { |
818 |
fDescriptionWidth = ((TreeColumn)e.getSource()).getWidth(); |
819 |
} |
820 |
}); |
821 |
|
822 |
|
823 |
gc.dispose(); |
824 |
|
825 |
createTreeViewer(); |
826 |
} |
827 |
|
828 |
/** |
829 |
* Create the viewer for the tree control and configure it. |
830 |
*/ |
831 |
private void createTreeViewer() { |
832 |
fTreeViewer = new TreeViewer(fTemplatesTree); |
833 |
fTreeViewer.setLabelProvider(new TemplateLabelProvider()); |
834 |
fTreeViewer.setContentProvider(new TemplatesContentProvider()); |
835 |
|
836 |
fTreeViewer.setComparator(new TemplateViewerComparator()); |
837 |
fTreeViewer.setInput(fTemplateStore); |
838 |
fTreeViewer.addDoubleClickListener(new IDoubleClickListener() { |
839 |
public void doubleClick(DoubleClickEvent e) { |
840 |
updateSelectedItems(); |
841 |
insertTemplate(getSelectedTemplate()); |
842 |
} |
843 |
}); |
844 |
|
845 |
fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() { |
846 |
public void selectionChanged(SelectionChangedEvent e) { |
847 |
updateSelectedItems(); |
848 |
updateButtons(); |
849 |
} |
850 |
}); |
851 |
fTreeViewer.expandAll(); |
852 |
decorateTemplates(); |
853 |
} |
854 |
|
855 |
/** |
856 |
* Create a viewer to display the pattern |
857 |
* |
858 |
* @param parent |
859 |
*/ |
860 |
private void createPatternViewer(Composite parent) { |
861 |
ViewForm viewForm = new ViewForm(parent, SWT.NONE); |
862 |
viewForm.setBorderVisible(false); |
863 |
CLabel previewLabel = new CLabel(viewForm, SWT.NONE); |
864 |
previewLabel.setText(TextEditorTemplateMessages.TemplatesPage_preview); |
865 |
previewLabel.setImage(TemplatesViewImages |
866 |
.getImage(ITemplateViewImageConstants.IMG_OBJ_PREVIEW)); |
867 |
viewForm.setTopLeft(previewLabel); |
868 |
|
869 |
fPatternViewer = createViewer(viewForm); |
870 |
fPatternViewer.setEditable(false); |
871 |
viewForm.setContent(fPatternViewer.getControl()); |
872 |
viewForm.addControlListener(new ControlListener() { |
873 |
public void controlMoved(ControlEvent e) { |
874 |
} |
875 |
|
876 |
public void controlResized(ControlEvent e) { |
877 |
int[] weights = fControl.getWeights(); |
878 |
fSashSize = (int) (weights[0] * 100.0 / (weights[0] + weights[1])); |
879 |
} |
880 |
}); |
881 |
} |
882 |
|
883 |
/** |
884 |
* Sets all templates in the tree that are disabled to grey. |
885 |
*/ |
886 |
private void decorateTemplates() { |
887 |
TreeItem[] contextItems = fTemplatesTree.getItems(); |
888 |
Color fgcDisabled = getShell().getDisplay().getSystemColor(SWT.COLOR_GRAY); |
889 |
Color fgcEnabled = getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND); |
890 |
for (int i = 0; i < contextItems.length; i++) { |
891 |
contextItems[i].setFont(getBoldFont(contextItems[i].getFont())); |
892 |
TreeItem[] templateItems = contextItems[i].getItems(); |
893 |
for (int j = 0; j < templateItems.length; j++) { |
894 |
TemplatePersistenceData templateData = (TemplatePersistenceData) templateItems[j] |
895 |
.getData(); |
896 |
if (templateData == null) |
897 |
continue; |
898 |
if (!templateData.isEnabled() && templateItems[j].getForeground() != fgcDisabled) |
899 |
templateItems[j].setForeground(fgcDisabled); |
900 |
else if (templateData.isEnabled() && templateItems[j].getForeground() != fgcEnabled) |
901 |
templateItems[j].setForeground(fgcEnabled); |
902 |
} |
903 |
} |
904 |
} |
905 |
|
906 |
private Font getBoldFont(Font font) { |
907 |
FontData[] fontData = font.getFontData(); |
908 |
for (int i = 0; i < fontData.length; i++) { |
909 |
fontData[i].setStyle(fontData[i].getStyle() | SWT.BOLD | SWT.ITALIC); |
910 |
} |
911 |
return new Font(font.getDevice(), fontData); |
912 |
} |
913 |
|
914 |
/** |
915 |
* Update the selected template list, type and pattern viewer |
916 |
*/ |
917 |
private void updateSelectedItems() { |
918 |
IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); |
919 |
|
920 |
fSelectedTemplateList.clear(); |
921 |
fSelectedType = null; |
922 |
|
923 |
if (selection.size() == 1) { |
924 |
Object item = selection.getFirstElement(); |
925 |
if (item instanceof TemplateContextType) |
926 |
fSelectedType = (TemplateContextType) item; |
927 |
else |
928 |
fSelectedTemplateList.add(item); |
929 |
} else if (selection.size() > 1) { |
930 |
for (Iterator iterator = selection.iterator(); iterator.hasNext();) { |
931 |
Object item = iterator.next(); |
932 |
if (item instanceof TemplateContextType) { |
933 |
fSelectedTemplateList.clear(); |
934 |
break; |
935 |
} |
936 |
fSelectedTemplateList.add(item); |
937 |
} |
938 |
} |
939 |
if (!fSelectedTemplateList.isEmpty()) { |
940 |
String contextTypeId = ((TemplatePersistenceData) fSelectedTemplateList.get(0)) |
941 |
.getTemplate().getContextTypeId(); |
942 |
fSelectedType = fContextTypeRegistry.getContextType(contextTypeId); |
943 |
for (Iterator iterator = fSelectedTemplateList.iterator(); iterator.hasNext();) { |
944 |
TemplatePersistenceData template = (TemplatePersistenceData) iterator.next(); |
945 |
if (!contextTypeId.equals(template.getTemplate().getContextTypeId())) { |
946 |
fSelectedType = null; |
947 |
break; |
948 |
} |
949 |
} |
950 |
} |
951 |
if (isSingleTemplateSelected()) { |
952 |
updateViewerInput(getSelectedTemplate()); |
953 |
} else |
954 |
fPatternViewer.getDocument().set(""); //$NON-NLS-1$ |
955 |
} |
956 |
|
957 |
/** |
958 |
* Checks whether only one template is selected in the tree |
959 |
* |
960 |
* @return true if only single template is selected |
961 |
*/ |
962 |
private boolean isSingleTemplateSelected() { |
963 |
return fSelectedTemplateList.size() == 1; |
964 |
} |
965 |
|
966 |
/** |
967 |
* Get the selected template. Note: should always precede with a check for |
968 |
* isSingleTemplateSelected |
969 |
* |
970 |
* @return the template (Not the data) |
971 |
*/ |
972 |
private Template getSelectedTemplate() { |
973 |
return ((TemplatePersistenceData) fSelectedTemplateList.get(0)).getTemplate(); |
974 |
} |
975 |
|
976 |
/** |
977 |
* Show the preference templates preference page |
978 |
*/ |
979 |
private void showPreferencePage() { |
980 |
PreferencesUtil.createPreferenceDialogOn(getShell(), getPreferencePageId(), null, null) |
981 |
.open(); |
982 |
} |
983 |
|
984 |
/** |
985 |
* Get the preference page ID for the templates for the given editor. |
986 |
* Subclasses should override. |
987 |
* |
988 |
* @return id of the preference page |
989 |
*/ |
990 |
protected String getPreferencePageId() { |
991 |
return null; |
992 |
} |
993 |
|
994 |
/** |
995 |
* Get the shell |
996 |
* |
997 |
* @return the shell for this view site |
998 |
*/ |
999 |
protected Shell getShell() { |
1000 |
return getSite().getShell(); |
1001 |
} |
1002 |
|
1003 |
/** |
1004 |
* Creates, configures and returns a source viewer to present the template |
1005 |
* pattern on the preference page. Clients may override to provide a custom |
1006 |
* source viewer featuring e.g. syntax coloring. |
1007 |
* |
1008 |
* @param parent |
1009 |
* the parent control |
1010 |
* @return a configured source viewer |
1011 |
*/ |
1012 |
protected SourceViewer createViewer(Composite parent) { |
1013 |
SourceViewer viewer = new SourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL |
1014 |
| SWT.H_SCROLL); |
1015 |
SourceViewerConfiguration configuration = new SourceViewerConfiguration(); |
1016 |
viewer.configure(configuration); |
1017 |
IDocument document = new Document(); |
1018 |
viewer.setDocument(document); |
1019 |
return viewer; |
1020 |
} |
1021 |
|
1022 |
/** |
1023 |
* Updates the pattern viewer. |
1024 |
* |
1025 |
* @param template |
1026 |
*/ |
1027 |
protected void updateViewerInput(Template template) { |
1028 |
if (template != null) |
1029 |
fPatternViewer.getDocument().set(template.getPattern()); |
1030 |
else |
1031 |
fPatternViewer.getDocument().set(""); //$NON-NLS-1$ |
1032 |
} |
1033 |
|
1034 |
/** |
1035 |
* Updates the buttons. |
1036 |
*/ |
1037 |
private void updateButtons() { |
1038 |
fCopyAction.setEnabled(isSingleTemplateSelected()); |
1039 |
fInsertAction.setEnabled(isSingleTemplateSelected()); |
1040 |
fEditAction.setEnabled(isSingleTemplateSelected()); |
1041 |
fRemoveAction.setEnabled(fSelectedTemplateList.size() != 0); |
1042 |
} |
1043 |
|
1044 |
/** |
1045 |
* Add a template |
1046 |
*/ |
1047 |
private void addTemplate() { |
1048 |
String id = getContextTypeId(); |
1049 |
if (id != null) { |
1050 |
Template template = new Template("", "", id, "", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
1051 |
|
1052 |
addTemplate(template, SHOW_DIALOG); |
1053 |
} |
1054 |
} |
1055 |
|
1056 |
/** |
1057 |
* Get the selected or the first context type from the registry. |
1058 |
* |
1059 |
* @return id of the context type |
1060 |
*/ |
1061 |
private String getContextTypeId() { |
1062 |
if (fSelectedType != null) |
1063 |
return fSelectedType.getId(); |
1064 |
Iterator it = fContextTypeRegistry.contextTypes(); |
1065 |
if (it.hasNext()) |
1066 |
return ((TemplateContextType) it.next()).getId(); |
1067 |
return null; |
1068 |
} |
1069 |
|
1070 |
/** |
1071 |
* Add a template |
1072 |
* |
1073 |
* Setting ui to SHOW_DIALOG will force a New dialog. Setting it to |
1074 |
* MAY_HIDE_DIALOG will respect the show new dialog on drop option. |
1075 |
* |
1076 |
* @param template |
1077 |
* @param ui |
1078 |
*/ |
1079 |
private void addTemplate(Template template, int ui) { |
1080 |
Template newTemplate; |
1081 |
if (ui == SHOW_DIALOG || fShowNewDialogOnDrop) |
1082 |
newTemplate = editTemplate(template, false, true); |
1083 |
else |
1084 |
newTemplate = template; |
1085 |
if (newTemplate != null) { |
1086 |
TemplatePersistenceData data = new TemplatePersistenceData(newTemplate, true); |
1087 |
fTemplateStore.add(data); |
1088 |
saveTemplateStore(); |
1089 |
fSelectedTemplateList.clear(); |
1090 |
fSelectedTemplateList.add(data); |
1091 |
changeSelection(); |
1092 |
} |
1093 |
} |
1094 |
|
1095 |
/** |
1096 |
* Save the template store |
1097 |
*/ |
1098 |
private void saveTemplateStore() { |
1099 |
try { |
1100 |
fTemplateStore.save(); |
1101 |
} catch (IOException e) { |
1102 |
e.printStackTrace(); |
1103 |
MessageDialog.openError(getShell(), |
1104 |
TextEditorTemplateMessages.TemplatesPage_save_error_message, e.getMessage()); |
1105 |
} |
1106 |
} |
1107 |
|
1108 |
/** |
1109 |
* Change selection in the viewer. oldData will contain the earlier selected |
1110 |
* template and fCurrentTemplateData will hold the current selection. Either |
1111 |
* can be null. |
1112 |
* |
1113 |
*/ |
1114 |
private void changeSelection() { |
1115 |
fTreeViewer.refresh(); |
1116 |
if (isSingleTemplateSelected()) |
1117 |
fTreeViewer.setSelection(new StructuredSelection(fSelectedTemplateList.get(0)), true); |
1118 |
else |
1119 |
fTreeViewer.setSelection(new StructuredSelection()); |
1120 |
updateSelectedItems(); |
1121 |
} |
1122 |
|
1123 |
/** |
1124 |
* Creates the edit dialog. Subclasses may override this method to provide a |
1125 |
* custom dialog. |
1126 |
* |
1127 |
* @param template |
1128 |
* the template being edited |
1129 |
* @param edit |
1130 |
* whether the dialog should be editable |
1131 |
* @param isNameModifiable |
1132 |
* whether the template name may be modified |
1133 |
* @return the created or modified template, or <code>null</code> if the |
1134 |
* edition failed |
1135 |
* @since 3.1 |
1136 |
*/ |
1137 |
protected Template editTemplate(Template template, boolean edit, boolean isNameModifiable) { |
1138 |
EditTemplateDialog dialog = new EditTemplateDialog(getShell(), template, edit, |
1139 |
isNameModifiable, fContextTypeRegistry); |
1140 |
if (dialog.open() == Window.OK) { |
1141 |
return dialog.getTemplate(); |
1142 |
} |
1143 |
return null; |
1144 |
} |
1145 |
|
1146 |
/** |
1147 |
* Edit the current template |
1148 |
*/ |
1149 |
private void editTemplate() { |
1150 |
Template oldTemplate = getSelectedTemplate(); |
1151 |
Template newTemplate = editTemplate(new Template(oldTemplate), true, true); |
1152 |
if (newTemplate != null) { |
1153 |
if (!newTemplate.getName().equals(oldTemplate.getName()) |
1154 |
&& MessageDialog.openQuestion(getShell(), |
1155 |
TextEditorTemplateMessages.TemplatesPage_question_create_new_title, |
1156 |
TextEditorTemplateMessages.TemplatesPage_question_create_new_message)) { |
1157 |
fSelectedTemplateList.clear(); |
1158 |
TemplatePersistenceData templateData = new TemplatePersistenceData(newTemplate, |
1159 |
true); |
1160 |
fSelectedTemplateList.add(templateData); |
1161 |
fTemplateStore.add(templateData); |
1162 |
} else { |
1163 |
((TemplatePersistenceData) fSelectedTemplateList.get(0)).setTemplate(newTemplate); |
1164 |
} |
1165 |
changeSelection(); |
1166 |
} |
1167 |
saveTemplateStore(); |
1168 |
} |
1169 |
|
1170 |
/** |
1171 |
* Remove the currently selected templates |
1172 |
*/ |
1173 |
private void removeTemplate() { |
1174 |
if (!MessageDialog.openQuestion(getShell(), |
1175 |
TextEditorTemplateMessages.TemplatesPage_remove_title, |
1176 |
TextEditorTemplateMessages.TemplatesPage_remove_message)) |
1177 |
return; |
1178 |
for (Iterator iterator = fSelectedTemplateList.iterator(); iterator.hasNext();) { |
1179 |
TemplatePersistenceData data = (TemplatePersistenceData) iterator.next(); |
1180 |
fTemplateStore.delete(data); |
1181 |
} |
1182 |
saveTemplateStore(); |
1183 |
fSelectedTemplateList.clear(); |
1184 |
changeSelection(); |
1185 |
} |
1186 |
|
1187 |
/** |
1188 |
* Get the pattern viewer. Subclass can override |
1189 |
* |
1190 |
* @return the viewer |
1191 |
*/ |
1192 |
protected SourceViewer getViewer() { |
1193 |
return fPatternViewer; |
1194 |
} |
1195 |
|
1196 |
/* |
1197 |
* (non-Javadoc) |
1198 |
* |
1199 |
* @see org.eclipse.ui.part.Page#getControl() |
1200 |
*/ |
1201 |
public Control getControl() { |
1202 |
return fControl; |
1203 |
} |
1204 |
|
1205 |
/** |
1206 |
* Subclasses should override and facilitate inserting the template code |
1207 |
* into the active editor. |
1208 |
* |
1209 |
* @param template |
1210 |
*/ |
1211 |
public void insertTemplate(Template template) { |
1212 |
} |
1213 |
|
1214 |
/** |
1215 |
* The caret position in the editor has moved into a new context type. It is |
1216 |
* the subclasses responsibility to see that this is called only when needed |
1217 |
* by keeping track of editor contents (eg. partitions). |
1218 |
* |
1219 |
* @param ids |
1220 |
*/ |
1221 |
protected void contextTypeChanged(String[] ids) { |
1222 |
fActiveTypes = Arrays.asList(ids); |
1223 |
if (fLinkWithEditorAction != null && fLinkWithEditor) |
1224 |
refresh(); |
1225 |
} |
1226 |
|
1227 |
/** |
1228 |
* Initialize drag and drop the the template items |
1229 |
*/ |
1230 |
private void initializeDND() { |
1231 |
DragSourceAdapter dragListener = new DragSourceAdapter() { |
1232 |
/* |
1233 |
* (non-Javadoc) |
1234 |
* |
1235 |
* @see org.eclipse.swt.dnd.DragSourceAdapter#dragSetData(org.eclipse.swt.dnd.DragSourceEvent) |
1236 |
*/ |
1237 |
public void dragSetData(DragSourceEvent event) { |
1238 |
if (isSingleTemplateSelected() |
1239 |
&& TemplateTransfer.getInstance().isSupportedType(event.dataType)) { |
1240 |
event.data = new Template[] { getSelectedTemplate() }; |
1241 |
} |
1242 |
} |
1243 |
}; |
1244 |
fTreeViewer.addDragSupport(DND.DROP_COPY, |
1245 |
new Transfer[] { TemplateTransfer.getInstance() }, dragListener); |
1246 |
DropTargetAdapter dropListener = new DropTargetAdapter() { |
1247 |
TextTransfer textTransfer = TextTransfer.getInstance(); |
1248 |
TemplateTransfer templateTransfer = TemplateTransfer.getInstance(); |
1249 |
|
1250 |
/* |
1251 |
* (non-Javadoc) |
1252 |
* |
1253 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#dragEnter(org.eclipse.swt.dnd.DropTargetEvent) |
1254 |
*/ |
1255 |
public void dragEnter(DropTargetEvent event) { |
1256 |
if (event.detail == DND.DROP_DEFAULT) |
1257 |
event.detail = DND.DROP_COPY; |
1258 |
} |
1259 |
|
1260 |
/* |
1261 |
* (non-Javadoc) |
1262 |
* |
1263 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent) |
1264 |
*/ |
1265 |
public void dragOperationChanged(DropTargetEvent event) { |
1266 |
if (event.detail == DND.DROP_DEFAULT) |
1267 |
event.detail = DND.DROP_COPY; |
1268 |
} |
1269 |
|
1270 |
/* |
1271 |
* (non-Javadoc) |
1272 |
* |
1273 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#dragOver(org.eclipse.swt.dnd.DropTargetEvent) |
1274 |
*/ |
1275 |
public void dragOver(DropTargetEvent event) { |
1276 |
event.detail = DND.DROP_NONE; |
1277 |
if (event.item == null) |
1278 |
return; |
1279 |
int index = 0; |
1280 |
while (index < event.dataTypes.length) { |
1281 |
if (textTransfer.isSupportedType(event.dataTypes[index])) |
1282 |
break; |
1283 |
if (templateTransfer.isSupportedType(event.dataTypes[index])) |
1284 |
break; |
1285 |
index++; |
1286 |
} |
1287 |
if (index < event.dataTypes.length) { |
1288 |
event.currentDataType = event.dataTypes[index]; |
1289 |
event.detail = DND.DROP_COPY; |
1290 |
return; |
1291 |
} |
1292 |
} |
1293 |
|
1294 |
/* |
1295 |
* (non-Javadoc) |
1296 |
* |
1297 |
* @see org.eclipse.swt.dnd.DropTargetAdapter#drop(org.eclipse.swt.dnd.DropTargetEvent) |
1298 |
*/ |
1299 |
public void drop(DropTargetEvent event) { |
1300 |
if (event.item == null) |
1301 |
return; |
1302 |
Object object = ((TreeItem) event.item).getData(); |
1303 |
String contextId; |
1304 |
if (object instanceof TemplateContextType) |
1305 |
contextId = ((TemplateContextType) object).getId(); |
1306 |
else |
1307 |
contextId = ((TemplatePersistenceData) object).getTemplate().getContextTypeId(); |
1308 |
if (textTransfer.isSupportedType(event.currentDataType)) { |
1309 |
String text = (String) event.data; |
1310 |
final Template template = new Template( |
1311 |
TextEditorTemplateMessages.TemplatesPage_snippet, |
1312 |
TextEditorTemplateMessages.TemplatesPage_paste_description, contextId, |
1313 |
text.replaceAll("\\$", "\\$\\$"), true); //$NON-NLS-1$//$NON-NLS-2$ |
1314 |
getShell().getDisplay().asyncExec(new Runnable() { |
1315 |
public void run() { |
1316 |
addTemplate(template, MAY_HIDE_DIALOG); |
1317 |
} |
1318 |
}); |
1319 |
} else if (templateTransfer.isSupportedType(event.currentDataType)) { |
1320 |
Template[] templates = (Template[]) event.data; |
1321 |
Template t = templates[0]; |
1322 |
final Template template = new Template(t.getName(), t.getDescription(), |
1323 |
contextId, t.getPattern(), true); |
1324 |
getShell().getDisplay().asyncExec(new Runnable() { |
1325 |
public void run() { |
1326 |
addTemplate(template, MAY_HIDE_DIALOG); |
1327 |
} |
1328 |
}); |
1329 |
} |
1330 |
} |
1331 |
|
1332 |
}; |
1333 |
fTreeViewer.addDropSupport(DND.DROP_COPY, new Transfer[] { TemplateTransfer.getInstance(), |
1334 |
TextTransfer.getInstance() }, dropListener); |
1335 |
} |
1336 |
|
1337 |
/** |
1338 |
* Setup the editor site as a drop target. Should be invoked by the |
1339 |
* subclasses for the D&D to work with the editor. |
1340 |
* |
1341 |
* @param site |
1342 |
* @param viewer |
1343 |
*/ |
1344 |
protected void setupEditorDropTarget(IWorkbenchPartSite site, Control viewer) { |
1345 |
IDragAndDropService dndService = (IDragAndDropService) site |
1346 |
.getService(IDragAndDropService.class); |
1347 |
EditorDropTarget editorDropTarget = new EditorDropTarget(); |
1348 |
dndService.addMergedDropTarget(viewer, DND.DROP_COPY, new Transfer[] { TemplateTransfer |
1349 |
.getInstance() }, editorDropTarget); |
1350 |
} |
1351 |
|
1352 |
/** |
1353 |
* Refresh the template tree contents |
1354 |
*/ |
1355 |
private void refresh() { |
1356 |
fTreeViewer.refresh(); |
1357 |
fTreeViewer.expandAll(); |
1358 |
decorateTemplates(); |
1359 |
} |
1360 |
|
1361 |
/* |
1362 |
* (non-Javadoc) |
1363 |
* |
1364 |
* @see org.eclipse.ui.part.Page#setFocus() |
1365 |
*/ |
1366 |
public void setFocus() { |
1367 |
} |
1368 |
|
1369 |
/* |
1370 |
* (non-Javadoc) |
1371 |
* |
1372 |
* @see org.eclipse.ui.part.Page#dispose() |
1373 |
*/ |
1374 |
public void dispose() { |
1375 |
if (fContextMenu != null && !fContextMenu.isDisposed()) |
1376 |
fContextMenu.dispose(); |
1377 |
fTemplatePreferenceStore.removePropertyChangeListener(fTemplateChangeListener); |
1378 |
savePreferences(); |
1379 |
super.dispose(); |
1380 |
} |
1381 |
|
1382 |
/** |
1383 |
* Save the preferences |
1384 |
*/ |
1385 |
private void savePreferences() { |
1386 |
fPreferenceStore.setValue(SASH_SIZE_PREF_ID, fSashSize); |
1387 |
fPreferenceStore.setValue(LINK_ACTION_PREF_ID, fLinkWithEditor); |
1388 |
fPreferenceStore.setValue(SHOW_DISABLED_ACTION_PREF_ID, fShowDisabled); |
1389 |
fPreferenceStore.setValue(SHOW_NEW_DIALOG_ACTION_PREF_ID, fShowNewDialogOnDrop); |
1390 |
fPreferenceStore.setValue(COLUMN_NAME_WIDTH_PREF_ID, fNameWidth); |
1391 |
fPreferenceStore.setValue(COLUMN_DESCRIPTION_WIDTH_PREF_ID, fDescriptionWidth); |
1392 |
} |
1393 |
} |