KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > CodeTemplateBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  * John Kaplan, johnkaplantech@gmail.com - 108071 [code templates] template for body of newly created class
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.BufferedOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.eclipse.core.resources.IProject;
30
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.KeyEvent;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.FileDialog;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Shell;
42
43 import org.eclipse.jface.dialogs.MessageDialog;
44 import org.eclipse.jface.preference.IPreferenceStore;
45 import org.eclipse.jface.resource.JFaceResources;
46 import org.eclipse.jface.viewers.LabelProvider;
47 import org.eclipse.jface.viewers.StructuredSelection;
48 import org.eclipse.jface.viewers.ViewerComparator;
49 import org.eclipse.jface.window.Window;
50
51 import org.eclipse.jface.text.Document;
52 import org.eclipse.jface.text.IDocument;
53 import org.eclipse.jface.text.source.SourceViewer;
54 import org.eclipse.jface.text.templates.Template;
55 import org.eclipse.jface.text.templates.TemplateContextType;
56 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
57 import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter;
58
59 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType;
60 import org.eclipse.jdt.internal.corext.util.Messages;
61
62 import org.eclipse.jdt.ui.PreferenceConstants;
63 import org.eclipse.jdt.ui.text.IJavaPartitions;
64 import org.eclipse.jdt.ui.text.JavaTextTools;
65
66 import org.eclipse.jdt.internal.ui.JavaPlugin;
67 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
68 import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
69 import org.eclipse.jdt.internal.ui.util.PixelConverter;
70 import org.eclipse.jdt.internal.ui.viewsupport.ProjectTemplateStore;
71 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
72 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
73 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ITreeListAdapter;
74 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
75 import org.eclipse.jdt.internal.ui.wizards.dialogfields.TreeListDialogField;
76
77 /**
78   */

