Java tutorial
/* * IATed -- It's All Text! Editor Daemon * Copyright (C) 2010,2011 Christian Hltje * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.docwhat.iated.ui; import java.io.File; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; import org.apache.commons.exec.OS; import org.docwhat.iated.AppState; /** * The preferences dialog for the Its All Text! application. This dialog box was * designed using the NetBeans GUI designer. * @author Christian Hltje * @author Jim Hurne */ public class PreferencesDialog extends javax.swing.JDialog { private static final long serialVersionUID = 1L; private AppState state; /** Creates new form PreferencesDialog */ public PreferencesDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); this.state = AppState.INSTANCE; editorField.setText(state.getEditor()); portField.setText(String.valueOf(state.getPort())); } public String getDefaultAppDirectory() { if (OS.isFamilyMac()) { return "/Applications"; } else if (OS.isFamilyWindows() || OS.isFamilyWin9x()) { //TODO There is probably a better directory. return "C:\\"; } else if (OS.isFamilyUnix()) { return "/usr/bin"; } else { return "."; } } /** * 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" // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { saveButton = new javax.swing.JButton(); jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); editorField = new javax.swing.JTextField(); browseButton = new javax.swing.JButton(); editorLabel = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); portField = new javax.swing.JTextField(); cancelButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("It's All Text! Preferences"); setResizable(false); saveButton.setText("Save"); saveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveButtonActionPerformed(evt); } }); editorField.setText("/usr/bin/gvim"); browseButton.setText("Browse"); browseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseButtonActionPerformed(evt); } }); editorLabel.setText("Editor:"); jLabel1.setText("Port:"); portField.setColumns(5); portField.setHorizontalAlignment(javax.swing.JTextField.TRAILING); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(editorLabel).addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(editorField, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(browseButton).addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(editorLabel) .addComponent(editorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(browseButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(221, Short.MAX_VALUE))); jTabbedPane1.addTab("General", jPanel1); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cancelButton).addComponent(saveButton)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed //TODO Catch the NumberFormatException and do something with it. state.setPort(Integer.parseInt(portField.getText())); state.setEditor(editorField.getText()); this.dispose(); }//GEN-LAST:event_saveButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed this.dispose(); }//GEN-LAST:event_cancelButtonActionPerformed private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Please select your editor"); chooser.setCurrentDirectory(new File(getDefaultAppDirectory())); chooser.setFileFilter(new FileFilter() { public boolean accept(File f) { if (OS.isFamilyMac() && f.isDirectory() && f.getName().toLowerCase().endsWith(".app")) { return (true); } return f.canExecute(); } public String getDescription() { return "Applications"; } }); int result = chooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { editorField.setText(chooser.getSelectedFile().getPath()); } }//GEN-LAST:event_browseButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton browseButton; private javax.swing.JButton cancelButton; private javax.swing.JTextField editorField; private javax.swing.JLabel editorLabel; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTextField portField; private javax.swing.JButton saveButton; // End of variables declaration//GEN-END:variables }