KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > SnippetPreview


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.preferences.formatter;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21
22 import org.eclipse.swt.widgets.Composite;
23
24 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
25
26 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
27 import org.eclipse.jdt.internal.ui.JavaPlugin;
28
29
30
31 public class SnippetPreview extends JavaPreview {
32   
33     public final static class PreviewSnippet {
34         
35         public String JavaDoc header;
36         public final String JavaDoc source;
37         public final int kind;
38         
39         public PreviewSnippet(int kind, String JavaDoc source) {
40             this.kind= kind;
41             this.source= source;
42         }
43     }
44     
45     private ArrayList JavaDoc fSnippets;
46
47     public SnippetPreview(Map JavaDoc workingValues, Composite parent) {
48         super(workingValues, parent);
49         fSnippets= new ArrayList JavaDoc();
50     }
51
52     protected void doFormatPreview() {
53         if (fSnippets.isEmpty()) {
54             fPreviewDocument.set(""); //$NON-NLS-1$
55
return;
56         }
57         
58         //This delimiter looks best for invisible characters
59
final String JavaDoc delimiter= "\n"; //$NON-NLS-1$
60

61         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
62         for (final Iterator JavaDoc iter= fSnippets.iterator(); iter.hasNext();) {
63             final PreviewSnippet snippet= (PreviewSnippet) iter.next();
64             String JavaDoc formattedSource;
65             try {
66                 formattedSource= CodeFormatterUtil.format(snippet.kind, snippet.source, 0, null, delimiter, fWorkingValues);
67             } catch (Exception JavaDoc e) {
68                 final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR,
69                     FormatterMessages.JavaPreview_formatter_exception, e);
70                 JavaPlugin.log(status);
71                 continue;
72             }
73             buffer.append(delimiter);
74             buffer.append(formattedSource);
75             buffer.append(delimiter);
76             buffer.append(delimiter);
77         }
78         fPreviewDocument.set(buffer.toString());
79     }
80     
81     
82     
83     public void add(PreviewSnippet snippet) {
84         fSnippets.add(snippet);
85     }
86     
87     public void remove(PreviewSnippet snippet) {
88         fSnippets.remove(snippet);
89     }
90     
91     public void addAll(Collection JavaDoc snippets) {
92         fSnippets.addAll(snippets);
93     }
94     
95     public void clear() {
96         fSnippets.clear();
97     }
98
99 }
100
Popular Tags