/* * TestBeanFrameComponents.java * * */ import java.awt.event.MouseListener; import javax.swing.JComponent; import javax.swing.JFrame; import sferyx.administration.editors.SferyxUtilities; import sferyx.javascript.engine.DocumentElement; import sferyx.javascript.engine.JavaScriptEmulatorEngine; /** * * @author Vassil Boyadjiev */ public class ReferencingDocumentElements extends JFrame implements MouseListener { /** Creates new form ReferencingDocumentElements */ public ReferencingDocumentElements() { initComponents(); hTMLEditor1.setContent("<form id=\"11\"><input type=\"submit\" name=\"my_button\" id=\"3246\"></form>"); jsEngine=new JavaScriptEmulatorEngine(hTMLEditor1); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents hTMLEditor1 = new sferyx.administration.editors.HTMLEditor(); buttonsPanel = new javax.swing.JPanel(); printFormElements = new javax.swing.JButton(); getByIDButton = new javax.swing.JButton(); getByTagButton = new javax.swing.JButton(); addMouseListenerButton = new javax.swing.JButton(); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); hTMLEditor1.setRemovedMenuItems(""); hTMLEditor1.setStatusMessage(""); hTMLEditor1.setTableItemsStatus(false); getContentPane().add(hTMLEditor1, java.awt.BorderLayout.CENTER); printFormElements.setText("Print Form Elements"); printFormElements.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { printFormElementsActionPerformed(evt); } }); buttonsPanel.add(printFormElements); getByIDButton.setText("Get Elements by ID"); getByIDButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { getByIDButtonActionPerformed(evt); } }); buttonsPanel.add(getByIDButton); getByTagButton.setText("Get Elements by Tag"); getByTagButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { getByTagButtonActionPerformed(evt); } }); buttonsPanel.add(getByTagButton); addMouseListenerButton.setText("Add Mouse Listener"); addMouseListenerButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addMouseListenerButtonActionPerformed(evt); } }); buttonsPanel.add(addMouseListenerButton); getContentPane().add(buttonsPanel, java.awt.BorderLayout.SOUTH); pack(); }//GEN-END:initComponents private void addMouseListenerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addMouseListenerButtonActionPerformed // Add your handling code here: attachMouseListenerToComponent(); }//GEN-LAST:event_addMouseListenerButtonActionPerformed private void getByTagButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getByTagButtonActionPerformed // Add your handling code here: printElementsByTag(); }//GEN-LAST:event_getByTagButtonActionPerformed private void getByIDButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getByIDButtonActionPerformed // Add your handling code here: printElementsByID(); }//GEN-LAST:event_getByIDButtonActionPerformed private void printFormElementsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printFormElementsActionPerformed // Add your handling code here: printFormElements(); }//GEN-LAST:event_printFormElementsActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { ReferencingDocumentElements frame=new ReferencingDocumentElements(); frame.setLocation(100,100); frame.setSize(800,600); frame.show(); } public void mouseClicked(java.awt.event.MouseEvent mouseEvent) { } public void mouseEntered(java.awt.event.MouseEvent mouseEvent) { } public void mouseExited(java.awt.event.MouseEvent mouseEvent) { } public void mousePressed(java.awt.event.MouseEvent mouseEvent) { } public void mouseReleased(java.awt.event.MouseEvent mouseEvent) { System.out.println("==> Mouse Released:"+mouseEvent); } public void printFormElements() { // Note that if there are no forms, the "forms" property will be null. Handle this accordingly // In these case use the alternative ways are to use the getElementById, getElementsByTagName or getElementsByName if(jsEngine.getDocument().forms==null) { System.out.println("No form elements in the document"); return; } DocumentElement[] elements=jsEngine.getDocument().forms[0].elements; for(int i=0;i<elements.length;i++) { System.out.println("------Element Start---------"); System.out.print(elements[i]); System.out.println("------Element End---------"); } } public void printElementsByID() { //Will return the element with specified ID attribute DocumentElement element=jsEngine.getDocument().getElementById("3246"); System.out.println("------Element Start---------"); System.out.print(element); System.out.println("------Element End---------"); } public void printElementsByTag() { //Will return all elements with specified tag DocumentElement[] elements=jsEngine.getDocument().getElementsByTagName("INPUT"); for(int i=0;i<elements.length;i++) { System.out.println("------Element Start---------"); System.out.print(elements[i]); System.out.println("------Element End---------"); } } public void attachMouseListenerToComponent() { //Will attach a mouse listener to an element with specified ID attribute. DocumentElement element=jsEngine.getDocument().getElementById("3246"); JComponent component=SferyxUtilities.getJavaFormItemComponent(element, hTMLEditor1); System.out.println("Adding Mouse Listener to :"+component); component.addMouseListener(this); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton getByIDButton; private javax.swing.JPanel buttonsPanel; private javax.swing.JButton addMouseListenerButton; private sferyx.administration.editors.HTMLEditor hTMLEditor1; private javax.swing.JButton getByTagButton; private javax.swing.JButton printFormElements; // End of variables declaration//GEN-END:variables JavaScriptEmulatorEngine jsEngine; }