(no commit message)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / project / model / LTTngProjectNode.java
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.lttng.ui.views.project.model;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.linuxtools.lttng.ui.views.project.LTTngProjectNature;
23
24 /**
25 * <b><u>LTTngProjectNode</u></b>
26 * <p>
27 * TODO: Implement me. Please.
28 */
29 public class LTTngProjectNode extends LTTngProjectTreeNode {
30
31 public static final String TRACE_FOLDER_NAME = "Traces";
32 public static final String EXPER_FOLDER_NAME = "Experiments";
33
34 private final IProject fProject;
35 private boolean fIsLTTngProject;
36 private boolean fIsOpen;
37 private LTTngTraceFolderNode fTracesFolder;
38 private LTTngExperimentFolderNode fExperimentsFolder;
39
40 // ------------------------------------------------------------------------
41 // Constructors
42 // ------------------------------------------------------------------------
43
44 public LTTngProjectNode(IProject project) {
45 this(null, project);
46 }
47
48 public LTTngProjectNode(ILTTngProjectTreeNode parent, IProject project) {
49 super(parent);
50 fProject = project;
51 updateState();
52 }
53
54 // ------------------------------------------------------------------------
55 // LTTngProjectTreeNode
56 // ------------------------------------------------------------------------
57
58 public String getName() {
59 return fProject.getName();
60 }
61
62 @Override
63 public void refreshChildren() {
64
65 if (!(fIsOpen && fIsLTTngProject))
66 return;
67
68 try {
69 IResource[] resources = fProject.members();
70 for (IResource resource : resources) {
71 if (resource.getType() == IResource.FOLDER) {
72 String name = resource.getName();
73 if (name.equals(TRACE_FOLDER_NAME) && !isIncluded(true, name, fChildren)) {
74 fTracesFolder = new LTTngTraceFolderNode(this, (IFolder) resource);
75 fChildren.add(fTracesFolder);
76 } else
77 if (name.equals(EXPER_FOLDER_NAME) && !isIncluded(false, name, fChildren)) {
78 fExperimentsFolder = new LTTngExperimentFolderNode(this, (IFolder) resource);
79 fChildren.add(fExperimentsFolder);
80 }
81 }
82 }
83 List<ILTTngProjectTreeNode> toRemove = new ArrayList<ILTTngProjectTreeNode>();
84 for (ILTTngProjectTreeNode node : fChildren) {
85 if (exists(node.getName(), resources)) {
86 node.refreshChildren();
87 }
88 else {
89 toRemove.add(node);
90 }
91 }
92 for (ILTTngProjectTreeNode node : toRemove) {
93 fChildren.remove(node);
94 }
95
96 } catch (CoreException e) {
97 }
98 }
99
100 private boolean isIncluded(boolean isTraces, String name, List<ILTTngProjectTreeNode> list) {
101 boolean found = false;
102 for (ILTTngProjectTreeNode node : list) {
103 if (node instanceof LTTngTraceFolderNode && isTraces) {
104 found |= node.getName().equals(name);
105 } else
106 if (node instanceof LTTngExperimentFolderNode && !isTraces) {
107 found |= node.getName().equals(name);
108 }
109 }
110 return found;
111 }
112
113 private boolean exists(String name, IResource[] resources) {
114 for (IResource resource : resources) {
115 if (resource.getName().equals(name))
116 return true;
117 }
118 return false;
119 }
120
121 // ------------------------------------------------------------------------
122 // Modifiers
123 // ------------------------------------------------------------------------
124
125 public void openProject() {
126 try {
127 fProject.open(null);
128 updateState();
129 refreshChildren();
130 } catch (CoreException e) {
131 e.printStackTrace();
132 }
133 }
134
135 public void closeProject() {
136 try {
137 fProject.close(null);
138 updateState();
139 removeChildren();
140 } catch (CoreException e) {
141 e.printStackTrace();
142 }
143 }
144
145 private boolean isLTTngProject(IProject project) {
146 boolean result = false;
147 if (project != null && project.isAccessible()) {
148 try {
149 result = project.hasNature(LTTngProjectNature.ID);
150 } catch (CoreException e) {
151 }
152 }
153 return result;
154 }
155
156 public void updateState() {
157 fIsOpen = (fProject != null) ? fProject.isAccessible() : false;
158 fIsLTTngProject = isLTTngProject(fProject);
159 }
160
161 // ------------------------------------------------------------------------
162 // Accessors
163 // ------------------------------------------------------------------------
164
165 /**
166 * @return
167 */
168 public boolean isLTTngProject() {
169 return fIsLTTngProject;
170 }
171
172 /**
173 * @return
174 */
175 public boolean isOpen() {
176 return fIsOpen;
177 }
178
179 /**
180 * @return
181 */
182 public IProject getProject() {
183 return fProject;
184 }
185
186 /**
187 * @return
188 */
189 public LTTngTraceFolderNode getTracesFolder() {
190 return fTracesFolder;
191 }
192
193 /**
194 * @return
195 */
196 public LTTngExperimentFolderNode getExperimentsFolder() {
197 return fExperimentsFolder;
198 }
199
200 }
This page took 0.034515 seconds and 5 git commands to generate.