Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: j3/addDBDictionaryActionListener.java


1   package j3;
2   
3   //
4   // $Id: addDBDictionaryActionListener.java,v 1.15 2002/06/03 20:11:41 inchoate Exp $
5   //
6   
7   //
8   // Copyright (c) 2002, Jason Vertrees
9   // All rights reserved.
10  //
11  
12  // gui stuff
13  import javax.swing.BorderFactory;
14  import javax.swing.JButton;
15  import javax.swing.JComponent;
16  import javax.swing.JFrame;
17  import javax.swing.JLabel;
18  import javax.swing.JScrollPane;
19  import javax.swing.JTextArea;
20  import javax.swing.JTextField;
21  
22  // i18n stuff
23  import java.util.Properties;
24  import java.util.ResourceBundle;
25  
26  // awt stuff
27  import java.awt.*;
28  import java.awt.event.*;
29  
30  // Developer Note:
31  // I fixed the GUI rebuild problem, by not really using the following class as a
32  //     JFrame, but making a new one inside it.  That seems to make things work well.
33  //     Notice I could have done, this... this... this... for the frame stuff, instead
34  //   I used JFrame newFrame to do all the calls.
35  
36  
37  /**
38   * Describe class <code>addDBDictionaryActionListener</code> here.
39   *
40   * @author <a href="mailto:tree@ieeecs.ece.utexas.edu"></a>
41   * @version 1.0
42   */
43  public class addDBDictionaryActionListener extends JFrame implements ActionListener {
44  
45    // fields for the hostmachine to use for querying
46    // these are class variables now, make them instance later
47    // when I allow multiple DBDs
48      public static final String str_DBDict = "DBDictionary";
49      public static final String str_DBuserName = "_userName";
50      public static final String str_DBpassword = "_password";
51      public static final String str_DBhostName = "_hostName";
52      public static final String str_DBname = "_name";
53      public static final String str_DBtableName = "_tableName";
54      public static final String str_DBtypeName = "_typeName";
55      public static final String str_DBdriverName = "_driverName";
56      public static final String str_DBtitle = "_title";
57      public static final String str_DBaddenum = "_addenum";
58  
59      protected static Properties globalProps = null;
60      protected static ResourceBundle messages = null;
61      protected static j3.J3 j3 = null;
62      protected static JTextArea statusBar = null;
63      protected static final int defaultLength = 50;
64      protected static boolean wasShown = false;
65  
66      // here so we can access them in inner classes
67      protected JTextField DBuserName;
68      protected JTextField DBpassword;
69      protected JTextField DBhostName;
70      protected JTextField DBname;
71      protected JTextField DBtableName;
72      protected JTextField DBtypeName;
73      protected JTextField DBdriverName;
74      protected JTextField DBtitle;
75      protected JTextField DBaddenum;
76  
77  
78    /** Constructor for creating a listener that creates and adds
79     *  a DBDictionary guy to the current J3.
80     */
81      public addDBDictionaryActionListener( JTextArea statusBar, 
82                        Properties globalProps, 
83                        ResourceBundle messages,
84                        j3.J3 j3 ){
85      super( messages.getString("addDBDictionary"));
86    
87      this.globalProps = globalProps;
88      this.messages = messages;
89      this.statusBar = statusBar;
90      this.j3 = j3;
91  
92      if ( globalProps == null ) {
93        System.out.println("We don't have properties!");
94        System.out.println("Things will fail!  Don't even think of adding a new dictionary!");
95      }      
96  
97      }
98  
99  
100   /** addComponentToGB (GridBag) 
101    * saves me from having to put up with gridx=0;
102    * and crap like that
103    */
104   protected void addComponentToGB( JComponent addMe, 
105                    Container cp,
106                    GridBagLayout gbl,
107                    GridBagConstraints gc,
108                    int gridx,
109                    int gridy,
110                    int gridwidth,
111                    int gridheight ){
112     gc.gridx=gridx; gc.gridy=gridy; gc.gridwidth=gridwidth;  gc.gridheight=gridheight;
113     gbl.setConstraints( addMe, gc );
114     cp.add( addMe );    
115   }
116 
117                    
118   /**  So the user wants to do something.  Ok, here we do it. */
119     public void actionPerformed( ActionEvent e ){
120 
121     JFrame newFrame = new JFrame( messages.getString("addDBDictionary"));
122 
123     if ( messages == null )
124       System.out.println("Error, messages is null");
125 
126     // add buttons & stuff
127     GridBagLayout gbl = new GridBagLayout();
128     GridBagConstraints gc = new GridBagConstraints();
129     newFrame.getContentPane().setLayout( gbl );
130   
131     // buttons for interaction
132     JButton setButton = new JButton( messages.getString("qwAdd"));
133     JButton testButton = new JButton( messages.getString("qwTest"));
134     JButton cancelButton = new JButton( messages.getString("Close"));
135     JButton helpButton = new JButton( messages.getString("Help"));
136 
137     // textfields to store values
138     DBuserName = new JTextField( messages.getString("defaultDBuserName"), defaultLength);
139     DBpassword = new JTextField( messages.getString("defaultDBpassword"), defaultLength);
140     DBhostName = new JTextField( messages.getString("defaultDBhostName"), defaultLength);
141     DBname = new JTextField( messages.getString("defaultDBname"), defaultLength);
142     DBtableName = new JTextField( messages.getString("defaultDBtableName"), defaultLength);
143     DBtypeName = new JTextField( messages.getString("defaultDBtypeName"), defaultLength);
144     DBdriverName = new JTextField( messages.getString("defaultDBdriverName"), defaultLength);
145     DBtitle  = new JTextField( messages.getString("defaultDBtitle"), defaultLength);
146     DBaddenum  = new JTextField( messages.getString("defaultDBaddenum"), defaultLength);
147 
148     // labels for the above textfields
149     JLabel userName   = new JLabel( messages.getString("DBuserName"));
150     JLabel password   = new JLabel( messages.getString("DBpassword"));
151     JLabel hostName   = new JLabel( messages.getString("DBhostName"));
152     JLabel name       = new JLabel( messages.getString("DBname"));
153     JLabel tableName  = new JLabel( messages.getString("DBtableName"));
154     JLabel typeName   = new JLabel( messages.getString("DBtypeName"));
155     JLabel driverName = new JLabel( messages.getString("DBdriverName"));
156     JLabel title      = new JLabel( messages.getString("DBtitle"));
157     JLabel addenum    = new JLabel( messages.getString("DBaddenum"));
158   
159     // right justify the text
160     userName.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
161     password.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
162     hostName.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
163     name.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
164     tableName.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
165     typeName.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
166     driverName.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
167     title.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
168     addenum.setHorizontalTextPosition( javax.swing.SwingConstants.LEFT );
169 
170     // set the actionListeners on the buttons
171     setButton.addActionListener( new setButtonActionListener(newFrame));
172     testButton.addActionListener( new testButtonActionListener());
173     cancelButton.addActionListener( new cancelButtonActionListener(newFrame));
174     helpButton.addActionListener( new helpButtonActionListener());
175 
176     gc.gridx=0; gc.gridy=0;
177     Container cp = newFrame.getContentPane();
178 
179     // add the components to the gridbag -- see the above method for details
180     addComponentToGB( userName,   cp, gbl, gc, 0, 0, 5, 1 );
181     addComponentToGB( DBuserName, cp, gbl, gc, 6, 0, 5, 1 );
182     addComponentToGB( password,   cp, gbl, gc, 0, 1, 5, 1 );
183     addComponentToGB( DBpassword, cp, gbl, gc, 6, 1, 5, 1 );
184     addComponentToGB( hostName,   cp, gbl, gc, 0, 2, 5, 1 );
185     addComponentToGB( DBhostName, cp, gbl, gc, 6, 2, 5, 1 );
186     addComponentToGB( name,       cp, gbl, gc, 0, 3, 5, 1 );
187     addComponentToGB( DBname,     cp, gbl, gc, 6, 3, 5, 1 );
188     addComponentToGB( tableName,  cp, gbl, gc, 0, 4, 5, 1 );
189     addComponentToGB( DBtableName, cp, gbl, gc, 6, 4, 5, 1 );
190     addComponentToGB( typeName,   cp, gbl, gc, 0, 5, 5, 1 );
191     addComponentToGB( DBtypeName, cp, gbl, gc, 6, 5, 5, 1 );
192     addComponentToGB( driverName, cp, gbl, gc, 0, 6, 5, 1 );
193     addComponentToGB( DBdriverName, cp, gbl, gc, 6, 6, 5, 1 );
194     addComponentToGB( title,      cp, gbl, gc, 0, 7, 5, 1 );
195     addComponentToGB( DBtitle,    cp, gbl, gc, 6, 7, 5, 1 );
196     addComponentToGB( addenum,    cp, gbl, gc, 0, 8, 5, 1 );
197     addComponentToGB( DBaddenum,  cp, gbl, gc, 6, 8, 5, 1 );
198     addComponentToGB( setButton,  cp, gbl, gc, 0, 9, 1, 1 );
199     //    addComponentToGB( testButton, cp, gbl, gc, 1, 9, 1, 1 );
200     addComponentToGB( cancelButton, cp, gbl, gc, 2, 9, 1, 1 );
201     addComponentToGB( helpButton, cp, gbl, gc, 3, 9, 1, 1 );
202 
203     // add a nifty border 
204     //   this.setBorder( BorderFactory.createEtchedBorder() );
205     // draw frame
206     newFrame.pack();
207     newFrame.show();
208     // be swell
209     }
210 
211   /** Once the user adds the DBDict, add it into the init.properties file
212    * so J3 can find it sometime.
213    */
214     private void addDBDictToProps( String DBDict, String DBfield, String value ){
215 
216     globalProps.setProperty( DBDict 
217                  + DBfield
218                  + "."
219                  + utils.getNextDictionaryNumber(globalProps, DBDict ),
220                  value );
221       
222     }
223 
224 
225     /** myriad actionListeners for buttons/actions. */
226     private class setButtonActionListener implements ActionListener {
227 
228     JFrame frame;
229     public setButtonActionListener( JFrame frame ) {
230       this.frame = frame;
231     }
232   
233 
234     public void actionPerformed( ActionEvent e ){
235       //add the info to the init.properties file
236       //then create a new J3DB with the info
237       
238       if ( globalProps == null ) {
239         System.out.println("Error: Trying to save new DB style dictionary to disk, " +
240                    "but can't because the global properties file was null.");
241         return;
242       }
243 
244       // add to the init file.
245       addDBDictToProps( str_DBDict, str_DBuserName, DBuserName.getText() );
246       addDBDictToProps( str_DBDict, str_DBpassword, DBpassword.getText() );
247       addDBDictToProps( str_DBDict, str_DBhostName, DBhostName.getText() );
248       addDBDictToProps( str_DBDict, str_DBname, DBname.getText() );
249       addDBDictToProps( str_DBDict, str_DBtableName, DBtableName.getText() );
250       addDBDictToProps( str_DBDict, str_DBtypeName, DBtypeName.getText() );
251       addDBDictToProps( str_DBDict, str_DBdriverName, DBdriverName.getText() );
252       addDBDictToProps( str_DBDict, str_DBtitle, DBtitle.getText() );
253       addDBDictToProps( str_DBDict, str_DBaddenum, DBaddenum.getText() );
254 
255       // save the init file
256       utils.saveInitFile(globalProps);
257 
258       new j3.J3DB( DBuserName.getText(),
259              DBpassword.getText(),
260              DBhostName.getText(),
261              DBname.getText(),
262              DBtypeName.getText(),
263              DBdriverName.getText(),
264              DBtableName.getText(),
265              DBtitle.getText(),
266              DBaddenum.getText(),
267              j3
268              );
269 
270       j3.progressBar.setMaximum( j3.getLexicaNames(j3).size());
271       frame.setVisible(false);
272     }
273     }
274 
275 
276   /** actionListener (what to do) for the test button.  I'm considering removing
277    * this, or simple filling in silly code for now.
278    */
279     private class testButtonActionListener implements ActionListener {
280     public void actionPerformed( ActionEvent e ){}
281     // makes a connection, if no throws, good.
282     
283     }
284 
285   /** actionListener for the cancelButton.  Close and save. */
286     private class cancelButtonActionListener implements ActionListener {
287     JFrame frame;
288 
289     cancelButtonActionListener( JFrame frame ){
290       this.frame = frame;
291     }
292 
293     public void actionPerformed( ActionEvent e ){
294       if ( frame != null )
295         frame.setVisible(false);
296     }
297     }
298 
299 
300   /** ActionListener for the help button.  Loads the right file,
301    * and displays it in a little Jpanel
302    */
303     private class helpButtonActionListener implements ActionListener {
304 
305     public String localeExtension = "_" + j3.currentLocale.getLanguage()
306       + "_" + j3.currentLocale.getCountry();
307 
308     public String helpFile = 
309       j3.J3HOME 
310       + utils.fileSep + "config"
311       + utils.fileSep + "help"
312       + utils.fileSep + "AddDBDict" + localeExtension;
313 
314         public void actionPerformed( ActionEvent e ){
315       // show a  stupid panel with info
316       JFrame helpFrame = new JFrame();
317       JTextArea helpText = new JTextArea( utils.smartOpenFile( helpFile ), 20, 72);
318       helpText.setLineWrap(true);
319       helpText.setWrapStyleWord(true);
320       JScrollPane helpScroller = new JScrollPane(helpText);
321       helpFrame.getContentPane().add( helpScroller );
322       helpFrame.setSize( 450, 200 );
323       helpFrame.setVisible(true);
324     }
325     }
326 }