001    /*
002     * CRIMSON
003     * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 
004     * University of Pennsylvania.
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License as
008     * published by the Free Software Foundation; either version 2 of the
009     * License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful, but
012     * WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019     * 02110-1301 USA.
020     *
021     * @(#)QueryDialog.java
022     */
023    
024    package edu.upenn.crimson.gui;
025    
026    import edu.upenn.crimson.*;
027    import java.awt.*;
028    import java.awt.event.*;
029    import javax.swing.*;
030    import java.io.File;
031    import javax.swing.border.Border;
032    
033    /**
034     * This will query the specified query, using a dialog box to get the
035     * filename, repeat, and incSequence info.
036     *
037     * @author  Stephen Fisher
038     * @version $Id: QueryDialog.java,v 1.10 2009/07/21 01:19:12 fisher Exp $
039     */
040    public class QueryDialog extends JDialog {
041             QueryDialog thisDialog;
042    
043             private Query query;
044    
045             private JTextField fileNameTF = new JTextField(10);
046             private JButton fileBrowseB;
047             private JCheckBox fileIntSeqCB = new JCheckBox("Include Internal Sequence");
048             // start = 1, min = 1, max = 100, step = 1
049             private JSpinner fileRepeatS = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
050    
051             private JButton runB;
052             private JButton cancelB;
053    
054             public QueryDialog(String queryID) {
055                      this(queryID, "", false, 1);
056             }
057    
058             public QueryDialog(String queryID, String file, boolean incSequence, int repeat) {
059                      super((Frame) null, "Run Query", true);
060    
061                      // keep pointer to self so can 'dispose' Dialog below
062                      thisDialog = this;
063    
064                      query = ObjectHandles.getQuery(queryID);
065                      if (query == null) {
066                                    CrimsonUtils.printMsg("Unable to access query: " + queryID, CrimsonUtils.ERROR);
067                                    return;
068                      }
069    
070                      // *************************************************
071                      // SETUP DEFAULTS
072                      fileNameTF.setText(file);
073                      
074                      fileIntSeqCB.setSelected(incSequence);
075    
076                      // if including all leaves and base pairs then disable option
077                      // for repeated runs
078                      if ((query.getLeafSelection() == 0) && (query.getSequenceSelection() == 0)) {
079                                    fileRepeatS.setEnabled(false);
080                      } else {
081                                    fileRepeatS.setValue(new Integer(repeat));
082                      }                             
083                      // END SETUP DEFAULTS
084                      // *************************************************
085    
086                      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
087                      addWindowListener(new WindowAdapter() {
088                                             public void windowClosing(WindowEvent e) { 
089                                                      thisDialog.dispose();
090                                             }
091                                    });
092    
093                      // *************************************************
094                      // FILE INFO SETUP
095                      JPanel fileInfoP = new JPanel(new BorderLayout());
096                      fileInfoP.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
097                      
098                      // file browse button
099                      fileBrowseB = new JButton("Browse...");
100                      fileBrowseB.addActionListener(new ActionListener() {
101                                             public void actionPerformed(ActionEvent e) {
102                                                      String name = fileNameTF.getText();
103                                                      
104                                                      if (CrimsonUtils.isEmpty(name)) {
105                                                                    // no current name so use queryID and
106                                                                    // treeID to create a default name
107                                                                    if (query != null) {
108                                                                             name = query.getID();
109                                                                             if (! CrimsonUtils.isEmpty(query.getTreeID())) {
110                                                                                      name += "_" + query.getTreeID();
111                                                                             }
112                                                                    }
113                                                      }
114                                                      File file = GUIUtils.saveFileChooser("Select Output NEXUS File", name, null);
115                                                      fileNameTF.setText(GUIUtils.getFilename(file));
116                                             }
117                                    });
118                      
119                      // Output filename
120                      JPanel fileP = new JPanel(new GridLayout(0,1,0,5));
121                      Border etchedBorder = BorderFactory.createEtchedBorder();
122                      Border loweredBorder = BorderFactory.createLoweredBevelBorder();
123                      fileP.setBorder(BorderFactory.createTitledBorder(etchedBorder, "Output File"));
124                      fileNameTF.setBorder(loweredBorder);
125                      fileP.add(fileNameTF);
126                      fileP.add(fileBrowseB);
127                      fileInfoP.add(fileP, BorderLayout.NORTH);
128                      
129                      // Number of Repeats
130                      JPanel fileRepeatP = new JPanel(new GridLayout(0,1,0,5));
131                      fileRepeatP.setBorder(BorderFactory.createTitledBorder(etchedBorder, "Number of Query Runs"));
132                      fileRepeatS.setBorder(loweredBorder);
133                      fileRepeatP.add(fileRepeatS);
134                      
135                      JPanel fileCtrlP = new JPanel(new GridLayout(0,1,0,5));
136                      fileCtrlP.add(fileIntSeqCB);
137                      fileCtrlP.add(fileRepeatP);
138                      fileInfoP.add(fileCtrlP, BorderLayout.SOUTH);
139                      // END FILE INFO SETUP
140                      // *************************************************
141                      
142                      // select button sub-panel
143                      JPanel buttonP = new JPanel(new GridLayout(1,0,5,5));
144                      buttonP.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
145                      // select objects "save" Button
146                      runB = new JButton("Run");
147                      runB.setEnabled(true);
148                      runB.addActionListener(new ActionListener() {
149                                             public void actionPerformed(ActionEvent e) {
150                                                      int repeats = fileRepeatS.isEnabled() ? ((Integer) fileRepeatS.getValue()).intValue() : 1;
151                                                      QueryUtils.runQuery(query.getID(), fileNameTF.getText(), fileIntSeqCB.isSelected(), repeats);
152                                                      /*
153                                                      String cmd = "runQuery(\"" + query.getID() + "\", \"";
154                                                      cmd += fileNameTF.getText() + "\", ";
155                                                      cmd += (fileIntSeqCB.isSelected()) ? "1, " : "0, ";
156                                                      cmd += ((Integer) fileRepeatS.getValue()).toString() + ")";
157                                                      Root.runCommand(cmd);
158                                                      */
159                                                      thisDialog.dispose();
160                                             }
161                                    });
162                      buttonP.add(runB);
163                      // select objects "cancel" Button
164                      cancelB = new JButton("Cancel");
165                      cancelB.setEnabled(true);
166                      cancelB.addActionListener(new ActionListener() {
167                                             public void actionPerformed(ActionEvent e) {
168                                                      thisDialog.dispose();
169                                             }
170                                    });
171                      buttonP.add(cancelB);
172                      
173                      getContentPane().setLayout(new BorderLayout());
174                      getContentPane().add(fileInfoP, BorderLayout.NORTH);
175                      getContentPane().add(buttonP, BorderLayout.SOUTH);
176                      pack();
177                      
178                      // set the default window size
179                      setSize(getSize().width + 100, getSize().height + 30);
180                      
181                      // display the window
182                      setVisible(true);
183             }
184    } // QueryDialog.java