Project: beanmill_1
/***************************************************
* cismet GmbH, Saarbruecken, Germany 
*              ... and it just works. 
****************************************************/
package de.cismet.beanmill; 
 
import com.traxel.lumbermill.event.Event; 
import com.traxel.lumbermill.event.TableView; 
 
import org.apache.log4j.Logger; 
 
import org.openide.util.NbPreferences; 
 
import java.awt.Container; 
import java.awt.Dialog; 
 
/**
 * DOCUMENT ME! 
 * 
 * @version  $Revision$, $Date$ 
 */
 
public final class NetbeansPanel extends javax.swing.JPanel { 
 
    //~ Static fields/initializers --------------------------------------------- 
 
    private static final transient Logger LOG = Logger.getLogger(NetbeansPanel.class); 
 
    public static final String PROP_LISTENER_PORT = "listenerPort";               // NOI18N 
    public static final String PROP_SHOW_HTML_MSG = "showHTMLMessage";            // NOI18N 
    public static final String PROP_SHOW_RAW_MSG = "showRawMessage";              // NOI18N 
    public static final String PROP_TOOLTIP_HK = "tooltipHotKey";                 // NOI18N 
    public static final String PROP_TOOLTIP_HK_SUBST = "tooltipHotkeySubstitute"// NOI18N 
    public static final String VAL_HK_NONE = "none";                              // NOI18N 
 
    //~ Instance fields -------------------------------------------------------- 
 
    private int tooltipHotkey; 
 
    // Variables declaration - do not modify//GEN-BEGIN:variables 
    private javax.swing.JButton btnClearHotkey; 
    private javax.swing.JCheckBox chkHTMLMessage; 
    private javax.swing.JCheckBox chkRawMessage; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JSeparator jSeparator1; 
    private javax.swing.JSeparator jSeparator2; 
    private javax.swing.JTextField txtHotkey; 
    private javax.swing.JTextField txtPort; 
    // End of variables declaration//GEN-END:variables 
 
    //~ Constructors ----------------------------------------------------------- 
 
    /**
     * Creates a new NetbeansPanel object. 
     * 
     * @param  controller  DOCUMENT ME! 
     */
 
    NetbeansPanel(final NetbeansOptionsPanelController controller) { 
        if (LOG.isDebugEnabled()) { 
            LOG.debug("creating new NetbeansPanel, controller: " + controller); // NOI18N 
        } 
        initComponents(); 
        // TODO listen to changes in form fields and call controller.changed() 
    } 
 
    //~ Methods ---------------------------------------------------------------- 
 
