1 | package de.ugoe.cs.swe.bnftools.ebnf.standalone; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | |
---|
6 | import org.apache.commons.io.FileUtils; |
---|
7 | import org.eclipse.emf.common.util.URI; |
---|
8 | import org.eclipse.emf.ecore.resource.Resource; |
---|
9 | import org.eclipse.emf.ecore.resource.ResourceSet; |
---|
10 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
---|
11 | import org.eclipse.jface.text.IDocument; |
---|
12 | import org.eclipse.jface.text.ITextInputListener; |
---|
13 | import org.eclipse.jface.viewers.IStructuredContentProvider; |
---|
14 | import org.eclipse.jface.viewers.ITableLabelProvider; |
---|
15 | import org.eclipse.jface.viewers.LabelProvider; |
---|
16 | import org.eclipse.jface.viewers.TableViewer; |
---|
17 | import org.eclipse.jface.viewers.Viewer; |
---|
18 | import org.eclipse.swt.SWT; |
---|
19 | import org.eclipse.swt.custom.SashForm; |
---|
20 | import org.eclipse.swt.graphics.Image; |
---|
21 | import org.eclipse.swt.layout.FillLayout; |
---|
22 | import org.eclipse.swt.widgets.Composite; |
---|
23 | import org.eclipse.ui.ISharedImages; |
---|
24 | import org.eclipse.ui.PlatformUI; |
---|
25 | import org.eclipse.ui.part.ViewPart; |
---|
26 | import org.eclipse.xtext.resource.XtextResource; |
---|
27 | import org.eclipse.xtext.ui.editor.XtextSourceViewer; |
---|
28 | import org.eclipse.xtext.ui.editor.embedded.EmbeddedEditor; |
---|
29 | import org.eclipse.xtext.ui.editor.embedded.EmbeddedEditorFactory; |
---|
30 | import org.eclipse.xtext.ui.editor.embedded.EmbeddedEditorModelAccess; |
---|
31 | import org.eclipse.xtext.ui.editor.embedded.IEditedResourceProvider; |
---|
32 | import org.eclipse.xtext.ui.editor.model.XtextDocument; |
---|
33 | |
---|
34 | import com.google.inject.Inject; |
---|
35 | import com.google.inject.Injector; |
---|
36 | |
---|
37 | import de.ugoe.cs.swe.bnftools.EbnfStandaloneSetup; |
---|
38 | import de.ugoe.cs.swe.bnftools.ui.internal.EbnfActivator; |
---|
39 | |
---|
40 | @SuppressWarnings("restriction") |
---|
41 | public class View extends ViewPart { |
---|
42 | private static final String FILE = "/Users/philip-iii/Dev/workspaces/bnf-v2/de.ugoe.cs.swe.bnftools.ebnf.standalone/somefile.bnf"; |
---|
43 | |
---|
44 | public static final String ID = "de.ugoe.cs.swe.bnftools.ebnf.standalone.view"; |
---|
45 | |
---|
46 | //NEW |
---|
47 | @Inject EBNFEditedResourceProvider editedResourceProvider; |
---|
48 | @Inject EmbeddedEditorFactory factory; |
---|
49 | |
---|
50 | |
---|
51 | private TableViewer viewer; |
---|
52 | private EmbeddedEditor handle; |
---|
53 | private SashForm sashForm; |
---|
54 | private EmbeddedEditorModelAccess partialEditor; |
---|
55 | private XtextSourceViewer viewer2; |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | * The content provider class is responsible for providing objects to the |
---|
60 | * view. It can wrap existing objects in adapters or simply return objects |
---|
61 | * as-is. These objects may be sensitive to the current input of the view, |
---|
62 | * or ignore it and always show the same content (like Task List, for |
---|
63 | * example). |
---|
64 | */ |
---|
65 | class ViewContentProvider implements IStructuredContentProvider { |
---|
66 | public void inputChanged(Viewer v, Object oldInput, Object newInput) { |
---|
67 | } |
---|
68 | |
---|
69 | public void dispose() { |
---|
70 | } |
---|
71 | |
---|
72 | public Object[] getElements(Object parent) { |
---|
73 | if (parent instanceof Object[]) { |
---|
74 | return (Object[]) parent; |
---|
75 | } |
---|
76 | return new Object[0]; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | //NEW |
---|
81 | private void createEditor() { |
---|
82 | IEditedResourceProvider resourceProvider = new IEditedResourceProvider() { |
---|
83 | |
---|
84 | @Override |
---|
85 | public XtextResource createResource() { |
---|
86 | try { |
---|
87 | EbnfStandaloneSetup.doSetup(); |
---|
88 | ResourceSet resourceSet = new ResourceSetImpl(); |
---|
89 | Resource resource = resourceSet.createResource(URI.createURI(FILE)); |
---|
90 | return (XtextResource) resource; |
---|
91 | } catch (Exception e) { |
---|
92 | return null; |
---|
93 | } |
---|
94 | } |
---|
95 | }; |
---|
96 | |
---|
97 | //or change to current activator... |
---|
98 | EbnfActivator activator = EbnfActivator.getInstance(); |
---|
99 | Injector injector = activator |
---|
100 | .getInjector(EbnfActivator.DE_UGOE_CS_SWE_BNFTOOLS_EBNF); |
---|
101 | EmbeddedEditorFactory factory = injector.getInstance(EmbeddedEditorFactory.class); |
---|
102 | handle = factory.newEditor(resourceProvider).withParent( |
---|
103 | sashForm); |
---|
104 | |
---|
105 | |
---|
106 | // keep the partialEditor as instance var to read / write the edited text |
---|
107 | partialEditor = handle.createPartialEditor(true); |
---|
108 | viewer2 = handle.getViewer(); |
---|
109 | final File f = new File(FILE); |
---|
110 | try { |
---|
111 | String string = FileUtils.readFileToString(f); |
---|
112 | viewer2.getDocument().set(string); |
---|
113 | } catch (IOException e1) { |
---|
114 | e1.printStackTrace(); |
---|
115 | } |
---|
116 | viewer2.addTextInputListener(new ITextInputListener() { |
---|
117 | |
---|
118 | @Override |
---|
119 | public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { |
---|
120 | System.out.println("-->save"); |
---|
121 | XtextDocument d = (XtextDocument) oldInput; |
---|
122 | System.out.println(oldInput.get()); |
---|
123 | System.out.println(f.getAbsolutePath()); |
---|
124 | try { |
---|
125 | FileUtils.writeStringToFile(f, oldInput.get()); |
---|
126 | } catch (IOException e) { |
---|
127 | e.printStackTrace(); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | @Override |
---|
132 | public void inputDocumentAboutToBeChanged(IDocument oldInput, |
---|
133 | IDocument newInput) { |
---|
134 | } |
---|
135 | }); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | class ViewLabelProvider extends LabelProvider implements |
---|
140 | ITableLabelProvider { |
---|
141 | public String getColumnText(Object obj, int index) { |
---|
142 | return getText(obj); |
---|
143 | } |
---|
144 | |
---|
145 | public Image getColumnImage(Object obj, int index) { |
---|
146 | return getImage(obj); |
---|
147 | } |
---|
148 | |
---|
149 | public Image getImage(Object obj) { |
---|
150 | return PlatformUI.getWorkbench().getSharedImages().getImage( |
---|
151 | ISharedImages.IMG_OBJ_ELEMENT); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * This is a callback that will allow us to create the viewer and initialize |
---|
157 | * it. |
---|
158 | */ |
---|
159 | public void createPartControl(Composite parent) { |
---|
160 | sashForm = new SashForm(parent, SWT.HORIZONTAL); |
---|
161 | sashForm.setLayout(new FillLayout()); |
---|
162 | createEditor(); |
---|
163 | |
---|
164 | viewer = new TableViewer(sashForm, SWT.MULTI | SWT.H_SCROLL |
---|
165 | | SWT.V_SCROLL); |
---|
166 | viewer.setContentProvider(new ViewContentProvider()); |
---|
167 | viewer.setLabelProvider(new ViewLabelProvider()); |
---|
168 | // Provide the input to the ContentProvider |
---|
169 | viewer.setInput(new String[] {"One", "Two", "Three"}); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Passing the focus request to the viewer's control. |
---|
174 | */ |
---|
175 | public void setFocus() { |
---|
176 | viewer.getControl().setFocus(); |
---|
177 | } |
---|
178 | } |
---|