1 /*
   2  * Copyright (c) 2000, 2008, 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,
  20  * CA 94065 USA or visit www.oracle.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 
  26 package com.sun.java.swing.ui;
  27 
  28 import com.sun.java.swing.action.ActionManager;
  29 import com.sun.java.swing.action.StateChangeAction;
  30 import java.awt.Dimension;
  31 import java.awt.Insets;
  32 import javax.swing.*;
  33 
  34 // Referenced classes of package com.sun.java.swing.ui:
  35 //            ToggleActionPropertyChangeListener, StatusBar, CommonUI
  36 
  37 public abstract class CommonToolBar extends JToolBar
  38 {
  39 
  40     protected CommonToolBar(ActionManager manager)
  41     {
  42         this(manager, StatusBar.getInstance());
  43     }
  44 
  45     protected CommonToolBar(ActionManager manager, StatusBar status)
  46     {
  47         this.manager = manager;
  48         statusBar = status;
  49         buttonSize = new Dimension(CommonUI.buttconPrefSize);
  50         buttonInsets = new Insets(0, 0, 0, 0);
  51         addComponents();
  52     }
  53 
  54     protected abstract void addComponents();
  55 
  56     protected void addButton(Action action)
  57     {
  58         javax.swing.JButton button = add(action);
  59         configureButton(button, action);
  60     }
  61 
  62     protected void addToggleButton(StateChangeAction a)
  63     {
  64         addToggleButton(a, null);
  65     }
  66 
  67     protected void addToggleButton(StateChangeAction a, ButtonGroup group)
  68     {
  69         JToggleButton button = new JToggleButton(a);
  70         button.addItemListener(a);
  71         button.setSelected(a.isSelected());
  72         if(group != null)
  73             group.add(button);
  74         add(button);
  75         configureToggleButton(button, a);
  76     }
  77 
  78     protected void configureToggleButton(JToggleButton button, Action action)
  79     {
  80         configureButton(button, action);
  81         action.addPropertyChangeListener(new ToggleActionPropertyChangeListener(button));
  82     }
  83 
  84     protected void configureButton(AbstractButton button, Action action)
  85     {
  86         button.setToolTipText((String)action.getValue("Name"));
  87         button.setText("");
  88         button.addMouseListener(statusBar);
  89     }
  90 
  91     protected ActionManager manager;
  92     private Dimension buttonSize;
  93     private Insets buttonInsets;
  94     private StatusBar statusBar;
  95 }