Switch to unified view

a/tool/run/plugins/basic/TreeNodeDetails.java b/tool/run/plugins/basic/TreeNodeDetails.java
...
...
15
15
16
import GUI.NodeType;
16
import GUI.NodeType;
17
import GUI.TreeNodeSPDX;
17
import GUI.TreeNodeSPDX;
18
import GUI.swingUtils;
18
import GUI.swingUtils;
19
import definitions.Messages;
19
import definitions.Messages;
20
import definitions.definition;
20
import definitions.is;
21
import definitions.is;
22
import java.awt.Cursor;
21
import java.io.File;
23
import java.io.File;
24
import java.util.Collection;
22
import java.util.Enumeration;
25
import java.util.Enumeration;
26
import java.util.HashMap;
27
import java.util.Iterator;
23
import main.core;
28
import main.core;
24
import main.param;
29
import main.param;
25
import script.Plugin;
30
import script.Plugin;
26
import script.log;
31
import script.log;
32
import spdxlib.FileInfo;
27
import spdxlib.Person;
33
import spdxlib.Person;
28
import spdxlib.SPDXfile;
34
import spdxlib.SPDXfile;
29
35
30
/**
36
/**
31
 *
37
 *
...
...
79
        SPDXfile spdx = new SPDXfile(spdxFile);
85
        SPDXfile spdx = new SPDXfile(spdxFile);
80
        
86
        
81
        
87
        
82
        // basic parts were done, now add up the needed details
88
        // basic parts were done, now add up the needed details
83
        addPeople(spdx, node);
89
        addPeople(spdx, node);
90
        addFiles(spdx, node);
91
        
92
        
93
        // all done, time to refresh things up
94
        swingUtils.setSelectedNode(node.getUID());
95
        // refresh things up
96
        core.studio.getTree().repaint();
97
        
98
    }
99
    
100
    
101
    /**
102
     * Add the files inside this SPDX document
103
     */
104
    private void addFiles(SPDXfile spdx, TreeNodeSPDX spdxNode){
105
        // title of the node that we want to create
106
        String title = "Files";
107
        
108
        // do we have more than one reviewer?
109
        if(spdx.fileSection.files.size() > 1){
110
            title = "Files " 
111
                    + " ("
112
                    + spdx.fileSection.files.size()
113
                    + ")";
84
        
114
        }
115
        
116
        // let's add the main node for files
117
        TreeNodeSPDX node = swingUtils.nodeCreate(
118
                title, 
119
                NodeType.sectionFile, spdxNode);
120
        node.nodeType = NodeType.sectionFile;
121
        node.id = "./";
122
        node.setUserObject(spdx.fileSection);
123
124
        
125
        
126
        core.studio.getTree().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
127
        // create the tree structure
128
        doTreeStructure(node, spdx);
129
        // if an exception occurs, this next line doesn't happen..
130
        core.studio.getTree().setCursor(Cursor.getDefaultCursor());
131
       
132
        
133
    }
134
    
135
    
136
    /**
137
     * Creates a tree of folders and respective files
138
     * @param root
139
     * @param spdx 
140
     */
141
    private void doTreeStructure(TreeNodeSPDX root, SPDXfile spdx){
142
        // go through all the files inside the SPDX 
143
        for(FileInfo fileInfo : spdx.fileSection.files){
144
            // get the path for this file bit
145
            String path = getFilePath(fileInfo);
146
            //String parentFolder = getParentFolder(path);
147
            // add it up to our list
148
            TreeNodeSPDX pathNode = addNodeFolder(root, path);
85
        
149
        }
150
        
151
        
152
         for(FileInfo fileInfo : spdx.fileSection.files){
153
            // get the path for this file bit
154
            String path = getFilePath(fileInfo);
155
            //String parentFolder = getParentFolder(path);
156
            // add it up to our list
157
            TreeNodeSPDX pathNode = addNodeFolder(root, path);
158
            
159
            TreeNodeSPDX nodeFile = new TreeNodeSPDX(getSimpleName(fileInfo));
160
            nodeFile.id = fileInfo.getName();
161
            nodeFile.nodeType = NodeType.file;
162
            nodeFile.setUserObject(fileInfo);
163
            // add this file to the parent path
164
            pathNode.add(nodeFile);
165
        }
166
       
167
        
168
    }
169
     
170
    
