package com.realobjects.customdialog.insertsymbol; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableCellRenderer; // This package must be included, edit-on-pro4.jar must be in the class path build the custom class import com.realobjects.eop.japplet.SwingEditorApplet; // This package is imported in order to be able to use centerOnComponent import com.realobjects.eop.bean.custom.CustomDialogUtil; // Sample custom class implementation public class InsertSymbolDialog extends AbstractAction { private String m_symbol = ""; // This method will be called when the custom action is fired public void actionPerformed(ActionEvent arg0) { Frame f = (Frame)getValue("frame"); SwingEditorApplet applet = (SwingEditorApplet)getValue("apiobject"); SymbolDlg dlg = new SymbolDlg(f, "Custom Insert Symbol Dialog", true); dlg.setVisible(true); if(dlg.retVal) { if(m_symbol.length()>0){ applet.insertHTMLData(applet.encode(m_symbol)); } } } // we extend the default table model to have a read-only table public class ReadOnlyTableModel extends DefaultTableModel { public ReadOnlyTableModel(Object[][] data, Object[] columnNames) { super(data, columnNames); } public boolean isCellEditable(int row, int column) { return false; } } protected class SymbolDlg extends JDialog implements ActionListener { public SymbolDlg(Frame frame, String title, boolean modal) { super(frame, title, modal); // we don't need column names String[] columnNames = { "", "", "", "", "" }; // populate the table data Object[][] data = { // first table row {String.valueOf((char) 945), String.valueOf((char) 946),String.valueOf((char) 947),String.valueOf((char) 948),String.valueOf((char) 949)}, // second row, etc. {String.valueOf((char) 950), String.valueOf((char) 951),String.valueOf((char) 952), String.valueOf((char) 953),String.valueOf((char) 954)}, {String.valueOf((char) 955), String.valueOf((char) 956),String.valueOf((char) 957), String.valueOf((char) 958), String.valueOf((char) 959)}, {String.valueOf((char) 961), String.valueOf((char) 962),String.valueOf((char) 963), String.valueOf((char) 964), String.valueOf((char) 965)}, {String.valueOf((char) 966), String.valueOf((char) 967),String.valueOf((char) 968), String.valueOf((char) 969), String.valueOf((char) 970)} }; ReadOnlyTableModel model = new ReadOnlyTableModel(data, columnNames); final JTable table = new JTable(model); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setSelectionBackground(Color.YELLOW); table.setSelectionForeground(Color.BLACK); table.setRowSelectionAllowed(true); table.setColumnSelectionAllowed(true); //Ask to be notified of selection changes. ListSelectionModel rowSM = table.getSelectionModel(); rowSM.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 } else { int row = table.getSelectedRow(); int column = table.getSelectedColumn(); String cell = (String)table.getValueAt(row, column); m_symbol = cell; } } } ); JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER)); JButton btn = new JButton("Ok"); btn.addActionListener(this); panel.add(btn); btn = new JButton("Cancel"); btn.addActionListener(this); panel.add(btn); Container cont = getContentPane(); cont.setLayout(new BorderLayout()); cont.add(table,BorderLayout.CENTER ); cont.add(panel,BorderLayout.SOUTH); setResizable(false); setSize(250,159); // Centers the dialog on the editor CustomDialogUtil.centerOnComponent(frame,this); } public void actionPerformed( ActionEvent e ) { if (e.getActionCommand().equalsIgnoreCase("Ok")) { retVal = true; dispose(); } else { dispose(); } } protected boolean retVal = false; } }