KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > menu > DatabaseSelector


1 package com.genimen.djeneric.tools.menu;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.FlowLayout JavaDoc;
6 import java.awt.Frame JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.io.File JavaDoc;
10 import java.util.ArrayList JavaDoc;
11
12 import javax.swing.DefaultComboBoxModel JavaDoc;
13 import javax.swing.JButton JavaDoc;
14 import javax.swing.JComboBox JavaDoc;
15 import javax.swing.JDialog JavaDoc;
16 import javax.swing.JFileChooser JavaDoc;
17 import javax.swing.JLabel JavaDoc;
18 import javax.swing.JPanel JavaDoc;
19
20 import com.genimen.djeneric.language.Messages;
21 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
22 import com.genimen.djeneric.ui.Util;
23 import com.genimen.djeneric.util.DjFileFilter;
24 import com.genimen.djeneric.util.DjLogger;
25
26 public class DatabaseSelector extends JDialog JavaDoc
27 {
28   private static final long serialVersionUID = 1L;
29   JPanel JavaDoc panel1 = new JPanel JavaDoc();
30   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
31   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
32   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
33   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
34   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
35   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
36   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
37   JButton JavaDoc _butOk = new JButton JavaDoc();
38   JButton JavaDoc _butCancel = new JButton JavaDoc();
39   JLabel JavaDoc _jLabel1 = new JLabel JavaDoc();
40   JComboBox JavaDoc _cbbDatabases = new JComboBox JavaDoc();
41   boolean _canceled = true;
42   String JavaDoc _selectedName = null;
43   JButton JavaDoc _butBrowse = new JButton JavaDoc();
44   FlowLayout JavaDoc _flowLayout1 = new FlowLayout JavaDoc();
45
46   public DatabaseSelector(Frame JavaDoc frame, ArrayList JavaDoc existingNames)
47   {
48     super(frame, Messages.getString("DatabaseSelector.selectDb"), true); //$NON-NLS-1$
49
try
50     {
51       jbInit();
52       _cbbDatabases.setModel(new DefaultComboBoxModel JavaDoc(existingNames.toArray()));
53       pack();
54       setBounds(frame.getX() + 50, frame.getY() + 50, getWidth(), getHeight());
55       setVisible(true);
56     }
57     catch (Exception JavaDoc ex)
58     {
59       DjLogger.log(ex);
60     }
61   }
62
63   public boolean wasCancelled()
64   {
65     return _canceled;
66   }
67
68   private void jbInit() throws Exception JavaDoc
69   {
70     panel1.setLayout(borderLayout1);
71     jPanel1.setLayout(verticalFlowLayout1);
72     jPanel2.setLayout(_flowLayout1);
73     jPanel3.setLayout(borderLayout2);
74     _butOk.setText(Messages.getString("global.Ok")); //$NON-NLS-1$
75
_butOk.addActionListener(new DatabaseSelector__butOk_actionAdapter(this));
76     _butCancel.setText(Messages.getString("global.Cancel")); //$NON-NLS-1$
77
_butCancel.addActionListener(new DatabaseSelector__butCancel_actionAdapter(this));
78     _jLabel1.setText(Messages.getString("DatabaseSelector.DatabaseName")); //$NON-NLS-1$
79
_cbbDatabases.setPreferredSize(new Dimension JavaDoc(200, 19));
80     _cbbDatabases.setEditable(true);
81     _butBrowse.setPreferredSize(new Dimension JavaDoc(19, 19));
82     _butBrowse.setText("...");
83     _butBrowse.addActionListener(new DatabaseSelector__butBrowse_actionAdapter(this));
84     _flowLayout1.setAlignment(FlowLayout.LEFT);
85     _flowLayout1.setHgap(0);
86     getContentPane().add(panel1);
87     panel1.add(jPanel1, BorderLayout.WEST);
88     jPanel1.add(_jLabel1, null);
89     panel1.add(jPanel2, BorderLayout.CENTER);
90     panel1.add(jPanel3, BorderLayout.SOUTH);
91     jPanel3.add(jPanel4, BorderLayout.EAST);
92     jPanel4.add(_butCancel, null);
93     jPanel4.add(_butOk, null);
94     jPanel2.add(_cbbDatabases, null);
95     jPanel2.add(_butBrowse, null);
96     Util.sizeButtons(jPanel4);
97
98   }
99
100   void _butOk_actionPerformed(ActionEvent JavaDoc e)
101   {
102     String JavaDoc nm = _cbbDatabases.getEditor().getItem().toString();
103     if (nm == null || nm.trim().length() == 0) return;
104     _selectedName = nm;
105
106     _canceled = false;
107     setVisible(false);
108   }
109
110   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
111   {
112     setVisible(false);
113
114   }
115
116   public String JavaDoc getSelectedName()
117   {
118     return _selectedName;
119   }
120
121   public void _butBrowse_actionPerformed(ActionEvent JavaDoc e)
122   {
123     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
124
125     DjFileFilter filter = new DjFileFilter(new String JavaDoc[]{"script"}, "Hypersonic Database");
126     fc.setFileFilter(filter);
127
128     fc.setDialogTitle(Messages.getString("MenuPanel.SelectDatabase"));
129     String JavaDoc path = System.getProperties().getProperty("user.dir");
130     fc.setCurrentDirectory(new File JavaDoc(path));
131     fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
132
133     if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
134     {
135       String JavaDoc dbname = fc.getSelectedFile().getAbsolutePath();
136       if (dbname.endsWith(".script"))
137       {
138         dbname = dbname.substring(0, dbname.length() - ".script".length());
139         _cbbDatabases.getEditor().setItem(dbname);
140       }
141     }
142   }
143 }
144
145 class DatabaseSelector__butBrowse_actionAdapter implements ActionListener JavaDoc
146 {
147   private DatabaseSelector adaptee;
148
149   DatabaseSelector__butBrowse_actionAdapter(DatabaseSelector adaptee)
150   {
151     this.adaptee = adaptee;
152   }
153
154   public void actionPerformed(ActionEvent JavaDoc e)
155   {
156     adaptee._butBrowse_actionPerformed(e);
157   }
158 }
159
160 class DatabaseSelector__butOk_actionAdapter implements java.awt.event.ActionListener JavaDoc
161 {
162   DatabaseSelector adaptee;
163
164   DatabaseSelector__butOk_actionAdapter(DatabaseSelector adaptee)
165   {
166     this.adaptee = adaptee;
167   }
168
169   public void actionPerformed(ActionEvent JavaDoc e)
170   {
171     adaptee._butOk_actionPerformed(e);
172   }
173 }
174
175 class DatabaseSelector__butCancel_actionAdapter implements java.awt.event.ActionListener JavaDoc
176 {
177   DatabaseSelector adaptee;
178
179   DatabaseSelector__butCancel_actionAdapter(DatabaseSelector adaptee)
180   {
181     this.adaptee = adaptee;
182   }
183
184   public void actionPerformed(ActionEvent JavaDoc e)
185   {
186     adaptee._butCancel_actionPerformed(e);
187   }
188 }
189
Popular Tags