import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import <BLA>.client.gui.GuiFactory;
import <BLA>.client.gui.utils.GuiConstants;
import <BLA>.common.client.res.ClientResources;
@SuppressWarnings("serial")
public class ToolbarView extends JPanel {
public ToolbarView(boolean inWithRigthArea) {
initialize(inWithRigthArea);
}
public ToolbarView() {
initialize(true);
}
private void initialize(boolean inWithRigthArea) {
this.setOpaque(GuiConstants.OPAQUE);
this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
this.setLayout(new BorderLayout());
mLeftArea = new JPanel();
mLeftArea.setOpaque(GuiConstants.OPAQUE);
mLeftArea.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(mLeftArea, BorderLayout.CENTER);
if(inWithRigthArea) {
mRightArea = new JPanel();
mRightArea.setOpaque(GuiConstants.OPAQUE);
mRightArea.setLayout(new FlowLayout(FlowLayout.RIGHT));
mBtnNavigator = new JButton();
ImageIcon aIcon = ClientResources.getImage("img.btn.home");
mBtnNavigator.setIcon(aIcon);
mBtnNavigator.setToolTipText(ClientResources.getRes("tooltip.btn.home"));
mBtnHelp = new JButton();
aIcon = ClientResources.getImage("img.btn.help");
mBtnHelp.setIcon(aIcon);
mBtnHelp.setToolTipText(ClientResources.getRes("tooltip.btn.help"));
mRightArea.add(mBtnNavigator);
mRightArea.add(mBtnHelp);
this.add(mRightArea, BorderLayout.EAST);
ActionListener aActionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == mBtnNavigator) {
GuiFactory.getProcessCtrl().navigationGoNavigation();
} else if(e.getSource() == mBtnHelp) {
if(mHelpContext != null) {
GuiFactory.getProcessCtrl().navigationGoHelp(mHelpContext);
}
}
}
};
mBtnNavigator.addActionListener(aActionListener);
mBtnHelp.addActionListener(aActionListener);
}
mActionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEvent(e);
}
};
}
/**
* { COMMAND , ToolbarButtonType , IMAGE , TOOLTIP, CAPTION},
* @param inButtons
* @param inListener
*/
public void addButtons(String[][] inButtons, ActionListener inListener) {
addButtonsToPanel(mLeftArea, inButtons, inListener);
}
public void addButtonsRight(String[][] inButtons, ActionListener inListener) {
addButtonsToPanel(mRightArea, inButtons, inListener);
}
private void addButtonsToPanel(Container inArea, String[][] inButtons, ActionListener inListener) {
for(int i=0; i < inButtons.length; i++) {
String aIdent = inButtons[i][0];
ToolbarButtonType aType = ToolbarButtonType.valueOf(inButtons[i][1]);
String aImage = null;
String aTooltip = null;
String aCaption = null;
if(aType == ToolbarButtonType.CUSTOM) {
} else if(aType == ToolbarButtonType.ADD) {
aImage = "img.btn.add";
aTooltip = "tooltip.btn.add";
aCaption = "tooltip.btn.add";
} else if(aType == ToolbarButtonType.REMOVE) {
aImage = "img.btn.delete";
aTooltip = "tooltip.btn.delete";
aCaption = "tooltip.btn.delete";
} else if(aType == ToolbarButtonType.EDIT) {
aImage = "img.btn.change";
aTooltip = "tooltip.btn.change";
aCaption = "tooltip.btn.change";
} else if(aType == ToolbarButtonType.SEARCH) {
aImage = "img.btn.search";
aTooltip = "tooltip.btn.search";
aCaption = "tooltip.btn.search";
} else if(aType == ToolbarButtonType.HELP) {
aImage = "img.btn.help";
aTooltip = "tooltip.btn.help";
aCaption = "tooltip.btn.help";
} else if(aType == ToolbarButtonType.NEW) {
aImage = "img.btn.new";
aTooltip = "tooltip.btn.new";
aCaption = "tooltip.btn.new";
} else if(aType == ToolbarButtonType.CLEAR) {
aImage = "img.btn.clear";
aTooltip = "tooltip.btn.clear";
aCaption = "tooltip.btn.clear";
} else if(aType == ToolbarButtonType.SAVE) {
aImage = "img.btn.save";
aTooltip = "tooltip.btn.save";
aCaption = "tooltip.btn.save";
} else if(aType == ToolbarButtonType.ACCEPT) {
aImage = "img.btn.accept";
aTooltip = "tooltip.btn.accept";
aCaption = "tooltip.btn.accept";
} else if(aType == ToolbarButtonType.CANCEL) {
aImage = "img.btn.cancel";
aTooltip = "tooltip.btn.cancel";
aCaption = "tooltip.btn.cancel";
} else if(aType == ToolbarButtonType.PRINT) {
aImage = "img.btn.print";
aTooltip = "tooltip.btn.print";
aCaption = "tooltip.btn.print";
} else if(aType == ToolbarButtonType.ATTACHMENT) {
aImage = "img.btn.attachment";
aTooltip = "tooltip.btn.attachment";
aCaption = "tooltip.btn.attachment";
}
if(inButtons[i].length > 2) {
aImage = inButtons[i][2];
}
if(inButtons[i].length > 3) {
aTooltip = inButtons[i][3];
}
if(inButtons[i].length > 4) {
aCaption = inButtons[i][4];
}
AbstractButton aBtn = createButton();
mBtnMap.put(aIdent, aBtn);
aBtn.setName(aIdent);
if(aImage != null) {
ImageIcon aImageIcon = ClientResources.getImage(aImage);
aBtn.setIcon(aImageIcon);
}
if(aTooltip != null) {
aBtn.setToolTipText(ClientResources.getRes(aTooltip));
}
if(aCaption != null) {
aBtn.setText(ClientResources.getRes(aCaption));
}
inArea.add(aBtn);
aBtn.addActionListener(mActionListener);
}
addActionListener(inListener);
}
public AbstractButton getButton(final String inBtnId) {
return mBtnMap.get(inBtnId);
}
public void setHelpContext(String inCtx) {
mHelpContext = inCtx;
}
public JButton getNavigtorButton() {
return mBtnNavigator;
}
public JButton getHelpButton() {
return mBtnHelp;
}
public void setDefaultButtonsVisible(boolean inVisible) {
mBtnHelp.setVisible(inVisible);
mBtnNavigator.setVisible(inVisible);
}
public JPanel getRightArea() {
return mRightArea;
}
public void addActionListener(ActionListener inListener) {
synchronized(mMutex) {
mListenerList.add(inListener);
}
}
public void removeActionListener(ActionListener inListener) {
synchronized(mMutex) {
mListenerList.remove(inListener);
}
}
private void fireEvent(ActionEvent inEvent) {
synchronized (mMutex ) {
List<ActionListener> aList = Collections.synchronizedList(mListenerList);
synchronized(aList) {
for(ActionListener aListener : aList)
aListener.actionPerformed(inEvent);
}
}
}
public AbstractButton createButton() {
return new JButton();
}
private Object mMutex = new Object();
private ArrayList<ActionListener> mListenerList = new ArrayList<ActionListener>();
private transient ActionListener mActionListener;
private String mHelpContext=null;
private JPanel mLeftArea;
private JPanel mRightArea;
private JButton mBtnNavigator;
private JButton mBtnHelp;
private HashMap<String, AbstractButton> mBtnMap = new HashMap<String, AbstractButton>();
}