001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * John Kaplan, johnkaplantech@gmail.com - 108071 [code templates] template for body of newly created class
011: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
012:
013: import java.io.BufferedInputStream;
014: import java.io.BufferedOutputStream;
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.FileNotFoundException;
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Set;
027:
028: import org.eclipse.core.resources.IProject;
029:
030: import org.eclipse.swt.SWT;
031: import org.eclipse.swt.events.KeyEvent;
032: import org.eclipse.swt.graphics.Font;
033: import org.eclipse.swt.graphics.Image;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.layout.GridLayout;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Control;
038: import org.eclipse.swt.widgets.FileDialog;
039: import org.eclipse.swt.widgets.Label;
040:
041: import org.eclipse.jface.dialogs.MessageDialog;
042: import org.eclipse.jface.preference.IPreferenceStore;
043: import org.eclipse.jface.resource.JFaceResources;
044: import org.eclipse.jface.viewers.LabelProvider;
045: import org.eclipse.jface.viewers.StructuredSelection;
046: import org.eclipse.jface.viewers.ViewerComparator;
047: import org.eclipse.jface.window.Window;
048:
049: import org.eclipse.jface.text.Document;
050: import org.eclipse.jface.text.IDocument;
051: import org.eclipse.jface.text.source.SourceViewer;
052: import org.eclipse.jface.text.templates.Template;
053: import org.eclipse.jface.text.templates.TemplateContextType;
054: import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
055: import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter;
056:
057: import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
058:
059: import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType;
060: import org.eclipse.jdt.internal.corext.util.Messages;
061:
062: import org.eclipse.jdt.ui.PreferenceConstants;
063: import org.eclipse.jdt.ui.text.IJavaPartitions;
064: import org.eclipse.jdt.ui.text.JavaTextTools;
065:
066: import org.eclipse.jdt.internal.ui.JavaPlugin;
067: import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
068: import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
069: import org.eclipse.jdt.internal.ui.util.PixelConverter;
070: import org.eclipse.jdt.internal.ui.viewsupport.ProjectTemplateStore;
071: import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
072: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
073: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
074: import org.eclipse.jdt.internal.ui.wizards.dialogfields.ITreeListAdapter;
075: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
076: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
077: import org.eclipse.jdt.internal.ui.wizards.dialogfields.TreeListDialogField;
078:
079: /**
080: */
081: public class CodeTemplateBlock extends OptionsConfigurationBlock {
082:
083: private class CodeTemplateAdapter extends ViewerComparator
084: implements ITreeListAdapter, IDialogFieldListener {
085:
086: private final Object[] NO_CHILDREN = new Object[0];
087:
088: public void customButtonPressed(TreeListDialogField field,
089: int index) {
090: doButtonPressed(index, field.getSelectedElements());
091: }
092:
093: public void selectionChanged(TreeListDialogField field) {
094: List selected = field.getSelectedElements();
095: field.enableButton(IDX_EDIT, canEdit(selected));
096: field.enableButton(IDX_EXPORT, !selected.isEmpty());
097:
098: updateSourceViewerInput(selected);
099: }
100:
101: public void doubleClicked(TreeListDialogField field) {
102: List selected = field.getSelectedElements();
103: if (canEdit(selected)) {
104: doButtonPressed(IDX_EDIT, selected);
105: }
106: }
107:
108: public Object[] getChildren(TreeListDialogField field,
109: Object element) {
110: if (element == COMMENT_NODE || element == CODE_NODE) {
111: return getTemplateOfCategory(element == COMMENT_NODE);
112: }
113: return NO_CHILDREN;
114: }
115:
116: public Object getParent(TreeListDialogField field,
117: Object element) {
118: if (element instanceof TemplatePersistenceData) {
119: TemplatePersistenceData data = (TemplatePersistenceData) element;
120: if (data.getTemplate().getName().endsWith(
121: CodeTemplateContextType.COMMENT_SUFFIX)) {
122: return COMMENT_NODE;
123: }
124: return CODE_NODE;
125: }
126: return null;
127: }
128:
129: public boolean hasChildren(TreeListDialogField field,
130: Object element) {
131: return (element == COMMENT_NODE || element == CODE_NODE);
132: }
133:
134: public void dialogFieldChanged(DialogField field) {
135: if (field == fGenerateComments) {
136: setValue(PREF_GENERATE_COMMENTS, fGenerateComments
137: .isSelected());
138: }
139: }
140:
141: public void keyPressed(TreeListDialogField field, KeyEvent event) {
142: }
143:
144: /* (non-Javadoc)
145: * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
146: */
147: public int category(Object element) {
148: if (element == COMMENT_NODE) {
149: return 1;
150: } else if (element == CODE_NODE) {
151: return 2;
152: }
153:
154: TemplatePersistenceData data = (TemplatePersistenceData) element;
155: String id = data.getId();
156:
157: if (CodeTemplateContextType.NEWTYPE_ID.equals(id)) {
158: return 101;
159: } else if (CodeTemplateContextType.CLASSBODY_ID.equals(id)) {
160: return 102;
161: } else if (CodeTemplateContextType.INTERFACEBODY_ID
162: .equals(id)) {
163: return 103;
164: } else if (CodeTemplateContextType.ENUMBODY_ID.equals(id)) {
165: return 104;
166: } else if (CodeTemplateContextType.ANNOTATIONBODY_ID
167: .equals(id)) {
168: return 105;
169: } else if (CodeTemplateContextType.METHODSTUB_ID.equals(id)) {
170: return 106;
171: } else if (CodeTemplateContextType.CONSTRUCTORSTUB_ID
172: .equals(id)) {
173: return 107;
174: } else if (CodeTemplateContextType.GETTERSTUB_ID.equals(id)) {
175: return 108;
176: } else if (CodeTemplateContextType.SETTERSTUB_ID.equals(id)) {
177: return 109;
178: } else if (CodeTemplateContextType.CATCHBLOCK_ID.equals(id)) {
179: return 110;
180: } else if (CodeTemplateContextType.FILECOMMENT_ID
181: .equals(id)) {
182: return 1;
183: } else if (CodeTemplateContextType.TYPECOMMENT_ID
184: .equals(id)) {
185: return 2;
186: } else if (CodeTemplateContextType.FIELDCOMMENT_ID
187: .equals(id)) {
188: return 3;
189: } else if (CodeTemplateContextType.CONSTRUCTORCOMMENT_ID
190: .equals(id)) {
191: return 4;
192: } else if (CodeTemplateContextType.METHODCOMMENT_ID
193: .equals(id)) {
194: return 5;
195: } else if (CodeTemplateContextType.OVERRIDECOMMENT_ID
196: .equals(id)) {
197: return 6;
198: } else if (CodeTemplateContextType.DELEGATECOMMENT_ID
199: .equals(id)) {
200: return 7;
201: } else if (CodeTemplateContextType.GETTERCOMMENT_ID
202: .equals(id)) {
203: return 8;
204: } else if (CodeTemplateContextType.SETTERCOMMENT_ID
205: .equals(id)) {
206: return 9;
207: }
208: return 1000;
209: }
210: }
211:
212: private static class CodeTemplateLabelProvider extends
213: LabelProvider {
214:
215: /* (non-Javadoc)
216: * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
217: */
218: public Image getImage(Object element) {
219: return null;
220:
221: }
222:
223: /* (non-Javadoc)
224: * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
225: */
226: public String getText(Object element) {
227: if (element == COMMENT_NODE || element == CODE_NODE) {
228: return (String) element;
229: }
230: TemplatePersistenceData data = (TemplatePersistenceData) element;
231: String id = data.getId();
232: if (CodeTemplateContextType.CATCHBLOCK_ID.equals(id)) {
233: return PreferencesMessages.CodeTemplateBlock_catchblock_label;
234: } else if (CodeTemplateContextType.METHODSTUB_ID.equals(id)) {
235: return PreferencesMessages.CodeTemplateBlock_methodstub_label;
236: } else if (CodeTemplateContextType.CONSTRUCTORSTUB_ID
237: .equals(id)) {
238: return PreferencesMessages.CodeTemplateBlock_constructorstub_label;
239: } else if (CodeTemplateContextType.GETTERSTUB_ID.equals(id)) {
240: return PreferencesMessages.CodeTemplateBlock_getterstub_label;
241: } else if (CodeTemplateContextType.SETTERSTUB_ID.equals(id)) {
242: return PreferencesMessages.CodeTemplateBlock_setterstub_label;
243: } else if (CodeTemplateContextType.NEWTYPE_ID.equals(id)) {
244: return PreferencesMessages.CodeTemplateBlock_newtype_label;
245: } else if (CodeTemplateContextType.CLASSBODY_ID.equals(id)) {
246: return PreferencesMessages.CodeTemplateBlock_classbody_label;
247: } else if (CodeTemplateContextType.INTERFACEBODY_ID
248: .equals(id)) {
249: return PreferencesMessages.CodeTemplateBlock_interfacebody_label;
250: } else if (CodeTemplateContextType.ENUMBODY_ID.equals(id)) {
251: return PreferencesMessages.CodeTemplateBlock_enumbody_label;
252: } else if (CodeTemplateContextType.ANNOTATIONBODY_ID
253: .equals(id)) {
254: return PreferencesMessages.CodeTemplateBlock_annotationbody_label;
255: } else if (CodeTemplateContextType.FILECOMMENT_ID
256: .equals(id)) {
257: return PreferencesMessages.CodeTemplateBlock_filecomment_label;
258: } else if (CodeTemplateContextType.TYPECOMMENT_ID
259: .equals(id)) {
260: return PreferencesMessages.CodeTemplateBlock_typecomment_label;
261: } else if (CodeTemplateContextType.FIELDCOMMENT_ID
262: .equals(id)) {
263: return PreferencesMessages.CodeTemplateBlock_fieldcomment_label;
264: } else if (CodeTemplateContextType.METHODCOMMENT_ID
265: .equals(id)) {
266: return PreferencesMessages.CodeTemplateBlock_methodcomment_label;
267: } else if (CodeTemplateContextType.OVERRIDECOMMENT_ID
268: .equals(id)) {
269: return PreferencesMessages.CodeTemplateBlock_overridecomment_label;
270: } else if (CodeTemplateContextType.DELEGATECOMMENT_ID
271: .equals(id)) {
272: return PreferencesMessages.CodeTemplateBlock_delegatecomment_label;
273: } else if (CodeTemplateContextType.CONSTRUCTORCOMMENT_ID
274: .equals(id)) {
275: return PreferencesMessages.CodeTemplateBlock_constructorcomment_label;
276: } else if (CodeTemplateContextType.GETTERCOMMENT_ID
277: .equals(id)) {
278: return PreferencesMessages.CodeTemplateBlock_gettercomment_label;
279: } else if (CodeTemplateContextType.SETTERCOMMENT_ID
280: .equals(id)) {
281: return PreferencesMessages.CodeTemplateBlock_settercomment_label;
282: }
283: return data.getTemplate().getDescription();
284: }
285: }
286:
287: private static final Key PREF_GENERATE_COMMENTS = getJDTUIKey(PreferenceConstants.CODEGEN_ADD_COMMENTS);
288:
289: private static Key[] getAllKeys() {
290: return new Key[] { PREF_GENERATE_COMMENTS };
291: }
292:
293: private final static int IDX_EDIT = 0;
294: private final static int IDX_IMPORT = 2;
295: private final static int IDX_EXPORT = 3;
296: private final static int IDX_EXPORTALL = 4;
297:
298: protected final static Object COMMENT_NODE = PreferencesMessages.CodeTemplateBlock_templates_comment_node;
299: protected final static Object CODE_NODE = PreferencesMessages.CodeTemplateBlock_templates_code_node;
300:
301: private TreeListDialogField fCodeTemplateTree;
302: private SelectionButtonDialogField fGenerateComments;
303:
304: protected ProjectTemplateStore fTemplateStore;
305:
306: private PixelConverter fPixelConverter;
307: private SourceViewer fPatternViewer;
308:
309: private TemplateVariableProcessor fTemplateProcessor;
310:
311: public CodeTemplateBlock(IStatusChangeListener context,
312: IProject project, IWorkbenchPreferenceContainer container) {
313: super (context, project, getAllKeys(), container);
314:
315: fTemplateStore = new ProjectTemplateStore(project);
316: try {
317: fTemplateStore.load();
318: } catch (IOException e) {
319: JavaPlugin.log(e);
320: }
321:
322: fTemplateProcessor = new TemplateVariableProcessor();
323:
324: CodeTemplateAdapter adapter = new CodeTemplateAdapter();
325:
326: String[] buttonLabels = new String[] {
327: PreferencesMessages.CodeTemplateBlock_templates_edit_button,
328: /* */null,
329: PreferencesMessages.CodeTemplateBlock_templates_import_button,
330: PreferencesMessages.CodeTemplateBlock_templates_export_button,
331: PreferencesMessages.CodeTemplateBlock_templates_exportall_button
332:
333: };
334: fCodeTemplateTree = new TreeListDialogField(adapter,
335: buttonLabels, new CodeTemplateLabelProvider());
336: fCodeTemplateTree.setDialogFieldListener(adapter);
337: fCodeTemplateTree
338: .setLabelText(PreferencesMessages.CodeTemplateBlock_templates_label);
339: fCodeTemplateTree.setViewerComparator(adapter);
340:
341: fCodeTemplateTree.enableButton(IDX_EXPORT, false);
342: fCodeTemplateTree.enableButton(IDX_EDIT, false);
343:
344: fCodeTemplateTree.addElement(COMMENT_NODE);
345: fCodeTemplateTree.addElement(CODE_NODE);
346:
347: fCodeTemplateTree.selectFirstElement();
348:
349: fGenerateComments = new SelectionButtonDialogField(SWT.CHECK
350: | SWT.WRAP);
351: fGenerateComments.setDialogFieldListener(adapter);
352: fGenerateComments
353: .setLabelText(PreferencesMessages.CodeTemplateBlock_createcomment_label);
354:
355: updateControls();
356: }
357:
358: public void postSetSelection(Object element) {
359: fCodeTemplateTree.postSetSelection(new StructuredSelection(
360: element));
361: }
362:
363: public boolean hasProjectSpecificOptions(IProject project) {
364: if (super .hasProjectSpecificOptions(project))
365: return true;
366:
367: if (project != null) {
368: return ProjectTemplateStore
369: .hasProjectSpecificTempates(project);
370: }
371: return false;
372: }
373:
374: protected Control createContents(Composite parent) {
375: fPixelConverter = new PixelConverter(parent);
376:
377: setShell(parent.getShell());
378:
379: Composite composite = new Composite(parent, SWT.NONE);
380: composite.setFont(parent.getFont());
381:
382: GridLayout layout = new GridLayout();
383: layout.marginHeight = 0;
384: layout.marginWidth = 0;
385: layout.numColumns = 2;
386: composite.setLayout(layout);
387:
388: fCodeTemplateTree.doFillIntoGrid(composite, 3);
389: LayoutUtil.setHorizontalSpan(fCodeTemplateTree
390: .getLabelControl(null), 2);
391: LayoutUtil.setHorizontalGrabbing(fCodeTemplateTree
392: .getTreeControl(null));
393:
394: fPatternViewer = createViewer(composite, 2);
395:
396: fGenerateComments.doFillIntoGrid(composite, 2);
397:
398: return composite;
399: }
400:
401: /* (non-Javadoc)
402: * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#updateControls()
403: */
404: protected void updateControls() {
405: fGenerateComments
406: .setSelection(getBooleanValue(PREF_GENERATE_COMMENTS));
407: }
408:
409: private SourceViewer createViewer(Composite parent, int nColumns) {
410: Label label = new Label(parent, SWT.NONE);
411: label.setText(PreferencesMessages.CodeTemplateBlock_preview);
412: GridData data = new GridData();
413: data.horizontalSpan = nColumns;
414: label.setLayoutData(data);
415:
416: IDocument document = new Document();
417: JavaTextTools tools = JavaPlugin.getDefault()
418: .getJavaTextTools();
419: tools.setupJavaDocumentPartitioner(document,
420: IJavaPartitions.JAVA_PARTITIONING);
421: IPreferenceStore store = JavaPlugin.getDefault()
422: .getCombinedPreferenceStore();
423: SourceViewer viewer = new JavaSourceViewer(parent, null, null,
424: false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
425: CodeTemplateSourceViewerConfiguration configuration = new CodeTemplateSourceViewerConfiguration(
426: tools.getColorManager(), store, null,
427: fTemplateProcessor);
428: viewer.configure(configuration);
429: viewer.setEditable(false);
430: viewer.setDocument(document);
431:
432: Font font = JFaceResources
433: .getFont(PreferenceConstants.EDITOR_TEXT_FONT);
434: viewer.getTextWidget().setFont(font);
435: new JavaSourcePreviewerUpdater(viewer, configuration, store);
436:
437: Control control = viewer.getControl();
438: data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
439: | GridData.FILL_VERTICAL);
440: data.horizontalSpan = nColumns;
441: data.heightHint = fPixelConverter
442: .convertHeightInCharsToPixels(5);
443: control.setLayoutData(data);
444:
445: return viewer;
446: }
447:
448: protected TemplatePersistenceData[] getTemplateOfCategory(
449: boolean isComment) {
450: ArrayList res = new ArrayList();
451: TemplatePersistenceData[] templates = fTemplateStore
452: .getTemplateData();
453: for (int i = 0; i < templates.length; i++) {
454: TemplatePersistenceData curr = templates[i];
455: if (isComment == curr.getTemplate().getName().endsWith(
456: CodeTemplateContextType.COMMENT_SUFFIX)) {
457: res.add(curr);
458: }
459: }
460: return (TemplatePersistenceData[]) res
461: .toArray(new TemplatePersistenceData[res.size()]);
462: }
463:
464: protected static boolean canEdit(List selected) {
465: return selected.size() == 1
466: && (selected.get(0) instanceof TemplatePersistenceData);
467: }
468:
469: protected void updateSourceViewerInput(List selection) {
470: if (fPatternViewer == null
471: || fPatternViewer.getTextWidget().isDisposed()) {
472: return;
473: }
474: if (selection.size() == 1
475: && selection.get(0) instanceof TemplatePersistenceData) {
476: TemplatePersistenceData data = (TemplatePersistenceData) selection
477: .get(0);
478: Template template = data.getTemplate();
479: TemplateContextType type = JavaPlugin.getDefault()
480: .getCodeTemplateContextRegistry().getContextType(
481: template.getContextTypeId());
482: fTemplateProcessor.setContextType(type);
483: fPatternViewer.getDocument().set(template.getPattern());
484: } else {
485: fPatternViewer.getDocument().set(""); //$NON-NLS-1$
486: }
487: }
488:
489: protected void doButtonPressed(int buttonIndex, List selected) {
490: if (buttonIndex == IDX_EDIT) {
491: edit((TemplatePersistenceData) selected.get(0));
492: } else if (buttonIndex == IDX_EXPORT) {
493: export(selected);
494: } else if (buttonIndex == IDX_EXPORTALL) {
495: exportAll();
496: } else if (buttonIndex == IDX_IMPORT) {
497: import_();
498: }
499: }
500:
501: private void edit(TemplatePersistenceData data) {
502: Template newTemplate = new Template(data.getTemplate());
503: EditTemplateDialog dialog = new EditTemplateDialog(getShell(),
504: newTemplate, true, false, JavaPlugin.getDefault()
505: .getCodeTemplateContextRegistry());
506: if (dialog.open() == Window.OK) {
507: // changed
508: data.setTemplate(dialog.getTemplate());
509: fCodeTemplateTree.refresh(data);
510: fCodeTemplateTree.selectElements(new StructuredSelection(
511: data));
512: }
513: }
514:
515: private void import_() {
516: FileDialog dialog = new FileDialog(getShell());
517: dialog
518: .setText(PreferencesMessages.CodeTemplateBlock_import_title);
519: dialog
520: .setFilterExtensions(new String[] { PreferencesMessages.CodeTemplateBlock_import_extension });
521: String path = dialog.open();
522:
523: if (path == null)
524: return;
525:
526: try {
527: TemplateReaderWriter reader = new TemplateReaderWriter();
528: File file = new File(path);
529: if (file.exists()) {
530: InputStream input = new BufferedInputStream(
531: new FileInputStream(file));
532: try {
533: TemplatePersistenceData[] datas = reader.read(
534: input, null);
535: for (int i = 0; i < datas.length; i++) {
536: updateTemplate(datas[i]);
537: }
538: } finally {
539: try {
540: input.close();
541: } catch (IOException x) {
542: }
543: }
544: }
545:
546: fCodeTemplateTree.refresh();
547: updateSourceViewerInput(fCodeTemplateTree
548: .getSelectedElements());
549:
550: } catch (FileNotFoundException e) {
551: openReadErrorDialog(e);
552: } catch (IOException e) {
553: openReadErrorDialog(e);
554: }
555:
556: }
557:
558: private void updateTemplate(TemplatePersistenceData data) {
559: TemplatePersistenceData[] datas = fTemplateStore
560: .getTemplateData();
561: for (int i = 0; i < datas.length; i++) {
562: String id = datas[i].getId();
563: if (id != null && id.equals(data.getId())) {
564: datas[i].setTemplate(data.getTemplate());
565: break;
566: }
567: }
568: }
569:
570: private void exportAll() {
571: export(fTemplateStore.getTemplateData());
572: }
573:
574: private void export(List selected) {
575: Set datas = new HashSet();
576: for (int i = 0; i < selected.size(); i++) {
577: Object curr = selected.get(i);
578: if (curr instanceof TemplatePersistenceData) {
579: datas.add(curr);
580: } else {
581: TemplatePersistenceData[] cat = getTemplateOfCategory(curr == COMMENT_NODE);
582: datas.addAll(Arrays.asList(cat));
583: }
584: }
585: export((TemplatePersistenceData[]) datas
586: .toArray(new TemplatePersistenceData[datas.size()]));
587: }
588:
589: private void export(TemplatePersistenceData[] templates) {
590: FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
591: dialog.setText(Messages.format(
592: PreferencesMessages.CodeTemplateBlock_export_title,
593: String.valueOf(templates.length)));
594: dialog
595: .setFilterExtensions(new String[] { PreferencesMessages.CodeTemplateBlock_export_extension });
596: dialog
597: .setFileName(PreferencesMessages.CodeTemplateBlock_export_filename);
598: String path = dialog.open();
599:
600: if (path == null)
601: return;
602:
603: File file = new File(path);
604:
605: if (file.isHidden()) {
606: String title = PreferencesMessages.CodeTemplateBlock_export_error_title;
607: String message = Messages
608: .format(
609: PreferencesMessages.CodeTemplateBlock_export_error_hidden,
610: file.getAbsolutePath());
611: MessageDialog.openError(getShell(), title, message);
612: return;
613: }
614:
615: if (file.exists() && !file.canWrite()) {
616: String title = PreferencesMessages.CodeTemplateBlock_export_error_title;
617: String message = Messages
618: .format(
619: PreferencesMessages.CodeTemplateBlock_export_error_canNotWrite,
620: file.getAbsolutePath());
621: MessageDialog.openError(getShell(), title, message);
622: return;
623: }
624:
625: if (!file.exists() || confirmOverwrite(file)) {
626: OutputStream output = null;
627: try {
628: output = new BufferedOutputStream(new FileOutputStream(
629: file));
630: TemplateReaderWriter writer = new TemplateReaderWriter();
631: writer.save(templates, output);
632: output.close();
633: } catch (IOException e) {
634: if (output != null) {
635: try {
636: output.close();
637: } catch (IOException e2) {
638: // ignore
639: }
640: }
641: openWriteErrorDialog();
642: }
643: }
644:
645: }
646:
647: private boolean confirmOverwrite(File file) {
648: return MessageDialog
649: .openQuestion(
650: getShell(),
651: PreferencesMessages.CodeTemplateBlock_export_exists_title,
652: Messages
653: .format(
654: PreferencesMessages.CodeTemplateBlock_export_exists_message,
655: file.getAbsolutePath()));
656: }
657:
658: public void performDefaults() {
659: super .performDefaults();
660: fTemplateStore.restoreDefaults();
661:
662: // refresh
663: fCodeTemplateTree.refresh();
664: updateSourceViewerInput(fCodeTemplateTree.getSelectedElements());
665: }
666:
667: public boolean performOk(boolean enabled) {
668: boolean res = super .performOk();
669: if (!res)
670: return false;
671:
672: if (fProject != null) {
673: TemplatePersistenceData[] templateData = fTemplateStore
674: .getTemplateData();
675: for (int i = 0; i < templateData.length; i++) {
676: fTemplateStore.setProjectSpecific(templateData[i]
677: .getId(), enabled);
678: }
679: }
680: try {
681: fTemplateStore.save();
682: } catch (IOException e) {
683: JavaPlugin.log(e);
684: openWriteErrorDialog();
685: }
686: return true;
687: }
688:
689: public void performCancel() {
690: try {
691: fTemplateStore.revertChanges();
692: } catch (IOException e) {
693: openReadErrorDialog(e);
694: }
695: }
696:
697: private void openReadErrorDialog(Exception e) {
698: String title = PreferencesMessages.CodeTemplateBlock_error_read_title;
699:
700: String message = e.getLocalizedMessage();
701: if (message != null)
702: message = Messages
703: .format(
704: PreferencesMessages.CodeTemplateBlock_error_parse_message,
705: message);
706: else
707: message = PreferencesMessages.CodeTemplateBlock_error_read_message;
708: MessageDialog.openError(getShell(), title, message);
709: }
710:
711: private void openWriteErrorDialog() {
712: String title = PreferencesMessages.CodeTemplateBlock_error_write_title;
713: String message = PreferencesMessages.CodeTemplateBlock_error_write_message;
714: MessageDialog.openError(getShell(), title, message);
715: }
716:
717: protected String[] getFullBuildDialogStrings(
718: boolean workspaceSettings) {
719: return null;
720: }
721:
722: /* (non-Javadoc)
723: * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#validateSettings(java.lang.String, java.lang.String)
724: */
725: protected void validateSettings(Key changedKey, String oldValue,
726: String newValue) {
727: // no validation here
728: }
729: }
|