/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package DisplayProject.factory;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.SystemColor;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import DisplayProject.ActivateActionListener;
import DisplayProject.Constants;
import DisplayProject.actions.WidgetState;
import DisplayProject.plaf.ForteMenuLayout;
import Framework.TextData;
/**
* this is a factory class for all menu components
*
*/
public class MenuFactory {
public class qq_Resolver {
public static final int cNAME = 1;
public static final int cTEXT = 2;
public static final int cNAME_TEXT = 3;
public static final int cTEXT_NAME = 4;
}
private MenuFactory(){
}
public static JCheckBoxMenuItem newMenuToggle(){
JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem();
return cbmi;
}
public static JCheckBoxMenuItem newMenuToggle(TextData name){
return newMenuToggle(name.toString());
}
public static JCheckBoxMenuItem newMenuToggle(String name){
JCheckBoxMenuItem cbmi = newMenuToggle();
cbmi.setName(name);
return cbmi;
}
public static ButtonGroup newMenuList(){
ButtonGroup bg = new ButtonGroup();
return bg;
}
//------------------------
// JMenuItem (MenuCommand)
//------------------------
public static JMenuItem newMenuItem(){
return new JMenuItem();
}
public static JMenuItem newMenuItem(String Name, boolean isInputFinalized){
JMenuItem mi = newMenuItem();
mi.setName(Name);
//mi.addActionListener(new ActivateActionListener()); PM:17/9/07 replaced with client event
mi.putClientProperty("isInputFinalized", new Boolean(isInputFinalized));
return mi;
}
public static JMenuItem newMenuItem(TextData first, TextData second, int resolver){
return newMenuItem(first.toString(), second.toString(), resolver);
}
public static JMenuItem newMenuItem(String first, String second, int resolver){
JMenuItem mi = newMenuItem();
switch (resolver){
case qq_Resolver.cNAME_TEXT:
mi.setName(first);
mi.setText(second);
break;
case qq_Resolver.cTEXT_NAME:
mi.setText(first);
mi.setName(second);
break;
}
return mi;
}
public static JMenuItem newMenuItem(TextData value, int resolver){
return newMenuItem(value.toString(), resolver);
}
public static JMenuItem newMenuItem(String value, int resolver){
JMenuItem mi = newMenuItem();
switch (resolver){
case qq_Resolver.cNAME:
mi.setName(value);
break;
case qq_Resolver.cTEXT:
mi.setText(value);
break;
}
return mi;
}
//---------------------
// JRadioButtonMenuItem
//---------------------
public static JRadioButtonMenuItem newRadioMenuItem(String name, ButtonGroup bg, int value){
JRadioButtonMenuItem rbmi = new JRadioButtonMenuItem(name);
bg.add(rbmi);
rbmi.putClientProperty("qq_bg", bg);
rbmi.putClientProperty("qq_value", new Integer(value));
return rbmi;
}
//---------------------
// JMenu (SubMenu)
//---------------------
public static JMenu newMenu(TextData value, int resolver){
return newMenu(value.toString(), resolver);
}
public static JMenu newMenu(String value, int resolver){
JMenu mi = newMenu();
switch (resolver){
case qq_Resolver.cNAME:
mi.setName(value);
break;
case qq_Resolver.cTEXT:
mi.setText(value);
break;
}
return mi;
}
@SuppressWarnings("serial")
public static JMenu newMenu(){
JMenu jm = new JMenu(){
public JMenuItem add(JMenuItem menuItem) {
JMenuItem jmi = super.add(menuItem);
menuItem.addActionListener(new ActivateActionListener());
return jmi;
}
};
return jm;
}
// FIX:TF:9 Sep 2009:Added a new menu option
@SuppressWarnings("serial")
public static JPopupMenu newPopupMenu(){
JPopupMenu jm = new JPopupMenu(){
public JMenuItem add(JMenuItem menuItem) {
JMenuItem jmi = super.add(menuItem);
menuItem.addActionListener(new ActivateActionListener());
return jmi;
}
};
return jm;
}
//---------------------
// JMenuBar (MenuBar)
//---------------------
@SuppressWarnings("serial")
private static class MenuBar extends JMenuBar implements ComponentListener {
public MenuBar() {
this.setLayout(new ForteMenuLayout()); //PM:18/07/2008: to allow for wrap menus
this.setBackground(null);
}
@Override
public JMenu add(JMenu menu) {
// If we set the background colour of the menu to null, like we really
// want to at this point, it gets overriden by the UI. We will just
// hard-code the control colour for the moment and fix it up when needed.
menu.setBackground(SystemColor.control);
super.add(menu);
menu.addComponentListener(this);
return menu;
}
@Override
protected void paintBorder(Graphics g) {
g.setColor(Color.white);
g.drawLine(0, getHeight()-1, getWidth(), getHeight()-1);
}
/**
* Forte would hide the menu bar if there were no items in the menu bar to show.
* This method mirrors that functionality and is called from the component listeners
* on the visibility of the children.
*/
private void determineVisibility() {
int count = this.getMenuCount();
// We should be visible iff:
// 1) We are not explicitly invisible
// 2) We have children
// 3) Not all of our children are invisible
boolean shouldBeVisible = WidgetState.get(this) != Constants.MS_INVISIBLE && count > 0;
if (shouldBeVisible) {
shouldBeVisible = false;
for (int i = 0; i < count; i++) {
JMenu menu = this.getMenu(i);
if (menu.isVisible()) {
shouldBeVisible = true;
break;
}
}
}
super.setVisible(shouldBeVisible);
}
@Override
public void setVisible(boolean pVisible) {
this.determineVisibility();
}
public void componentHidden(ComponentEvent arg0) {
this.determineVisibility();
}
public void componentShown(ComponentEvent arg0) {
this.determineVisibility();
}
public void componentMoved(ComponentEvent arg0) {}
public void componentResized(ComponentEvent arg0) {}
}
public static JMenuBar newMenuBar(){
JMenuBar jmb = new MenuBar();
return jmb;
}
}