Advertisement
Guest User

Untitled

a guest
Nov 11th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.87 KB | None | 0 0
  1. /**
  2.  * @(#)JDisclosureToolBar.java  1.0  April 12, 2008
  3.  *
  4.  * Copyright (c) 2008 by the original authors of JHotDraw
  5.  * and all its contributors.
  6.  * All rights reserved.
  7.  *
  8.  * The copyright of this software is owned by the authors and  
  9.  * contributors of the JHotDraw project ("the copyright holders").  
  10.  * You may not use, copy or modify this software, except in  
  11.  * accordance with the license agreement you entered into with  
  12.  * the copyright holders. For details see accompanying license terms.
  13.  */
  14. package org.jhotdraw.gui;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import javax.swing.*;
  19. import org.jhotdraw.gui.plaf.palette.*;
  20.  
  21. /**
  22.  * A ToolBar with disclosure functionality.
  23.  *
  24.  * @author Werner Randelshofer
  25.  * @version 1.0 JDisclosureToolBar Created.
  26.  */
  27. public class JDisclosureToolBar extends JToolBar {
  28.  
  29.     private JButton disclosureButton;
  30.     public final static String DISCLOSURE_STATE_PROPERTY = "disclosureState";
  31.     public final static String DISCLOSURE_STATE_COUNT_PROPERTY = "disclosureStateCount";
  32.  
  33.     /** Creates new form. */
  34.     public JDisclosureToolBar() {
  35.         setUI(PaletteToolBarUI.createUI(this));
  36.         initComponents();
  37.     }
  38.  
  39.     private void initComponents() {
  40.         GridBagConstraints gbc;
  41.         AbstractButton btn;
  42.         setLayout(new GridBagLayout());        
  43.  
  44.         gbc = new GridBagConstraints();
  45.         if (disclosureButton == null) {
  46.             btn = new JButton();
  47.             btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn));
  48.             btn.setBorderPainted(false);
  49.             btn.setIcon(new DisclosureIcon());
  50.             btn.setOpaque(false);
  51.             disclosureButton = (JButton) btn;
  52.             disclosureButton.putClientProperty(DisclosureIcon.CURRENT_STATE_PROPERTY, 1);
  53.             disclosureButton.putClientProperty(DisclosureIcon.STATE_COUNT_PROPERTY, 2);
  54.             disclosureButton.addActionListener(new ActionListener() {
  55.                
  56.  
  57.                 public void actionPerformed(ActionEvent e) {
  58.                     int newState = ((Integer) disclosureButton.getClientProperty(DisclosureIcon.CURRENT_STATE_PROPERTY) + 1) %
  59.                             (Integer) disclosureButton.getClientProperty(DisclosureIcon.STATE_COUNT_PROPERTY);
  60.                     setDisclosureState(newState);
  61.                 }
  62.             });
  63.         } else {
  64.             btn = disclosureButton;
  65.         }
  66.  
  67.         gbc.gridx = 0;
  68.         gbc.insets = new Insets(0, 1, 0, 1);
  69.         gbc.anchor = GridBagConstraints.SOUTHWEST;
  70.         gbc.fill = GridBagConstraints.NONE;
  71.         gbc.weighty = 1d;
  72.         gbc.weightx = 1d;
  73.         add(btn, gbc);
  74.  
  75.         putClientProperty(PaletteToolBarUI.TOOLBAR_INSETS_OVERRIDE_PROPERTY, new Insets(0, 0, 0, 0));
  76.         putClientProperty(PaletteToolBarUI.TOOLBAR_ICON_PROPERTY, new EmptyIcon(10, 8));
  77.     }
  78.  
  79.     public void setDisclosureStateCount(int newValue) {
  80.         int oldValue = getDisclosureStateCount();
  81.         disclosureButton.putClientProperty(DisclosureIcon.STATE_COUNT_PROPERTY, newValue);
  82.         firePropertyChange(DISCLOSURE_STATE_COUNT_PROPERTY, oldValue, newValue);
  83.     }
  84.  
  85.     public void setDisclosureState(int newValue) {
  86.         int oldValue = getDisclosureState();
  87.         disclosureButton.putClientProperty(DisclosureIcon.CURRENT_STATE_PROPERTY, newValue);
  88.  
  89.         removeAll();
  90.         JComponent c = getDisclosedComponent(newValue);
  91.         GridBagLayout layout = (GridBagLayout) getLayout();
  92.         GridBagConstraints gbc = new GridBagConstraints();
  93.         if (c != null) {
  94.             gbc = new GridBagConstraints();
  95.             gbc.gridx = 1;
  96.             gbc.weightx = 1d;
  97.             gbc.weighty = 1d;
  98.             gbc.fill = GridBagConstraints.BOTH;
  99.             gbc.anchor = GridBagConstraints.WEST;
  100.             add(c, gbc);  
  101.             gbc = new GridBagConstraints();
  102.             gbc.gridx = 0;
  103.             gbc.weightx = 0d;
  104.             gbc.insets = new Insets(0, 1, 0, 1);
  105.             gbc.weighty = 1d;
  106.             gbc.fill = GridBagConstraints.NONE;
  107.             gbc.anchor = GridBagConstraints.SOUTHWEST;
  108.             add(disclosureButton, gbc);
  109.         } else {
  110.             gbc = new GridBagConstraints();
  111.             gbc.gridx = 1;
  112.             gbc.weightx = 1d;
  113.             gbc.weighty = 1d;
  114.             gbc.fill = GridBagConstraints.NONE;
  115.             gbc.anchor = GridBagConstraints.SOUTHWEST;
  116.             gbc.insets = new Insets(0, 1, 0, 1);
  117.             add(disclosureButton, gbc);
  118.         }
  119.  
  120.         invalidate();
  121.         Container parent = getParent();
  122.         parent.setBackground(Color.red);
  123.         while (parent.getParent() != null && !parent.getParent().isValid()) {
  124.             parent = parent.getParent();
  125.         }
  126.         parent.validate();
  127.         repaint();
  128.  
  129.         firePropertyChange(DISCLOSURE_STATE_PROPERTY, oldValue, newValue);
  130.     }
  131.  
  132.     public int getDisclosureStateCount() {
  133.         Integer value = (Integer) disclosureButton.getClientProperty(DisclosureIcon.STATE_COUNT_PROPERTY);
  134.         return (value == null) ? 2 : value;
  135.     }
  136.  
  137.     public int getDisclosureState() {
  138.         Integer value = (Integer) disclosureButton.getClientProperty(DisclosureIcon.CURRENT_STATE_PROPERTY);
  139.         return (value == null) ? 1 : value;
  140.     }
  141.  
  142.     protected JComponent getDisclosedComponent(int state) {
  143.         return new JLabel(Integer.toString(state));
  144.     }
  145.     /** This method is called from within the constructor to
  146.      * initialize the form.
  147.      * WARNING: Do NOT modify this code. The content of this method is
  148.      * always regenerated by the Form Editor.
  149.      * /
  150.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  151.     private void initComponents() {
  152.     }// </editor-fold>                        
  153.      */
  154.     // Variables declaration - do not modify                    
  155.     // End of variables declaration                  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement