tn5250j/src/org/tn5250j ExternalProgramConfig.java,NONE,1.1 Connect.java,1.31,1.32
Christian Geisert <[email protected]> Fri, 09 Dec 2005 14:39:10 +0000
| Newsgroups | gmane.comp.java.tn5250j.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/tn5250j/tn5250j/src/org/tn5250j
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14634/src/org/tn5250j
Modified Files:
Connect.java
Added Files:
ExternalProgramConfig.java
Log Message:
Support for launching customized external program function via hotspot
Submitted by: Richard Houston <rhouston -(at)- rlhc.net>
Index: Connect.java
===================================================================
RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/Connect.java,v
retrieving revision 1.31
retrieving revision 1.32
diff -C2 -d -r1.31 -r1.32
*** Connect.java 8 Jul 2005 06:05:13 -0000 1.31
--- Connect.java 9 Dec 2005 14:39:08 -0000 1.32
***************
*** 45,48 ****
--- 45,50 ----
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
+ import java.beans.PropertyChangeEvent;
+ import java.beans.PropertyChangeListener;
import java.util.Collections;
import java.util.Comparator;
***************
*** 63,66 ****
--- 65,69 ----
import javax.swing.JFrame;
import javax.swing.JLabel;
+ import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
***************
*** 110,115 ****
JPanel appenderPanel = new JPanel();
JPanel externalPanel = new JPanel();
!
JTable sessions = null;
GridBagConstraints gbc;
--- 113,122 ----
JPanel appenderPanel = new JPanel();
JPanel externalPanel = new JPanel();
! JPanel externalOpts = new JPanel();
! JPanel externalOptPanel = new JPanel(
! new FlowLayout(FlowLayout.CENTER, 30, 10));
!
JTable sessions = null;
+ JTable externals = null;
GridBagConstraints gbc;
***************
*** 127,143 ****
JButton connectButton = null;
JButton applyButton = null;
!
// custom table model
ConfigureTableModel ctm = null;
!
// The scroll pane that holds the table.
JScrollPane scrollPane;
// ListSelectionModel of our custom table.
ListSelectionModel rowSM = null;
!
// Properties
Properties props = null;
!
// property input structures
JRadioButton intTABS = null;
--- 134,159 ----
JButton connectButton = null;
JButton applyButton = null;
!
! JButton cAddButton = null;
! JButton cEditButton = null;
! JButton cRemoveButton = null;
!
!
// custom table model
ConfigureTableModel ctm = null;
!
! CustomizedTableModel etm = null;
!
// The scroll pane that holds the table.
JScrollPane scrollPane;
+ JScrollPane scrollPane2;
// ListSelectionModel of our custom table.
ListSelectionModel rowSM = null;
! ListSelectionModel rowSM2 = null;
// Properties
Properties props = null;
! Properties etnProps = null;
!
// property input structures
JRadioButton intTABS = null;
***************
*** 173,177 ****
props = ConfigureFactory.getInstance().getProperties(
ConfigureFactory.SESSIONS);
!
try {
jbInit();
--- 189,194 ----
props = ConfigureFactory.getInstance().getProperties(
ConfigureFactory.SESSIONS);
! etnProps = ExternalProgramConfig.getInstance().getEtnPgmProps();
!
try {
jbInit();
***************
*** 778,782 ****
--- 795,868 ----
externalPanel.add(externalPrograms, BorderLayout.NORTH);
+
+ //For Customized External Program
+ etm = new CustomizedTableModel();
+ // create a table using our custom table model
+ externals = new JSortTable(etm);
+ externals.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ externals.setPreferredScrollableViewportSize(new Dimension(500, 100));
+ externals.setShowGrid(false);
+ // Create the scroll pane and add the table to it.
+ scrollPane2 = new JScrollPane(externals);
+ scrollPane2
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
+ scrollPane2
+ .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+
+ // This will make the connect dialog react to two clicks instead of having
+ // to click on the selection and then clicking twice
+ externals.addMouseListener(new MouseAdapter() {
+ public void mouseClicked(MouseEvent event) {
+ if (event.getClickCount() == 2) {
+ doActionConnect();
+ }
+ }
+ });
+
+ // Setup our selection model listener
+ rowSM2 = externals.getSelectionModel();
+ rowSM2.addListSelectionListener(new ListSelectionListener() {
+ public void valueChanged(ListSelectionEvent e) {
+ // Ignore extra messages.
+ if (e.getValueIsAdjusting())
+ return;
+ ListSelectionModel lsm = (ListSelectionModel) e.getSource();
+ if (lsm.isSelectionEmpty()) {
+ // no rows are selected
+ cEditButton.setEnabled(false);
+ cRemoveButton.setEnabled(false);
+ cAddButton.setEnabled(false);
+ }
+ else {
+ int selectedRow = lsm.getMinSelectionIndex();
+ // selectedRow is selected
+ cEditButton.setEnabled(true);
+ cRemoveButton.setEnabled(true);
+ cAddButton.setEnabled(true);
+ }
+ }
+ });
+
+ // Setup panels
+ JPanel cExternalPrograms = new JPanel();
+ cExternalPrograms.setLayout(new BorderLayout());
+ cExternalPrograms.setBorder(BorderFactory.createTitledBorder(LangTool.getString("customized.title")));
+ externalOpts.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
+ externalOpts.add(scrollPane2, BorderLayout.CENTER);
+ cExternalPrograms.add(externalOpts, BorderLayout.NORTH);
+ cExternalPrograms.add(externalOptPanel, BorderLayout.SOUTH);
+ // sessionPanel.setBorder(BorderFactory.createRaisedBevelBorder());
+
+ // add the option buttons
+ cAddButton = addOptButton(LangTool.getString("ss.optAdd"), "cADD", externalOptPanel);
+
+ cRemoveButton = addOptButton(LangTool.getString("ss.optDelete"), "cREMOVE",
+ externalOptPanel, false);
+
+ cEditButton = addOptButton(LangTool.getString("ss.optEdit"), "cEDIT",
+ externalOptPanel, false);
+
+ externalPanel.add(cExternalPrograms, BorderLayout.CENTER);
}
***************
*** 890,893 ****
--- 976,1016 ----
sessions.requestFocus();
}
+ if (e.getActionCommand().equals("cADD")) {
+ String name = ExternalProgramConfig.doEntry((JFrame) getParent(), null,
+ etnProps);
+
+ etm.addSession();
+ // I we have only one row then select the first one so that
+ // we do not get a selection index out of range problem.
+ if (etm.getRowCount() == 1) {
+ externals.getSelectionModel().setSelectionInterval(0, 0);
+ }
+ else {
+ // Here we will select the entry that we just added.
+ int selInterval = 0;
+ for (int x = 0; x < externals.getRowCount(); x++) {
+ if (((String) etm.getValueAt(x, 0)).equals(name))
+ selInterval = x;
+ }
+ externals.getSelectionModel().setSelectionInterval(selInterval,
+ selInterval);
+ }
+
+ externals.requestFocus();
+
+ }
+ if (e.getActionCommand().equals("cEDIT")) {
+ int selectedRow = rowSM2.getMinSelectionIndex();
+ ExternalProgramConfig.doEntry((JFrame) getParent(), (String) etm.getValueAt(
+ selectedRow, 0), etnProps);
+ etm.chgSession(selectedRow);
+ externals.requestFocus();
+ }
+
+ if (e.getActionCommand().equals("cREMOVE")) {
+ removeExternalProgram();
+ cEditButton.setEnabled(false);
+ cRemoveButton.setEnabled(false);
+ }
if (e.getActionCommand().equals("CONNECT")) {
***************
*** 938,942 ****
ConfigureFactory.getInstance().saveSettings(ConfigureFactory.SESSIONS,
"------ Session Information --------");
!
OptionAccessFactory.getInstance().reload();
}
--- 1061,1067 ----
ConfigureFactory.getInstance().saveSettings(ConfigureFactory.SESSIONS,
"------ Session Information --------");
! ConfigureFactory.getInstance().saveSettings(ExternalProgramConfig.EXTERNAL_PROGRAM_REGISTRY_KEY,
! ExternalProgramConfig.EXTERNAL_PROGRAM_PROPERTIES_FILE_NAME,
! ExternalProgramConfig.EXTERNAL_PROGRAM_HEADER);
OptionAccessFactory.getInstance().reload();
}
***************
*** 1049,1052 ****
--- 1174,1215 ----
ctm.removeSession(selectedRow);
}
+
+ private void removeExternalProgram() {
+ int selectedRow = rowSM2.getMinSelectionIndex();
+ String propKey = (String)etm.getValueAt(selectedRow, 0);
+ int num=0;
+
+ for (Enumeration e = etnProps.keys() ; e.hasMoreElements() ;) {
+ String key = (String)e.nextElement();
+ if(etnProps.getProperty(key) == propKey){
+ String subKey = key.substring(8);
+ int index = subKey.indexOf(".");
+ num = Integer.parseInt(subKey.substring(0,index));
+ break;
+ }
+ }
+ Properties newProps = new Properties();
+ String count = etnProps.getProperty("etn.pgm.support.total.num");
+ if(count != null && count.length() > 0){
+ int total = Integer.parseInt(count);
+ for(int i=1;i<=total;i++){
+ int order =i;
+ if(i > num) order = i -1;
+ if(i != num){
+ String program = etnProps.getProperty("etn.pgm."+i+".command.name");
+ String wCommand = etnProps.getProperty("etn.pgm."+i+".command.window");
+ String uCommand = etnProps.getProperty("etn.pgm."+i+".command.unix");
+ newProps.setProperty("etn.pgm."+order+".command.name",program);
+ newProps.setProperty("etn.pgm."+order+".command.window",wCommand);
+ newProps.setProperty("etn.pgm."+order+".command.unix",uCommand);
+ }
+ }
+ newProps.setProperty("etn.pgm.support.total.num",String.valueOf(total - 1));
+ }
+
+ etnProps.clear();
+ etnProps.putAll(newProps);
+ etm.removeSession(selectedRow);
+ }
void intTABS_itemStateChanged(ItemEvent e) {
***************
*** 1332,1336 ****
--- 1495,1658 ----
}
+
+ class CustomizedExternalProgram {
+ private String name;
+ private String wCommand;
+ private String uCommand;
+
+ CustomizedExternalProgram(String name, String wCommand, String uCommand){
+ this.name=name;
+ this.wCommand=wCommand;
+ this.uCommand=uCommand;
+ }
+
+ public String toString(){
+ return this.name;
+ }
+
+ /**
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+
+ return getName().hashCode();
+ }
+
+ /**
+ * @see java.lang.Object#equals(Object)
+ */
+ public boolean equals(Object other) {
+ if ( !(other instanceof CustomizedExternalProgram) ) return false;
+ CustomizedExternalProgram castOther = (CustomizedExternalProgram) other;
+ if(this.hashCode()!=castOther.hashCode()) return false;
+ return true;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+
+ public String getUCommand() {
+ return uCommand;
+ }
+
+
+ public String getWCommand() {
+ return wCommand;
+ }
+
+
+ }
+ class CustomizedTableModel extends AbstractTableModel implements
+ SortTableModel {
+ final String[] cols = { LangTool.getString("customized.name"),
+ LangTool.getString("customized.window"),
+ LangTool.getString("customized.unix") };
+
+ Vector mySort = new Vector();
+ int sortedColumn = 0;
+ boolean isAscending = true;
+
+ public CustomizedTableModel() {
+ super();
+ resetSorted();
+ }
+
+ private void resetSorted() {
+ mySort.clear();
+
+ String count = etnProps.getProperty("etn.pgm.support.total.num");
+ if(count != null && count.length() > 0){
+ int total = Integer.parseInt(count);
+ for(int i=1;i<=total;i++){
+ String program = etnProps.getProperty("etn.pgm."+i+".command.name");
+ String wCommand = etnProps.getProperty("etn.pgm."+i+".command.window");
+ String uCommand = etnProps.getProperty("etn.pgm."+i+".command.unix");
+ mySort.add(new CustomizedExternalProgram(program,wCommand,uCommand));
+ }
+ }
+
+ sortColumn(sortedColumn, isAscending);
+ }
+
+ public boolean isSortable(int col) {
+ if (col == 0)
+ return true;
+ else
+ return false;
+ }
+
+ public void sortColumn(int col, boolean ascending) {
+ sortedColumn = col;
+ isAscending = ascending;
+ Collections.sort(mySort, new SessionComparator(col, ascending));
+ }
+
+ public int getColumnCount() {
+
+ return cols.length;
+ }
+
+ public String getColumnName(int col) {
+ return cols[col];
+ }
+
+ public int getRowCount() {
+ return mySort.size();
+ }
+
+ /*
+ * Implement this so that the default session can be selected.
+ */
+ public void setValueAt(Object value, int row, int col) {
+
+ }
+
+ public Object getValueAt(int row, int col) {
+ CustomizedExternalProgram c = (CustomizedExternalProgram)mySort.get(row);
+ if (col == 0)
+ return c.getName();
+ if (col == 1)
+ return c.getWCommand();
+ if (col == 2) {
+ return c.getUCommand();
+ }
+ return null;
+
+ }
+
+ public boolean isCellEditable(int row, int col) {
+ return false;
+ }
+
+ /*
+ * JTable uses this method to determine the default renderer/
+ * editor for each cell. If we didn't implement this method,
+ * then the default column would contain text ("true"/"false"),
+ * rather than a check box.
+ */
+ public Class getColumnClass(int c) {
+ return getValueAt(0, c).getClass();
+ }
+
+ public void addSession() {
+ resetSorted();
+ fireTableRowsInserted(mySort.size() - 1, mySort.size() - 1);
+ }
+
+ public void chgSession(int row) {
+ resetSorted();
+ fireTableRowsUpdated(row, row);
+ }
+
+ public void removeSession(int row) {
+ resetSorted();
+ fireTableRowsDeleted(row, row);
+ }
+
+ }
+
public class SessionComparator implements Comparator {
protected int index;
--- NEW FILE: ExternalProgramConfig.java ---
package org.tn5250j;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import org.tn5250j.Configure.SomethingEnteredDocument;
import org.tn5250j.encoding.CharMappings;
import org.tn5250j.interfaces.ConfigureFactory;
import org.tn5250j.tools.AlignLayout;
import org.tn5250j.tools.LangTool;
import org.tn5250j.tools.logging.TN5250jLogFactory;
import org.tn5250j.tools.logging.TN5250jLogger;
public class ExternalProgramConfig {
private static TN5250jLogger log =
TN5250jLogFactory.getLogger("org.tn5250j.ExternalProgramConfig");
private static ExternalProgramConfig etnConfig;
public static final String EXTERNAL_PROGRAM_REGISTRY_KEY = "etnPgmProps";
public static final String EXTERNAL_PROGRAM_PROPERTIES_FILE_NAME = "tn5250jExternalProgram.properties";
public static final String EXTERNAL_PROGRAM_HEADER = "External Program Settings";
private Properties etnPgmProps;
private static Properties props = null;
private static JTextField name = null;
private static JTextField wCommand = null;
private static JTextField uCommand = null;
private static JDialog dialog = null;
private static Object[] options;
private static String num = "1";
public static ExternalProgramConfig getInstance(){
if(etnConfig == null){
etnConfig = new ExternalProgramConfig();
}
return etnConfig;
}
private ExternalProgramConfig(){
etnPgmProps = loadExternalProgramSettings();
}
public Properties getEtnPgmProps(){
return this.etnPgmProps;
}
private final Properties loadExternalProgramSettings() {
Properties etnProps = null;
try {
etnProps = ConfigureFactory.getInstance().getProperties(
EXTERNAL_PROGRAM_REGISTRY_KEY,
EXTERNAL_PROGRAM_PROPERTIES_FILE_NAME,false,
"Default Settings");
log.info("begin loading external program settings");
if (etnProps.size() == 0) {
Properties defaultProps = new Properties();
java.net.URL file=null;
ClassLoader cl = this.getClass().getClassLoader();
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
file = cl.getResource(EXTERNAL_PROGRAM_PROPERTIES_FILE_NAME);
defaultProps.load(file.openStream());
// we will now load the default settings
for (Enumeration e = defaultProps.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
etnProps.setProperty(key,defaultProps.getProperty(key));
}
ConfigureFactory.getInstance().saveSettings(EXTERNAL_PROGRAM_REGISTRY_KEY,
EXTERNAL_PROGRAM_PROPERTIES_FILE_NAME,
EXTERNAL_PROGRAM_HEADER);
}
}
catch (IOException ioe) {
log.error(ioe.getMessage());
}
catch (SecurityException se) {
log.error(se.getMessage());
}
return etnProps;
}
public static String doEntry(Frame parent, String propKey, Properties props2) {
props= props2;
name = new JTextField(20);
wCommand = new JTextField(40);
uCommand = new JTextField(40);
if (propKey != null) {
for (Enumeration e = props.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
if(props.getProperty(key) == propKey){
String subKey = key.substring(8);
int index = subKey.indexOf(".");
num = subKey.substring(0,index);
name.setText(props.getProperty("etn.pgm."+num+".command.name"));
wCommand.setText(props.getProperty("etn.pgm."+num+".command.window"));
uCommand.setText(props.getProperty("etn.pgm."+num+".command.unix"));
}
}
}
//External Program settings panel
JPanel etnp = new JPanel();
AlignLayout snpLayout = new AlignLayout(2,5,5);
etnp.setLayout(snpLayout);
etnp.setBorder(BorderFactory.createEtchedBorder());
addLabelComponent(LangTool.getString("customized.name"),
name,
etnp);
addLabelComponent(LangTool.getString("customized.window"),
wCommand,
etnp);
addLabelComponent(LangTool.getString("customized.unix"),
uCommand,
etnp);
name.setAlignmentX(Component.CENTER_ALIGNMENT);
Object[] message = new Object[1];
message[0] = etnp;
options = new JButton[2];
String title;
final String propKey2 = propKey;
if (propKey2 == null) {
Action add = new AbstractAction(LangTool.getString("conf.optAdd")) {
public void actionPerformed(ActionEvent e) {
doConfigureAction(propKey2);
}
};
options[0] = new JButton(add);
((JButton)options[0]).setEnabled(false);
title = LangTool.getString("customized.addEntryTitle");
name.setDocument(new SomethingEnteredDocument());
}
else {
Action edit = new AbstractAction(LangTool.getString("conf.optEdit")) {
public void actionPerformed(ActionEvent e) {
doConfigureAction(propKey2);
}
};
options[0] = new JButton(edit);
title = LangTool.getString("customized.editEntryTitle");
name.setEditable(false);
}
Action cancel = new AbstractAction(LangTool.getString("conf.optCancel")) {
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
};
options[1] = new JButton(cancel);
JOptionPane pane = new JOptionPane(message, JOptionPane.PLAIN_MESSAGE,
JOptionPane.DEFAULT_OPTION, null,
options, options[0]);
Component parentComponent = parent;
pane.setInitialValue(options[0]);
pane.setComponentOrientation(parentComponent.getComponentOrientation());
dialog = pane.createDialog(parentComponent, title); //, JRootPane.PLAIN_DIALOG);
dialog.setVisible(true);
return name.getText();
}
private static void addLabelComponent(String text,Component comp,Container container) {
JLabel label = new JLabel(text);
label.setAlignmentX(Component.LEFT_ALIGNMENT);
label.setHorizontalTextPosition(JLabel.LEFT);
container.add(label);
container.add(comp);
}
/**
* React to the configuration action button to perform to Add or Edit the
* entry
*
* @param e - key to act upon
*/
private static void doConfigureAction(String propKey) {
if (propKey == null) {
String count = props.getProperty("etn.pgm.support.total.num");
int maxNum = Integer.parseInt(count)+1;
props.setProperty("etn.pgm.support.total.num", String.valueOf(maxNum));
props.setProperty("etn.pgm."+maxNum+".command.name", name.getText());
props.setProperty("etn.pgm."+maxNum+".command.window", wCommand.getText());
props.setProperty("etn.pgm."+maxNum+".command.unix", uCommand.getText());
}
else {
props.setProperty("etn.pgm."+num+".command.name", name.getText());
props.setProperty("etn.pgm."+num+".command.window", wCommand.getText());
props.setProperty("etn.pgm."+num+".command.unix", uCommand.getText());
}
dialog.dispose();
}
private static class SomethingEnteredDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
super.insertString(offs, str, a);
if (getText(0, getLength()).length() > 0)
doSomethingEntered();
}
public void remove(int offs, int len) throws BadLocationException {
super.remove(offs, len);
if (getText(0, getLength()).length() == 0)
doNothingEntered();
}
}
private static void doSomethingEntered() {
((JButton)options[0]).setEnabled(true);
}
private static void doNothingEntered() {
((JButton)options[0]).setEnabled(false);
}
}
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click