Source Code Cross Referenced for ConnectionPanel.java in  » Science » weka » weka » gui » sql » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » weka » weka.gui.sql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *    This program is free software; you can redistribute it and/or modify
003:         *    it under the terms of the GNU General Public License as published by
004:         *    the Free Software Foundation; either version 2 of the License, or
005:         *    (at your option) any later version.
006:         *
007:         *    This program is distributed in the hope that it will be useful,
008:         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
009:         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010:         *    GNU General Public License for more details.
011:         *
012:         *    You should have received a copy of the GNU General Public License
013:         *    along with this program; if not, write to the Free Software
014:         *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015:         */
016:
017:        /*
018:         * ConnectionPanel.java
019:         * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
020:         *
021:         */
022:
023:        package weka.gui.sql;
024:
025:        import weka.gui.DatabaseConnectionDialog;
026:        import weka.gui.ListSelectorDialog;
027:        import weka.gui.sql.event.ConnectionEvent;
028:        import weka.gui.sql.event.ConnectionListener;
029:        import weka.gui.sql.event.HistoryChangedEvent;
030:        import weka.gui.sql.event.HistoryChangedListener;
031:
032:        import java.awt.BorderLayout;
033:        import java.awt.FlowLayout;
034:        import java.awt.event.ActionEvent;
035:        import java.awt.event.ActionListener;
036:        import java.util.HashSet;
037:        import java.util.Iterator;
038:
039:        import javax.swing.DefaultListModel;
040:        import javax.swing.JButton;
041:        import javax.swing.JFrame;
042:        import javax.swing.JLabel;
043:        import javax.swing.JList;
044:        import javax.swing.JOptionPane;
045:        import javax.swing.JPanel;
046:        import javax.swing.JTextField;
047:        import javax.swing.event.CaretListener;
048:        import javax.swing.event.CaretEvent;
049:
050:        /**
051:         * Enables the user to insert a database URL, plus user/password to connect
052:         * to this database.
053:         *
054:         * @author      FracPete (fracpete at waikato dot ac dot nz)
055:         * @version     $Revision: 1.2 $
056:         */
057:
058:        public class ConnectionPanel extends JPanel implements  CaretListener {
059:
060:            /** for serialization */
061:            static final long serialVersionUID = 3499317023969723490L;
062:
063:            /** the name of the history */
064:            public final static String HISTORY_NAME = "connection";
065:
066:            /** the parent frame */
067:            protected JFrame m_Parent = null;
068:
069:            /** the databae connection dialog */
070:            protected DatabaseConnectionDialog m_DbDialog;
071:
072:            /** the URL to use */
073:            protected String m_URL = "";
074:
075:            /** the user to use for connecting to the DB */
076:            protected String m_User = "";
077:
078:            /** the password to use for connecting to the DB */
079:            protected String m_Password = "";
080:
081:            /** the label for the URL */
082:            protected JLabel m_LabelURL = new JLabel("URL ");
083:
084:            /** the textfield for the URL */
085:            protected JTextField m_TextURL = new JTextField(40);
086:
087:            /** the button for the DB-Dialog */
088:            protected JButton m_ButtonDatabase = new JButton("User...");
089:
090:            /** the button for connecting to the database */
091:            protected JButton m_ButtonConnect = new JButton("Connect");
092:
093:            /** the button for the history */
094:            protected JButton m_ButtonHistory = new JButton("History...");
095:
096:            /** the connection listeners */
097:            protected HashSet m_ConnectionListeners;
098:
099:            /** the history listeners */
100:            protected HashSet m_HistoryChangedListeners;
101:
102:            /** for connecting to the database */
103:            protected DbUtils m_DbUtils;
104:
105:            /** the history of connections */
106:            protected DefaultListModel m_History = new DefaultListModel();
107:
108:            /**
109:             * initializes the panel
110:             * @param parent      the parent of this panel
111:             */
112:            public ConnectionPanel(JFrame parent) {
113:                super ();
114:
115:                m_Parent = parent;
116:                m_ConnectionListeners = new HashSet();
117:                m_HistoryChangedListeners = new HashSet();
118:
119:                try {
120:                    m_DbUtils = new DbUtils();
121:                    m_URL = m_DbUtils.getDatabaseURL();
122:                    m_User = m_DbUtils.getUsername();
123:                    m_Password = m_DbUtils.getPassword();
124:                } catch (Exception e) {
125:                    e.printStackTrace();
126:                    m_URL = "";
127:                    m_User = "";
128:                    m_Password = "";
129:                }
130:
131:                createPanel();
132:            }
133:
134:            /**
135:             * builds the panel with all its components
136:             */
137:            protected void createPanel() {
138:                JPanel panel;
139:                JPanel panel2;
140:
141:                setLayout(new BorderLayout());
142:                panel2 = new JPanel(new FlowLayout());
143:                add(panel2, BorderLayout.WEST);
144:
145:                // label
146:                m_LabelURL.setLabelFor(m_ButtonDatabase);
147:                m_LabelURL.setDisplayedMnemonic('U');
148:                panel2.add(m_LabelURL);
149:
150:                // editfield
151:                m_TextURL.setText(m_URL);
152:                m_TextURL.addCaretListener(this );
153:                panel2.add(m_TextURL);
154:
155:                // buttons
156:                panel = new JPanel(new FlowLayout());
157:                panel2.add(panel);
158:
159:                m_ButtonDatabase.setMnemonic('s');
160:                m_ButtonDatabase.addActionListener(new ActionListener() {
161:                    public void actionPerformed(ActionEvent e) {
162:                        showDialog();
163:                    }
164:                });
165:                panel.add(m_ButtonDatabase);
166:
167:                m_ButtonConnect.setMnemonic('o');
168:                m_ButtonConnect.addActionListener(new ActionListener() {
169:                    public void actionPerformed(ActionEvent e) {
170:                        connect();
171:                    }
172:                });
173:                panel.add(m_ButtonConnect);
174:
175:                m_ButtonHistory.addActionListener(new ActionListener() {
176:                    public void actionPerformed(ActionEvent e) {
177:                        showHistory();
178:                    }
179:                });
180:                panel.add(m_ButtonHistory);
181:
182:                setButtons();
183:            }
184:
185:            /**
186:             * sets the buttons according to the connected-state
187:             */
188:            protected void setButtons() {
189:                boolean isEmpty;
190:
191:                isEmpty = m_TextURL.getText().equals("");
192:
193:                m_ButtonConnect.setEnabled(!isEmpty);
194:                m_ButtonDatabase.setEnabled(!isEmpty);
195:                m_ButtonHistory.setEnabled(m_History.size() > 0);
196:            }
197:
198:            /**
199:             * sets the parameters back to standard
200:             */
201:            public void clear() {
202:                setURL(m_DbUtils.getDatabaseURL());
203:                setUser(m_DbUtils.getUsername());
204:                setPassword(m_DbUtils.getPassword());
205:            }
206:
207:            /**
208:             * sets the focus in a designated control
209:             */
210:            public void setFocus() {
211:                m_TextURL.requestFocus();
212:            }
213:
214:            /**
215:             * sets the URL
216:             * @param url       the new value of the URL
217:             */
218:            public void setURL(String url) {
219:                m_URL = url;
220:                m_TextURL.setText(url);
221:            }
222:
223:            /**
224:             * returns the current URL
225:             * 
226:             * @return the current URL
227:             */
228:            public String getURL() {
229:                m_URL = m_TextURL.getText();
230:                return m_URL;
231:            }
232:
233:            /**
234:             * sets the User
235:             * @param user       the new value of the User
236:             */
237:            public void setUser(String user) {
238:                m_User = user;
239:            }
240:
241:            /**
242:             * returns the current User
243:             * 
244:             * @return the current user
245:             */
246:            public String getUser() {
247:                return m_User;
248:            }
249:
250:            /**
251:             * sets the Password
252:             * @param pw       the new value of the Password
253:             */
254:            public void setPassword(String pw) {
255:                m_Password = pw;
256:            }
257:
258:            /**
259:             * returns the current Password
260:             * 
261:             * @return the current password
262:             */
263:            public String getPassword() {
264:                return m_Password;
265:            }
266:
267:            /**
268:             * adds the given string to the history (removes duplicates)
269:             * @param s           the string to add
270:             */
271:            protected void addHistory(String s) {
272:                if (s.equals(""))
273:                    return;
274:
275:                // no duplicates!
276:                if (m_History.contains(s))
277:                    m_History.removeElement(s);
278:
279:                m_History.add(0, s);
280:
281:                // send notification
282:                notifyHistoryChangedListeners();
283:            }
284:
285:            /**
286:             * sets the local history to the given one
287:             * @param history     the history to use
288:             */
289:            public void setHistory(DefaultListModel history) {
290:                int i;
291:
292:                m_History.clear();
293:                for (i = 0; i < history.size(); i++)
294:                    m_History.addElement(history.get(i));
295:
296:                setButtons();
297:            }
298:
299:            /**
300:             * returns the history
301:             * @return        the current history
302:             */
303:            public DefaultListModel getHistory() {
304:                return m_History;
305:            }
306:
307:            /**
308:             * displays the database dialog
309:             */
310:            protected void showDialog() {
311:                m_DbDialog = new DatabaseConnectionDialog(m_Parent, getURL(),
312:                        getUser(), false);
313:                m_DbDialog.setVisible(true);
314:                if (m_DbDialog.getReturnValue() == JOptionPane.OK_OPTION) {
315:                    setURL(m_DbDialog.getURL());
316:                    setUser(m_DbDialog.getUsername());
317:                    setPassword(m_DbDialog.getPassword());
318:                }
319:
320:                setButtons();
321:            }
322:
323:            /**
324:             * connects to the database, notifies the listeners
325:             */
326:            protected void connect() {
327:                // disconnect if still connected
328:                if (m_DbUtils.isConnected()) {
329:                    try {
330:                        m_DbUtils.disconnectFromDatabase();
331:                        notifyConnectionListeners(ConnectionEvent.DISCONNECT);
332:                    } catch (Exception e) {
333:                        e.printStackTrace();
334:                        notifyConnectionListeners(ConnectionEvent.DISCONNECT, e);
335:                    }
336:                }
337:
338:                // connect
339:                try {
340:                    m_DbUtils.setDatabaseURL(getURL());
341:                    m_DbUtils.setUsername(getUser());
342:                    m_DbUtils.setPassword(getPassword());
343:                    m_DbUtils.connectToDatabase();
344:                    notifyConnectionListeners(ConnectionEvent.CONNECT);
345:                    // add to history
346:                    addHistory(getUser() + "@" + getURL());
347:                } catch (Exception e) {
348:                    e.printStackTrace();
349:                    notifyConnectionListeners(ConnectionEvent.CONNECT, e);
350:                }
351:
352:                setButtons();
353:            }
354:
355:            /**
356:             * displays the query history
357:             */
358:            public void showHistory() {
359:                JList list;
360:                ListSelectorDialog dialog;
361:                String tmpStr;
362:
363:                list = new JList(m_History);
364:                dialog = new ListSelectorDialog(m_Parent, list);
365:
366:                if (dialog.showDialog() == ListSelectorDialog.APPROVE_OPTION) {
367:                    if (list.getSelectedValue() != null) {
368:                        tmpStr = list.getSelectedValue().toString();
369:                        if (tmpStr.indexOf("@") > -1) {
370:                            setUser(tmpStr.substring(0, tmpStr.indexOf("@")));
371:                            setURL(tmpStr.substring(tmpStr.indexOf("@") + 1));
372:                            showDialog();
373:                        } else {
374:                            setUser("");
375:                            setURL(tmpStr);
376:                        }
377:                    }
378:                }
379:
380:                setButtons();
381:            }
382:
383:            /**
384:             * adds the given listener to the list of listeners
385:             * @param l       the listener to add to the list
386:             */
387:            public void addConnectionListener(ConnectionListener l) {
388:                m_ConnectionListeners.add(l);
389:            }
390:
391:            /**
392:             * removes the given listener from the list of listeners
393:             * @param l       the listener to remove
394:             */
395:            public void removeConnectionListener(ConnectionListener l) {
396:                m_ConnectionListeners.remove(l);
397:            }
398:
399:            /**
400:             * notifies the connection listeners of the event
401:             * @param type      the type of the action, CONNECT or DISCONNECT
402:             */
403:            protected void notifyConnectionListeners(int type) {
404:                notifyConnectionListeners(type, null);
405:            }
406:
407:            /**
408:             * notifies the connection listeners of the event
409:             * @param type      the type of the action, CONNECT or DISCONNECT
410:             * @param ex        an optional exception that happened (indicates failure!)
411:             */
412:            protected void notifyConnectionListeners(int type, Exception ex) {
413:                Iterator iter;
414:                ConnectionListener l;
415:
416:                iter = m_ConnectionListeners.iterator();
417:                while (iter.hasNext()) {
418:                    l = (ConnectionListener) iter.next();
419:                    l.connectionChange(new ConnectionEvent(this , type,
420:                            m_DbUtils, ex));
421:                }
422:            }
423:
424:            /**
425:             * adds the given listener to the list of listeners
426:             * @param l       the listener to add to the list
427:             */
428:            public void addHistoryChangedListener(HistoryChangedListener l) {
429:                m_HistoryChangedListeners.add(l);
430:            }
431:
432:            /**
433:             * removes the given listener from the list of listeners
434:             * @param l       the listener to remove
435:             */
436:            public void removeHistoryChangedListener(HistoryChangedListener l) {
437:                m_HistoryChangedListeners.remove(l);
438:            }
439:
440:            /**
441:             * notifies the history listeners of the event
442:             */
443:            protected void notifyHistoryChangedListeners() {
444:                Iterator iter;
445:                HistoryChangedListener l;
446:
447:                iter = m_HistoryChangedListeners.iterator();
448:                while (iter.hasNext()) {
449:                    l = (HistoryChangedListener) iter.next();
450:                    l.historyChanged(new HistoryChangedEvent(this ,
451:                            HISTORY_NAME, getHistory()));
452:                }
453:            }
454:
455:            /**
456:             * Called when the caret position is updated.
457:             * 
458:             * @param event the event to process
459:             */
460:            public void caretUpdate(CaretEvent event) {
461:                setButtons();
462:            }
463:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.