Package: VdmBuildPathPropertyPage$1$1SrcContentProvider

VdmBuildPathPropertyPage$1$1SrcContentProvider

nameinstructionbranchcomplexitylinemethod
getChildren(Object)
M: 39 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getElements(Object)
M: 39 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
hasChildren(Object)
M: 12 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.property;
23:
24: import java.util.List;
25: import java.util.Vector;
26:
27: import org.eclipse.core.resources.IContainer;
28: import org.eclipse.core.resources.IFolder;
29: import org.eclipse.core.resources.IProject;
30: import org.eclipse.core.runtime.CoreException;
31: import org.eclipse.jface.viewers.ISelection;
32: import org.eclipse.jface.viewers.IStructuredSelection;
33: import org.eclipse.jface.viewers.ITreeContentProvider;
34: import org.eclipse.jface.viewers.ITreeSelection;
35: import org.eclipse.jface.viewers.TreeViewer;
36: import org.eclipse.jface.viewers.Viewer;
37: import org.eclipse.jface.viewers.ViewerComparator;
38: import org.eclipse.jface.window.Window;
39: import org.eclipse.swt.SWT;
40: import org.eclipse.swt.events.SelectionAdapter;
41: import org.eclipse.swt.events.SelectionEvent;
42: import org.eclipse.swt.graphics.Image;
43: import org.eclipse.swt.layout.FillLayout;
44: import org.eclipse.swt.layout.GridData;
45: import org.eclipse.swt.layout.GridLayout;
46: import org.eclipse.swt.widgets.Button;
47: import org.eclipse.swt.widgets.Composite;
48: import org.eclipse.swt.widgets.Control;
49: import org.eclipse.ui.IWorkbenchPropertyPage;
50: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
51: import org.eclipse.ui.dialogs.PropertyPage;
52: import org.eclipse.ui.internal.WorkbenchPlugin;
53: import org.eclipse.ui.model.BaseWorkbenchContentProvider;
54: import org.eclipse.ui.model.WorkbenchLabelProvider;
55: import org.overture.ide.core.resources.IVdmProject;
56: import org.overture.ide.core.resources.ModelBuildPath;
57: import org.overture.ide.ui.VdmUIPlugin;
58: import org.overture.ide.ui.utility.VdmTypeCheckerUi;
59:
60: @SuppressWarnings("restriction")
61: public class VdmBuildPathPropertyPage extends PropertyPage implements
62:                 IWorkbenchPropertyPage
63: {
64:         private static class ModelPathContentProvider implements
65:                         ITreeContentProvider
66:         {
67:
68:                 public Object[] getChildren(Object parentElement)
69:                 {
70:                         return null;
71:                 }
72:
73:                 public Object getParent(Object element)
74:                 {
75:                         return null;
76:                 }
77:
78:                 public boolean hasChildren(Object element)
79:                 {
80:                         return false;
81:                 }
82:
83:                 @SuppressWarnings("rawtypes")
84:                 public Object[] getElements(Object inputElement)
85:                 {
86:                         if(inputElement instanceof ModelBuildPath)
87:                         {
88:                                 return ((ModelBuildPath)inputElement).getModelSrcPaths().toArray();
89:                         }
90:                         if (inputElement instanceof List)
91:                         {
92:                                 return ((List) inputElement).toArray();
93:                         }
94:                         return new Object[] {};
95:                 }
96:
97:                 public void dispose()
98:                 {
99:
100:                 }
101:
102:                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
103:                 {
104:
105:                 }
106:
107:         }
108:
109:         ModelBuildPath modelpath = null;
110:         TreeViewer tree = null;
111: //        List<IContainer> data = null;
112:
113:         @Override
114:         protected Control createContents(Composite parent)
115:         {
116:                 Composite myComposite = new Composite(parent, SWT.NONE);
117:                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
118:                 myComposite.setLayoutData(gd);
119:
120:                 GridLayout layout = new GridLayout();
121:                 layout.makeColumnsEqualWidth = false;
122:                 layout.numColumns = 2;
123:                 myComposite.setLayout(layout);
124:
125:                 // FillLayout layout = new FillLayout();
126:                 //
127:                 // layout.type = SWT.VERTICAL;
128:                 // myComposite.setLayout(layout);
129:
130:                 Composite myCompositeTree = new Composite(myComposite, SWT.NONE);
131:                 FillLayout layout1 = new FillLayout();
132:
133:                 layout1.type = SWT.VERTICAL;
134:                 myCompositeTree.setLayout(layout1);
135:                 tree = new TreeViewer(myCompositeTree, SWT.BORDER);
136:                 gd = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
137:                 myCompositeTree.setLayoutData(gd);
138:
139:                 IProject project = getSelectedProject();
140: //                modelpath = new ModelBuildPath((IVdmProject) project.getAdapter(IVdmProject.class));
141: //                data = modelpath.getModelSrcPaths();
142:                 modelpath =((IVdmProject) project.getAdapter(IVdmProject.class)).getModelBuildPath();
143:                 tree.setLabelProvider(new WorkbenchLabelProvider());
144:                 tree.setContentProvider(new ModelPathContentProvider());
145:                 tree.setInput(modelpath);
146:
147:                 Button addFolderButtom = createPushButton(myComposite, "Add...", null);
148:                 gd = new GridData(GridData.FILL_HORIZONTAL
149:                                 | GridData.VERTICAL_ALIGN_BEGINNING);
150:                 addFolderButtom.setLayoutData(gd);
151:                 addFolderButtom.addSelectionListener(new SelectionAdapter()
152:                 {
153:                         public void widgetSelected(SelectionEvent e)
154:                         {
155:                                 class SrcContentProvider extends BaseWorkbenchContentProvider
156:                                 {
157:                                         @Override
158:                                         public boolean hasChildren(Object element)
159:                                         {
160:•                                                if (element instanceof IProject
161:•                                                                || element instanceof IFolder)
162:                                                 {
163:                                                         return super.hasChildren(element);
164:                                                 }
165:                                                 return false;
166:                                         }
167:
168:                                         @Override
169:                                         public Object[] getElements(Object element)
170:                                         {
171:                                                 List<IFolder> elements = new Vector<IFolder>();
172:                                                 Object[] arr = super.getElements(element);
173:•                                                if (arr != null)
174:                                                 {
175:•                                                        for (Object object : arr)
176:                                                         {
177:                                                                 if (// object instanceof IFile ||
178:•                                                                object instanceof IFolder)
179:                                                                 {
180:                                                                         elements.add((IFolder) object);
181:                                                                 }
182:                                                         }
183:                                                         return elements.toArray();
184:                                                 }
185:                                                 return null;
186:                                         }
187:
188:                                         @Override
189:                                         public Object[] getChildren(Object element)
190:                                         {
191:                                                 List<IFolder> elements = new Vector<IFolder>();
192:                                                 Object[] arr = super.getChildren(element);
193:•                                                if (arr != null)
194:                                                 {
195:•                                                        for (Object object : arr)
196:                                                         {
197:                                                                 if (// object instanceof IFile ||
198:•                                                                object instanceof IFolder)
199:                                                                 {
200:                                                                         elements.add((IFolder) object);
201:                                                                 }
202:                                                         }
203:                                                         return elements.toArray();
204:                                                 }
205:                                                 return null;
206:                                         }
207:
208:                                 }
209:                                 
210:                                 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new WorkbenchLabelProvider(), new SrcContentProvider());
211:                                 dialog.setTitle("Select source folder");
212:                                 dialog.setMessage("Select a source folder:");
213:                                 dialog.setComparator(new ViewerComparator());
214:                                 dialog.setInput(getSelectedProject());
215:
216:                                 if (dialog.open() == Window.OK)
217:                                 {
218:                                         if (dialog.getFirstResult() != null
219:                                         // && dialog.getFirstResult() instanceof IProject
220:                                         // && ((IProject) dialog.getFirstResult()).getAdapter(IVdmProject.class) != null)
221:                                         )
222:                                         {
223:                                                 Object o = dialog.getFirstResult();
224:                                                 if (o instanceof IContainer)
225:                                                 {
226:                                                         IContainer source = (IContainer) o;
227: //                                                        if (!data.contains(source))
228: //                                                        {
229:                                                         modelpath.add(source);
230: //                                                                data.add(source);
231:                                                                 tree.refresh();
232: //                                                        }
233:                                                 }
234:                                                 // fScenarioText.setText(((IFile) dialog.getFirstResult()).getProjectRelativePath().toString());
235:                                         }
236:
237:                                 }
238:
239:                         }
240:
241:                 });
242:                 addFolderButtom.setEnabled(false);
243:
244:                 return myComposite;
245:         }
246:
247:         @SuppressWarnings( { "deprecation" })
248:         public static IProject getSelectedProject()
249:         {
250:                 ISelection selectedItem = WorkbenchPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
251:                 IProject selectedProject = null;
252:
253:                 if (selectedItem instanceof ITreeSelection)
254:                 {
255:                         ITreeSelection selection = (ITreeSelection) selectedItem;
256:                         if (selection.getPaths().length > 0)
257:                         {
258:                                 Object project = selection.getPaths()[0].getFirstSegment();
259:                                 if (project instanceof IProject)
260:                                         selectedProject = (IProject) project;
261:
262:                         }
263:                 } else if (selectedItem instanceof IStructuredSelection)
264:                 {
265:                         IStructuredSelection selection = (IStructuredSelection) selectedItem;
266:                         if (selection.getFirstElement() instanceof IProject)
267:                                 selectedProject = (IProject) selection.getFirstElement();
268:                 }
269:
270:                 return selectedProject;
271:         }
272:
273:         /**
274:          * Creates and returns a new push button with the given label and/or image.
275:          *
276:          * @param parent
277:          * parent control
278:          * @param label
279:          * button label or <code>null</code>
280:          * @param image
281:          * image of <code>null</code>
282:          * @return a new push button
283:          */
284:         public static Button createPushButton(Composite parent, String label,
285:                         Image image)
286:         {
287:                 Button button = new Button(parent, SWT.PUSH);
288:                 button.setFont(parent.getFont());
289:                 if (image != null)
290:                 {
291:                         button.setImage(image);
292:                 }
293:                 if (label != null)
294:                 {
295:                         button.setText(label);
296:                 }
297:                 GridData gd = new GridData();
298:                 button.setLayoutData(gd);
299:
300:                 return button;
301:         }
302:
303:         @Override
304:         public boolean performOk()
305:         {
306:                 if (modelpath != null)
307:                 {
308:                         try
309:                         {
310:                                 modelpath.save();
311: //                                modelpath.save(data, modelpath.getOutput());
312:
313:                                 IVdmProject vdmProject = (IVdmProject) getSelectedProject().getAdapter(IVdmProject.class);
314:                                 VdmTypeCheckerUi.typeCheck(getShell(), vdmProject);
315:                         } catch (CoreException e)
316:                         {
317:                                 VdmUIPlugin.log("Faild to save model path changed", e);
318:                         }
319:                         return true;
320:                 }
321:                 return false;
322:         }
323:
324: }