View Javadoc

1   package com.explosion.expf.menusandtools.tool;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.awt.Dimension;
23  import java.awt.Image;
24  import java.util.Vector;
25  
26  import javax.swing.ImageIcon;
27  import javax.swing.JButton;
28  import javax.swing.JToolBar;
29  
30  import com.explosion.expf.Application;
31  import com.explosion.expf.ExpConstants;
32  import com.explosion.expf.ExpListener;
33  import com.explosion.utilities.FileSystemUtils;
34  
35  /***
36   *
37   * @author  stephen
38   */
39  public class ExpToolBar extends JToolBar
40  {
41    private boolean firstTaken = false;
42    private boolean lastTaken = false;
43    private Vector segments = new Vector();
44    private Vector toolBarItems = new Vector();
45    private ExpListener listener = null;
46    private static final int BORDER = 6;
47    
48    /*** Creates a new instance of ExpToolbar */
49    public ExpToolBar(ExpListener listener)
50    {
51      super();
52      try
53      {
54        this.listener = listener;
55        
56        /* Set the name of this tool bar */
57        this.setName(ExpConstants.COMPNAME_EXPTOOLBAR);
58      }
59      catch (Exception e)
60      {
61        com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while creating main toolbar.");
62      }
63    }
64  
65    /***
66     * This mthod runs through the elements in the toolButtons vector and
67     * inserts them into the actual toolbar.
68     */
69    public void constructToolBar()
70    {
71      for (int i = 0; i < toolBarItems.size(); i++)
72      {
73        if (toolBarItems.elementAt(i) instanceof JButton)
74        {
75          JButton toolButton = (JButton) toolBarItems.elementAt(i);
76          this.add(toolButton);
77        }
78        else
79        {
80          this.addSeparator(new Dimension(20,20));
81        }
82      }
83    }
84  
85    /***
86     * This method runs through the list of menu's and their children and asks each of them to enable or diable themselves
87     */
88    public void checkEnabled()
89    {
90      try
91      {
92        for (int i = 0; i < toolBarItems.size(); i++)
93        {
94          if (toolBarItems.elementAt(i) instanceof ExpToolBarItem)
95          {
96            ExpToolBarItem item = (ExpToolBarItem) toolBarItems.elementAt(i);
97            item.checkEnabled(Application.getActiveComponentCookies(), Application.getLocalCookies(),Application.getGlobalCookies());
98          }
99        }
100     }
101     catch (Exception e)
102     {
103       com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while checking toolBarItems to see if they should be enabled or not.");
104     }
105   }
106 
107   /***
108    * Adds a new button to the toolBar
109    */
110   public ExpToolBarItem createExpToolBarItem(String imageLocation, String toolTipText, String actionCommand, int enabledThreshold) throws Exception
111   {
112     Image ic = FileSystemUtils.loadImage(imageLocation, this);
113     ExpToolBarItem toolButton = new ExpToolBarItem(actionCommand,enabledThreshold);
114     ImageIcon icon = new ImageIcon(ic);
115     toolButton.setIcon(icon);
116     toolButton.setMaximumSize(new Dimension(icon.getIconWidth()+BORDER,icon.getIconHeight()+BORDER));
117 	toolButton.setPreferredSize(new Dimension(icon.getIconWidth()+BORDER,icon.getIconHeight()+BORDER));
118 	toolButton.setMinimumSize(new Dimension(icon.getIconWidth()+BORDER,icon.getIconHeight()+BORDER));
119 	toolButton.setToolTipText(toolTipText);
120     toolButton.setActionCommand(actionCommand);
121     toolButton.addActionListener(listener);
122     return toolButton;
123   }
124 
125   /***
126    * Creates a new segment in this menu
127    */
128   public ExpToolBarSegment createNewSegment(int relativePositionOfSegmentOnMenu) throws Exception
129   {
130     ExpToolBarSegment segment = null;
131     if (relativePositionOfSegmentOnMenu == ExpToolBarSegment.ALWAYS_FIRST_SEGMENT)
132     {
133       if (firstTaken)
134         throw new ToolBarException("Relative position ALWAYS_FIRST_SEGMENT is already being used on menu " + this.getName() + ".");
135 
136       firstTaken = true;
137       segment = new ExpToolBarSegment(0, this, toolBarItems, relativePositionOfSegmentOnMenu);
138       if (segments.size() == 0)
139         segments.addElement(segment);
140       else
141         segments.insertElementAt(segment, 0);
142     }
143     else if (relativePositionOfSegmentOnMenu == ExpToolBarSegment.ANY_POSITION)
144     {
145       int segmentStartPos = 0;
146 
147       if (segments.size() != 0)
148       {
149         if (lastTaken) // use the startIndex of the last one
150           segmentStartPos = ((ExpToolBarSegment) segments.elementAt(segments.size() - 1)).getStartIndex();
151         else //otherwise just pop it on the end
152           segmentStartPos = toolBarItems.size() - 1;
153       }
154 
155       segment = new ExpToolBarSegment(segmentStartPos, this, toolBarItems, relativePositionOfSegmentOnMenu);
156 
157       if (segments.size() == 0 || !lastTaken)
158         segments.addElement(segment);
159       else
160         segments.insertElementAt(segment, segments.size() - 1);
161     }
162     else if (relativePositionOfSegmentOnMenu == ExpToolBarSegment.ALWAYS_LAST_SEGMENT)
163     {
164       if (lastTaken)
165         throw new ToolBarException("Relative position ALWAYS_LAST_SEGMENT is already being used on menu " + this.getName() + ".");
166 
167       lastTaken = true;
168       int segmentStartPos = 0;
169 
170       if (segments.size() != 0)
171         segmentStartPos = toolBarItems.size() - 1;
172 
173       segment = new ExpToolBarSegment(segmentStartPos, this, toolBarItems, relativePositionOfSegmentOnMenu);
174       segments.addElement(segment); // always add it last
175     }
176     return segment;
177   }
178 
179   /***
180    * Updates all of the start positions after this segment
181    */
182   public void updateStartPositions(ExpToolBarSegment segment, int numberOfPositions)
183   {
184     boolean found = false;
185     for (int i = 0; i < segments.size(); i++)
186     {
187       if (!found && segment == segments.elementAt(i))
188       {
189         found = true;
190       }
191       else
192       {
193         ((ExpToolBarSegment) segments.elementAt(i)).modifyStartPos(numberOfPositions);
194       }
195     }
196   }
197 
198 
199 }