package net.codjo.utils.sql;
import net.codjo.profile.User;
import net.codjo.utils.sql.event.DbChangeListener;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TooManyListenersException;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
public static final int FIND = 0;
public static final int SHOW = 1;
public static final int CLOSE = 2;
public static final int MODIFY = 3;
public static final int DUPLICATE = 4;
public static final int DELETE = 5;
public static final int ADD = 6;
public static final int SEPARATOR = 7;
public static final int PREVIOUS = 8;
public static final int NEXT = 9;
public static final int PRINT = 10;
public static final int EXPORT = 11;
public static final int FORCE = 12;
static final int[] ALL_ACTIONS =
{
PREVIOUS, NEXT, SEPARATOR, EXPORT, PRINT, SEPARATOR, ADD, MODIFY, DUPLICATE,
DELETE, SEPARATOR, FIND, SHOW, SEPARATOR, CLOSE
};
static final int[] ALL_ACTIONS_NO_CLOSE =
{
PREVIOUS, NEXT, SEPARATOR, EXPORT, PRINT, SEPARATOR, ADD, MODIFY, DUPLICATE,
DELETE, SEPARATOR, FIND, SHOW
};
AddAction addAction;
List allActions = new ArrayList();
CloseAction closeAction;
DeleteAction deleteAction;
ForceRecordAction forceAction;
JDesktopPane desktopPane;
DuplicateAction duplicateAction;
ExportAction exportAction;
FindAction findAction;
GenericTable genericTable;
JInternalFrame internalFrame;
ModifyAction modifyAction;
NextPageAction nextPageAction;
String packageName;
JPopupMenu popupMenu = new JPopupMenu();
PreviousPageAction previousPageAction;
PrintAction printAction;
ShowAllAction showAllAction;
public static final String USER_PROFILE_KEY = "user.profile";
add(new JButton(new ImageIcon("../images/Previous.gif")));
add(new JButton(new ImageIcon("../images/Next.gif")));
addSeparator();
add(new JButton(new ImageIcon("../images/Add.gif")));
add(new JButton(new ImageIcon("../images/Edit.gif")));
add(new JButton(new ImageIcon("../images/Delete.gif")));
add(new JButton(new ImageIcon("../images/force.gif")));
addSeparator();
add(new JButton(new ImageIcon("../images/Find.gif")));
add(new JButton(new ImageIcon("../images/ShowAll.gif")));
addSeparator();
add(new JButton(new ImageIcon("../images/Close.gif")));
}
public DbToolBar(JDesktopPane dp, GenericTable gt, JInternalFrame jf, String packName) {
this(dp, gt, jf, packName, ALL_ACTIONS);
}
public DbToolBar(JDesktopPane dp, GenericTable gt, JInternalFrame jf,
String packName, boolean closeButton) {
this(dp, gt, jf, packName, ((closeButton) ? ALL_ACTIONS : ALL_ACTIONS_NO_CLOSE));
}
public DbToolBar(JDesktopPane dp, GenericTable gt, JInternalFrame jf,
String packName, int[] actions) {
desktopPane = dp;
genericTable = gt;
internalFrame = jf;
packageName = packName;
try {
jbInit();
initPopup();
init(actions);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public (JDesktopPane dp, GenericTable gt, JInternalFrame jf,
String packName, int[] actions, boolean popUp) {
desktopPane = dp;
genericTable = gt;
internalFrame = jf;
packageName = packName;
try {
jbInit();
if (popUp) {
initPopup();
}
else {
initRightClick();
}
init(actions);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void add(DbChangeListener l)
throws TooManyListenersException {
for (Iterator iter = allActions.iterator(); iter.hasNext();) {
AbstractDbAction action = (AbstractDbAction)iter.next();
action.addDbChangeListener(l);
}
}
for (Iterator iter = allActions.iterator(); iter.hasNext();) {
AbstractDbAction action = (AbstractDbAction)iter.next();
action.addActionListener(l);
}
}
public JPopupMenu () {
return popupMenu;
}
if (addAction != null) {
addAction.setDefaultValue(columnName, columnValue);
}
if (modifyAction != null) {
modifyAction.setDefaultValue(columnName, columnValue);
}
}
for (Iterator iter = allActions.iterator(); iter.hasNext();) {
AbstractDbAction action = (AbstractDbAction)iter.next();
action.removeActionListener(l);
}
}
if (deleteAction != null) {
deleteAction.setConfirmMsg(msg);
}
if (forceAction != null) {
forceAction.setConfirmMsg(msg);
}
}
if (findAction != null) {
findAction.setDefaultValue(impliciteClause);
}
}
if (findAction != null) {
findAction.setMandatoryClause(mandatoryClause);
}
}
if (showAllAction != null) {
showAllAction.setDefaultValue(impliciteClause);
}
}
for (Iterator iter = allActions.iterator(); iter.hasNext();) {
AbstractDbAction action = (AbstractDbAction)iter.next();
action.setConnection(connection);
}
}
if (e.getClickCount() == 2 && modifyAction.isEnabled()) {
modifyAction.actionPerformed(new ActionEvent(this, 0, "Modification"));
}
}
if (javax.swing.SwingUtilities.isRightMouseButton(e)) {
int row = genericTable.rowAtPoint(e.getPoint());
if (row != -1) {
genericTable.setRowSelectionInterval(row, row);
}
}
maybeShowPopup(e);
}
private static void doEffect(JToolBar tb) {
java.awt.Component component;
int i = 0;
do {
component = tb.getComponentAtIndex(i);
if (component instanceof JButton) {
((JButton)component).setMargin(new java.awt.Insets(1, 3, 1, 3));
}
i++;
}
while (component != null);
}
allActions.add(action);
addAction(action);
}
User user = (User)desktopPane.getClientProperty(USER_PROFILE_KEY);
if (user == null) {
return action;
}
String className = action.getClass().getSimpleName();
String function = className.substring(0, className.length() - "Action".length()).toLowerCase()
+ "." + genericTable.getTable().getDBTableName();
action.setForcedDisabled(!user.isAllowedTo(function));
return action;
}
private void init(
int[] actions) {
if (BcpOutAction.isInited() && "ON".equals(System.getProperty("DEBUG"))) {
addAction(new BcpOutAction(genericTable.getTable().getDBTableName()));
addSeparator();
}
for (int i = 0; i < actions.length; i++) {
switch (actions[i]) {
case EXPORT:
exportAction = new ExportAction(genericTable);
addAction(exportAction);
break;
case PRINT:
printAction = new PrintAction(genericTable);
addAction(printAction);
break;
case PREVIOUS:
previousPageAction = new PreviousPageAction(genericTable);
addAction(previousPageAction);
break;
case NEXT:
nextPageAction = new NextPageAction(genericTable);
addAction(nextPageAction);
break;
case ADD:
addAction =
new AddAction(desktopPane, internalFrame, genericTable, packageName);
addAction.setModifyAction(modifyAction);
addDbAction(manageSecurity(addAction));
break;
case MODIFY:
modifyAction =
new ModifyAction(desktopPane, internalFrame, genericTable, packageName);
if (addAction != null) {
addAction.setModifyAction(modifyAction);
}
addDbAction(manageSecurity(modifyAction));
popupMenu.add(modifyAction);
break;
case FIND:
findAction = new FindAction(desktopPane, internalFrame, genericTable);
addDbAction(findAction);
break;
case SHOW:
showAllAction = new ShowAllAction(genericTable);
addDbAction(showAllAction);
break;
case CLOSE:
closeAction = new CloseAction(internalFrame);
addDbAction(closeAction);
break;
case DUPLICATE:
duplicateAction = new DuplicateAction(genericTable);
addDbAction(manageSecurity(duplicateAction));
popupMenu.add(duplicateAction);
break;
case DELETE:
deleteAction = new DeleteAction(genericTable);
addDbAction(manageSecurity(deleteAction));
popupMenu.add(deleteAction);
break;
case FORCE:
forceAction = new ForceRecordAction(genericTable);
addDbAction(manageSecurity(forceAction));
popupMenu.add(forceAction);
break;
case SEPARATOR:
addSeparator();
break;
default:
throw new IllegalArgumentException();
}
setNameForPopUpMenuForTest();
doEffect(this);
}
}
private void () {
try {
Component[] component = popupMenu.getComponents();
for (Component aComponent : component) {
if (aComponent instanceof JMenuItem) {
aComponent.setName("popUpMenu." +
((JMenuItem)aComponent).getAction().getValue(AbstractAction.NAME));
}
}
}
catch (Exception ex) {
}
}
private void addAction(AbstractAction dbAction) {
try {
add(dbAction).setName(genericTable.getTable().getTableName() + "."
+ dbAction.getValue(AbstractAction.NAME));
}
catch (Exception e) {
add(dbAction);
}
}
public AbstractAction
getAction(
int actionId) {
switch (actionId) {
case EXPORT:
return exportAction;
case PRINT:
return printAction;
case PREVIOUS:
return previousPageAction;
case NEXT:
return nextPageAction;
case ADD:
return addAction;
case MODIFY:
return modifyAction;
case FIND:
return findAction;
case SHOW:
return showAllAction;
case CLOSE:
return closeAction;
case DUPLICATE:
return duplicateAction;
case DELETE:
return deleteAction;
case FORCE:
return forceAction;
default:
throw new IllegalArgumentException();
}
}
private void () {
MouseListener l =
new java.awt.event.MouseAdapter() {
tableMouseClicked(e);
}
tableMousePressed(e);
}
maybeShowPopup(e);
}
};
genericTable.addMouseListener(l);
}
MouseListener l =
new java.awt.event.MouseAdapter() {
if (javax.swing.SwingUtilities.isRightMouseButton(e)) {
int row = genericTable.rowAtPoint(e.getPoint());
if (row != -1) {
genericTable.setRowSelectionInterval(row, row);
}
}
}
};
genericTable.addMouseListener(l);
}
private void jbInit()
throws Exception {
setLayout(new FlowLayout(FlowLayout.RIGHT));
setFloatable(false);
}
private void (MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}