171
    /**
172
     * Provides a pretty name to place on the tree view
173
     * @param fileInfo
174
     * @return 
175
     */
176
    private String getSimpleName(FileInfo fileInfo){
177
        String result = fileInfo.tagFileName.getValue();
178
        if(result.contains("/")){
179
            return result.substring(result.lastIndexOf("/")+1);
180
        }
181
        return result;
182
    }
183
    
184
    
185
    /**
186
     * Adds a given folder as child from a specific folder
187
     */
188
    private TreeNodeSPDX addNodeFolder(TreeNodeSPDX root, String newFolder){
189
        // don't accept requests for the root node
190
        if(newFolder.equals("./")){
191
            return root;
192
        }
193
        
194
        // try to locate this parent
195
        TreeNodeSPDX TheOne = findTheOne(root, newFolder);
196
        // if no folder/node was found, create it
197
        if(TheOne == null){
198
            TheOne = createTheOne(root, newFolder);
199
        }
200
         
201
        return TheOne;
202
    }
203
    
204
    
205
    /**
206
     * Create the node inside a given node as father
207
     */
208
    private TreeNodeSPDX createTheOne(TreeNodeSPDX root, String what){
209
        // we want to break apart all the folder components
210
        String path = what;
211
        // we don't need the root part
212
        if(path.startsWith("./")){
213
            // remove it
214
            path = path.substring(2);
215
        }
216
        
217
        // break everything into little pieces
218
        String[] pathPortions = path.split("/");
219
        // go through each little piece
220
        TreeNodeSPDX lastNode = root;
221
        String lastPortion = ".";
222
        for(String portion : pathPortions){
223
            String thisPath = lastPortion + "/" + portion;
224
            // get the node if it exists, return null if not existing
225
            TreeNodeSPDX node = getNode(lastNode, thisPath);
226
            // doesn't exist? Create one
227
            if(node == null){
228
                node = new TreeNodeSPDX(portion);
229
                node.nodeType = NodeType.folder;
230
                node.id = thisPath;
231
                // add up this node as child
232
                lastNode.add(node);
233
                //System.err.println("TND23s - " + thisPath);
234
            }
235
            // increment our data
236
            lastNode = node;
237
            lastPortion = thisPath;
238
        }
239
        // give back the last node that was created
240
        return lastNode;
241
    }
242
    
243
    /**
244
     * Does a given node contain a child with a specific ID?
245
     * @return 
246
     */
247
    private TreeNodeSPDX getNode(TreeNodeSPDX where, String what){
248
        // list all the children folders
249
        Enumeration list = where.children();
250
        while(list.hasMoreElements()){
251
            TreeNodeSPDX child = (TreeNodeSPDX) list.nextElement();
252
            // first question: Are you the one?
253
            if(child.id.equals(what)){
254
                // no need to continue, he is the one
255
                return child;
256
            }
257
        }
258
       return null; 
259
    }
260
    
261
    /**
262
     * Tries to find a node with a given ID from a given 
263
     * node as root location
264
     * @param what
265
     * @return 
266
     */
267
    private TreeNodeSPDX findTheOne(TreeNodeSPDX where, String what){
268
    // list all the children folders
269
        Enumeration list = where.children();
270
        while(list.hasMoreElements()){
271
            TreeNodeSPDX child = (TreeNodeSPDX) list.nextElement();
272
            // first question: Are you the one?
273
            if(child.id.equals(what)){
274
                // no need to continue, he is the one
275
                return child;
276
            }
277
            // Is any of your children, the one?
278
            TreeNodeSPDX result = findTheOne(child, what);
279
            if(result != null){
280
                // he is the one
281
                return result;
282
            }
283
        }
284
        return null;
285
    }
286
    
287
    
288
    /**
289
     * Returns the parent path (if available)
290
     */
291
    private String getParentFolder(String path){
292
        // we need to count the number of slashes
293
        int lengthOriginal = path.length();
294
        String temp = path.replace("/", "");
295
        // do we have at least two slashes?
296
        if(lengthOriginal > (temp.length() + 1)){
297
            // get everything up to last slash
298
            return path.substring(0, path.lastIndexOf("/"));
299
        }
300
        // we have nothing, return the root folder
301
        return "./";
302
    }
303
    
304
    
305
    /**
306
     * Returns the path portion from a given FileInfo object
307
     * @return 
308
     */
