Java tutorial
/** * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package savant.view.dialog; import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JCheckBox; import javax.swing.JDialog; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import savant.api.adapter.DataSourceAdapter; import savant.api.data.DataFormat; import savant.api.util.DialogUtils; import savant.controller.DataSourcePluginController; import savant.controller.FrameController; import savant.controller.GenomeController; import savant.controller.ProjectController; import savant.data.types.Genome; import savant.data.types.Genome.Auxiliary; import savant.data.types.Genome.AuxiliaryType; import savant.view.swing.Savant; import savant.view.tracks.Track; import savant.view.tracks.TrackFactory; /** * Dialog which allows users to select a genome for their project. Originally, this * just meant a sequence file, but we now allow loading a genome to include auxiliary * tracks such as genes and SNPs. * * @author mfiume, tarkvara */ public class LoadGenomeDialog extends JDialog { private static final Log LOG = LogFactory.getLog(LoadGenomeDialog.class); private File lastTrackDirectory; /** Creates new form LoadGenomeDialog */ public LoadGenomeDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); if (DataSourcePluginController.getInstance().hasOnlySavantRepoDataSource()) { fromOtherButton.setText("Repository"); } initDropDowns(); updateEnabledControls(); getRootPane().setDefaultButton(okButton); setLocationRelativeTo(parent); } /** 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; withoutSequenceButtonGroup = new javax.swing.ButtonGroup(); javax.swing.JLabel fromLabel = new javax.swing.JLabel(); fromFileButton = new javax.swing.JButton(); fromURLButton = new javax.swing.JButton(); fromOtherButton = new javax.swing.JButton(); javax.swing.JSeparator jSeparator1 = new javax.swing.JSeparator(); publishedGenomeRadio = new javax.swing.JRadioButton(); javax.swing.JLabel speciesLabel = new javax.swing.JLabel(); genomesCombo = new javax.swing.JComboBox(); auxiliaryPanel = new javax.swing.JPanel(); javax.swing.JSeparator jSeparator2 = new javax.swing.JSeparator(); userSpecifiedRadio = new javax.swing.JRadioButton(); javax.swing.JLabel nameLabel = new javax.swing.JLabel(); nameField = new javax.swing.JTextField(); javax.swing.JLabel lengthLabel = new javax.swing.JLabel(); lengthField = new javax.swing.JTextField(); javax.swing.JSeparator jSeparator3 = new javax.swing.JSeparator(); okButton = new javax.swing.JButton(); javax.swing.JButton cancelButton = new javax.swing.JButton(); withoutSequenceButtonGroup.add(publishedGenomeRadio); withoutSequenceButtonGroup.add(userSpecifiedRadio); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Load Genome"); getContentPane().setLayout(new java.awt.GridBagLayout()); fromLabel.setText("Load from:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(30, 30, 3, 3); getContentPane().add(fromLabel, gridBagConstraints); fromFileButton.setText("File"); fromFileButton.setPreferredSize(new java.awt.Dimension(150, 29)); fromFileButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromFileButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3); getContentPane().add(fromFileButton, gridBagConstraints); fromURLButton.setText("URL"); fromURLButton.setPreferredSize(new java.awt.Dimension(150, 29)); fromURLButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromURLButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3); getContentPane().add(fromURLButton, gridBagConstraints); fromOtherButton.setText("Other Datasource"); fromOtherButton.setPreferredSize(new java.awt.Dimension(150, 29)); fromOtherButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromOtherButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3); getContentPane().add(fromOtherButton, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.ipadx = 9; gridBagConstraints.ipady = 9; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(3, 15, 3, 15); getContentPane().add(jSeparator1, gridBagConstraints); publishedGenomeRadio.setSelected(true); publishedGenomeRadio.setText("Published Genome"); publishedGenomeRadio.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { publishedGenomeRadioActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 30, 3, 3); getContentPane().add(publishedGenomeRadio, gridBagConstraints); speciesLabel.setText("Genome:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(3, 30, 3, 3); getContentPane().add(speciesLabel, gridBagConstraints); genomesCombo.setToolTipText("Species of a published genome to load"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 30); getContentPane().add(genomesCombo, gridBagConstraints); auxiliaryPanel.setLayout(new java.awt.GridBagLayout()); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 5; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.ipadx = 30; gridBagConstraints.ipady = 30; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 30); getContentPane().add(auxiliaryPanel, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 6; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.ipadx = 9; gridBagConstraints.ipady = 9; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(3, 15, 3, 15); getContentPane().add(jSeparator2, gridBagConstraints); userSpecifiedRadio.setText("User-specified"); userSpecifiedRadio.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { userSpecifiedRadioActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 7; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 30, 3, 3); getContentPane().add(userSpecifiedRadio, gridBagConstraints); nameLabel.setText("Name:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 8; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; getContentPane().add(nameLabel, gridBagConstraints); nameField.setToolTipText("Name of reference (must correspond to name in records)"); nameField.setEnabled(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 8; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 90); getContentPane().add(nameField, gridBagConstraints); lengthLabel.setText("Length:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 9; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; getContentPane().add(lengthLabel, gridBagConstraints); lengthField.setToolTipText("Length in basepairs of reference"); lengthField.setEnabled(false); lengthField.addFocusListener(new java.awt.event.FocusAdapter() { public void focusLost(java.awt.event.FocusEvent evt) { lengthFieldFocusLost(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 9; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 150); getContentPane().add(lengthField, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 10; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.ipadx = 9; gridBagConstraints.ipady = 9; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(3, 15, 3, 15); getContentPane().add(jSeparator3, gridBagConstraints); okButton.setText("OK"); okButton.setPreferredSize(new java.awt.Dimension(90, 29)); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 5; gridBagConstraints.gridy = 11; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(3, 3, 30, 30); getContentPane().add(okButton, gridBagConstraints); cancelButton.setText("Cancel"); cancelButton.setPreferredSize(new java.awt.Dimension(90, 29)); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 4; gridBagConstraints.gridy = 11; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; gridBagConstraints.weightx = 0.5; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(3, 3, 30, 3); getContentPane().add(cancelButton, gridBagConstraints); pack(); }// </editor-fold>//GEN-END:initComponents private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed if (!userSpecifiedRadio.isSelected() && !publishedGenomeRadio.isSelected()) { return; } if (userSpecifiedRadio.isSelected() && !validateUserSpecifiedLength()) { return; } setVisible(false); Genome genome; boolean usingPublished = publishedGenomeRadio.isSelected(); if (usingPublished) { genome = (Genome) genomesCombo.getSelectedItem(); try { List<URI> trackURIs = new ArrayList<URI>(); Auxiliary[] auxes = genome.getAuxiliaries(); for (int i = 0; i < auxes.length; i++) { if (((JCheckBox) auxiliaryPanel.getComponent(i)).isSelected()) { trackURIs.add(auxes[i].uri); } } ProjectController.getInstance().setProjectFromGenome(genome, trackURIs.toArray(new URI[0])); } catch (Throwable x) { DialogUtils.displayException("Error Loading Genome", String.format("<html>Unable to load genome for <i>%s</i>.</html>", genome), x); } } else { genome = new Genome(nameField.getText(), Integer.parseInt(lengthField.getText())); GenomeController.getInstance().setGenome(genome); } }//GEN-LAST:event_okButtonActionPerformed private void lengthFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_lengthFieldFocusLost //validateUserSpecifiedLength(); }//GEN-LAST:event_lengthFieldFocusLost private void publishedGenomeRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_publishedGenomeRadioActionPerformed updateEnabledControls(); }//GEN-LAST:event_publishedGenomeRadioActionPerformed private void userSpecifiedRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userSpecifiedRadioActionPerformed updateEnabledControls(); }//GEN-LAST:event_userSpecifiedRadioActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed setVisible(false); }//GEN-LAST:event_cancelButtonActionPerformed private void fromOtherButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromOtherButtonActionPerformed try { DataSourceAdapter s = DataSourcePluginDialog.getDataSource(this); if (s != null) { setVisible(false); Track t = TrackFactory.createTrack(s); FrameController.getInstance().createFrame(new Track[] { t }); GenomeController.getInstance().setGenome(Genome.createFromTrack(t)); } } catch (Exception x) { DialogUtils.displayException("Error Loading Genome", "Unable to load genome from the plugin datasource.", x); } }//GEN-LAST:event_fromOtherButtonActionPerformed private void fromURLButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromURLButtonActionPerformed URL url = OpenURLDialog.getURL(this); if (url != null) { try { setVisible(false); ProjectController projectController = ProjectController.getInstance(); if (projectController.promptToSaveChanges(false)) { projectController.clearExistingProject(); GenomeController.getInstance().setGenome(null); FrameController.getInstance().addTrackFromURI(url.toURI(), DataFormat.SEQUENCE, null); } } catch (Throwable x) { DialogUtils.displayException("Error Loading Genome", String.format("Unable to load genome from %s.", url), x); } } }//GEN-LAST:event_fromURLButtonActionPerformed private void fromFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromFileButtonActionPerformed File selectedFile = DialogUtils.chooseFileForOpen("Load Genome", null, lastTrackDirectory); // set the genome if (selectedFile != null) { try { setVisible(false); ProjectController projectController = ProjectController.getInstance(); if (projectController.promptToSaveChanges(false)) { projectController.clearExistingProject(); GenomeController.getInstance().setGenome(null); Savant.getInstance().setLastTrackDirectory(selectedFile.getParentFile()); FrameController.getInstance().addTrackFromPath(selectedFile.getAbsolutePath(), DataFormat.SEQUENCE, null); } } catch (Throwable x) { DialogUtils.displayException("Error Loading Genome", String.format("Unable to load genome from %s.", selectedFile.getName()), x); } } }//GEN-LAST:event_fromFileButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel auxiliaryPanel; private javax.swing.JButton fromFileButton; private javax.swing.JButton fromOtherButton; private javax.swing.JButton fromURLButton; private javax.swing.JComboBox genomesCombo; private javax.swing.JTextField lengthField; private javax.swing.JTextField nameField; private javax.swing.JButton okButton; private javax.swing.JRadioButton publishedGenomeRadio; private javax.swing.JRadioButton userSpecifiedRadio; private javax.swing.ButtonGroup withoutSequenceButtonGroup; // End of variables declaration//GEN-END:variables private void initDropDowns() { genomesCombo.setModel(new DefaultComboBoxModel(Genome.getDefaultGenomes())); genomesCombo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateAuxiliaryList(); } }); updateAuxiliaryList(); } private void updateAuxiliaryList() { Genome curGenome = (Genome) genomesCombo.getSelectedItem(); auxiliaryPanel.removeAll(); Auxiliary[] auxes = curGenome.getAuxiliaries(); if (auxes.length > 0) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridy = GridBagConstraints.RELATIVE; gbc.anchor = GridBagConstraints.WEST; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; for (Auxiliary aux : auxes) { JCheckBox cb = new JCheckBox(aux.toString()); cb.setSelected(aux.type == AuxiliaryType.SEQUENCE); // Sequence track is checked by default. auxiliaryPanel.add(cb, gbc); } } pack(); } /** * Enable the appropriate controls depending on whether we're using a published * genome, or specifying by length. */ private void updateEnabledControls() { boolean usingPublished = publishedGenomeRadio.isSelected(); genomesCombo.setEnabled(usingPublished); for (Component c : auxiliaryPanel.getComponents()) { c.setEnabled(usingPublished); } lengthField.setEnabled(!usingPublished); nameField.setEnabled(!usingPublished); } private boolean validateUserSpecifiedLength() { String refname = nameField.getText(); if (refname.equals("")) { DialogUtils.displayError("Invalid name."); nameField.requestFocus(); return false; } String text = lengthField.getText(); try { int i = Integer.parseInt(text); if (i <= 0) { throw new Exception(); } } catch (Exception e) { DialogUtils.displayError("Invalid length."); lengthField.requestFocus(); return false; } return true; } public void setFromFileDirectory(File lastTrackDirectory) { this.lastTrackDirectory = lastTrackDirectory; } }