Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ProgramSettingsPanel.java

Go to the documentation of this file.
00001 /*
00002  * ProgramSettingsPanel.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki.gui;
00024 
00025 import com.intellij.uiDesigner.core.GridConstraints;
00026 import com.intellij.uiDesigner.core.GridLayoutManager;
00027 import com.intellij.uiDesigner.core.Spacer;
00028 import ikayaki.Ikayaki;
00029 import ikayaki.MeasurementSequence;
00030 import ikayaki.Settings;
00031 
00032 import javax.swing.*;
00033 import javax.swing.event.ListSelectionEvent;
00034 import javax.swing.event.ListSelectionListener;
00035 import javax.swing.table.AbstractTableModel;
00036 import javax.swing.text.DefaultFormatterFactory;
00037 import javax.swing.text.NumberFormatter;
00038 import java.awt.*;
00039 import java.awt.event.*;
00040 import java.beans.PropertyChangeEvent;
00041 import java.beans.PropertyChangeListener;
00042 import java.io.File;
00043 import java.util.List;
00044 
00050 public class ProgramSettingsPanel extends JPanel {
00051 
00052     private JDialog creator;
00053 
00054     private JFormattedTextField measurementRotationsField;
00055     private JComboBox holderCalibrationCombo;
00056     private JTable sequencesTable;
00057     private JButton sequencesDeleteButton;
00058     private JPanel defaultColumnsPane;
00059     private JButton closeButton;
00060     private JPanel contentPane;
00061 
00062     public ProgramSettingsPanel(JDialog dialog) {
00063         this.creator = dialog;
00064 
00065         $$$setupUI$$$();
00066         setLayout(new BorderLayout());
00067         add(contentPane, BorderLayout.CENTER);
00068 
00069         sequencesDeleteButton.setMnemonic(KeyEvent.VK_D);
00070         closeButton.setMnemonic(KeyEvent.VK_C);
00071 
00072         /* Measurement Rotations */
00073 
00074         NumberFormatter format = new NumberFormatter();
00075         format.setMinimum(new Integer(0));
00076         format.setMaximum(new Integer(999));
00077         measurementRotationsField.setFormatterFactory(new DefaultFormatterFactory(format));
00078         measurementRotationsField.setValue(new Integer(Settings.getMeasurementRotations()));
00079 
00080         // autosaving
00081         measurementRotationsField.addPropertyChangeListener("value", new PropertyChangeListener() {
00082             public void propertyChange(PropertyChangeEvent evt) {
00083                 int value = ((Number) measurementRotationsField.getValue()).intValue();
00084                 Settings.setMeasurementRotations(value);
00085             }
00086         });
00087 
00088         // automatically select all text to make the entering of a new value easy
00089         measurementRotationsField.addFocusListener(new FocusListener() {
00090             public void focusGained(FocusEvent e) {
00091                 SwingUtilities.invokeLater(new Runnable() {
00092                     public void run() {
00093                         measurementRotationsField.setSelectionStart(0);
00094                         measurementRotationsField.setSelectionEnd(measurementRotationsField.getText().length());
00095                     }
00096                 });
00097             }
00098 
00099             public void focusLost(FocusEvent e) {
00100                 // DO NOTHING
00101             }
00102         });
00103 
00104         /* Sample Holder Calibration */
00105 
00106         final File[] calibrationFiles = Settings.getCalibrationProjectFiles();
00107         File holderCalibrationFile = Settings.getHolderCalibrationFile();
00108         holderCalibrationCombo.addItem("");     // option for selecting no file
00109 
00110         // add all calibration projects to the list
00111         for (int i = 0; i < calibrationFiles.length; i++) {
00112             File file = calibrationFiles[i];
00113             String name = file.getName().substring(0, file.getName().lastIndexOf(Ikayaki.FILE_TYPE));
00114             holderCalibrationCombo.addItem(name);
00115 
00116             if (file.equals(holderCalibrationFile)) {
00117                 holderCalibrationCombo.setSelectedIndex(holderCalibrationCombo.getItemCount() - 1);
00118             }
00119         }
00120 
00121         // autosaving
00122         holderCalibrationCombo.addActionListener(new ActionListener() {
00123             public void actionPerformed(ActionEvent e) {
00124                 int index = holderCalibrationCombo.getSelectedIndex();
00125                 index--;
00126                 if (index >= 0 && index < calibrationFiles.length) {
00127                     Settings.setHolderCalibrationFile(calibrationFiles[index]);
00128                 } else {
00129                     Settings.setHolderCalibrationFile(null);
00130                 }
00131             }
00132         });
00133 
00134         /* Saved Sequences */
00135 
00136         final EditSequencesTableModel tableModel = new EditSequencesTableModel();
00137         sequencesTable.setModel(tableModel);
00138         sequencesTable.setTableHeader(null);
00139         sequencesTable.getParent().setBackground(sequencesTable.getBackground());
00140         sequencesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
00141 
00142         // deleting of sequences
00143         sequencesDeleteButton.addActionListener(new ActionListener() {
00144             public void actionPerformed(ActionEvent e) {
00145                 int index = sequencesTable.getSelectedRow();
00146                 if (sequencesTable.isEditing()) {
00147                     // cancel the editing of the selected cell to avoid renaming the next sequence in the list
00148                     sequencesTable.editCellAt(-1, -1);
00149                 }
00150                 if (index >= 0) {
00151                     tableModel.deleteSequence(index);
00152                 }
00153             }
00154         });
00155 
00156         // a sequence must be selected to be able to delete it
00157         sequencesDeleteButton.setEnabled(false);
00158         sequencesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
00159             public void valueChanged(ListSelectionEvent e) {
00160                 sequencesDeleteButton.setEnabled(sequencesTable.getSelectedRow() >= 0);
00161             }
00162         });
00163 
00164         /* Default Columns */
00165 
00166         SequenceColumn[] allColumns = SequenceColumn.getAllColumns();
00167         List<SequenceColumn> defaultColumns = Settings.getDefaultColumns();
00168 
00169         defaultColumnsPane.setLayout(new GridBagLayout());
00170         GridBagConstraints gc = new GridBagConstraints();
00171         gc.fill = GridBagConstraints.BOTH;
00172         gc.ipadx = 10;
00173         gc.gridy = 0;
00174 
00175         // add all columns to the list as checkboxes
00176         for (final SequenceColumn column : allColumns) {
00177             final JCheckBox checkBox = new JCheckBox(column.getColumnName(null));
00178             JLabel description = new JLabel(column.getToolTipText(null));
00179 
00180             // autosaving
00181             checkBox.addActionListener(new ActionListener() {
00182                 public void actionPerformed(ActionEvent e) {
00183                     Settings.setDefaultColumn(column, checkBox.isSelected());
00184                 }
00185             });
00186             checkBox.setSelected(defaultColumns.contains(column));
00187 
00188             gc.gridx = 0;
00189             defaultColumnsPane.add(checkBox, gc);
00190             gc.gridx = 1;
00191             defaultColumnsPane.add(description, gc);
00192             gc.gridy++;
00193         }
00194 
00195         /* Close */
00196 
00197         closeButton.addActionListener(new ActionListener() {
00198             public void actionPerformed(ActionEvent e) {
00199                 creator.setVisible(false);
00200             }
00201         });
00202 
00203         // small hack to set the default button
00204         this.addHierarchyListener(new HierarchyListener() {
00205             public void hierarchyChanged(HierarchyEvent e) {
00206                 if (getRootPane().getDefaultButton() != closeButton) {
00207                     getRootPane().setDefaultButton(closeButton);
00208                 }
00209             }
00210         });
00211 
00212         // avoid the need to press enter twise in the measurementRotationsField to close the window
00213         measurementRotationsField.addActionListener(new ActionListener() {
00214             public void actionPerformed(ActionEvent e) {
00215                 closeButton.doClick();
00216             }
00217         });
00218     }
00219 
00220     {
00221 // GUI initializer generated by IntelliJ IDEA GUI Designer
00222 // !!! IMPORTANT !!!
00223 // DO NOT EDIT OR ADD ANY CODE HERE!
00224         $$$setupUI$$$();
00225     }
00226 
00231     private void $$$setupUI$$$() {
00232         contentPane = new JPanel();
00233         contentPane.setLayout(new GridLayoutManager(3, 2, new Insets(11, 11, 11, 11), -1, -1));
00234         final JPanel panel1 = new JPanel();
00235         panel1.setLayout(new GridLayoutManager(2, 1, new Insets(0, 4, 4, 4), -1, -1));
00236         contentPane.add(panel1,
00237                 new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00238                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00239                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
00240         panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Saved Sequences"));
00241         final JScrollPane scrollPane1 = new JScrollPane();
00242         scrollPane1.setVerticalScrollBarPolicy(22);
00243         panel1.add(scrollPane1,
00244                 new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00245                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
00246                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null));
00247         sequencesTable = new JTable();
00248         sequencesTable.setPreferredScrollableViewportSize(new Dimension(200, 200));
00249         scrollPane1.setViewportView(sequencesTable);
00250         final JPanel panel2 = new JPanel();
00251         panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
00252         panel1.add(panel2,
00253                 new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00254                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00255                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
00256         final Spacer spacer1 = new Spacer();
00257         panel2.add(spacer1,
00258                 new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
00259                         GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null));
00260         sequencesDeleteButton = new JButton();
00261         sequencesDeleteButton.setText("Delete Sequence");
00262         panel2.add(sequencesDeleteButton,
00263                 new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
00264                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00265                         GridConstraints.SIZEPOLICY_FIXED, null, null, null));
00266         final JPanel panel3 = new JPanel();
00267         panel3.setLayout(new GridLayoutManager(2, 1, new Insets(0, 4, 4, 4), -1, -1));
00268         contentPane.add(panel3,
00269                 new GridConstraints(0, 1, 2, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00270                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00271                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
00272         panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Default Columns"));
00273         defaultColumnsPane = new JPanel();
00274         panel3.add(defaultColumnsPane,
00275                 new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00276                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00277                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
00278         final Spacer spacer2 = new Spacer();
00279         panel3.add(spacer2,
00280                 new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1,
00281                         GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null));
00282         final JPanel panel4 = new JPanel();
00283         panel4.setLayout(new GridLayoutManager(1, 1, new Insets(0, 4, 4, 4), -1, -1));
00284         contentPane.add(panel4,
00285                 new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00286                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00287                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
00288         panel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Measurements"));
00289         final JPanel panel5 = new JPanel();
00290         panel5.setLayout(new GridLayoutManager(2, 3, new Insets(0, 0, 0, 0), -1, -1));
00291         panel4.add(panel5,
00292                 new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00293                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00294                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
00295         holderCalibrationCombo = new JComboBox();
00296         panel5.add(holderCalibrationCombo,
00297                 new GridConstraints(1, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
00298                         GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
00299         final JLabel label1 = new JLabel();
00300         label1.setText("Sample Holder Calibration");
00301         panel5.add(label1,
00302                 new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
00303                         GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
00304         final JLabel label2 = new JLabel();
00305         label2.setText("Measurement Rotations");
00306         panel5.add(label2,
00307                 new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
00308                         GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
00309         measurementRotationsField = new JFormattedTextField();
00310         panel5.add(measurementRotationsField,
00311                 new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
00312                         GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(50, -1),
00313                         null));
00314         final Spacer spacer3 = new Spacer();
00315         panel5.add(spacer3,
00316                 new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
00317                         GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null));
00318         final JPanel panel6 = new JPanel();
00319         panel6.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
00320         contentPane.add(panel6,
00321                 new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
00322                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00323                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
00324         closeButton = new JButton();
00325         closeButton.setText("Close");
00326         panel6.add(closeButton,
00327                 new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
00328                         GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
00329                         GridConstraints.SIZEPOLICY_FIXED, null, null, null));
00330         final Spacer spacer4 = new Spacer();
00331         panel6.add(spacer4,
00332                 new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
00333                         GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null));
00334     }
00335 
00336     private class EditSequencesTableModel extends AbstractTableModel {
00337 
00338         private MeasurementSequence[] sequences = new MeasurementSequence[0];
00339 
00340         public EditSequencesTableModel() {
00341             updateSequences();
00342         }
00343 
00348         private void updateSequences() {
00349 
00350             // save selection
00351             MeasurementSequence selected = null;
00352             int selectedRow = sequencesTable.getSelectedRow();
00353             if (selectedRow >= 0) {
00354                 selected = sequences[selectedRow];
00355             }
00356 
00357             // update table data
00358             sequences = Settings.getSequences();
00359             sequencesTable.getSelectionModel().clearSelection();
00360             fireTableDataChanged();
00361 
00362             // restore selection
00363             if (selected != null) {
00364                 for (int i = 0; i < sequences.length; i++) {
00365                     if (sequences[i] == selected) {
00366                         sequencesTable.getSelectionModel().setSelectionInterval(i, i);
00367                         break;
00368                     }
00369                 }
00370             }
00371         }
00372 
00373         public int getRowCount() {
00374             return sequences.length;
00375         }
00376 
00377         public int getColumnCount() {
00378             return 1;
00379         }
00380 
00381         @Override public boolean isCellEditable(int rowIndex, int columnIndex) {
00382             return true;
00383         }
00384 
00385         public Object getValueAt(int rowIndex, int columnIndex) {
00386             return sequences[rowIndex].getName();
00387         }
00388 
00396         @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
00397             String name = aValue.toString();
00398             MeasurementSequence target = sequences[rowIndex];
00399 
00400             if (target.getName().equals(name)) {
00401                 return;     // no change
00402             }
00403 
00404             // check for a duplicate name
00405             for (MeasurementSequence sequence : sequences) {
00406                 if (sequence.getName().equals(name)) {
00407                     return;
00408                 }
00409             }
00410 
00411             // set the new name
00412             target.setName(name);
00413             Settings.fireSequencesModified();
00414             updateSequences();
00415         }
00416 
00420         public void deleteSequence(int rowIndex) {
00421             Settings.removeSequence(sequences[rowIndex]);
00422             updateSequences();
00423         }
00424     }
00425 }

Generated on Fri May 6 16:00:32 2005 for Squid by  doxygen 1.4.1