package org.enhydra.kelp.ant.deployer;
// ToolBox imports
import org.enhydra.tool.common.IconSet;
// Kelp imports
import org.enhydra.kelp.common.event.SwingTableSelectionEvent;
import org.enhydra.kelp.common.event.SwingTableSelectionListener;
import org.enhydra.kelp.common.node.OtterTemplateNode;
import org.enhydra.kelp.common.node.OtterNode;
import org.enhydra.kelp.common.node.OtterProject;
// Standard imports
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.*;
import java.util.ResourceBundle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.util.HashMap;
import org.enhydra.kelp.ant.node.AntProject;
import java.util.Iterator;
import java.util.Map;
//
public class AntDeployInputReplacementsPanel extends JPanel
implements ListSelectionListener, SwingTableSelectionListener {
static ResourceBundle res =
ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
private OtterNode node = null;
private GridBagLayout layoutMain;
private JTable table;
private JScrollPane scrollTable;
private JButton buttonAdd;
private JButton buttonEdit;
private JButton buttonRemove;
private JButton buttonReset;
private JButton buttonUp;
private JButton buttonDown;
private LocalButtonListener buttonListener;
private LocalTableModel tableModel = new LocalTableModel();
private int currentSelectionIndex = -1;
private SwingTableSelectionListener[] swingTableSelectionListeners =
new SwingTableSelectionListener[0];
private String[][] defaultTable = null;
private AntProject project = null;
public AntDeployInputReplacementsPanel() {
try {
jbInit();
pmInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void clearAll() {
buttonAdd.removeActionListener(buttonListener);
buttonEdit.removeActionListener(buttonListener);
buttonReset.removeActionListener(buttonListener);
buttonRemove.removeActionListener(buttonListener);
buttonUp.removeActionListener(buttonListener);
buttonDown.removeActionListener(buttonListener);
removeSwingTableSelectionListener(this);
table.getSelectionModel().removeListSelectionListener(this);
tableModel.removeTableModelListener(table);
tableModel.removeAllRows();
table.setModel(new DefaultTableModel());
removeAll();
tableModel = null;
buttonListener = null;
node = null;
swingTableSelectionListeners = null;
}
/**
* ListSelectionListener event
*/
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
currentSelectionIndex = -1;
} else {
currentSelectionIndex = lsm.getMinSelectionIndex();
}
fireSwingTableSelectionEvent();
}
private void fireSwingTableSelectionEvent() {
for (int i = 0; i < swingTableSelectionListeners.length; i++) {
swingTableSelectionListeners[i].onSwingTableSelection(new SwingTableSelectionEvent(this,
currentSelectionIndex));
}
}
public synchronized void addSwingTableSelectionListener(SwingTableSelectionListener l) {
ArrayList list = null;
list = new ArrayList(Arrays.asList(swingTableSelectionListeners));
if (!list.contains(l)) {
list.add(l);
list.trimToSize();
swingTableSelectionListeners =
new SwingTableSelectionListener[list.size()];
swingTableSelectionListeners =
(SwingTableSelectionListener[]) list.toArray(swingTableSelectionListeners);
}
list.clear();
}
public synchronized void removeSwingTableSelectionListener(SwingTableSelectionListener l) {
ArrayList list = null;
list = new ArrayList(Arrays.asList(swingTableSelectionListeners));
if (list.contains(l)) {
list.remove(l);
list.trimToSize();
swingTableSelectionListeners =
new SwingTableSelectionListener[list.size()];
swingTableSelectionListeners =
(SwingTableSelectionListener[]) list.toArray(swingTableSelectionListeners);
}
list.clear();
}
public void onSwingTableSelection(SwingTableSelectionEvent event) {
boolean selection = (!event.isSelectionNull());
buttonEdit.setEnabled(selection);
buttonRemove.setEnabled(selection);
if (selection) {
int index = event.getSelectionIndex();
boolean down = (index < tableModel.getRowCount() - 1);
boolean up = (index > 0);
buttonUp.setEnabled(up);
buttonDown.setEnabled(down);
} else {
buttonUp.setEnabled(false);
buttonDown.setEnabled(false);
}
}
public OtterNode getNode() {
return node;
}
public void setNode(OtterNode n) {
node = n;
}
public void setIconView(boolean b) {
if (b) {
buttonAdd.setIcon(IconSet.getNewRowIcon());
buttonAdd.setText(new String());
buttonDown.setIcon(IconSet.getDownIcon());
buttonDown.setText(new String());
buttonEdit.setIcon(IconSet.getRowIcon());
buttonEdit.setText(new String());
buttonUp.setIcon(IconSet.getUpIcon());
buttonUp.setText(new String());
buttonRemove.setIcon(IconSet.getDeleteRowIcon());
buttonRemove.setText(new String());
buttonReset.setIcon(IconSet.getUndoIcon());
buttonReset.setText(new String());
} else {
buttonAdd.setIcon(null);
buttonAdd.setText(res.getString("buttonAdd_Text"));
buttonDown.setIcon(null);
buttonDown.setText(res.getString("buttonDown_Text"));
buttonEdit.setIcon(null);
buttonEdit.setText(res.getString("Edit"));
buttonUp.setIcon(null);
buttonUp.setText(res.getString("buttonUp_Text"));
buttonRemove.setIcon(null);
buttonRemove.setText(res.getString("Remove"));
buttonReset.setIcon(null);
buttonReset.setText(res.getString("buttonReset_Text"));
}
}
public boolean isIconView() {
return (buttonAdd.getIcon() == null);
}
public void readProperties() {
OtterProject project = null;
if (node == null) {
System.err.println("AntDeployInputReplacementsPanel.readProperties(): node is null");
} else {
project = node.getProject();
if (node instanceof OtterTemplateNode) {}
else {
enableUI(true);
}
if (project.isDefaultProject()) {
buttonAdd.setEnabled(false);
buttonDown.setEnabled(false);
buttonUp.setEnabled(false);
buttonReset.setEnabled(false);
} else {
setReplacementTable(project.getReplacementTable());
}
}
invalidate();
}
public void writeProperties() {
OtterProject project = null;
if (node == null) {
System.err.println("AntDeployInputReplacementsPanel.writeProperties() - node null");
return;
} else {
project = node.getProject();
}
if (project.isDefaultProject()) {
// done
} else {
project.setReplacementTable(getReplacementTable());
}
}
public boolean isDataEqual() {
String[][] projReps = new String[0][0];
String[][] memReps = new String[0][0];
boolean equal = false;
OtterProject project = null;
if (node == null) {
System.err.println("AntDeployInputReplacementsPanel.isDataEqual(): node is null");
} else {
project = node.getProject();
projReps = project.getReplacementTable();
memReps = getReplacementTable();
if (projReps.length == memReps.length) {
equal = true;
for (int i = 0; i < memReps.length; i++) {
if (equal && (projReps[i].length == memReps[i].length)) {
for (int j = 0; j < memReps[i].length; j++) {
if (projReps[i][j].equals(memReps[i][j])) {}
else {
equal = false;
break;
}
}
} else {
equal = false;
break;
}
}
}
}
return equal;
}
public void enableUI(boolean enable) {
table.setEnabled(enable);
buttonAdd.setEnabled(enable);
buttonEdit.setEnabled(currentSelectionIndex > -1);
buttonRemove.setEnabled(currentSelectionIndex > -1);
buttonReset.setEnabled(enable);
}
//
//
private String[][] getReplacementTable() {
String[][] repTable = null;
LocalRow row = null;
int length = tableModel.getRowCount();
repTable = new String[length][2];
for (int i = 0; i < length; i++) {
row = tableModel.getRow(i);
repTable[i][0] = row.getFind();
repTable[i][1] = row.getReplaceWith();
}
return repTable;
}
private void setReplacementTable(String[][] repTable) {
tableModel.removeAllRows();
for (int i = 0; i < repTable.length; i++) {
tableModel.addRow(repTable[i][0], repTable[i][1]);
}
removeTableSelections();
}
private void removeTableSelections() {
if (table.getSelectedRow() > -1) {
table.removeRowSelectionInterval(table.getSelectedRow(),
table.getSelectedRow());
}
currentSelectionIndex = -1;
fireSwingTableSelectionEvent();
table.updateUI();
}
private void setDefaultTable() {
int result = JOptionPane.YES_OPTION;
if (tableModel.getRowCount() > 0) {
result =
JOptionPane.showConfirmDialog(this,
res.getString("Replace_current_set"),
res.getString("Switch_to_Default"),
JOptionPane.YES_NO_OPTION);
}
if (result == JOptionPane.YES_OPTION) {
// OtterProject project = node.getProject();
// String[][] def = new String[0][0];
//
// try {
// def = ConfigTool.createReplacementStringArray(project.getRootPath(),
// project.getDeployRootPath(),
// ToolBoxInfo.getJavaPath());
// def =
// Backward.createReplacementTable(def,
// PathUtil.getInputTemplates(project));
// } catch (ToolException e) {
// def = new String[0][0];
// e.printStackTrace();
// }
setReplacementTable(defaultTable);
}
}
private void addRow() {
AntDeployReplaceEditorDialog d = null;
if (getTopLevelAncestor() instanceof JDialog) {
JDialog parentDialog = (JDialog) this.getTopLevelAncestor();
d = new AntDeployReplaceEditorDialog(parentDialog,
res.getString("Add_Replace_Text"),
true);
d.setFind(new String());
d.setReplaceWith(new String());
d.show();
if (d.getOption() == AntDeployReplaceEditorDialog.ACTION_OK) {
if (d.getFind().trim().length() > 0) {
tableModel.addRow(d.getFind(), d.getReplaceWith());
removeTableSelections();
}
}
}
}
private void removeCurrent() {
int result = 0;
if (currentSelectionIndex > -1
&& currentSelectionIndex < tableModel.getRowCount()) {
result =
JOptionPane.showConfirmDialog(this,
res.getString("Remove_current"),
res.getString("Confirm_Remove"),
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
tableModel.removeRow(currentSelectionIndex);
removeTableSelections();
}
}
}
private void editCurrent() {
AntDeployReplaceEditorDialog d = null;
LocalRow row = tableModel.getRow(currentSelectionIndex);
if (getTopLevelAncestor() instanceof JDialog) {
JDialog parentDialog = (JDialog) this.getTopLevelAncestor();
d = new AntDeployReplaceEditorDialog(parentDialog,
res.getString("Edit_Replace_Text"),
true);
d.setFind(row.getFind());
d.setReplaceWith(row.getReplaceWith());
d.show();
if (d.getOption() == AntDeployReplaceEditorDialog.ACTION_OK) {
if (d.getFind().trim().length() > 0) {
row.setFind(d.getFind());
row.setReplaceWith(d.getReplaceWith());
table.updateUI();
}
}
}
}
private void moveRowUp() {
tableModel.moveUp(currentSelectionIndex);
table.updateUI();
}
private void moveRowDown() {
tableModel.moveDown(currentSelectionIndex);
table.updateUI();
}
private void pmInit() throws Exception {
buttonEdit.setEnabled(false);
buttonEdit.setToolTipText(res.getString("buttonEdit_ToolTipText"));
buttonRemove.setEnabled(false);
buttonRemove.setToolTipText(res.getString("buttonRemove_ToolTipText"));
buttonListener = new LocalButtonListener();
buttonAdd.addActionListener(buttonListener);
buttonAdd.setToolTipText(res.getString("buttonAdd_ToolTipText"));
buttonEdit.addActionListener(buttonListener);
buttonReset.addActionListener(buttonListener);
buttonReset.setToolTipText(res.getString("buttonReset_ToolTipText"));
buttonRemove.addActionListener(buttonListener);
buttonUp.addActionListener(buttonListener);
buttonUp.setToolTipText(res.getString("buttonUp_ToolTipText"));
buttonDown.addActionListener(buttonListener);
buttonDown.setToolTipText(res.getString("buttonDown_ToolTipText"));
tableModel = new LocalTableModel();
table.setModel(tableModel);
// table.setDefaultRenderer(String.class, new CellRendererWithToolTip());
tableModel.addTableModelListener(table);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getTableHeader().setUpdateTableInRealTime(false);
table.getTableHeader().setReorderingAllowed(false);
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(this);
addSwingTableSelectionListener(this);
setIconView(false);
}
private void jbInit() throws Exception {
layoutMain =
(GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
GridBagLayout.class.getName());
table = (JTable) Beans.instantiate(getClass().getClassLoader(),
JTable.class.getName());
buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(),
JButton.class.getName());
buttonEdit = (JButton) Beans.instantiate(getClass().getClassLoader(),
JButton.class.getName());
buttonRemove =
(JButton) Beans.instantiate(getClass().getClassLoader(),
JButton.class.getName());
buttonReset = (JButton) Beans.instantiate(getClass().getClassLoader(),
JButton.class.getName());
buttonUp = (JButton) Beans.instantiate(getClass().getClassLoader(),
JButton.class.getName());
buttonDown = (JButton) Beans.instantiate(getClass().getClassLoader(),
JButton.class.getName());
scrollTable = new JScrollPane(table);
table.setToolTipText(new String());
table.sizeColumnsToFit(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
table.setColumnSelectionAllowed(false);
scrollTable.setMinimumSize(new Dimension(300, 100));
scrollTable.setPreferredSize(new Dimension(300, 100));
scrollTable.getViewport().add(table, BorderLayout.CENTER);
//
this.setLayout(layoutMain);
this.add(scrollTable,
new GridBagConstraints(3, 0, 6, 1, 0.8, 0.2,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
this.add(buttonAdd,
new GridBagConstraints(3, 1, 1, 1, 0.1, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(2, 1, 5, 1), 0, 0));
this.add(buttonEdit,
new GridBagConstraints(4, 1, 1, 1, 0.1, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(2, 1, 5, 1), 0, 0));
this.add(buttonRemove,
new GridBagConstraints(5, 1, 1, 1, 0.1, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(2, 1, 5, 1), 0, 0));
this.add(buttonReset,
new GridBagConstraints(8, 1, 1, 1, 0.1, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(2, 1, 5, 1), 0, 0));
this.add(buttonUp,
new GridBagConstraints(6, 1, 1, 1, 0.1, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(2, 1, 5, 1), 0, 0));
this.add(buttonDown,
new GridBagConstraints(7, 1, 1, 1, 0.1, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(2, 1, 5, 1), 0, 0));
}
public void setProject(OtterProject otterProject){
if(otterProject instanceof AntProject) {
project = (AntProject) otterProject;
initOptions();
}
else
System.err.println("DEBUG project must be AntProject");//FIXME throw Exception
}
private void initOptions(){
HashMap replacementsMap = project.getReplacements();
Iterator replacementsIterator = replacementsMap.entrySet().iterator();
defaultTable = new String[replacementsMap.size()][2];
int i = 0;
Map.Entry mapEntry = null;
while (replacementsIterator.hasNext()) {
mapEntry = (Map.Entry)replacementsIterator.next();
defaultTable[i][0] = (String)mapEntry.getKey();
defaultTable[i][1] = (String)mapEntry.getValue();
i++;
}
setReplacementTable(defaultTable);
}
private void saveOptions() {
HashMap replacementsMap = new HashMap();
String[][] replacementsTable = this.getReplacementTable();
for (int i = 0; i < replacementsTable.length; i++) {
replacementsMap.put(replacementsTable[i][0], replacementsTable[i][1]);
}
project.setReplacements(replacementsMap);
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//
private class LocalButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == buttonReset) {
setDefaultTable();
} else if (source == buttonRemove) {
removeCurrent();
} else if (source == buttonAdd) {
addRow();
} else if (source == buttonEdit) {
editCurrent();
} else if (source == buttonUp) {
moveRowUp();
} else if (source == buttonDown) {
moveRowDown();
}
saveOptions();
}
}
//
private class LocalTableModel extends AbstractTableModel {
private Vector rowVector = new Vector();
public LocalTableModel() {}
public int getRowCount() {
int count = 0;
if (rowVector != null) {
count = rowVector.size();
}
return count;
}
public int getColumnCount() {
return 2;
}
public String getColumnName(int columnIndex) {
String name = new String();
switch (columnIndex) {
case 0:
name = res.getString("Text_to_find");
break;
case 1:
name = res.getString("Replace_with");
break;
}
return name;
}
public Class getColumnClass(int columnIndex) {
Class columnClass = null;
Object value = getValueAt(0, columnIndex);
if (value != null) {
columnClass = value.getClass();
}
return columnClass;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
if (!isTableEmpty()) {
LocalRow row = (LocalRow) rowVector.elementAt(rowIndex);
LocalCell cell = new LocalCell(columnIndex, row);
value = cell.getValue();
}
return value;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (!isTableEmpty()) {
LocalRow row = (LocalRow) rowVector.elementAt(rowIndex);
LocalCell cell = new LocalCell(columnIndex, row);
cell.setValue(aValue);
fireTableCellUpdated(columnIndex, rowIndex);
}
}
protected LocalRow getRow(int rowIndex) {
LocalRow row = null;
if (!isTableEmpty()) {
if (rowVector.size() > rowIndex) {
row = (LocalRow) rowVector.elementAt(rowIndex);
}
}
return row;
}
protected void saveMap() {
OtterProject project = node.getProject();
int rowCount = rowVector.size();
String[][] map = new String[rowCount][2];
LocalRow row = null;
for (int i = 0; i < rowCount; i++) {
row = (LocalRow) rowVector.elementAt(i);
map[i][0] = row.getFind();
map[i][1] = row.getReplaceWith();
}
project.setPackageMap(map);
}
protected void populateModel() {
OtterProject project = node.getProject();
String[][] map = project.getPackageMap();
if (map != null) {
int rowCount = map.length;
for (int i = 0; i < rowCount; i++) {
addRow(map[i][0], map[i][1]);
}
}
}
// /
// /
private boolean isTableEmpty() {
boolean empty = true;
if (rowVector != null) {
if (rowVector.size() > 0) {
empty = false;
}
}
return empty;
}
private void addRow(String f, String p) {
LocalRow newRow = null;
newRow = new LocalRow(f, p);
rowVector.addElement(newRow);
fireTableDataChanged();
}
private void removeRow(int index) {
if (index < rowVector.size()) {
rowVector.removeElementAt(index);
fireTableDataChanged();
}
}
private void moveUp(int index) {
if (index > 0 && index < rowVector.size()) {
Object current = rowVector.elementAt(index);
Object previous = rowVector.elementAt(index - 1);
rowVector.setElementAt(current, index - 1);
rowVector.setElementAt(previous, index);
fireTableDataChanged();
}
}
private void moveDown(int index) {
if (index < (rowVector.size() - 1)) {
Object current = rowVector.elementAt(index);
Object next = rowVector.elementAt(index + 1);
rowVector.setElementAt(current, index + 1);
rowVector.setElementAt(next, index);
fireTableDataChanged();
}
}
private void removeAllRows() {
rowVector.removeAllElements();
fireTableDataChanged();
}
}
//
private class LocalRow {
private String find = new String();
private String replaceWith = new String();
public LocalRow(String f, String rw) {
find = f;
replaceWith = rw;
}
public String getFind() {
return find;
}
public void setFind(String f) {
find = f;
saveToProject();
}
public String getReplaceWith() {
return replaceWith;
}
public void setReplaceWith(String rw) {
replaceWith = rw;
saveToProject();
}
private void saveToProject() {}
}
//
private class LocalCell {
private LocalRow row;
private int column;
public LocalCell(int c, LocalRow r) {
column = c;
row = r;
}
public boolean setValue(Object value) {
boolean set = true;
switch (column) {
case 0:
row.setFind((String) value);
break;
case 1:
row.setReplaceWith((String) value);
break;
default:
set = false;
break;
}
return set;
}
public Object getValue() {
Object value = null;
switch (column) {
case 0:
value = row.getFind();
break;
case 1:
value = row.getReplaceWith();
break;
}
return value;
}
//
//
protected LocalRow getRow() {
return row;
}
}
}
|