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.connection.Server;
  27 import com.sun.hotspot.igv.coordinator.actions.*;
  28 import com.sun.hotspot.igv.data.GraphDocument;
  29 import com.sun.hotspot.igv.data.Group;
  30 import com.sun.hotspot.igv.data.services.GroupCallback;
  31 import java.awt.BorderLayout;
  32 import java.io.IOException;
  33 import java.io.ObjectInput;
  34 import java.io.ObjectOutput;
  35 import java.io.Serializable;
  36 import javax.swing.UIManager;
  37 import javax.swing.border.Border;
  38 import org.openide.ErrorManager;
  39 import org.openide.actions.GarbageCollectAction;
  40 import org.openide.awt.Toolbar;
  41 import org.openide.awt.ToolbarPool;
  42 import org.openide.explorer.ExplorerManager;
  43 import org.openide.explorer.ExplorerUtils;
  44 import org.openide.explorer.view.BeanTreeView;
  45 import org.openide.util.LookupEvent;
  46 import org.openide.util.LookupListener;
  47 import org.openide.util.NbBundle;
  48 import org.openide.util.actions.NodeAction;
  49 import org.openide.windows.TopComponent;
  50 import org.openide.windows.WindowManager;
  51 
  52 /**
  53  *
  54  * @author Thomas Wuerthinger
  55  */
  56 public final class OutlineTopComponent extends TopComponent implements ExplorerManager.Provider, LookupListener {
  57 
  58     public static OutlineTopComponent instance;
  59     public static final String PREFERRED_ID = "OutlineTopComponent";
  60     private ExplorerManager manager;
  61     private GraphDocument document;
  62     private FolderNode root;
  63     private Server server;
  64     private Server binaryServer;
  65 
  66     private OutlineTopComponent() {
  67         initComponents();
  68 
  69         setName(NbBundle.getMessage(OutlineTopComponent.class, "CTL_OutlineTopComponent"));
  70         setToolTipText(NbBundle.getMessage(OutlineTopComponent.class, "HINT_OutlineTopComponent"));
  71 
  72         document = new GraphDocument();
  73         initListView();
  74         initToolbar();
  75         initReceivers();
  76     }
  77 
  78     private void initListView() {
  79         manager = new ExplorerManager();
  80         root = new FolderNode(document);
  81         manager.setRootContext(root);
  82         ((BeanTreeView) this.treeView).setRootVisible(false);
  83 
  84         associateLookup(ExplorerUtils.createLookup(manager, getActionMap()));
  85     }
  86 
  87     private void initToolbar() {
  88 
  89         Toolbar toolbar = new Toolbar();
  90         Border b = (Border) UIManager.get("Nb.Editor.Toolbar.border"); //NOI18N
  91         toolbar.setBorder(b);
  92         this.add(toolbar, BorderLayout.NORTH);
  93 
  94         toolbar.add(ImportAction.get(ImportAction.class));
  95 
  96         toolbar.add(((NodeAction) SaveAsAction.get(SaveAsAction.class)).createContextAwareInstance(this.getLookup()));
  97         toolbar.add(SaveAllAction.get(SaveAllAction.class));
  98 
  99         toolbar.add(((NodeAction) RemoveAction.get(RemoveAction.class)).createContextAwareInstance(this.getLookup()));
 100         toolbar.add(RemoveAllAction.get(RemoveAllAction.class));
 101 
 102         toolbar.add(GarbageCollectAction.get(GarbageCollectAction.class).getToolbarPresenter());
 103 
 104         for (Toolbar tb : ToolbarPool.getDefault().getToolbars()) {
 105             tb.setVisible(false);
 106         }
 107     }
 108 
 109     private void initReceivers() {
 110 
 111         final GroupCallback callback = new GroupCallback() {
 112 
 113             @Override
 114             public void started(Group g) {
 115                 synchronized(OutlineTopComponent.this) {
 116                     getDocument().addElement(g);
 117                 }
 118             }
 119         };
 120 
 121         server = new Server(getDocument(), callback, false);
 122         binaryServer = new Server(getDocument(), callback, true);
 123     }
 124 
 125     public void clear() {
 126         document.clear();
 127     }
 128 
 129     @Override
 130     public ExplorerManager getExplorerManager() {
 131         return manager;
 132     }
 133 
 134     public GraphDocument getDocument() {
 135         return document;
 136     }
 137 
 138     /**
 139      * Gets default instance. Do not use directly: reserved for *.settings files only,
 140      * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
 141      * To obtain the singleton instance, use {@link findInstance}.
 142      */
 143     public static synchronized OutlineTopComponent getDefault() {
 144         if (instance == null) {
 145             instance = new OutlineTopComponent();
 146         }
 147         return instance;
 148     }
 149 
 150     /**
 151      * Obtain the OutlineTopComponent instance. Never call {@link #getDefault} directly!
 152      */
 153     public static synchronized OutlineTopComponent findInstance() {
 154         TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
 155         if (win == null) {
 156             ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot find Outline component. It will not be located properly in the window system.");
 157             return getDefault();
 158         }
 159         if (win instanceof OutlineTopComponent) {
 160             return (OutlineTopComponent) win;
 161         }
 162         ErrorManager.getDefault().log(ErrorManager.WARNING, "There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
 163         return getDefault();
 164     }
 165 
 166     @Override
 167     public int getPersistenceType() {
 168         return TopComponent.PERSISTENCE_ALWAYS;
 169     }
 170 
 171     @Override
 172     public void componentOpened() {
 173         this.requestActive();
 174     }
 175 
 176     @Override
 177     public void componentClosed() {
 178     }
 179 
 180     @Override
 181     protected String preferredID() {
 182         return PREFERRED_ID;
 183     }
 184 
 185     @Override
 186     public void requestActive() {
 187         super.requestActive();
 188         treeView.requestFocus();
 189     }
 190 
 191     @Override
 192     public boolean requestFocus(boolean temporary) {
 193         treeView.requestFocus();
 194         return super.requestFocus(temporary);
 195     }
 196 
 197     @Override
 198     protected boolean requestFocusInWindow(boolean temporary) {
 199         treeView.requestFocus();
 200         return super.requestFocusInWindow(temporary);
 201     }
 202 
 203     @Override
 204     public void resultChanged(LookupEvent lookupEvent) {
 205     }
 206 
 207     @Override
 208     public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {
 209         // Not called when user starts application for the first time
 210         super.readExternal(objectInput);
 211         ((BeanTreeView) this.treeView).setRootVisible(false);
 212     }
 213 
 214     @Override
 215     public void writeExternal(ObjectOutput objectOutput) throws IOException {
 216         super.writeExternal(objectOutput);
 217     }
 218 
 219     static final class ResolvableHelper implements Serializable {
 220 
 221         private static final long serialVersionUID = 1L;
 222 
 223         public Object readResolve() {
 224             return OutlineTopComponent.getDefault();
 225         }
 226     }
 227 
 228     /** This method is called from within the constructor to
 229      * initialize the form.
 230      * WARNING: Do NOT modify this code. The content of this method is
 231      * always regenerated by the Form Editor.
 232      */
 233     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 234     private void initComponents() {
 235 
 236         treeView = new BeanTreeView();
 237 
 238         setLayout(new java.awt.BorderLayout());
 239         add(treeView, java.awt.BorderLayout.CENTER);
 240     }// </editor-fold>//GEN-END:initComponents
 241 
 242     // Variables declaration - do not modify//GEN-BEGIN:variables
 243     private javax.swing.JScrollPane treeView;
 244     // End of variables declaration//GEN-END:variables
 245 }