1 /*
2  * Copyright (c) 2008-2019 Emmanuel Dupuy.
3  * This project is distributed under the GPLv3 license.
4  * This is a Copyleft license that gives the user the right to use,
5  * copy and modify the code freely for non-commercial purposes.
6  */
7 
8 package org.jd.gui.service.treenode;
9 
10 import org.jd.gui.api.API;
11 import org.jd.gui.api.feature.ContainerEntryGettable;
12 import org.jd.gui.api.feature.UriGettable;
13 import org.jd.gui.api.model.Container;
14 import org.jd.gui.view.component.JavaFilePage;
15 import org.jd.gui.view.data.TreeNodeBean;
16 
17 import javax.swing.*;
18 import javax.swing.tree.DefaultMutableTreeNode;
19 import java.io.File;
20 
21 public class JavaFileTreeNodeFactoryProvider extends AbstractTypeFileTreeNodeFactoryProvider {
22     protected static final ImageIcon JAVA_FILE_ICON = new ImageIcon(JavaFileTreeNodeFactoryProvider.class.getClassLoader().getResource("org/jd/gui/images/jcu_obj.png"));
23     protected static final Factory FACTORY = new Factory();
24 
getSelectors()25     @Override public String[] getSelectors() { return appendSelectors("*:file:*.java"); }
26 
27     @Override
28     @SuppressWarnings("unchecked")
make(API api, Container.Entry entry)29     public <T extends DefaultMutableTreeNode & ContainerEntryGettable & UriGettable> T make(API api, Container.Entry entry) {
30         int lastSlashIndex = entry.getPath().lastIndexOf('/');
31         String label = entry.getPath().substring(lastSlashIndex+1);
32         String location = new File(entry.getUri()).getPath();
33         return (T)new FileTreeNode(entry, new TreeNodeBean(label, "Location: " + location, JAVA_FILE_ICON), FACTORY);
34     }
35 
36     protected static class Factory implements AbstractTypeFileTreeNodeFactoryProvider.PageAndTipFactory {
37         // --- PageAndTipFactory --- //
38         @Override
39         @SuppressWarnings("unchecked")
makePage(API a, Container.Entry e)40         public <T extends JComponent & UriGettable> T makePage(API a, Container.Entry e) {
41             return (T)new JavaFilePage(a, e);
42         }
43 
44         @Override
makeTip(API api, Container.Entry entry)45         public String makeTip(API api, Container.Entry entry) {
46             String location = new File(entry.getUri()).getPath();
47             StringBuilder tip = new StringBuilder("<html>Location: ");
48 
49             tip.append(location);
50             tip.append("</html>");
51 
52             return tip.toString();
53         }
54     }
55 }
56