1 /*
   2  * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 package com.sun.hotspot.igv.coordinator;
  25 
  26 import com.sun.hotspot.igv.coordinator.actions.RemoveCookie;
  27 import com.sun.hotspot.igv.data.*;
  28 import java.awt.Image;
  29 import java.util.List;
  30 import org.openide.nodes.AbstractNode;
  31 import org.openide.nodes.Children;
  32 import org.openide.nodes.Node;
  33 import org.openide.util.ImageUtilities;
  34 import org.openide.util.lookup.AbstractLookup;
  35 import org.openide.util.lookup.InstanceContent;
  36 
  37 /**
  38  *
  39  * @author Thomas Wuerthinger
  40  */
  41 public class FolderNode extends AbstractNode {
  42 
  43     private InstanceContent content;
  44     private FolderChildren children;
  45 
  46     private static class FolderChildren extends Children.Keys<FolderElement> implements ChangedListener {
  47 
  48         private final Folder folder;
  49 
  50         public FolderChildren(Folder folder) {
  51             this.folder = folder;
  52             folder.getChangedEvent().addListener(this);
  53         }
  54 
  55         @Override
  56         protected Node[] createNodes(FolderElement e) {
  57              if (e instanceof InputGraph) {
  58                 return new Node[]{new GraphNode((InputGraph) e)};
  59             } else if (e instanceof Folder) {
  60                  return new Node[]{new FolderNode((Folder) e)};
  61              } else {
  62                 return null;
  63             }
  64         }
  65 
  66         @Override
  67         public void addNotify() {
  68             this.setKeys(folder.getElements());
  69         }
  70 
  71         @Override
  72         public void changed(Object source) {
  73             addNotify();
  74          }
  75     }
  76 
  77     @Override
  78     public Image getIcon(int i) {
  79         return ImageUtilities.loadImage("com/sun/hotspot/igv/coordinator/images/folder.png");
  80     }
  81 
  82     protected FolderNode(Folder folder) {
  83         this(folder, new FolderChildren(folder), new InstanceContent());
  84     }
  85 
  86     private FolderNode(final Folder folder, FolderChildren children, InstanceContent content) {
  87         super(children, new AbstractLookup(content));
  88         this.content = content;
  89         this.children = children;
  90         if (folder instanceof FolderElement) {
  91             final FolderElement folderElement = (FolderElement) folder;
  92             this.setDisplayName(folderElement.getName());
  93             content.add(new RemoveCookie() {
  94                 @Override
  95                 public void remove() {
  96                     folderElement.getParent().removeElement(folderElement);
  97                 }
  98             });
  99         }
 100     }
 101 
 102     public void init(String name, List<Group> groups) {
 103         this.setDisplayName(name);
 104         children.addNotify();
 105 
 106         for (Group g : groups) {
 107             content.add(g);
 108         }
 109     }
 110 
 111     @Override
 112     public Image getOpenedIcon(int i) {
 113         return getIcon(i);
 114     }
 115 }