OpenDialog.java

Go to the documentation of this file.
00001 package theba.core.gui;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Container;
00005 import java.awt.Dialog;
00006 import java.awt.GridBagConstraints;
00007 import java.awt.GridBagLayout;
00008 import java.awt.event.ActionEvent;
00009 import java.awt.event.ActionListener;
00010 import java.io.File;
00011 import java.util.ArrayList;
00012 import java.util.Arrays;
00013 
00014 import javax.swing.JButton;
00015 import javax.swing.JComboBox;
00016 import javax.swing.JDialog;
00017 import javax.swing.JFileChooser;
00018 import javax.swing.JLabel;
00019 import javax.swing.JOptionPane;
00020 import javax.swing.JPanel;
00021 import javax.swing.JTextField;
00022 import javax.swing.border.EmptyBorder;
00023 
00024 import theba.core.PluginLoader;
00025 import theba.core.Tracker;
00026 
00027 
00035 public class OpenDialog extends JDialog implements ActionListener {
00036 
00040         private static final long serialVersionUID = 1L;
00041 
00042         private ThebaGUI control;
00043 
00044         private boolean canceled = true;
00045 
00046         private JComboBox dataType;
00047 
00048         private JComboBox trackerType;
00049 
00050         private PluginLoader loader;
00051 
00052         public OpenDialog(Dialog owner) {
00053                 super(owner);
00054                 setModal(true);
00055                 initComponents();
00056         }
00057         
00058         public OpenDialog(ThebaGUI owner) {
00059                 super(owner.getWindow());
00060                 control = owner;
00061                 loader = owner.getPluginLoader();
00062                 initComponents();
00063                 pack();
00064                 setLocationRelativeTo(owner.getWindow());
00065                 setVisible(true);
00066                 okButton.requestFocus();
00067 
00068         }
00069         
00074         public int getDataType() {
00075                 int type = dataType.getSelectedIndex();
00076                 if (type == 0)
00077                         return control.BYTE_TYPE;
00078                 return control.SHORT_TYPE;
00079         }
00080 
00081         private void initComponents() {
00082                 dialogPane = new JPanel();
00083                 contentPane = new JPanel();
00084                 label1 = new JLabel();
00085                 label2 = new JLabel();
00086                 tempFile = new JTextField();
00087                 inputFile = new JTextField();
00088                 label8 = new JLabel();
00089                 widthField = new JTextField();
00090                 label5 = new JLabel();
00091                 heightField = new JTextField();
00092                 depthLabel = new JLabel();
00093                 depthField = new JTextField();
00094                 buttonBar = new JPanel();
00095                 okButton = new JButton();
00096                 cancelButton = new JButton();
00097                 GridBagConstraints gbc;
00098                 okButton.addActionListener(this);
00099                 cancelButton.addActionListener(this);
00100                 // ======== this ========
00101                 setTitle("Open stack");
00102                 Container contentPane2 = getContentPane();
00103                 contentPane2.setLayout(new BorderLayout());
00104                 // ======== dialogPane ========
00105                 {
00106                         dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
00107                         // JFormDesigner evaluation mark
00108                         dialogPane.setLayout(new BorderLayout());
00109                         // ======== contentPane ========
00110                         {
00111                                 contentPane.setLayout(new GridBagLayout());
00112                                 ((GridBagLayout) contentPane.getLayout()).columnWidths = new int[] {
00113                                                 0, 0, 0, 0, 0, 0, 0 };
00114                                 ((GridBagLayout) contentPane.getLayout()).rowHeights = new int[] {
00115                                                 0, 0, 0, 0 };
00116                                 ((GridBagLayout) contentPane.getLayout()).columnWeights = new double[] {
00117                                                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4 };
00118                                 ((GridBagLayout) contentPane.getLayout()).rowWeights = new double[] {
00119                                                 0.0, 0.0, 0.0, 1.0E-4 };
00120                                 
00121                                 // ---- label1 ----
00122                                 label1.setText("Input file");
00123                                 gbc = new GridBagConstraints();
00124                                 gbc.gridx = 0;
00125                                 gbc.gridy = 0;
00126                                 gbc.fill = GridBagConstraints.BOTH;
00127                                 gbc.insets.bottom = 12;
00128                                 gbc.insets.right = 5;
00129                                 gbc.ipady = 10;
00130                                 contentPane.add(label1, gbc);
00131                                 
00132                                 // ---- label2 ----
00133                                 label2.setText("Tempfile");
00134                                 gbc = new GridBagConstraints();
00135                                 gbc.gridx = 2;
00136                                 gbc.gridy = 0;
00137                                 gbc.fill = GridBagConstraints.BOTH;
00138                                 gbc.insets.bottom = 12;
00139                                 gbc.insets.right = 5;
00140                                 contentPane.add(label2, gbc);
00141                                 
00142                                 // ---- textField5 ----
00143                                 tempFile.setColumns(10);
00144                                 tempFile.setText(control.getPreferences().getString("tempfileName", "temp.raw"));
00145                                 if (control.getPreferences().getInt("use_cached_access", 0) == 0)
00146                                         tempFile.setEnabled(false);
00147                                 tempFile.setToolTipText("Only available when the use_cached_access property is enabled");
00148                                 gbc = new GridBagConstraints();
00149                                 gbc.gridx = 3;
00150                                 gbc.gridy = 0;
00151                                 gbc.fill = GridBagConstraints.BOTH;
00152                                 gbc.insets.bottom = 12;
00153                                 gbc.insets.right = 5;
00154                                 contentPane.add(tempFile, gbc);
00155 
00156                                 inputFile.setColumns(10);
00157                                 inputFile.setText(control.getPreferences().getString("fileName", "sample.raw"));
00158                                 inputFile.setToolTipText("This is the name of the input file, it will be copied and will not be altered by the segmentation routine.");
00159                                 gbc = new GridBagConstraints();
00160                                 gbc.gridx = 1;
00161                                 gbc.gridy = 0;
00162                                 gbc.fill = GridBagConstraints.BOTH;
00163                                 gbc.insets.bottom = 12;
00164                                 gbc.insets.right = 5;
00165                                 contentPane.add(inputFile, gbc);
00166 
00167                                 // Create a filler-panel for the last column
00168                                 gbc = new GridBagConstraints();
00169                                 gbc.gridx = 4;
00170                                 gbc.gridy = 0;
00171                                 gbc.fill = GridBagConstraints.BOTH;
00172                                 gbc.insets.bottom = 12;
00173                                 gbc.insets.right = 5;
00174                                 JPanel p = new JPanel();
00175                                 contentPane.add(p, gbc);
00176                                 
00177                                 gbc = new GridBagConstraints();
00178                                 gbc.gridx = 5;
00179                                 gbc.gridy = 0;
00180                                 gbc.fill = GridBagConstraints.BOTH;
00181                                 gbc.insets.bottom = 12;
00182                                 contentPane.add(new JPanel(), gbc);
00183                                 
00184                                 // ---- label8 ----
00185                                 label8.setText("Width");
00186                                 widthField.setText(""
00187                                                 + control.getPreferences().getInt("volumeWidth", 1024));
00188                                 gbc = new GridBagConstraints();
00189                                 gbc.gridx = 0;
00190                                 gbc.gridy = 2;
00191                                 gbc.fill = GridBagConstraints.BOTH;
00192                                 gbc.insets.right = 5;
00193                                 gbc.ipady = 10;
00194                                 contentPane.add(label8, gbc);
00195                                 gbc = new GridBagConstraints();
00196                                 gbc.gridx = 1;
00197                                 gbc.gridy = 2;
00198                                 gbc.fill = GridBagConstraints.BOTH;
00199                                 gbc.insets.right = 5;
00200                                 contentPane.add(widthField, gbc);
00201                                 
00202                                 // ---- label5 ----
00203                                 label5.setText("Height");
00204                                 heightField.setText("" + control.getPreferences().getInt("volumeHeight", 256));
00205                                 gbc = new GridBagConstraints();
00206                                 gbc.gridx = 2;
00207                                 gbc.gridy = 2;
00208                                 gbc.fill = GridBagConstraints.BOTH;
00209                                 gbc.insets.right = 5;
00210                                 contentPane.add(label5, gbc);
00211                                 gbc = new GridBagConstraints();
00212                                 gbc.gridx = 3;
00213                                 gbc.gridy = 2;
00214                                 gbc.fill = GridBagConstraints.BOTH;
00215                                 gbc.insets.right = 5;
00216                                 contentPane.add(heightField, gbc);
00217                                 
00218                                 // ---- label6 ----
00219                                 depthLabel.setText("Depth");
00220                                 depthField.setText("" + control.getPreferences().getInt("volumeDepth", 100));
00221                                 depthField.setColumns(12);
00222                                 gbc = new GridBagConstraints();
00223                                 gbc.gridx = 4;
00224                                 gbc.gridy = 2;
00225                                 gbc.fill = GridBagConstraints.BOTH;
00226                                 gbc.insets.right = 5;
00227                                 contentPane.add(depthLabel, gbc);
00228                                 gbc = new GridBagConstraints();
00229                                 gbc.gridx = 5;
00230                                 gbc.gridy = 2;
00231                                 gbc.fill = GridBagConstraints.BOTH;
00232                                 contentPane.add(depthField, gbc);
00233                         }
00234                         dialogPane.add(contentPane, BorderLayout.WEST);
00235                         // ======== buttonBar ========
00236                         {
00237                                 buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
00238                                 buttonBar.setLayout(new GridBagLayout());
00239                                 ((GridBagLayout) buttonBar.getLayout()).columnWidths = new int[] {
00240                                                 0, 85, 80 };
00241                                 ((GridBagLayout) buttonBar.getLayout()).columnWeights = new double[] {
00242                                                 1.0, 0.0, 0.0 };
00243                                 // ---- okButton ----
00244                                 okButton.setText("OK");
00245                                 gbc = new GridBagConstraints();
00246                                 gbc.gridx = 1;
00247                                 gbc.gridy = 0;
00248                                 gbc.fill = GridBagConstraints.BOTH;
00249                                 gbc.insets.right = 5;
00250                                 buttonBar.add(okButton, gbc);
00251                                 // ---- cancelButton ----
00252                                 cancelButton.setText("Cancel");
00253                                 gbc = new GridBagConstraints();
00254                                 gbc.gridx = 2;
00255                                 gbc.gridy = 0;
00256                                 gbc.fill = GridBagConstraints.BOTH;
00257                                 buttonBar.add(cancelButton, gbc);
00258                         }
00259                         dialogPane.add(buttonBar, BorderLayout.SOUTH);
00260                 }
00261                 contentPane2.add(dialogPane, BorderLayout.CENTER);
00262                 JPanel panel = new JPanel();
00263                 JButton openButton = new JButton("Open file...");
00264                 openButton.addActionListener(new ActionListener() {
00265                         public void actionPerformed(ActionEvent e) {
00266                                 JFileChooser chooser = new JFileChooser();
00267                                 // Try to open the directory used in previous session :
00268                                 String currDir = control.getPreferences().getString("current_directory", chooser.getCurrentDirectory().getAbsolutePath());
00269                                 File defaultdir = new File(currDir+File.pathSeparator+"sampledata");
00270                                 boolean defaultdir_found = defaultdir.exists();
00271                                 if (currDir != null){
00272                                         if(defaultdir_found){
00273                                                 chooser.setCurrentDirectory(defaultdir);        
00274                                         }
00275                                         else{
00276                                                 chooser.setCurrentDirectory(new File(currDir)); 
00277                                         }
00278                                 }
00279                                 int f = chooser.showOpenDialog(control.getWindow());
00280                                 if (f != JFileChooser.CANCEL_OPTION)
00281                                         inputFile.setText(chooser.getSelectedFile().getAbsolutePath());
00282 
00283 
00284                                 control.getPreferences().setString("current_directory", chooser.getCurrentDirectory().getAbsolutePath());
00285                         }
00286                 });
00287                 panel.add(openButton);
00288                 String[] values = { "byte", "short" };
00289                 dataType = new JComboBox(values);
00290                 dataType.setSelectedIndex(control.getPreferences().getInt("inputDataType", 0));
00291                 panel.add(new JLabel("Input type"));
00292                 panel.add(dataType);
00293                 ArrayList<Tracker> trackers = loader.getTrackers();
00294                 String default_tracker = "FiberTracker";
00295                 String[] names = new String[trackers.size()];
00296                 for (int i = 0; i < names.length; i++) {
00297                         //removes the prefix "theba.trackers"
00298                         names[i] = trackers.get(i).getClass().getName().substring(15); 
00299                         if(names[i].equalsIgnoreCase(default_tracker)){ 
00300                                 names[i] = default_tracker+" (recommended)";
00301                         }
00302                 }
00303                 Arrays.sort(names);
00304                 trackerType = new JComboBox(names);
00305                 trackerType.setSelectedIndex(control.getPreferences().getInt("trackerType", 0));
00306                 panel.add(new JLabel("Tracker"));
00307                 panel.add(trackerType);
00308                 contentPane2.add(panel, BorderLayout.NORTH);
00309         }
00310 
00311         private JPanel dialogPane;
00312 
00313         private JPanel contentPane;
00314 
00315         private JLabel label1;
00316 
00317         private JLabel label2;
00318 
00319         private JTextField tempFile;
00320 
00321         private JTextField inputFile;
00322 
00323         private JLabel label8;
00324 
00325         private JTextField widthField;
00326 
00327         private JLabel label5;
00328 
00329         private JTextField heightField;
00330 
00331         private JLabel depthLabel;
00332 
00333         private JTextField depthField;
00334 
00335         private JPanel buttonBar;
00336 
00337         private JButton okButton;
00338 
00339         private JButton cancelButton;
00340 
00341         public void actionPerformed(ActionEvent e) {
00342                 if (e.getActionCommand().equals("OK")) {
00343                         setCanceled(false);
00344                         if (validateFields())
00345                                 setVisible(false);
00346                 } else if (e.getActionCommand().equals("Cancel")) {
00347                         setCanceled(true);
00348                         setVisible(false);
00349                 }
00350         }
00351 
00355         public Tracker getSelectedTracker() {
00356                 return loader.getTrackers().get(
00357                                 trackerType.getSelectedIndex());
00358         }
00359 
00363         public boolean validateFields() {
00364                 try {
00365                         volumeWidth = Integer.parseInt(widthField.getText());
00366                         volumeHeight = Integer.parseInt(heightField.getText());
00367                         volumeDepth = Integer.parseInt(depthField.getText());
00368                 } catch (NumberFormatException e) {
00369                         JOptionPane.showMessageDialog(this, "Bad input " + e.getMessage());
00370                         return false;
00371                 }
00372                 if (!(new File(inputFile.getText())).exists()) {
00373                         JOptionPane.showMessageDialog(this, "File " + inputFile.getText()
00374                                         + " not found!");
00375                         return false;
00376                 }
00377                 // store values in prefs
00378                 control.getPreferences().setInt("trackerType", trackerType.getSelectedIndex());
00379                 control.getPreferences().setInt("inputDataType", dataType.getSelectedIndex());
00380                 control.getPreferences().setInt("volumeWidth", volumeWidth);
00381                 control.getPreferences().setInt("volumeHeight", volumeHeight);
00382                 control.getPreferences().setInt("volumeDepth", volumeDepth);
00383                 control.getPreferences().setString("fileName", inputFile.getText());
00384                 control.getPreferences().setString("tempfileName", tempFile.getText());
00385                 return true;
00386         }
00387 
00388         public void setCanceled(boolean canceled) {
00389                 this.canceled = canceled;
00390         }
00391 
00392         public boolean isCanceled() {
00393                 return canceled;
00394         }
00395 
00396         int volumeWidth = 0;
00397 
00398         int volumeHeight = 0;
00399 
00400         int volumeDepth = 0;
00401 
00402         public int getVolumeDepth() {
00403                 return volumeDepth;
00404         }
00405 
00406         public void setVolumeDepth(int volumeDepth) {
00407                 this.volumeDepth = volumeDepth;
00408         }
00409 
00410         public int getVolumeHeight() {
00411                 return volumeHeight;
00412         }
00413 
00414         public void setVolumeHeight(int volumeHeight) {
00415                 this.volumeHeight = volumeHeight;
00416         }
00417 
00418         public int getVolumeWidth() {
00419                 return volumeWidth;
00420         }
00421 
00422         public void setVolumeWidth(int volumeWidth) {
00423                 this.volumeWidth = volumeWidth;
00424         }
00425 }

Generated on Fri Nov 13 08:57:07 2009 for Theba by  doxygen 1.6.1