79 public class CodeTemplateBlock {
80     
81     private class CodeTemplateAdapter extends ViewerComparator implements ITreeListAdapter, IDialogFieldListener {
82
83         private final Object JavaDoc[] NO_CHILDREN= new Object JavaDoc[0];
84
85         public void customButtonPressed(TreeListDialogField field, int index) {
86             doButtonPressed(index, field.getSelectedElements());
87         }
88             
89         public void selectionChanged(TreeListDialogField field) {
90             List JavaDoc selected= field.getSelectedElements();
91             field.enableButton(IDX_EDIT, canEdit(selected));
92             field.enableButton(IDX_EXPORT, !selected.isEmpty());
93             
94             updateSourceViewerInput(selected);
95         }
96
97         public void doubleClicked(TreeListDialogField field) {
98             List JavaDoc selected= field.getSelectedElements();
99             if (canEdit(selected)) {
100                 doButtonPressed(IDX_EDIT, selected);
101             }
102         }
103
104         public Object JavaDoc[] getChildren(TreeListDialogField field, Object JavaDoc element) {
105             if (element == COMMENT_NODE || element == CODE_NODE) {
106                 return getTemplateOfCategory(element == COMMENT_NODE);
107             }
108             return NO_CHILDREN;
109         }
110
111         public Object JavaDoc getParent(TreeListDialogField field, Object JavaDoc element) {
112             if (element instanceof TemplatePersistenceData) {
113                 TemplatePersistenceData data= (TemplatePersistenceData) element;
114                 if (data.getTemplate().getName().endsWith(CodeTemplateContextType.COMMENT_SUFFIX)) {
115                     return COMMENT_NODE;
116                 }
117                 return CODE_NODE;
118             }
119             return null;
120         }
121
122         public boolean hasChildren(TreeListDialogField field, Object JavaDoc element) {
123             return (element == COMMENT_NODE || element == CODE_NODE);
124         }
125
126         public void dialogFieldChanged(DialogField field) {
127         }
128
129         public void keyPressed(TreeListDialogField field, KeyEvent event) {
130         }
131         
132         /* (non-Javadoc)
133          * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
134          */

135         public int category(Object JavaDoc element) {
136             if (element == COMMENT_NODE) {
137                 return 1;
138             } else if (element == CODE_NODE) {
139                 return 2;
140             }
141             
142             TemplatePersistenceData data= (TemplatePersistenceData) element;
143             String JavaDoc id= data.getId();
144             
145             if (CodeTemplateContextType.NEWTYPE_ID.equals(id)) {
146                 return 101;
147             } else if (CodeTemplateContextType.CLASSBODY_ID.equals(id)) {
148                 return 102;
149             } else if (CodeTemplateContextType.INTERFACEBODY_ID.equals(id)) {
150                 return 103;
151             } else if (CodeTemplateContextType.ENUMBODY_ID.equals(id)) {
152                 return 104;
153             } else if (CodeTemplateContextType.ANNOTATIONBODY_ID.equals(id)) {
154                 return 105;
155             } else if (CodeTemplateContextType.METHODSTUB_ID.equals(id)) {
156                 return 106;
157             } else if (CodeTemplateContextType.CONSTRUCTORSTUB_ID.equals(id)) {
158                 return 107;
159             } else if (CodeTemplateContextType.GETTERSTUB_ID.equals(id)) {
160                 return 108;
161             } else if (CodeTemplateContextType.SETTERSTUB_ID.equals(id)) {
162                 return 109;
163             } else if (CodeTemplateContextType.CATCHBLOCK_ID.equals(id)) {
164                 return 110;
165             } else if (CodeTemplateContextType.FILECOMMENT_ID.equals(id)) {
166                 return 1;
167             } else if (CodeTemplateContextType.TYPECOMMENT_ID.equals(id)) {
168                 return 2;
169             } else if (CodeTemplateContextType.FIELDCOMMENT_ID.equals(id)) {
170                 return 3;
171             } else if (CodeTemplateContextType.CONSTRUCTORCOMMENT_ID.equals(id)) {
172                 return 4;
173             } else if (CodeTemplateContextType.METHODCOMMENT_ID.equals(id)) {
174                 return 5;
175             } else if (CodeTemplateContextType.OVERRIDECOMMENT_ID.equals(id)) {
176                 return 6;
177             } else if (CodeTemplateContextType.DELEGATECOMMENT_ID.equals(id)) {
178                 return 7;
179             } else if (CodeTemplateContextType.GETTERCOMMENT_ID.equals(id)) {
180                 return 8;
181             } else if (CodeTemplateContextType.SETTERCOMMENT_ID.equals(id)) {
182                 return 9;
183             }
184             return 1000;
185         }
186     }
187
188     private static class CodeTemplateLabelProvider extends LabelProvider {
189     
190         /* (non-Javadoc)
191          * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
192          */

193         public Image getImage(Object JavaDoc element) {
194             return null;
195
196         }
197
198         /* (non-Javadoc)
199          * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
200          */

201         public String JavaDoc getText(Object JavaDoc element) {
202             if (element == COMMENT_NODE || element == CODE_NODE) {
203                 return (String JavaDoc) element;
204             }
205             TemplatePersistenceData data= (TemplatePersistenceData) element;
206             String JavaDoc id=data.getId();
207             if (CodeTemplateContextType.CATCHBLOCK_ID.equals(id)) {
208                 return PreferencesMessages.CodeTemplateBlock_catchblock_label;
209             } else if (CodeTemplateContextType.METHODSTUB_ID.equals(id)) {
210                 return PreferencesMessages.CodeTemplateBlock_methodstub_label;
211             } else if (CodeTemplateContextType.CONSTRUCTORSTUB_ID.equals(id)) {
212                 return PreferencesMessages.CodeTemplateBlock_constructorstub_label;
213             } else if (CodeTemplateContextType.GETTERSTUB_ID.equals(id)) {
214                 return PreferencesMessages.CodeTemplateBlock_getterstub_label;
215             } else if (CodeTemplateContextType.SETTERSTUB_ID.equals(id)) {
216                 return PreferencesMessages.CodeTemplateBlock_setterstub_label;
217             } else if (CodeTemplateContextType.NEWTYPE_ID.equals(id)) {
218                 return PreferencesMessages.CodeTemplateBlock_newtype_label;
219             } else if (CodeTemplateContextType.CLASSBODY_ID.equals(id)) {
220                 return PreferencesMessages.CodeTemplateBlock_classbody_label;
221             } else if (CodeTemplateContextType.INTERFACEBODY_ID.equals(id)) {
222                 return PreferencesMessages.CodeTemplateBlock_interfacebody_label;
223             } else if (CodeTemplateContextType.ENUMBODY_ID.equals(id)) {
224                 return PreferencesMessages.CodeTemplateBlock_enumbody_label;
225             } else if (CodeTemplateContextType.ANNOTATIONBODY_ID.equals(id)) {
226                 return PreferencesMessages.CodeTemplateBlock_annotationbody_label;
227             } else if (CodeTemplateContextType.FILECOMMENT_ID.equals(id)) {
228                 return PreferencesMessages.CodeTemplateBlock_filecomment_label;
229             } else if (CodeTemplateContextType.TYPECOMMENT_ID.equals(id)) {
230                 return PreferencesMessages.CodeTemplateBlock_typecomment_label;
231             } else if (CodeTemplateContextType.FIELDCOMMENT_ID.equals(id)) {
232                 return PreferencesMessages.CodeTemplateBlock_fieldcomment_label;
233             } else if (CodeTemplateContextType.METHODCOMMENT_ID.equals(id)) {
234                 return PreferencesMessages.CodeTemplateBlock_methodcomment_label;
235             } else if (CodeTemplateContextType.OVERRIDECOMMENT_ID.equals(id)) {
236                 return PreferencesMessages.CodeTemplateBlock_overridecomment_label;
237             } else if (CodeTemplateContextType.DELEGATECOMMENT_ID.equals(id)) {
238                 return PreferencesMessages.CodeTemplateBlock_delegatecomment_label;
239             } else if (CodeTemplateContextType.CONSTRUCTORCOMMENT_ID.equals(id)) {
240                 return PreferencesMessages.CodeTemplateBlock_constructorcomment_label;
241             } else if (CodeTemplateContextType.GETTERCOMMENT_ID.equals(id)) {
242                 return PreferencesMessages.CodeTemplateBlock_gettercomment_label;
243             } else if (CodeTemplateContextType.SETTERCOMMENT_ID.equals(id)) {
244                 return PreferencesMessages.CodeTemplateBlock_settercomment_label;
245             }
246             return data.getTemplate().getDescription();
247         }
248     }
249     
250     private final static int IDX_EDIT= 0;
251     private final static int IDX_IMPORT= 2;
252     private final static int IDX_EXPORT= 3;
253     private final static int IDX_EXPORTALL= 4;
254     
255     protected final static Object JavaDoc COMMENT_NODE= PreferencesMessages.CodeTemplateBlock_templates_comment_node;
256     protected final static Object JavaDoc CODE_NODE= PreferencesMessages.CodeTemplateBlock_templates_code_node;
257
258     private TreeListDialogField fCodeTemplateTree;
259
260     protected ProjectTemplateStore fTemplateStore;
261     
262     private PixelConverter fPixelConverter;
263     private SourceViewer fPatternViewer;
264     private Control fSWTWidget;
265     private TemplateVariableProcessor fTemplateProcessor;
266     
267     private final IProject fProject;
268
269     public CodeTemplateBlock(IProject project) {
270         
271         fProject= project;
272         
273         fTemplateStore= new ProjectTemplateStore(project);
274         try {
275             fTemplateStore.load();
276         } catch (IOException JavaDoc e) {
277             JavaPlugin.log(e);
278         }
279         
280         fTemplateProcessor= new TemplateVariableProcessor();
281         
282         CodeTemplateAdapter adapter= new CodeTemplateAdapter();
283
284         String JavaDoc[] buttonLabels= new String JavaDoc[] {
285             PreferencesMessages.CodeTemplateBlock_templates_edit_button,
286             /* */ null,
287             PreferencesMessages.CodeTemplateBlock_templates_import_button,
288             PreferencesMessages.CodeTemplateBlock_templates_export_button,
289             PreferencesMessages.CodeTemplateBlock_templates_exportall_button
290
291         };
292         fCodeTemplateTree= new TreeListDialogField(adapter, buttonLabels, new CodeTemplateLabelProvider());
293         fCodeTemplateTree.setDialogFieldListener(adapter);
294         fCodeTemplateTree.setLabelText(PreferencesMessages.CodeTemplateBlock_templates_label);
295         fCodeTemplateTree.setViewerComparator(adapter);
296
297         fCodeTemplateTree.enableButton(IDX_EXPORT, false);
298         fCodeTemplateTree.enableButton(IDX_EDIT, false);
299         
300         fCodeTemplateTree.addElement(COMMENT_NODE);
301         fCodeTemplateTree.addElement(CODE_NODE);
302
303         fCodeTemplateTree.selectFirstElement();
304     }
305
306     public void postSetSelection(Object JavaDoc element) {
307         fCodeTemplateTree.postSetSelection(new StructuredSelection(element));
308     }
309
310     public boolean hasProjectSpecificOptions(IProject project) {
311         if (project != null) {
312             return ProjectTemplateStore.hasProjectSpecificTempates(project);
313         }
314         return false;
315     }
316     
317     protected Control createContents(Composite parent) {
318         fPixelConverter= new PixelConverter(parent);
319         fSWTWidget= parent;
320         
321         Composite composite= new Composite(parent, SWT.NONE);
322         composite.setFont(parent.getFont());
323         
324         GridLayout layout= new GridLayout();
325         layout.marginHeight= 0;
326         layout.marginWidth= 0;
327         layout.numColumns= 2;
328         composite.setLayout(layout);
329         
330         fCodeTemplateTree.doFillIntoGrid(composite, 3);
331         LayoutUtil.setHorizontalSpan(fCodeTemplateTree.getLabelControl(null), 2);
332         LayoutUtil.setHorizontalGrabbing(fCodeTemplateTree.getTreeControl(null));
333         
334         fPatternViewer= createViewer(composite, 2);
335         
336         return composite;
337     }
338     
339     private Shell getShell() {
340         if (fSWTWidget != null) {
341             return fSWTWidget.getShell();
342         }
343         return JavaPlugin.getActiveWorkbenchShell();
344     }
345     
346     private SourceViewer createViewer(Composite parent, int nColumns) {
347         Label label= new Label(parent, SWT.NONE);
348         label.setText(PreferencesMessages.CodeTemplateBlock_preview);
349         GridData data= new GridData();
350         data.horizontalSpan= nColumns;
351         label.setLayoutData(data);
352         
353         IDocument document= new Document();
354         JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
355         tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
356         IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
357         SourceViewer viewer= new JavaSourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
358         CodeTemplateSourceViewerConfiguration configuration= new CodeTemplateSourceViewerConfiguration(tools.getColorManager(), store, null, fTemplateProcessor);
359         viewer.configure(configuration);
360         viewer.setEditable(false);
361         viewer.setDocument(document);
362     
363         Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
364         viewer.getTextWidget().setFont(font);
365         new JavaSourcePreviewerUpdater(viewer, configuration, store);
366         
367         Control control= viewer.getControl();
368         data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
369         data.horizontalSpan= nColumns;
370         data.heightHint= fPixelConverter.convertHeightInCharsToPixels(5);
371         control.setLayoutData(data);
372         
373         return viewer;
374     }
375     
376     protected TemplatePersistenceData[] getTemplateOfCategory(boolean isComment) {
377         ArrayList JavaDoc res= new ArrayList JavaDoc();
378         TemplatePersistenceData[] templates= fTemplateStore.getTemplateData();
379         for (int i= 0; i < templates.length; i++) {
380             TemplatePersistenceData curr= templates[i];
381             if (isComment == curr.getTemplate().getName().endsWith(CodeTemplateContextType.COMMENT_SUFFIX)) {
382                 res.add(curr);
383             }
384         }
385         return (TemplatePersistenceData[]) res.toArray(new TemplatePersistenceData[res.size()]);
386     }
387     
388     protected static boolean canEdit(List JavaDoc selected) {
389         return selected.size() == 1 && (selected.get(0) instanceof TemplatePersistenceData);
390     }
391     
392     protected void updateSourceViewerInput(List JavaDoc selection) {
393         if (fPatternViewer == null || fPatternViewer.getTextWidget().isDisposed()) {
394             return;
395         }
396         if (selection.size() == 1 && selection.get(0) instanceof TemplatePersistenceData) {
397             TemplatePersistenceData data= (TemplatePersistenceData) selection.get(0);
398             Template template= data.getTemplate();
399             TemplateContextType type= JavaPlugin.getDefault().getCodeTemplateContextRegistry().getContextType(template.getContextTypeId());
400             fTemplateProcessor.setContextType(type);
401             fPatternViewer.getDocument().set(template.getPattern());
402         } else {
403             fPatternViewer.getDocument().set(""); //$NON-NLS-1$
404
}
405     }
406         
407     protected void doButtonPressed(int buttonIndex, List JavaDoc selected) {
408         if (buttonIndex == IDX_EDIT) {
409             edit((TemplatePersistenceData) selected.get(0));
410         } else if (buttonIndex == IDX_EXPORT) {
411             export(selected);
412         } else if (buttonIndex == IDX_EXPORTALL) {
413             exportAll();
414         } else if (buttonIndex == IDX_IMPORT) {
415             import_();
416         }
417     }
418     
419     private void edit(TemplatePersistenceData data) {
420         Template newTemplate= new Template(data.getTemplate());
421         EditTemplateDialog dialog= new EditTemplateDialog(getShell(), newTemplate, true, false, JavaPlugin.getDefault().getCodeTemplateContextRegistry());
422         if (dialog.open() == Window.OK) {
423             // changed
424
data.setTemplate(dialog.getTemplate());
425             fCodeTemplateTree.refresh(data);
426             fCodeTemplateTree.selectElements(new StructuredSelection(data));
427         }
428     }
429         
430     private void import_() {
431         FileDialog dialog= new FileDialog(getShell());
432         dialog.setText(PreferencesMessages.CodeTemplateBlock_import_title);
433         dialog.setFilterExtensions(new String JavaDoc[] {PreferencesMessages.CodeTemplateBlock_import_extension});
434         String JavaDoc path= dialog.open();
435         
436         if (path == null)
437             return;
438         
439         try {
440             TemplateReaderWriter reader= new TemplateReaderWriter();
441             File JavaDoc file= new File JavaDoc(path);
442             if (file.exists()) {
443                 InputStream JavaDoc input= new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
444                 try {
445                     TemplatePersistenceData[] datas= reader.read(input, null);
446                     for (int i= 0; i < datas.length; i++) {
447                         updateTemplate(datas[i]);
448                     }
449                 } finally {
450                     try {
451                         input.close();
452                     } catch (IOException JavaDoc x) {
453                     }
454                 }
455             }
456
457             fCodeTemplateTree.refresh();
458             updateSourceViewerInput(fCodeTemplateTree.getSelectedElements());
459
460         } catch (FileNotFoundException JavaDoc e) {
461             openReadErrorDialog(e);
462         } catch (IOException JavaDoc e) {
463             openReadErrorDialog(e);
464         }
465
466     }
467     
468     private void updateTemplate(TemplatePersistenceData data) {
469         TemplatePersistenceData[] datas= fTemplateStore.getTemplateData();
470         for (int i= 0; i < datas.length; i++) {
471             String JavaDoc id= datas[i].getId();
472             if (id != null && id.equals(data.getId())) {
473                 datas[i].setTemplate(data.getTemplate());
474                 break;
475             }
476         }
477     }
478     
479     private void exportAll() {
480         export(fTemplateStore.getTemplateData());
481     }
482     
483     private void export(List JavaDoc selected) {
484         Set JavaDoc datas= new HashSet JavaDoc();
485         for (int i= 0; i < selected.size(); i++) {
486             Object JavaDoc curr= selected.get(i);
487             if (curr instanceof TemplatePersistenceData) {
488                 datas.add(curr);
489             } else {
490                 TemplatePersistenceData[] cat= getTemplateOfCategory(curr == COMMENT_NODE);
491                 datas.addAll(Arrays.asList(cat));
492             }
493         }
494         export((TemplatePersistenceData[]) datas.toArray(new TemplatePersistenceData[datas.size()]));
495     }
496     
497     private void export(TemplatePersistenceData[] templates) {
498         FileDialog dialog= new FileDialog(getShell(), SWT.SAVE);
499         dialog.setText(Messages.format(PreferencesMessages.CodeTemplateBlock_export_title, String.valueOf(templates.length)));
500         dialog.setFilterExtensions(new String JavaDoc[] {PreferencesMessages.CodeTemplateBlock_export_extension});
501         dialog.setFileName(PreferencesMessages.CodeTemplateBlock_export_filename);
502         String JavaDoc path= dialog.open();
503
504         if (path == null)
505             return;
506         
507         File JavaDoc file= new File JavaDoc(path);
508
509         if (file.isHidden()) {
510             String JavaDoc title= PreferencesMessages.CodeTemplateBlock_export_error_title;
511             String JavaDoc message= Messages.format(PreferencesMessages.CodeTemplateBlock_export_error_hidden, file.getAbsolutePath());
512             MessageDialog.openError(getShell(), title, message);
513             return;
514         }
515         
516         if (file.exists() && !file.canWrite()) {
517             String JavaDoc title= PreferencesMessages.CodeTemplateBlock_export_error_title;
518             String JavaDoc message= Messages.format(PreferencesMessages.CodeTemplateBlock_export_error_canNotWrite, file.getAbsolutePath());
519             MessageDialog.openError(getShell(), title, message);
520             return;
521         }
522
523         if (!file.exists() || confirmOverwrite(file)) {
524             OutputStream JavaDoc output= null;
525             try {
526                 output= new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
527                 TemplateReaderWriter writer= new TemplateReaderWriter();
528                 writer.save(templates, output);
529                 output.close();
530             } catch (IOException JavaDoc e) {
531                 if (output != null) {
532                     try {
533                         output.close();
534                     } catch (IOException JavaDoc e2) {
535                         // ignore
536
}
537                 }
538                 openWriteErrorDialog(e);
539             }
540         }
541         
542     }
543
544     private boolean confirmOverwrite(File JavaDoc file) {
545         return MessageDialog.openQuestion(getShell(),
546             PreferencesMessages.CodeTemplateBlock_export_exists_title,
547             Messages.format(PreferencesMessages.CodeTemplateBlock_export_exists_message, file.getAbsolutePath()));
548     }
549
550     public void performDefaults() {
551         fTemplateStore.restoreDefaults();
552         
553         // refresh
554
fCodeTemplateTree.refresh();
555         updateSourceViewerInput(fCodeTemplateTree.getSelectedElements());
556     }
557     
558     public boolean performOk(boolean enabled) {
559         if (fProject != null) {
560             TemplatePersistenceData[] templateData= fTemplateStore.getTemplateData();
561             for (int i= 0; i < templateData.length; i++) {
562                 fTemplateStore.setProjectSpecific(templateData[i].getId(), enabled);
563             }
564         }
565         try {
566             fTemplateStore.save();
567         } catch (IOException JavaDoc e) {
568             JavaPlugin.log(e);
569             openWriteErrorDialog(e);
570         }
571         return true;
572     }
573     
574     public void performCancel() {
575         try {
576             fTemplateStore.revertChanges();
577         } catch (IOException JavaDoc e) {
578             openReadErrorDialog(e);
579         }
580     }
581     
582     private void openReadErrorDialog(Exception JavaDoc e) {
583         String JavaDoc title= PreferencesMessages.CodeTemplateBlock_error_read_title;
584         
585         String JavaDoc message= e.getLocalizedMessage();
586         if (message != null)
587             message= Messages.format(PreferencesMessages.CodeTemplateBlock_error_parse_message, message);
588         else
589             message= PreferencesMessages.CodeTemplateBlock_error_read_message;
590         MessageDialog.openError(getShell(), title, message);
591     }
592     
593     private void openWriteErrorDialog(Exception JavaDoc e) {
594         String JavaDoc title= PreferencesMessages.CodeTemplateBlock_error_write_title;
595         String JavaDoc message= PreferencesMessages.CodeTemplateBlock_error_write_message;
596         MessageDialog.openError(getShell(), title, message);
597     }
598 }
599
Popular Tags