Commit | Line | Data |
---|---|---|
abfad0aa FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.views.project.model; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.List; | |
17 | ||
18 | import org.eclipse.core.resources.IProject; | |
19 | import org.eclipse.core.resources.IProjectNature; | |
20 | import org.eclipse.core.resources.IWorkspaceRoot; | |
21 | import org.eclipse.core.resources.ResourcesPlugin; | |
22 | import org.eclipse.core.runtime.CoreException; | |
23 | import org.eclipse.linuxtools.tmf.ui.views.project.ProjectView; | |
24 | import org.eclipse.linuxtools.tmf.ui.views.project.TmfProjectNature; | |
25 | ||
26 | /** | |
27 | * <b><u>TmfProjectRoot</u></b> | |
28 | * <p> | |
29 | * TODO: Implement me. Please. | |
30 | */ | |
31 | public class TmfProjectRoot extends TmfProjectTreeNode { | |
32 | ||
33 | private final ProjectView fView; | |
34 | ||
35 | // ------------------------------------------------------------------------ | |
36 | // Constructors | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
39 | public TmfProjectRoot(ProjectView view) { | |
40 | super(null); | |
41 | fView = view; | |
42 | refreshChildren(); | |
43 | } | |
44 | ||
45 | @Override | |
46 | public void refresh() { | |
47 | fView.refresh(); | |
48 | } | |
49 | ||
50 | // ------------------------------------------------------------------------ | |
51 | // TmfProjectTreeNode | |
52 | // ------------------------------------------------------------------------ | |
53 | ||
d4011df2 | 54 | @Override |
abfad0aa FC |
55 | public String getName() { |
56 | return null; | |
57 | } | |
58 | ||
59 | @Override | |
60 | public void refreshChildren() { | |
61 | IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); | |
62 | IProject[] projects = root.getProjects(); | |
63 | for (IProject project : projects) { | |
64 | if (!project.isOpen() || isTmfProject(project)) { | |
65 | TmfProjectNode node = find(project.getName()); | |
66 | if (node == null) { | |
67 | node = new TmfProjectNode(this, project); | |
68 | fChildren.add(node); | |
69 | } else { | |
70 | node.updateState(); | |
71 | } | |
72 | } | |
73 | } | |
74 | List<ITmfProjectTreeNode> toRemove = new ArrayList<ITmfProjectTreeNode>(); | |
75 | for (ITmfProjectTreeNode node : fChildren) { | |
76 | if (exists(node.getName(), projects)) { | |
77 | node.refreshChildren(); | |
78 | } else { | |
79 | toRemove.add(node); | |
80 | } | |
81 | } | |
82 | for (ITmfProjectTreeNode node : toRemove) { | |
83 | fChildren.remove(node); | |
84 | } | |
85 | } | |
86 | ||
87 | private TmfProjectNode find(String name) { | |
88 | for (ITmfProjectTreeNode node : fChildren) { | |
89 | if (node instanceof TmfProjectNode && node.getName().equals(name)) { | |
90 | return (TmfProjectNode) node; | |
91 | } | |
92 | } | |
93 | return null; | |
94 | } | |
95 | ||
96 | private boolean exists(String name, IProject[] projects) { | |
97 | for (IProject project : projects) { | |
98 | if (project.getName().equals(name) && (!project.isOpen() || isTmfProject(project))) | |
99 | return true; | |
100 | } | |
101 | return false; | |
102 | } | |
103 | ||
104 | private boolean isTmfProject(IProject project) { | |
105 | if (project.isOpen()) { | |
106 | IProjectNature nature; | |
107 | try { | |
108 | nature = project.getNature(TmfProjectNature.ID); | |
109 | if (nature instanceof TmfProjectNature) { | |
110 | return true; | |
111 | } | |
112 | } catch (CoreException e) { | |
113 | e.printStackTrace(); | |
114 | } | |
115 | } | |
116 | return false; | |
117 | } | |
118 | } |