Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   * Other names may be trademarks of their respective owners.
   *
   * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, the
  * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Oracle in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * Contributor(s):
  * The Original Software is NetBeans. The Initial Developer of the Original
  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  * Microsystems, Inc. All Rights Reserved.
  *
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  */
 
 package org.netbeans.modules.profiler.ppoints.ui;
 
 import java.awt.Font;
 import java.awt.Point;

Author(s):
Jiri Sedlacek
 
     "ToggleProfilingPointAction_ActionName=Toggle Profiling Point",
     "ToggleProfilingPointAction_ActionDescr=Toggles Profiling Point",
     "ToggleProfilingPointAction_ProfilingProgressMsg=Cannot create new Profiling Point during profiling session.",
     "ToggleProfilingPointAction_BadSourceMsg=Profiling Points cannot be created in this source file.",
     "ToggleProfilingPointAction_CancelString=Cancel",
     "ToggleProfilingPointAction_SwitcherWindowCaption=New Profiling Point",
    "ToggleProfilingPointAction_InvalidShortcutMsg=<html><b>Invalid shortcut assigned to {0} action.</b><br><br>Make sure that exactly one shortcut is assigned to the action<br>and at least one modifier key is used in the shortcut.</html>"
})
public class ToggleProfilingPointAction extends AbstractAction implements AWTEventListener {
    //~ Inner Classes ------------------------------------------------------------------------------------------------------------
    private static class ProfilingPointsSwitcher extends JFrame {
        //~ Static fields/initializers -------------------------------------------------------------------------------------------
        private static final String NO_ACTION_NAME = Bundle.ToggleProfilingPointAction_CancelString();
        private static final Icon NO_ACTION_ICON = null;
        //~ Instance fields ------------------------------------------------------------------------------------------------------
        private Dimension size;
        private JLabel label;
        private JPanel previewPanel;
        private ProfilingPointFactory ppFactory;
        //~ Constructors ---------------------------------------------------------------------------------------------------------
        public ProfilingPointsSwitcher() {
            super(Bundle.ToggleProfilingPointAction_SwitcherWindowCaption());
            initProperties();
            initComponents();
            setProfilingPointFactory(null, -1);
        }
        //~ Methods --------------------------------------------------------------------------------------------------------------
        public void setProfilingPointFactory(ProfilingPointFactory ppFactoryint index) {
            this. = ppFactory;
            if (ppFactory != null) {
                .setText(ppFactory.getType());
                .setIcon(ppFactory.getIcon());
            } else {
                .setText();
                .setIcon();
            }
            Component selected = null;
            if ((index >= 0) && (index < .getComponentCount())) {
                selected = .getComponent(index);
            }
            for (Component c : .getComponents()) {
                if (c == selected) {
                    Border empt1 = BorderFactory.createEmptyBorder(2, 2, 2, 2);
                    Border sel = BorderFactory.createMatteBorder(1, 1, 1, 1, .);
                    Border empt2 = BorderFactory.createEmptyBorder(0, 2, 0, 2);
                    Border comp1 = BorderFactory.createCompoundBorder(empt2sel);
                    Border comp2 = BorderFactory.createCompoundBorder(comp1empt1);
                    ((JComponentc).setBorder(comp2);
                } else {
                    ((JComponentc).setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
                }
            }
        }
            return ;
        }
        public void setVisible(boolean visible) {
            if (visible) {
                if ( == null) {
                     = getSize();
                }
                TopComponent editor = WindowManager.getDefault().getRegistry().getActivated();
                Rectangle b = editor.getBounds();
                Point location = new Point((b.x + (b.width / 2)) - (. / 2), (b.y + (b.height / 2)) - (. / 2));
                SwingUtilities.convertPointToScreen(locationeditor);
                setLocation(location);
            }
            super.setVisible(visible);
        }
        private void initComponents() {
            setLayout(new BorderLayout());
             = new JPanel(new FlowLayout(0, 0, .));
            .setBorder(BorderFactory.createEmptyBorder(4, 7, 2, 7));
             = new JLabel();
            .setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2, 7, 7, 7),
                                                               new ThinBevelBorder(.)));
            .setBorder(BorderFactory.createCompoundBorder(.getBorder(), BorderFactory.createEmptyBorder(4, 3, 4, 3)));
            .setFont(.getFont().deriveFont(.));
            JPanel p = new JPanel(new BorderLayout());
            p.setBorder(BorderFactory.createRaisedBevelBorder());
            p.add(.);
            p.add(.);
            add(p.);
        }
        private void initPanel(ProfilingPointFactory[] ppFactories) {
            Dimension prefSize = new Dimension(230, 0);
            for (int i = 0; i < ppFactories.lengthi++) {
                JLabel previewIcon = new JLabel(ppFactories[i].getIcon());
                previewIcon.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
                .add(previewIcon);
                setProfilingPointFactory(ppFactories[i], i);
                pack();
                Dimension currPrefSize = getPreferredSize();
                prefSize = new Dimension(Math.max(prefSize.widthcurrPrefSize.width),
                                         Math.max(prefSize.heightcurrPrefSize.height));
            }
            setProfilingPointFactory(nullppFactories.length);
            setSize(prefSize);
        }
        private void initProperties() {
            setAlwaysOnTop(true);
            setUndecorated(true);
            setResizable(false);
            WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
                public void run() { setIconImage(WindowManager.getDefault().getMainWindow().getIconImage()); }
            });
        }
    }
    //~ Static fields/initializers -----------------------------------------------------------------------------------------------
    
    private static ToggleProfilingPointAction instance;
    //~ Instance fields ----------------------------------------------------------------------------------------------------------
    volatile private boolean warningDialogOpened = false;
    private int currentFactory;
    //~ Constructors -------------------------------------------------------------------------------------------------------------
    public ToggleProfilingPointAction() {
        putValue("noIconInMenu".); // NOI18N
    }
    
    public static synchronized ToggleProfilingPointAction getInstance() {
        if ( == null = new ToggleProfilingPointAction();
        return ;
    }
    //~ Methods ------------------------------------------------------------------------------------------------------------------
    public boolean isEnabled() {
        return true;
    }

    
Invoked when an action occurs.
    public void actionPerformed(final ActionEvent e) {
         = Utilities.stringToKey(e.getActionCommand());
        if ( == null || .getModifiers() == 0) {
            ProfilerDialogs.displayError(
                    Bundle.ToggleProfilingPointAction_InvalidShortcutMsg(
                    Bundle.ToggleProfilingPointAction_ActionName()));
            return;
        }
        
        if () {
            return;
        }
        if (ProfilingPointsManager.getDefault().isProfilingSessionInProgress()) {
             = true;
            ProfilerDialogs.displayWarning(
                    Bundle.ToggleProfilingPointAction_ProfilingProgressMsg());
             = false;
            return;
        }
        if (Utils.getCurrentLocation(0).equals(..)) {
             = true;
            ProfilerDialogs.displayWarning(
                    Bundle.ToggleProfilingPointAction_BadSourceMsg());
             = false;
            return;
        }
        
        ProfilingPointsSwitcher chooserFrame = getChooserFrame();
        
        boolean toggleOff = false;
        for(CodeProfilingPoint pp : ProfilingPointsManager.getDefault().getProfilingPoints(CodeProfilingPoint.class, Utils.getCurrentProject(), falsefalse)) {
            if (location.equals(pp.getLocation())) {
                ProfilingPointsManager.getDefault().removeProfilingPoint(pp);
                toggleOff = true;
            }
        }
        if (!toggleOff) {
            if (chooserFrame.isVisible()) {
                nextFactory();
                chooserFrame.setProfilingPointFactory(( == .) ? null : [],
                                                      );
            } else {
                if (EditorSupport.currentlyInJavaEditor()) {
                    resetFactories();
                    chooserFrame.setProfilingPointFactory(( == .) ? null : [],
                                                          );
                    Toolkit.getDefaultToolkit().addAWTEventListener(this.);
                    chooserFrame.setVisible(true);
                }
            }
        }
    }
    public void eventDispatched(AWTEvent event) {
        if (!(event instanceof KeyEvent)) return;
        
        KeyStroke eventKeyStroke = KeyStroke.getKeyStrokeForEvent((KeyEvent)event);
        if ( == null || eventKeyStroke == nullreturn;
        
        int acceleratorModifiers = .getModifiers();
        if (acceleratorModifiers == 0) return;
        
        if (acceleratorModifiers != eventKeyStroke.getModifiers()) modifierKeyStateChanged();
    }
    
    private synchronized ProfilingPointsSwitcher getChooserFrame() {
        if ( == null) {
             = new ProfilingPointsSwitcher();
            .addWindowListener(new WindowAdapter() {
                public void windowDeactivated(WindowEvent event) {
                    .setVisible(false);
                }
            });
        }
        
        return ;
    }
    private synchronized void modifierKeyStateChanged() {
        if (ProfilingPointsManager.getDefault().isProfilingSessionInProgress()) {
            return;
        }
        ProfilingPointsSwitcher chooserFrame = getChooserFrame();
        if (chooserFrame.isVisible()) {
            ProfilingPointFactory ppFactory = chooserFrame.getProfilingPointFactory();
            Lookup.Provider project = Utils.getCurrentProject();
            if ((ppFactory != null) && (project != null)) {
                ProfilingPoint ppoint = ppFactory.create(project);
                if (ppoint != null) {
                    ProfilingPointsManager.getDefault().addProfilingPoint(ppoint);
                    if (ppoint instanceof GlobalProfilingPoint) {
                        SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                    if (!ProfilingPointsWindow.getDefault().isOpened()) {
                                        ProfilingPointsWindow.getDefault().open();
                                        ProfilingPointsWindow.getDefault().requestVisible();
                                    }
                                }
                            });
                    }
                    ppoint.customize(truetrue);
                }
            }
        }
        Toolkit.getDefaultToolkit().removeAWTEventListener(this);
        chooserFrame.setVisible(false);
    }
    private void nextFactory() {
        ++;
        if ( > .) {
             = 0;
        }
    }
    private void resetFactories() {
        if ( == null) {
             = ProfilingPointsManager.getDefault().getProfilingPointFactories();
            getChooserFrame().initPanel();
        }
         = 0;
    }
New to GrepCode? Check out our FAQ X