Switch to unified view

a/tool/src/main/actions.java b/tool/src/main/actions.java
...
...
11
 * related to SPDX handling </text> 
11
 * related to SPDX handling </text> 
12
 */
12
 */
13
13
14
package main;
14
package main;
15
15
16
import GUI.NodeType;
17
import GUI.TreeNodeSPDX;
18
import GUI.swingUtils;
19
import static GUI.swingUtils.nodeCreate;
16
import definitions.is;
20
import definitions.is;
17
import java.io.File;
21
import java.io.File;
18
import java.io.IOException;
22
import java.io.IOException;
19
import java.util.ArrayList;
23
import java.util.ArrayList;
24
import java.util.HashMap;
20
import java.util.logging.Level;
25
import java.util.logging.Level;
21
import java.util.logging.Logger;
26
import java.util.logging.Logger;
27
import javax.swing.JTree;
22
import script.log;
28
import script.log;
23
import spdxlib.SPDXfile;
29
import spdxlib.SPDXfile;
30
import static utils.files.findFilesFiltered;
24
31
25
32
26
public class actions {
33
public class actions {
34
35
    /**
36
     * This method adds up all the licenses found on the licenses folder
37
     */
38
    public static void addLicenses(){
39
        File folder = new File(core.getWorkFolder(), "licenses");
40
        ArrayList<File> files = utils.files.findFilesFiltered(folder, ".java", 2);
41
        for(File file : files){
42
            core.script.runJava(file, null, is.license);
43
        }
44
    }
27
45
28
    /**
46
    /**
29
     * Find all files within a given folder, return the respective
47
     * Find all files within a given folder, return the respective
30
     * details such as hashes and other details relevant for SPDX processing
48
     * details such as hashes and other details relevant for SPDX processing
31
     * @param folder The folder location where we will look for SPDX files
49
     * @param folder The folder location where we will look for SPDX files
50
     * @return 
32
     */
51
     */
33
    public static ArrayList<SPDXfile> findSPDX(File folder){
52
    public static ArrayList<SPDXfile> findSPDX(File folder){
34
        // only find the SPDX documents with .SPDX extension
53
        // only find the SPDX documents with .SPDX extension
35
        ArrayList<File> files = utils.files.findFilesFiltered(folder, ".spdx", 25);
54
        ArrayList<File> files = utils.files.findFilesFiltered(folder, ".spdx", 25);
36
        ArrayList<SPDXfile> list = new ArrayList();
55
        ArrayList<SPDXfile> list = new ArrayList();
...
...
63
            }
82
            }
64
       
83
       
65
        return list;
84
        return list;
66
    }
85
    }
67
86
87
    
88
     /**
89
     * This is a critical method. It will look for all SPDX files inside a given
90
     * folder and them as tree nodes
91
     * @param tree 
92
     */
93
    public static void addTreeSPDX(JTree tree) {
94
        // only find the SPDX documents with .SPDX extension
95
        ArrayList<File> fileList = utils.files
96
                .findFilesFiltered(core.getProductsFolder(), ".spdx", 25);
97
        
98
        if(fileList.isEmpty()){
99
            // no need to continue, just leave here
100
            return;
101
        }
102
    
103
        // get the root node where we have our treeview
104
        TreeNodeSPDX rootNode = swingUtils.getRootNode(tree);
105
        
106
         // create the node for hosting our list of SPDX documents
107
        TreeNodeSPDX softwareNode = nodeCreate(
108
                "Software"
109
                        //+ " (" + fileList.size() + ")"
110
                , NodeType.other, rootNode);
111
        // add the identification to this tag
112
        softwareNode.id = "Software";
113
        softwareNode.setIcon("box-label.png");
114
        
115
        // create a tree based on folder tree on disk
116
        findFolders(core.getProductsFolder(), 25, softwareNode);
117
        
118
    }
119
    
68
    /**
120
    /**
69
     * This method adds up all the licenses found on the licenses folder
121
     * Find all subfolders in a given folder.
122
     * @param where A file object of the start folder
123
     * @param maxDeep How deep is the crawl allowed to proceed
124
     * @param rootNode
125
//     * @return An array containing all the found files, returns null if none is
126
//     * found
127
     * @return the number of relevant children that should be accounted
128
     */
