View Javadoc

1   package edu.sc.seis.TauP;
2   
3   import javax.swing.DefaultListModel;
4   import javax.swing.ListSelectionModel;
5   import javax.swing.ScrollPaneConstants;
6   import javax.swing.SwingUtilities;
7   
8   /*
9    The TauP Toolkit: Flexible Seismic Travel-Time and Raypath Utilities.
10   Copyright (C) 1998-2000 University of South Carolina
11  
12   This program is free software; you can redistribute it and/or
13   modify it under the terms of the GNU General Public License
14   as published by the Free Software Foundation; either version 2
15   of the License, or (at your option) any later version.
16  
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21  
22   You should have received a copy of the GNU General Public License
23   along with this program; if not, write to the Free Software
24   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  
26   The current version can be found at
27   <A HREF="www.seis.sc.edu">http://www.seis.sc.edu</A>
28  
29   Bug reports and comments should be directed to
30   H. Philip Crotwell, crotwell@seis.sc.edu or
31   Tom Owens, owens@seis.sc.edu
32  
33   */
34  
35  /***
36   *
37   * @author
38   * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
39  
40  
41  
42   */
43  public class PhaseDialog extends javax.swing.JDialog {
44  
45      /*** Initializes the Form */
46      public PhaseDialog(java.awt.Frame parent, boolean modal) {
47          super (parent, modal);
48          initComponents ();
49          phaseListModel = new DefaultListModel();
50          phaseList.setModel(phaseListModel);
51          pack();
52      }
53  
54      /*** This method is called from within the constructor to
55       * initialize the form.
56       * WARNING: Do NOT modify this code. The content of this method is
57       * always regenerated by the FormEditor.
58       */
59      private void initComponents () {//GEN-BEGIN:initComponents
60          // This code was developed using a non-commercially licensed version of NetBeans Developer 2.x.
61          // For details, see http://www.netbeans.com/non_commercial.html
62  
63          setBackground (new java.awt.Color (192, 192, 192));
64          addWindowListener (new java.awt.event.WindowAdapter () {
65                      public void windowClosing (java.awt.event.WindowEvent evt) {
66                          closeDialog (evt);
67                      }
68                  }
69                            );
70          getContentPane ().setLayout (new java.awt.BorderLayout ());
71  
72          jLabel1 = new javax.swing.JLabel ();
73          jLabel1.setText ("Choose Phases");
74          jLabel1.setHorizontalAlignment (javax.swing.SwingConstants.CENTER);
75          getContentPane ().add (jLabel1, "North");
76  
77          jPanel1 = new javax.swing.JPanel ();
78          jPanel1.setLayout (new java.awt.FlowLayout ());
79  
80          jLabel2 = new javax.swing.JLabel ();
81          jLabel2.setText ("Add new");
82          jPanel1.add (jLabel2);
83  
84          addPhaseTextField = new javax.swing.JTextField ();
85          addPhaseTextField.setPreferredSize (new java.awt.Dimension(100, 21));
86          addPhaseTextField.setMinimumSize (new java.awt.Dimension(100, 21));
87          addPhaseTextField.addActionListener (new java.awt.event.ActionListener () {
88                      public void actionPerformed (java.awt.event.ActionEvent evt) {
89                          addPhaseActionPerformed (evt);
90                      }
91                  }
92                                              );
93          jPanel1.add (addPhaseTextField);
94  
95          closeButton = new javax.swing.JButton ();
96          closeButton.setText ("Close");
97          closeButton.setActionCommand ("ClosePhaseDialog");
98          closeButton.setLabel ("Close");
99          closeButton.addActionListener (new java.awt.event.ActionListener () {
100                     public void actionPerformed (java.awt.event.ActionEvent evt) {
101                         closeActionPerformed (evt);
102                     }
103                 }
104                                       );
105         jPanel1.add (closeButton);
106 
107         getContentPane ().add (jPanel1, "South");
108 
109         jPanel2 = new javax.swing.JPanel ();
110         jPanel2.setLayout (new java.awt.BorderLayout ());
111 
112         jScrollPane1 = new javax.swing.JScrollPane ();
113 
114         phaseList = new javax.swing.JList ();
115         phaseList.addKeyListener (new java.awt.event.KeyAdapter () {
116                     public void keyTyped (java.awt.event.KeyEvent evt) {
117                         phaseListKeyTyped (evt);
118                     }
119                 }
120                                  );
121         jScrollPane1.add (phaseList);
122 
123         jScrollPane1.setViewportView (phaseList);
124         jPanel2.add (jScrollPane1, "Center");
125 
126         getContentPane ().add (jPanel2, "Center");
127 
128     }//GEN-END:initComponents
129 
130     private void phaseListKeyTyped (java.awt.event.KeyEvent evt) {//GEN-FIRST:event_phaseListKeyTyped
131         // Add your handling code here:
132         String areaText = addPhaseTextField.getText();
133         if (evt.getKeyChar() != java.awt.event.KeyEvent.VK_ENTER) {
134             areaText += evt.getKeyChar();
135             addPhaseTextField.setText(areaText);
136         } else {
137             addPhaseActionPerformed(new java.awt.event.ActionEvent(
138                                                                    this,
139                                                                    0,
140                                                                    areaText));
141         }
142     }//GEN-LAST:event_phaseListKeyTyped
143 
144     private void addPhaseActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPhaseActionPerformed
145         // Add your handling code here:
146         phaseListModel.addElement(evt.getActionCommand());
147         ListSelectionModel sel =
148             phaseList.getSelectionModel();
149         sel.addSelectionInterval(phaseListModel.getSize()-1,
150                                  phaseListModel.getSize()-1);
151         SwingUtilities.invokeLater(new Runnable() {
152                     public void run() {
153                         phaseList.ensureIndexIsVisible(phaseListModel.getSize()-1);
154                     }
155                 });
156     }//GEN-LAST:event_addPhaseActionPerformed
157 
158     private void closeActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeActionPerformed
159         // Add your handling code here:
160         setVisible(false);
161     }//GEN-LAST:event_closeActionPerformed
162 
163     /*** Closes the dialog */
164     private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
165         setVisible (false);
166         dispose ();
167     }//GEN-LAST:event_closeDialog
168 
169 
170     // Variables declaration - do not modify//GEN-BEGIN:variables
171     private javax.swing.JLabel jLabel1;
172     private javax.swing.JPanel jPanel1;
173     private javax.swing.JPanel jPanel2;
174     private javax.swing.JLabel jLabel2;
175     private javax.swing.JTextField addPhaseTextField;
176     private javax.swing.JButton closeButton;
177     private javax.swing.JScrollPane jScrollPane1;
178     private javax.swing.JList phaseList;
179     // End of variables declaration//GEN-END:variables
180 
181     private DefaultListModel phaseListModel;
182 
183     public String[] getPhases() {
184         int[] selected = phaseList.getSelectedIndices();
185         String[] phases = new String[selected.length];
186         for (int i=0;i<selected.length;i++) {
187             phases[i] = (String)phaseListModel.elementAt(selected[i]);
188         }
189         return phases;
190     }
191 
192     public void setPhases(String[] phases) {
193         phaseListModel.removeAllElements();
194         for (int i=0; i<phases.length; i++) {
195             phaseListModel.addElement(phases[i]);
196         }
197         ListSelectionModel sel =
198             phaseList.getSelectionModel();
199         sel.addSelectionInterval(0,
200                                  phaseListModel.getSize()-1);
201 
202     }
203 
204     public void setSelectedPhases(String[] phases){
205         ListSelectionModel sel = phaseList.getSelectionModel();
206         sel.clearSelection();
207         for (int i = 0; i < phases.length; i++){
208             for(int j = 0; j < phaseListModel.size(); j++) {
209                 if(phases[i].equals(phaseListModel.elementAt(j))){
210                     sel.addSelectionInterval(j,j);
211                 }
212             }
213         }
214     }
215 
216     public static void main(java.lang.String[] args) {
217         System.out.println(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
218         new PhaseDialog (new java.awt.Frame (), false).show ();
219     }
220 
221 }