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