Skip to main content
summaryrefslogtreecommitdiffstats
blob: 92f6ee94e7b161fddb6ae2be82c77fc8de35de62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package org.eclipse.e4.ui.preferences;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine;
import org.eclipse.e4.ui.css.swt.theme.ITheme;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.EditorReference;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
import org.eclipse.ui.internal.tweaklets.PreferencePageEnhancer;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.xtext.ui.editor.XtextEditor;

public class CSSEditorPreferences extends PreferencePageEnhancer {

	ITheme selection;
	XtextEditor cssEditor;
	IThemeEngine engine;
	boolean resetCurrentTheme;
	
	@SuppressWarnings("restriction")
	@Override
	public void createContents(Composite parent) {
		resetCurrentTheme = false;
		IWorkbenchWindow wbw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		MWindow hostWin = (MWindow) wbw.getService(MWindow.class);
		EPartService partService = hostWin.getContext().get(EPartService.class);
		EModelService modelService = hostWin.getContext().get(EModelService.class);
		MPart editor = partService.createPart(CompatibilityEditor.MODEL_ELEMENT_ID);
		engine = hostWin.getContext().get(IThemeEngine.class);
	
		IFile file = updateInput();
		
		IEditorInput input = new FileEditorInput(file);

		IWorkbenchPage wbPage = wbw.getActivePage();
		EditorReference reference = ((WorkbenchPage) wbPage).createEditorReferenceForPart(editor,
				input,
				"org.eclipse.e4.CSS", null); //$NON-NLS-1$
		IEclipseContext localContext = hostWin.getContext().createChild();
		localContext.set(IEditorInput.class, input);
		localContext.set(EditorReference.class, reference);

		// Render it
		Composite composite3 = new Composite(parent, SWT.BORDER);
		composite3.setLayout(new FillLayout());
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 2);
		data.widthHint = 500;
		data.heightHint = 500;
		composite3.setLayoutData(data);
		
		modelService.hostElement(editor, hostWin, composite3, localContext);
		partService.activate(editor);
		IEditorPart tmpEditor = editor.getContext().get(IEditorPart.class);
		if (tmpEditor instanceof XtextEditor) {
			cssEditor = (XtextEditor) tmpEditor;
		}
	}

	@Override
	public void setSelection(Object sel) {
		if (sel instanceof ITheme) {
			ITheme newTheme = (ITheme) sel;
			ITheme oldSelection  = selection;
			selection = newTheme;
			if (oldSelection != null && !newTheme.getId().equals(oldSelection.getId())) {
					IFile file = updateInput();
					IEditorInput input = new FileEditorInput(file);
					cssEditor.setInput(input);
			}
		}
		
	}

	@Override
	public void performOK() {
		if (cssEditor.isDirty()) {
			// make a copy of file
			IDocumentProvider docProvider = cssEditor.getDocumentProvider();
			
			IEditorInput editorInput = cssEditor.getEditorInput();
			IDocument doc = docProvider.getDocument(editorInput);
			String more = doc.get();
			//check for .e4css folder
			String e4CSSPath = System.getProperty("user.home") + System.getProperty("file.separator") + ".e4css";
			File e4CSS = new File(e4CSSPath);
			if (!e4CSS.exists()) {
				File userHome = e4CSS.getParentFile( );
				if ( userHome.exists() && userHome.canWrite() )
				{
					e4CSS.mkdir();
				}
			}
			IPath path = new Path(e4CSSPath + System.getProperty("file.separator") + editorInput.getName()); //$NON-NLS-1$
			
			byte[] bytes = more.getBytes();
			FileOutputStream outputStream = null;
			try {
				outputStream = new FileOutputStream(path.toOSString());
				outputStream.write(bytes, 0, bytes.length);
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			} finally {
				if (outputStream != null)
					try {
						outputStream.close();
					} catch (IOException e) {
					} 
			}

			if (engine instanceof ThemeEngine) {
				ArrayList<String> styleSheets = new ArrayList<String>();
				try {
					URL styleSheetURL = FileLocator.toFileURL(path.toFile().toURI().toURL());
					styleSheets.add(styleSheetURL.toString());
					((ThemeEngine) engine).themeModified(selection, styleSheets );
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			cssEditor.doRevertToSaved();
		}
		
		if (resetCurrentTheme) {
			((ThemeEngine) engine).resetCurrentTheme();
			resetCurrentTheme = false;
		}
	}

	
	IFile updateInput() {

		IProject newProject = ResourcesPlugin.getWorkspace().getRoot()
				.getProject(".e4css"); //$NON-NLS-1$
		URL styleSheetURL = null;

		if (engine instanceof ThemeEngine) {
			List<String> ss = ((ThemeEngine) engine).getStylesheets(selection);
			List<String> mod = ((ThemeEngine) engine).getModifiedStylesheets(selection);
			if (mod.size() > 0) {
				ss = mod;
			}
			if (ss.size() > 0) {
				// For now just get the first element
				String path = ss.get(0);
				try {
					styleSheetURL = FileLocator.toFileURL(new URL(path));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		IProjectDescription desc = newProject.getWorkspace()
				.newProjectDescription(newProject.getName());
		IFile file = null;
		try {
			if (!newProject.exists())
				newProject.create(desc, null);
			if (!newProject.isOpen()) {
				newProject.open(null);
			}
			newProject.setHidden(true);
			// currentTheme.
			IPath location = new Path(styleSheetURL.getPath());
			// IPath location = new Path(styleSheetURL.getPath());
			file = newProject.getFile(location.lastSegment());
			file.delete(true, null);
			if (!file.exists())
				file.createLink(location, IResource.NONE, null);
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return file;

	}

	@Override
	public void performCancel() {
		if (cssEditor.isDirty()) cssEditor.doRevertToSaved();
	}

	@Override
	public void performDefaults() {
		List<String> mod = ((ThemeEngine) engine).getModifiedStylesheets(selection);
		if (mod.size() > 0) {
		
			// For now just get the first element
			String path = mod.get(0);
			try {
				
				URL styleSheetURL = FileLocator.toFileURL(new URL(path));
				File file = new File(styleSheetURL.getFile());
				if (file.exists()) file.delete();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (engine instanceof ThemeEngine) ((ThemeEngine) engine).resetModifiedStylesheets(selection);
		IFile file = updateInput();
		IEditorInput input = new FileEditorInput(file);
		cssEditor.setInput(input);
		resetCurrentTheme = true;
	}

}

Back to the top