All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.netbeans.modules.palette.ui.PalettePanel Maven / Gradle / Ivy

There is a newer version: RELEASE240
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.netbeans.modules.palette.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import org.netbeans.modules.palette.Category;
import org.netbeans.modules.palette.Item;
import org.netbeans.modules.palette.Model;
import org.netbeans.modules.palette.ModelListener;
import org.netbeans.modules.palette.Settings;
import org.netbeans.modules.palette.Utils;
import org.netbeans.spi.palette.PaletteController;
import org.openide.util.Utilities;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.ScrollPaneLayout;
import javax.swing.Scrollable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;



/**
 * Palette's visual component implementation.
 *
 * @author S. Aubrecht
 */
public class PalettePanel extends JPanel implements Scrollable {

    private static PalettePanel theInstance;
    
    private PaletteController controller;
    private Model model;
    private Settings settings;
    
    private ModelListener modelListener;
    private PropertyChangeListener settingsListener;
    
    private CategoryDescriptor[] descriptors = new CategoryDescriptor[0];
    private Category selectedCategory;
    
    private final Object lock = new Object();
    private MouseListener mouseListener;
    
    private static final boolean isAquaLaF = "Aqua".equals(UIManager.getLookAndFeel().getID()); //NOI18N

    private MyScrollPane scrollPane;
    
    private DnDSupport dndSupport;

    private final KeyListener kl;

    private PalettePanel () {
        setLayout( new PaletteLayoutManager() );
        addMouseListener( mouseListener() );
        kl = new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                int keyCode = e.getKeyCode();

                if (keyCode == KeyEvent.VK_ESCAPE) {
                    if( removeSearchField() ) {
                        e.consume();
                    }
                }
            }

            @Override
            public void keyTyped(KeyEvent e) {
                if( !e.isConsumed() && e.getKeyChar() != KeyEvent.VK_ESCAPE )
                   displaySearchField(e.getKeyChar());
            }
        };

        if (!GraphicsEnvironment.isHeadless()) {
            dndSupport = new DnDSupport(this);
        }

        if( "Aqua".equals(UIManager.getLookAndFeel().getID()) )
            setBackground( UIManager.getColor("NbExplorerView.background") ); //NOI18N
        else
            setBackground( UIManager.getColor ("Panel.background") );
        
        addKeyListener(kl);

        SearchFieldListener searchFieldListener = new SearchFieldListener();
        searchTextField.addKeyListener(searchFieldListener);
        searchTextField.addFocusListener(searchFieldListener);
        searchTextField.getDocument().addDocumentListener(searchFieldListener);
    }
    
    public static synchronized PalettePanel getDefault() {
        if( null == theInstance ) {
            theInstance = new PalettePanel();
        }
        return theInstance;
    }
    
    public JScrollPane getScrollPane() {
        if( null == scrollPane ) {
            scrollPane = new MyScrollPane( this );
        }
        return scrollPane;
    }

    CategoryDescriptor getCategoryDescriptor( Category category ) {
        for( int i=0; i newDescriptors = new ArrayList(categories.length);
        for( int i=0; i tmp = new ArrayList( cats.length );
        for( int i=0; i 0 ) {
            boolean isAnyCategoryOpened = false;
            for( int i=0; i 0 ) {
                        PalettePanel.this.scrollToCategory(addedCategories[0] );
                    }
                }

                @Override
                public void categoriesRemoved( Category[] removedCategories ) {
                    PalettePanel.this.refresh();
                }

                @Override
                public void categoriesReordered() {
                    PalettePanel.this.refresh();
                }
                
                @Override
                public void propertyChange( PropertyChangeEvent evt ) {
                    if( ModelListener.PROP_SELECTED_ITEM.equals( evt.getPropertyName() ) ) {
                        Item selectedItem = model.getSelectedItem();
                        Category selectedCategory = model.getSelectedCategory();
                        setSelectedItemFromModel( selectedCategory, selectedItem );
                    }
                }

            };
        } 
        return modelListener;
    }
    
    private PropertyChangeListener getSettingsListener() {
        if( null == settingsListener ) {
            settingsListener = new PropertyChangeListener() {
                @Override
                public void propertyChange( PropertyChangeEvent evt ) {
                    if( PaletteController.ATTR_IS_VISIBLE.equals( evt.getPropertyName() ) ) {
                        PalettePanel.this.refresh();
                        for( int i=0; null != descriptors && i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy