hotspot/src/share/tools/IdealGraphVisualizer/Coordinator/src/com/sun/hotspot/igv/coordinator/actions/StructuredViewAction.java
changeset 768 d0bebc7eefc2
child 5547 f4b087cbb361
equal deleted inserted replaced
767:64fb1fd7186d 768:d0bebc7eefc2
       
     1 /*
       
     2  * Copyright 1998-2007 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 package com.sun.hotspot.igv.coordinator.actions;
       
    26 
       
    27 import com.sun.hotspot.igv.coordinator.OutlineTopComponent;
       
    28 import com.sun.hotspot.igv.data.services.GroupOrganizer;
       
    29 import java.awt.Component;
       
    30 import java.awt.Image;
       
    31 import java.awt.event.ActionEvent;
       
    32 import java.awt.event.ActionListener;
       
    33 import java.awt.event.ItemEvent;
       
    34 import java.awt.event.ItemListener;
       
    35 import java.awt.image.BufferedImage;
       
    36 import java.util.ArrayList;
       
    37 import java.util.Collection;
       
    38 import java.util.Collections;
       
    39 import java.util.Comparator;
       
    40 import java.util.HashMap;
       
    41 import java.util.List;
       
    42 import java.util.Map;
       
    43 import javax.swing.Action;
       
    44 import javax.swing.ButtonGroup;
       
    45 import javax.swing.ImageIcon;
       
    46 import javax.swing.JButton;
       
    47 import javax.swing.JCheckBoxMenuItem;
       
    48 import javax.swing.JMenuItem;
       
    49 import javax.swing.JPopupMenu;
       
    50 import javax.swing.event.PopupMenuEvent;
       
    51 import javax.swing.event.PopupMenuListener;
       
    52 import org.openide.awt.DropDownButtonFactory;
       
    53 import org.openide.util.HelpCtx;
       
    54 import org.openide.util.Lookup;
       
    55 import org.openide.util.Utilities;
       
    56 import org.openide.util.actions.CallableSystemAction;
       
    57 
       
    58 public class StructuredViewAction extends CallableSystemAction {
       
    59 
       
    60     private static JButton dropDownButton;
       
    61     private static ButtonGroup buttonGroup;
       
    62     private static JPopupMenu popup;
       
    63     private MyMenuItemListener menuItemListener;
       
    64     private Map<JMenuItem, GroupOrganizer> map;
       
    65 
       
    66     public StructuredViewAction() {
       
    67 
       
    68         putValue(Action.SHORT_DESCRIPTION, "Cluster nodes into blocks");
       
    69     }
       
    70 
       
    71     @Override
       
    72     public Component getToolbarPresenter() {
       
    73 
       
    74         Image iconImage = Utilities.loadImage("com/sun/hotspot/igv/coordinator/images/structure.gif");
       
    75         ImageIcon icon = new ImageIcon(iconImage);
       
    76 
       
    77         popup = new JPopupMenu();
       
    78 
       
    79         menuItemListener = new MyMenuItemListener();
       
    80 
       
    81         buttonGroup = new ButtonGroup();
       
    82 
       
    83         Collection<? extends GroupOrganizer> organizersCollection = Lookup.getDefault().lookupAll(GroupOrganizer.class);
       
    84 
       
    85         List<GroupOrganizer> organizers = new ArrayList<GroupOrganizer>(organizersCollection);
       
    86         Collections.sort(organizers, new Comparator<GroupOrganizer>() {
       
    87             public int compare(GroupOrganizer a, GroupOrganizer b) {
       
    88                 return a.getName().compareTo(b.getName());
       
    89             }
       
    90         });
       
    91 
       
    92         map = new HashMap<JMenuItem, GroupOrganizer>();
       
    93 
       
    94         boolean first = true;
       
    95         for(GroupOrganizer organizer : organizers) {
       
    96             JCheckBoxMenuItem item = new JCheckBoxMenuItem(organizer.getName());
       
    97             map.put(item, organizer);
       
    98             item.addActionListener(menuItemListener);
       
    99             buttonGroup.add(item);
       
   100             popup.add(item);
       
   101             if(first) {
       
   102                 item.setSelected(true);
       
   103                 first = false;
       
   104             }
       
   105         }
       
   106 
       
   107         dropDownButton = DropDownButtonFactory.createDropDownButton(
       
   108                 new ImageIcon(
       
   109                 new BufferedImage(32, 32, BufferedImage.TYPE_BYTE_GRAY)),
       
   110                 popup);
       
   111 
       
   112         dropDownButton.setIcon(icon);
       
   113 
       
   114         dropDownButton.setToolTipText("Insert Layer Registration");
       
   115 
       
   116         dropDownButton.addItemListener(new ItemListener() {
       
   117 
       
   118             public void itemStateChanged(ItemEvent e) {
       
   119                 int state = e.getStateChange();
       
   120                 if (state == ItemEvent.SELECTED) {
       
   121                     performAction();
       
   122                 }
       
   123             }
       
   124         });
       
   125 
       
   126         dropDownButton.addActionListener(new ActionListener() {
       
   127             public void actionPerformed(ActionEvent e) {
       
   128                 performAction();
       
   129             }
       
   130         });
       
   131 
       
   132         popup.addPopupMenuListener(new PopupMenuListener() {
       
   133 
       
   134             public void popupMenuCanceled(PopupMenuEvent e) {
       
   135                 dropDownButton.setSelected(false);
       
   136             }
       
   137 
       
   138             public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
       
   139                 dropDownButton.setSelected(false);
       
   140             }
       
   141 
       
   142             public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
       
   143                 dropDownButton.setSelected(true);
       
   144             }
       
   145         });
       
   146 
       
   147         return dropDownButton;
       
   148 
       
   149     }
       
   150 
       
   151     private class MyMenuItemListener implements ActionListener {
       
   152 
       
   153         public void actionPerformed(ActionEvent ev) {
       
   154             JMenuItem item = (JMenuItem) ev.getSource();
       
   155             GroupOrganizer organizer = map.get(item);
       
   156             assert organizer != null : "Organizer must exist!";
       
   157             OutlineTopComponent.findInstance().setOrganizer(organizer);
       
   158         }
       
   159     }
       
   160 
       
   161 
       
   162     @Override
       
   163     public void performAction() {
       
   164         popup.show(dropDownButton, 0, dropDownButton.getHeight());
       
   165     }
       
   166 
       
   167     public String getName() {
       
   168         return "Structured View";
       
   169     }
       
   170 
       
   171     public HelpCtx getHelpCtx() {
       
   172         return HelpCtx.DEFAULT_HELP;
       
   173     }
       
   174 
       
   175     @Override
       
   176     protected boolean asynchronous() {
       
   177         return false;
       
   178     }
       
   179 
       
   180 }