309
    private String getFilePath(FileInfo fileInfo){
310
        // if we have a FilePath tag available, use it as default
311
        if(fileInfo.tagFilePath != null){
312
            return fileInfo.tagFilePath.getValue();
313
        }
314
        // or else, the path is found inside the FileName tag
315
        String fileName = fileInfo.tagFileName.getValue();
316
        // if no path is available, just mention it as root
317
        if(fileName.contains("/")==false){
318
            return "./";
319
        }
320
        // there is a path available, let's get it
321
        return fileName.substring(0, fileName.lastIndexOf("/"));
86
    }
322
    }
87
    
323
    
88
    
324
    
89
    /**
325
    /**
90
     * When necessary, add nodes listing the people that have created the SPDX
326
     * When necessary, add nodes listing the people that have created the SPDX
...
...
98
                // no need to continue, people were already listed
334
                // no need to continue, people were already listed
99
                return;
335
                return;
100
            }
336
            }
101
        }
337
        }
102
        
338
        
339
        // title of the node that we want to create
340
        String title = definition.nodeReviewerSPDX;
341
        
342
        // do we have more than one reviewer?
343
        if(spdx.creatorSection.people.size() > 1){
344
            title = definition.nodeReviewersSPDX 
345
                    + " ("
346
                    + spdx.creatorSection.people.size()
347
                    + ")";
348
        }
349
        
103
        // let's add people, this is our main node
350
        // let's add people, this is our main node
104
        TreeNodeSPDX node = swingUtils.nodeCreate("SPDX author", 
351
        TreeNodeSPDX node = swingUtils.nodeCreate(
352
                title, 
105
                NodeType.sectionCreator, spdxNode);
353
                NodeType.sectionCreator, spdxNode);
106
        node.nodeType = NodeType.sectionCreator;
354
        node.nodeType = NodeType.sectionCreator;
107
        node.id = "SPDX author";
355
        node.id = definition.nodeReviewerSPDX;
108
        node.setUserObject(spdx.creatorSection);
356
        node.setUserObject(spdx.creatorSection);
109
        
357
        
110
        // create now a node for each author
358
        // create now a node for each author
111
        for(Person person : spdx.creatorSection.people){
359
        for(Person person : spdx.creatorSection.people){
112
            TreeNodeSPDX nodePerson = swingUtils.addNodePerson(node, person);
360
            TreeNodeSPDX nodePerson = swingUtils.addNodePerson(node, person);
113
            // add here the action we want to happen when clicked
361
            // add here the action we want to happen when clicked
114
            File scriptFile = new File(core.getPluginsFolder(), "/people/show.java");
362
            File scriptFile = new File(core.getPluginsFolder(), "/people/show.java");
115
              nodePerson.scriptFile = scriptFile;
363
            nodePerson.scriptFile = scriptFile;
116
              nodePerson.scriptFolder = scriptFile.getParentFile();
364
            nodePerson.scriptFolder = scriptFile.getParentFile();
117
              nodePerson.scriptMethod = "details";
365
            nodePerson.scriptMethod = "details";
118
              // create the correct parameters
366
            // create the correct parameters
119
              String relativePath = 
367
            String relativePath = 
120
                      spdx.file.getAbsolutePath().replace(core.getProductsFolder().getAbsolutePath(), "");
368
                    spdx.file.getAbsolutePath().replace(core.getProductsFolder().getAbsolutePath(), "");
121
              // add the SPDX that we want to edit/show
369
            // add the SPDX that we want to edit/show
122
              nodePerson.scriptParameters.add(new String[]{param.spdx, relativePath});
370
            nodePerson.scriptParameters.add(new String[]{param.spdx, relativePath});
123
              // add the user name that we want to edit
371
            // add the user name that we want to edit
124
              nodePerson.scriptParameters.add(new String[]{param.filter, person.getTitle()});
372
            nodePerson.scriptParameters.add(new String[]{param.filter, person.getTitle()});
125
        }
126
        
373
        }
127
        // all done, time to refresh things up
374
        
128
        swingUtils.setSelectedNode(spdxNode.getUID());
375
       
129
        // refresh things up
130
        core.studio.getTree().repaint();
131
    } 
376
    } 
132
    
377
    
133
    
378
    
134
    
379
    
135
}
380
}