129
     public static int findFolders(File where, int maxDeep
130
             , TreeNodeSPDX rootNode){
131
132
        // get a list of the current files on this folder 
133
        File[] files = where.listFiles();
70
     */
134
       
71
    public static void addLicenses(){
135
        // we use this for counting files that were added
72
        File folder = new File(core.getWorkFolder(), "licenses");
136
        int count = 0;
73
        ArrayList<File> files = utils.files.findFilesFiltered(folder, ".java", 2);
137
        
138
        // go through all the files/folders that were listed
139
        if(files != null)
74
        for(File file : files){
140
        for (File file : files) {
75
            core.script.runJava(file, null, is.license);
141
          // only accept folders, ignore files  
142
          if ( (file.isDirectory())
143
             &&( maxDeep-1 > 0 ) ){
144
              String folderName = file.getName();
145
              // ignore SVN folders
146
              if(folderName.equals(".svn")){
147
                  continue;
148
              }
149
              // create a new node for the current folder
150
              TreeNodeSPDX node = new TreeNodeSPDX(folderName);
151
              node.setIcon("folder-horizontal.png");
152
              node.setIconWhenSelected("folder-horizontal-open.png");
153
              node.nodeType = NodeType.folder;
154
              node.id = folderName;
155
              // we want to track if something new was added
156
              int oldCount = count;
157
              // do the recursive crawling
158
              count += findFolders(file, maxDeep-1, node);
159
              // only add this node if we found something interesting there
160
              if(count > oldCount){
161
                // add the node as a child of the root node
162
                rootNode.add(node);
163
              }
164
          }
165
          
166
          // perhaps we can add this file if it is an SPDX?
167
          if(file.isFile()){
168
              String fileName = file.getName();
169
              // we only want files with an SPDX extension
170
              if(file.getName().endsWith(".spdx")==false){
171
                  continue;
172
              }
173
              // we don't generic SPDX files here
174
              if(file.getName().endsWith("-info.spdx")){
175
                  continue;
176
              }
177
              // create the new node
178
              TreeNodeSPDX node = new TreeNodeSPDX(fileName);
179
              node.setIcon("box.png");
180
              node.nodeType = NodeType.SPDX;
181
              node.id = fileName;
182
              // set here what we want to happen when the user clicks on it
183
              File scriptFile = new File(core.getPluginsFolder(), "/spdx/show.java");
184
              node.scriptFile = scriptFile;
185
              node.scriptFolder = scriptFile.getParentFile();
186
              node.scriptMethod = "summary";
187
              // create the correct parameters
188
              String relativePath = 
189
                      file.getAbsolutePath().replace(core.getProductsFolder().getAbsolutePath(), "");
190
              node.scriptParameters.add(new String[]{param.spdx, relativePath});
191
              // add it up
192
              rootNode.add(node);
193
              count++;
194
          }
195
          
196
        }
197
        //  how many files were found here?
198
        rootNode.setCounter(count);
199
        return count;
200
      }
201
    
202
     /**
203
     * This is a critical method. It will look for all SPDX files inside a given
204
     * folder and them as tree nodes
205
     * @param tree 
206
     */
207
    public static void addTreeSPDXOld(JTree tree) {
208
        // only find the SPDX documents with .SPDX extension
209
        ArrayList<File> initialFileList = utils.files
210
                .findFilesFiltered(core.getProductsFolder(), ".spdx", 25);
76
        }
211
        
212
        if(initialFileList.isEmpty()){
213
            // no need to continue, just leave here
214
            return;
215
        }
216
        
217
        // this is used for filtering duplicate folders in the initial indexing
218
        ArrayList<File> tempFolderList = new ArrayList();
219
        // where are our spdx files stored?
220
        String baseFolder = core.getProductsFolder().getAbsolutePath();
221
        // get the root node where we have our treeview
222
        TreeNodeSPDX rootNode = swingUtils.getRootNode(tree);
223
        
224
        // where we place all results after being filtered
225
        ArrayList<File> fileList = new ArrayList();
226
        // iterate all files in our array that were found as products
227
        for(File file: initialFileList){
228
            // now we need to filter what we don't want to have in our array
229
            String filename = file.getName();
230
            // we don't need SVN files, ignore them completely
231
            if(filename.endsWith(".svn-base")){
232
                continue;
233
            }
234
            // add this file to our final list
235
            fileList.add(file);
236
            // now take care of the folder where this file was placed
237
            // get only the folder base, be careful with path separators here
238
            String folderName = file.getParent().replace(baseFolder, "");
239
            // no need to add in case the folder is the root
240
            if(folderName.isEmpty()){
241
                continue;
242
            }
243
            File folder = file.getParentFile();
244
            // no need to continue in case this folder was already added before
245
            if(tempFolderList.contains(folder)){
246
                continue;
247
            }
248
            // add it up
249
            tempFolderList.add(folder);
250
        }
251
        
252
         // create the node for hosting our list of SPDX documents
253
        TreeNodeSPDX softwareNode = nodeCreate(
254
                "Software (" + fileList.size() + ")"
255
                , NodeType.other, rootNode);
256
        // add the identification to this tag
257
        softwareNode.id = "Software";
258
        softwareNode.setIcon("box.png");
259
        
260
        // we need to map a list of spdx folders and files
261
        HashMap<File, TreeNodeSPDX> folderList = new HashMap();
262
        // first pass to add up all nodes and folders
263
        for(File folder : tempFolderList){
264
            TreeNodeSPDX node = new TreeNodeSPDX(folder.getName());
265
            node.setIcon("envelope.png");
266
            folderList.put(folder, node);
267
        }
268
       
269
        
270
        
271
        // second pass to add them all up on the tree view
272
        for(File folder : folderList.keySet()){
273
            TreeNodeSPDX node = folderList.get(folder);
274
            softwareNode.add(node);
275
        }
276
        
277
                
278
      
279
        //System.exit(-1981);
77
    }
280
    }  
78
281
        
282
       
283
//        
284
//        // iterate through the final list, build the folder list, add tree nodes
285
//        for(File file: fileList){
286
//            // get only the folder base, be careful with path separators here
287
//            String folderName = file.getParent().replace(baseFolder, "");
288
//            // no need to add in case the folder is the root
289
//            if(folderName.isEmpty()){
290
//                continue;
291
//            }
292
//            // no need to continue in case this folder was already added before
293
//            if(folderList.containsKey(folderName)){
294
//                continue;
295
//            }
296
//            
297
//            String simpleFolderName = file.getParentFile().getName();
298
//            
299
//            // create the node for hosting our list of SPDX documents
300
//            TreeNodeSPDX node = nodeCreate(
301
//                    simpleFolderName // get the normal name (no path components)
302
//                    , NodeType.folder // just a normal folder
303
//                    , rootNode); // add 
304
//        // add the identification to this tag
305
//        softwareNode.id = "";
306
//            
307
//            // build up our list of folders
308
//            System.err.println(folderName);
309
//            folderList.put(folderName, rootNode);
310
//        }             
311
            
312
  
79
    
313
    
80
}
314
}