    /**
     * 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. 
     */
 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 
    private void initComponents() { 
        jLabel1 = new javax.swing.JLabel(); 
        txtPort = new javax.swing.JTextField(); 
        jLabel2 = new javax.swing.JLabel(); 
        jSeparator1 = new javax.swing.JSeparator(); 
        jLabel3 = new javax.swing.JLabel(); 
        txtHotkey = new javax.swing.JTextField(); 
        btnClearHotkey = new javax.swing.JButton(); 
        jSeparator2 = new javax.swing.JSeparator(); 
        chkHTMLMessage = new javax.swing.JCheckBox(); 
        chkRawMessage = new javax.swing.JCheckBox(); 
 
        setBackground(java.awt.Color.white); 
        setOpaque(false); 
 
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Port:"); 
 
        txtPort.setText("4446"); 
        txtPort.addActionListener(new java.awt.event.ActionListener() { 
 
                @Override 
                public void actionPerformed(final java.awt.event.ActionEvent evt) { 
                    txtPortActionPerformed(evt); 
                } 
            }); 
 
        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "(Restart required)"); 
 
        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "Tooltip Hotkey:"); 
 
        txtHotkey.setEditable(false); 
        txtHotkey.setHorizontalAlignment(javax.swing.JTextField.CENTER); 
        txtHotkey.setText("none"); 
        txtHotkey.setMinimumSize(new java.awt.Dimension(4528)); 
        txtHotkey.addMouseListener(new java.awt.event.MouseAdapter() { 
 
                @Override 
                public void mouseClicked(final java.awt.event.MouseEvent evt) { 
                    txtHotkeyMouseClicked(evt); 
                } 
            }); 
        txtHotkey.addActionListener(new java.awt.event.ActionListener() { 
 
                @Override 
                public void actionPerformed(final java.awt.event.ActionEvent evt) { 
                    txtHotkeyActionPerformed(evt); 
                } 
            }); 
 
        org.openide.awt.Mnemonics.setLocalizedText(btnClearHotkey, "Clear"); 
        btnClearHotkey.addActionListener(new java.awt.event.ActionListener() { 
 
                @Override 
                public void actionPerformed(final java.awt.event.ActionEvent evt) { 
                    btnClearHotkeyActionPerformed(evt); 
                } 
            }); 
 
        org.openide.awt.Mnemonics.setLocalizedText(chkHTMLMessage, "Show HTML message"); 
 
        org.openide.awt.Mnemonics.setLocalizedText(chkRawMessage, "Show raw message"); 
 
        final org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 
        this.setLayout(layout); 
        layout.setHorizontalGroup( 
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add( 
                layout.createSequentialGroup().addContainerGap().add(jLabel1).addPreferredGap( 
                    org.jdesktop.layout.LayoutStyle.RELATED).add( 
                    txtPort, 
                    org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 
                    212
                    Short.MAX_VALUE).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(jLabel2) 
                            .addContainerGap()).add( 
                org.jdesktop.layout.GroupLayout.TRAILING, 
                jSeparator1, 
                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 
                409
                Short.MAX_VALUE).add( 
                layout.createSequentialGroup().addContainerGap().add(jLabel3).addPreferredGap( 
                    org.jdesktop.layout.LayoutStyle.RELATED).add( 
                    txtHotkey, 
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 
                    87
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap( 
                    org.jdesktop.layout.LayoutStyle.RELATED).add(btnClearHotkey).addContainerGap( 
                    109
                    Short.MAX_VALUE)).add( 
                layout.createSequentialGroup().add( 
                    jSeparator2, 
                    org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 
                    389
                    Short.MAX_VALUE).addContainerGap()).add( 
                layout.createSequentialGroup().addContainerGap().add(chkHTMLMessage).addContainerGap( 
                    228
                    Short.MAX_VALUE)).add( 
                layout.createSequentialGroup().addContainerGap().add(chkRawMessage).addContainerGap( 
                    242
                    Short.MAX_VALUE))); 
        layout.setVerticalGroup( 
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add( 
                layout.createSequentialGroup().addContainerGap().add( 
                    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel1).add(jLabel2).add( 
                        txtPort, 
                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 
                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 
                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).addPreferredGap( 
                    org.jdesktop.layout.LayoutStyle.RELATED).add( 
                    jSeparator1, 
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 
                    10
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap( 
                    org.jdesktop.layout.LayoutStyle.RELATED).add( 
                    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add( 
                        txtHotkey, 
                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 
                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 
                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jLabel3).add(btnClearHotkey)) 
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add( 
                    jSeparator2, 
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 
                    10
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap( 
                    org.jdesktop.layout.LayoutStyle.RELATED).add(chkHTMLMessage).addPreferredGap( 
                    org.jdesktop.layout.LayoutStyle.RELATED).add(chkRawMessage).addContainerGap(43, Short.MAX_VALUE))); 
    } // </editor-fold>//GEN-END:initComponents 
 
    /**
     * DOCUMENT ME! 
     * 
     * @param  evt  DOCUMENT ME! 
     */
 
    private void txtHotkeyActionPerformed(final java.awt.event.ActionEvent evt) //GEN-FIRST:event_txtHotkeyActionPerformed 
    {                                                                           //GEN-HEADEREND:event_txtHotkeyActionPerformed 
    }                                                                           //GEN-LAST:event_txtHotkeyActionPerformed 
 
    /**
     * DOCUMENT ME! 
     * 
     * @param  evt  DOCUMENT ME! 
     */
 
    private void txtHotkeyMouseClicked(final java.awt.event.MouseEvent evt) //GEN-FIRST:event_txtHotkeyMouseClicked 
    {                                                                       //GEN-HEADEREND:event_txtHotkeyMouseClicked 
        if (LOG.isDebugEnabled()) { 
            LOG.debug("creating ChooseHotkeyDialog");                       // NOI18N 
        } 
        final ChooseHotkeyDialog dialog = ChooseHotkeyDialog.showHotkeyDialog(getParentDialog(this)); 
 
        if (dialog.getHotkey() != -1) { 
            tooltipHotkey = dialog.getHotkey(); 
            txtHotkey.setText(dialog.getHKSubstitute()); 
        } 
    } //GEN-LAST:event_txtHotkeyMouseClicked 
 
    /**
     * DOCUMENT ME! 
     * 
     * @param  evt  DOCUMENT ME! 
     */
 
    private void txtPortActionPerformed(final java.awt.event.ActionEvent evt) //GEN-FIRST:event_txtPortActionPerformed 
    {                                                                         //GEN-HEADEREND:event_txtPortActionPerformed 
                                                                              // TODO add your handling code here: 
    }                                                                         //GEN-LAST:event_txtPortActionPerformed 
 
    /**
     * DOCUMENT ME! 
     * 
     * @param  evt  DOCUMENT ME! 
     */
 
    private void btnClearHotkeyActionPerformed(final java.awt.event.ActionEvent evt) //GEN-FIRST:event_btnClearHotkeyActionPerformed 
    {                                                                                //GEN-HEADEREND:event_btnClearHotkeyActionPerformed 
        txtHotkey.setText(VAL_HK_NONE); 
        tooltipHotkey = -1
    }                                                                                //GEN-LAST:event_btnClearHotkeyActionPerformed 
 
    /**
     * DOCUMENT ME! 
     * 
     * @param   c  DOCUMENT ME! 
     * 
     * @return  DOCUMENT ME! 
     */
 
    private Dialog getParentDialog(final Container c) { 
        if (c == null) { 
            return null
        } else if (c instanceof Dialog) { 
            return (Dialog)c; 
        } else { 
            return getParentDialog(c.getParent()); 
        } 
    } 
 
    /**
     * DOCUMENT ME! 
     */
 
    void load() { 
        txtPort.setText(String.valueOf( 
                NbPreferences.forModule(LoggingTopComponent.class).getInt(PROP_LISTENER_PORT, 4445))); 
        txtHotkey.setText(NbPreferences.forModule(TableView.class).get(PROP_TOOLTIP_HK_SUBST, VAL_HK_NONE)); 
        tooltipHotkey = NbPreferences.forModule(TableView.class).getInt(PROP_TOOLTIP_HK, -1); 
        chkHTMLMessage.setSelected(NbPreferences.forModule(Event.class).getBoolean(PROP_SHOW_HTML_MSG, true)); 
        chkRawMessage.setSelected(NbPreferences.forModule(Event.class).getBoolean(PROP_SHOW_RAW_MSG, true)); 
    } 
 
    /**
     * INFO: the hotkey prefs will be stored relative to the TableView class to enable retrievel from the not-netbeans 
     * jar 
     */
 
    void store() { 
        try { 
            NbPreferences.forModule(LoggingTopComponent.class
                    .putInt(PROP_LISTENER_PORT, new Integer(txtPort.getText())); 
        } catch (final Exception e) { 
            LOG.warn("could not store Beanmill port prefs", e); // NOI18N 
            NbPreferences.forModule(LoggingTopComponent.class).putInt(PROP_LISTENER_PORT, 4445); 
        } 
        try { 
            NbPreferences.forModule(TableView.class).put(PROP_TOOLTIP_HK_SUBST, txtHotkey.getText()); 
            NbPreferences.forModule(TableView.class).putInt(PROP_TOOLTIP_HK, tooltipHotkey); 
        } catch (final Exception e) { 
            LOG.warn("could not store Beanmill hotkey prefs", e); // NOI18N 
            NbPreferences.forModule(TableView.class).put(PROP_TOOLTIP_HK_SUBST, VAL_HK_NONE); // NOI18N 
            NbPreferences.forModule(TableView.class).putInt(PROP_TOOLTIP_HK, -1); 
        } 
        try { 
            NbPreferences.forModule(Event.class).putBoolean(PROP_SHOW_HTML_MSG, chkHTMLMessage.isSelected()); 
        } catch (final Exception e) { 
            LOG.warn("could not store Beanmill showHTMLMessage prefs", e); // NOI18N 
            NbPreferences.forModule(Event.class).putBoolean(PROP_SHOW_HTML_MSG, true); 
        } 
        try { 
            NbPreferences.forModule(Event.class).putBoolean(PROP_SHOW_RAW_MSG, chkRawMessage.isSelected()); 
        } catch (final Exception e) { 
            LOG.warn("could not store Beanmill showRawMessage prefs", e); // NOI18N 
            NbPreferences.forModule(Event.class).putBoolean(PROP_SHOW_RAW_MSG, true); 
        } 
    } 
 
    /**
     * DOCUMENT ME! 
     * 
     * @return  DOCUMENT ME! 
     */
 
    boolean valid() { 
        // TODO check whether form is consistent and complete 
        return true
    } 
}