1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.test.assistant.views;
25 
26 import static org.openoffice.test.vcl.widgets.VclControl.*;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.resources.IProject;
33 import org.eclipse.core.resources.IWorkspace;
34 import org.eclipse.core.resources.IWorkspaceRoot;
35 import org.eclipse.core.resources.ResourcesPlugin;
36 import org.eclipse.core.runtime.CoreException;
37 import org.eclipse.jdt.core.ICompilationUnit;
38 import org.eclipse.jdt.core.IJavaProject;
39 import org.eclipse.jdt.core.IPackageFragment;
40 import org.eclipse.jdt.core.IPackageFragmentRoot;
41 import org.eclipse.jdt.core.JavaCore;
42 import org.eclipse.jdt.core.JavaModelException;
43 import org.eclipse.jdt.core.WorkingCopyOwner;
44 import org.eclipse.jdt.core.dom.AST;
45 import org.eclipse.jdt.core.dom.ASTParser;
46 import org.eclipse.jdt.core.dom.CompilationUnit;
47 import org.eclipse.jdt.core.dom.FieldDeclaration;
48 import org.eclipse.jdt.core.dom.MethodInvocation;
49 import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
50 import org.eclipse.jdt.core.dom.StringLiteral;
51 import org.eclipse.jdt.core.dom.TypeDeclaration;
52 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
53 import org.eclipse.jface.text.Document;
54 import org.eclipse.text.edits.TextEdit;
55 import org.openoffice.test.vcl.IDList;
56 import org.openoffice.test.vcl.client.SmartId;
57 @SuppressWarnings({ "unchecked", "rawtypes" })
58 public class UIMapOp {
59 
60 	private ICompilationUnit uiMap = null;
61 
62 	private IDList idList = null;
63 
64 	public ArrayList<String> names = new ArrayList<String>();
65 
66 	public ArrayList<String> ids = new ArrayList<String>();
67 
UIMapOp()68 	public UIMapOp() {
69 
70 	}
71 
locateUIMap()72 	private boolean locateUIMap() {
73 		uiMap = null;
74 
75 		IWorkspace workspace = ResourcesPlugin.getWorkspace();
76 		IWorkspaceRoot root = workspace.getRoot();
77 		IProject[] projects = root.getProjects();
78 		for (IProject project : projects) {
79 			try {
80 				if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
81 					IJavaProject javaProject = JavaCore.create(project);
82 					uiMap = findUIMap(javaProject);
83 					if (uiMap != null) {
84 						return true;
85 					}
86 				}
87 			} catch (Exception e) {
88 
89 			}
90 		}
91 
92 		return false;
93 	}
94 
findUIMap(IJavaProject javaProject)95 	private ICompilationUnit findUIMap(IJavaProject javaProject) throws JavaModelException {
96 		IPackageFragment[] packages = javaProject.getPackageFragments();
97 		for (IPackageFragment mypackage : packages) {
98 			if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
99 				ICompilationUnit ret = findUIMap(mypackage);
100 				if (ret != null) {
101 					return ret;
102 				}
103 			}
104 		}
105 
106 		return null;
107 	}
108 
findUIMap(IPackageFragment mypackage)109 	private ICompilationUnit findUIMap(IPackageFragment mypackage) throws JavaModelException {
110 		for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
111 			if ("UIMap.java".equals(unit.getElementName())) {
112 				return unit;
113 			}
114 		}
115 
116 		return null;
117 	}
118 
parse(ICompilationUnit unit)119 	private static CompilationUnit parse(ICompilationUnit unit) {
120 		ASTParser parser = ASTParser.newParser(AST.JLS3);
121 		parser.setKind(ASTParser.K_COMPILATION_UNIT);
122 		parser.setSource(unit);
123 		parser.setResolveBindings(true);
124 		return (CompilationUnit) parser.createAST(null); // parse
125 	}
126 
scan()127 	public boolean scan() {
128 		if (!locateUIMap())
129 			return false;
130 
131 		IProject project = uiMap.getResource().getProject();
132 		try {
133 			uiMap.getResource().refreshLocal(1, null);
134 		} catch (CoreException e) {
135 			// TODO Auto-generated catch block
136 			e.printStackTrace();
137 		}
138 		IFile file = project.getFile("ids");
139 		idList = new IDList(file.getLocation().toFile());
140 		names.clear();
141 		ids.clear();
142 		CompilationUnit compilationUnit = parse(uiMap);
143 		TypeDeclaration typeDeclaration = (TypeDeclaration) compilationUnit.types().get(0);
144 		FieldDeclaration[] fields = typeDeclaration.getFields();
145 		for (FieldDeclaration field : fields) {
146 			List fragments = field.fragments();
147 			for (Object o : fragments) {
148 				VariableDeclarationFragment fragment = (VariableDeclarationFragment) o;
149 				System.out.println(fragment);
150 				names.add(fragment.getName().toString());
151 
152 				String code = fragment.toString();
153 				int left = code.indexOf('(');
154 				if (left >= 0) {
155 					int right = code.indexOf(')', left);
156 					if (right >= 0) {
157 						String idInDeclaration = code.substring(left + 1, right);
158 						if (idInDeclaration.startsWith("\"")) {
159 							// It's string....
160 							idInDeclaration = idInDeclaration.substring(1, idInDeclaration.length() - 1);
161 							ids.add(idInDeclaration);
162 							continue;
163 						}
164 					}
165 				}
166 
167 				ids.add(null);
168 
169 			}
170 		}
171 
172 		return true;
173 	}
174 
175 	/**
176 	 * Return the control name defined in UIMap with the given id
177 	 *
178 	 * @param id
179 	 * @return
180 	 */
populateName(ArrayList<ControlInfo> controlInfos)181 	public void populateName(ArrayList<ControlInfo> controlInfos) {
182 		for (int i = names.size() - 1; i >= 0; i--) {
183 			String name = names.get(i);
184 			String id = ids.get(i);
185 			if (id == null)
186 				continue;
187 
188 			SmartId smartId = idList.getId(id);
189 			for (ControlInfo ci : controlInfos) {
190 				if (ci.id.equals(smartId)) {
191 					ci.name = name;
192 					break;
193 				}
194 			}
195 		}
196 	}
197 
198 	/**
199 	 *
200 	 */
codeParts(ControlInfo info)201 	private String[] codeParts(ControlInfo info) {
202 		String method = null;
203 		String type = null;
204 		switch ((int) info.type) {
205 		case WINDOW_BUTTON:
206 		case WINDOW_PUSHBUTTON:
207 		case WINDOW_IMAGEBUTTON:
208 		case WINDOW_MENUBUTTON:
209 		case WINDOW_MOREBUTTON:
210 		case WINDOW_RADIOBUTTON:
211 		case WINDOW_IMAGERADIOBUTTON:
212 		case WINDOW_CHECKBOX:
213 		case WINDOW_TRISTATEBOX:
214 			method = "button";
215 			type = "VclButton";
216 			break;
217 		case WINDOW_EDIT:
218 		case WINDOW_MULTILINEEDIT:
219 		case WINDOW_PATTERNBOX:
220 		case WINDOW_NUMERICBOX:
221 		case WINDOW_METRICBOX:
222 		case WINDOW_CURRENCYBOX:
223 		case WINDOW_DATEBOX:
224 		case WINDOW_TIMEBOX:
225 		case WINDOW_LONGCURRENCYBOX:
226 			method = "editbox";
227 			type = "VclEditBox";
228 			break;
229 		case WINDOW_COMBOBOX:
230 			method = "combobox";
231 			type = "VclComboBox";
232 			break;
233 		case WINDOW_LISTBOX:
234 		case WINDOW_MULTILISTBOX:
235 		case WINDOW_VALUESETLISTBOX:
236 			method = "listbox";
237 			type = "VclListBox";
238 			break;
239 		case WINDOW_TABPAGE:
240 			method = "tabpage";
241 			type = "VclTabPage";
242 			break;
243 		case WINDOW_TOOLBOX:
244 			method = "toolbox";
245 			type = "VclToolBox";
246 			break;
247 
248 		case WINDOW_TABCONTROL:
249 			method = "tabcontrol";
250 			type = "VclTabControl";
251 			break;
252 
253 		case WINDOW_WINDOW:
254 		case WINDOW_BORDERWINDOW:
255 		case WINDOW_SYSTEMCHILDWINDOW:
256 			method = "window";
257 			type = "VclWindow";
258 			break;
259 		case WINDOW_DIALOG:
260 		case WINDOW_MODELESSDIALOG:
261 		case WINDOW_MODALDIALOG:
262 		case WINDOW_SYSTEMDIALOG :
263 		case WINDOW_PATHDIALOG :
264 		case WINDOW_FILEDIALOG :
265 		case WINDOW_PRINTERSETUPDIALOG :
266 		case WINDOW_PRINTDIALOG :
267 		case WINDOW_COLORDIALOG :
268 		case WINDOW_FONTDIALOG:
269 			method = "dialog";
270 			type = "VclDialog";
271 			break;
272 		case WINDOW_DOCKINGWINDOW:
273 			method = "dockingwin";
274 			type = "VclDocingWin";
275 			break;
276 		case WINDOW_SPINFIELD:
277 		case WINDOW_PATTERNFIELD:
278 		case WINDOW_NUMERICFIELD:
279 		case WINDOW_METRICFIELD:
280 		case WINDOW_CURRENCYFIELD:
281 		case WINDOW_DATEFIELD:
282 		case WINDOW_TIMEFIELD:
283 		case WINDOW_LONGCURRENCYFIELD:
284 			method = "field";
285 			type = "VclField";
286 			break;
287 		default:
288 			method = "control";
289 			type = "VclControl";
290 		}
291 		return new String[] { type, method };
292 	}
293 
define(ControlInfo info)294 	public void define(ControlInfo info) throws Exception {
295 		String[] codeParts = codeParts(info);
296 		String type = codeParts[0];
297 		String method = codeParts[1];
298 
299 		String source = uiMap.getSource();
300 		Document document = new Document(source);
301 
302 		// creation of DOM/AST from a ICompilationUnit
303 		ASTParser parser = ASTParser.newParser(AST.JLS3);
304 		parser.setSource(uiMap);
305 
306 		CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
307 		AST ast = astRoot.getAST();
308 		astRoot.recordModifications();
309 		VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
310 
311 		StringLiteral literal = ast.newStringLiteral();
312 		literal.setLiteralValue(info.id.toString());
313 
314 		MethodInvocation methodInvocation = ast.newMethodInvocation();
315 		methodInvocation.setName(ast.newSimpleName(method));
316 		methodInvocation.arguments().add(literal);
317 
318 		vdf.setInitializer(methodInvocation);
319 		vdf.setName(ast.newSimpleName(info.name));
320 
321 		FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(vdf);
322 		fieldDeclaration.setType(ast.newSimpleType(ast.newSimpleName(type)));
323 
324 		fieldDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
325 		fieldDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
326 		fieldDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
327 
328 		TypeDeclaration typeDeclaration = (TypeDeclaration) astRoot.types().get(0);
329 
330 		typeDeclaration.bodyDeclarations().add(fieldDeclaration);
331 
332 		TextEdit edits = astRoot.rewrite(document, uiMap.getJavaProject().getOptions(true));
333 
334 		edits.apply(document);
335 		String newSource = document.get();
336 		WorkingCopyOwner owner = new WorkingCopyOwner() {};
337 
338 		// Create working copy
339 		ICompilationUnit workingCopy = uiMap.getWorkingCopy(owner, null);
340 		workingCopy.getBuffer().setContents(newSource);
341 		workingCopy.reconcile(ICompilationUnit.NO_AST, false, null, null);
342 		// Commit changes
343 		workingCopy.commitWorkingCopy(true, null);
344 		// Destroy working copy
345 		workingCopy.discardWorkingCopy();
346 		// return "public static final " + type + " " + info.name + " = " +
347 		// method + "(\"" + info.id + "\")";
348 	}
349 }
350