Java tutorial
package com.photon.ui; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import javax.swing.AbstractAction; import javax.swing.ActionMap; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.InputMap; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JTextField; import javax.swing.KeyStroke; import javax.swing.LayoutStyle.ComponentPlacement; public class Json2JavaDialog extends javax.swing.JDialog { /** * A return status code - returned if Cancel button has been pressed */ public static final int RET_CANCEL = 0; /** * A return status code - returned if OK button has been pressed */ public static final int RET_OK = 1; private Project mProject = null; /** * Creates new form NewOkCancelDialog */ public Json2JavaDialog(Frame parent, boolean modal, Project aProject) { super(parent, modal); mProject = aProject; setResizable(false); initComponents(); // Close the dialog when Esc is pressed String cancelName = "cancel"; InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName); ActionMap actionMap = getRootPane().getActionMap(); actionMap.put(cancelName, new AbstractAction() { public void actionPerformed(ActionEvent e) { doClose(RET_CANCEL); } }); } /** * @return the return status of this dialog - one of RET_OK or RET_CANCEL */ public int getReturnStatus() { return returnStatus; } /** * 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // Generated using JFormDesigner Evaluation license - Arokiya Francis Simon private void initComponents() { okButton = new JButton(); cancelButton = new JButton(); jButton1 = new JButton(); jTextField1 = new JTextField(); jFileChooser1 = new JFileChooser(); //======== this ======== setTitle("JSON to Java conversion"); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { closeDialog(e); } }); Container contentPane = getContentPane(); //---- okButton ---- okButton.setFont(new Font("Tahoma", Font.PLAIN, 15)); okButton.setText("Convert"); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { okButtonActionPerformed(e); } }); //---- cancelButton ---- cancelButton.setFont(new Font("Tahoma", Font.PLAIN, 15)); cancelButton.setText("Cancel"); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cancelButtonActionPerformed(e); } }); //---- jButton1 ---- jButton1.setFont(new Font("Tahoma", Font.PLAIN, 15)); jButton1.setText("Browse"); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jButton1ActionPerformed(e); } }); //---- jTextField1 ---- jTextField1.setEditable(false); jTextField1.setFont(new Font("Tahoma", Font.PLAIN, 16)); jTextField1.setText("Select your JSON file"); jTextField1.setToolTipText(""); jTextField1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1ActionPerformed(e); } }); GroupLayout contentPaneLayout = new GroupLayout(contentPane); contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup(Alignment.TRAILING) .addGroup(contentPaneLayout.createSequentialGroup().addGap(18) .addGroup(contentPaneLayout.createParallelGroup(Alignment.LEADING) .addComponent(okButton, Alignment.TRAILING).addComponent(jTextField1, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED, 19, Short.MAX_VALUE) .addGroup(contentPaneLayout.createParallelGroup(Alignment.LEADING) .addComponent(jButton1, Alignment.TRAILING) .addComponent(cancelButton, Alignment.TRAILING)) .addContainerGap())); contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup(Alignment.TRAILING) .addGroup(contentPaneLayout.createSequentialGroup().addContainerGap(49, Short.MAX_VALUE) .addGroup(contentPaneLayout.createParallelGroup(Alignment.BASELINE).addComponent(jButton1) .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(59).addGroup(contentPaneLayout.createParallelGroup(Alignment.BASELINE) .addComponent(cancelButton).addComponent(okButton)) .addContainerGap())); contentPane.setLayout(contentPaneLayout); pack(); setLocationRelativeTo(getOwner()); }// </editor-fold>//GEN-END:initComponents private void okButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed doClose(RET_OK); Messages.showMessageDialog(mProject, "JSON to Java conversion successful.\nOut put path : ", "Alert", Messages.getInformationIcon()); }//GEN-LAST:event_okButtonActionPerformed private void cancelButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed doClose(RET_CANCEL); }//GEN-LAST:event_cancelButtonActionPerformed /** * Closes the dialog */ private void closeDialog(WindowEvent evt) {//GEN-FIRST:event_closeDialog doClose(RET_CANCEL); }//GEN-LAST:event_closeDialog private void jButton1ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: int returnVal = jFileChooser1.showOpenDialog(this); if (returnVal == jFileChooser1.APPROVE_OPTION) { File file = jFileChooser1.getSelectedFile(); try { // What to do with the file, e.g. display it in a TextArea // jTextField1.read( new FileReader( file.getAbsolutePath() ), null ); jTextField1.setText(file.getAbsolutePath()); System.out.println("File Path:::::" + file.getAbsolutePath()); } catch (Exception ex) { System.out.println("problem accessing file" + file.getAbsolutePath()); } } else { System.out.println("File access cancelled by user."); } }//GEN-LAST:event_jButton1ActionPerformed private void jTextField1ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextField1ActionPerformed private void doClose(int retStatus) { returnStatus = retStatus; setVisible(false); dispose(); } // Variables declaration - do not modify//GEN-BEGIN:variables // Generated using JFormDesigner Evaluation license - Arokiya Francis Simon private JButton okButton; private JButton cancelButton; private JButton jButton1; private JTextField jTextField1; private JFileChooser jFileChooser1; // End of variables declaration//GEN-END:variables private int returnStatus = RET_CANCEL; }