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