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