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