Mercurial > hg > ltpda-connection-manager
comparison ConnectionManager/src/connectionmanager/DatabaseSelectorDialog.java @ 5:35f1cfcaa5a9
Add crude Java GUI.
author | Daniele Nicolodi <daniele@science.unitn.it> |
---|---|
date | Wed, 02 Jun 2010 17:29:18 +0200 |
parents | |
children | 670905eb688a |
comparison
equal
deleted
inserted
replaced
4:c706c10a76bd | 5:35f1cfcaa5a9 |
---|---|
1 package connectionmanager; | |
2 | |
3 import java.awt.Point; | |
4 import java.awt.event.MouseAdapter; | |
5 import java.awt.event.MouseEvent; | |
6 import javax.swing.table.DefaultTableModel; | |
7 | |
8 public class DatabaseSelectorDialog extends javax.swing.JDialog { | |
9 | |
10 private boolean cancelled = true; | |
11 | |
12 public String hostname; | |
13 public String database; | |
14 public String username; | |
15 | |
16 public DatabaseSelectorDialog(java.awt.Frame parent) { | |
17 super(parent, true); | |
18 initComponents(); | |
19 databasesTable.addMouseListener(new MouseListenerDoubleClick()); | |
20 } | |
21 | |
22 public DatabaseSelectorDialog() { | |
23 super(new javax.swing.JFrame(), true); | |
24 initComponents(); | |
25 databasesTable.addMouseListener(new MouseListenerDoubleClick()); | |
26 } | |
27 | |
28 private class MouseListenerDoubleClick extends MouseAdapter { | |
29 | |
30 @Override | |
31 public void mouseClicked(MouseEvent e) { | |
32 if (e.getClickCount() >= 2) { | |
33 cancelled = false; | |
34 Point p = e.getPoint(); | |
35 int row = databasesTable.rowAtPoint(p); | |
36 DefaultTableModel model = (DefaultTableModel)databasesTable.getModel(); | |
37 hostname = (String)model.getValueAt(row, 0); | |
38 database = (String)model.getValueAt(row, 1); | |
39 username = (String)model.getValueAt(row, 2); | |
40 doClose(); | |
41 } | |
42 } | |
43 } | |
44 | |
45 @SuppressWarnings("unchecked") | |
46 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |
47 private void initComponents() { | |
48 | |
49 cancelButton = new javax.swing.JButton(); | |
50 newButton = new javax.swing.JButton(); | |
51 selectButton = new javax.swing.JButton(); | |
52 jScrollPane1 = new javax.swing.JScrollPane(); | |
53 databasesTable = new javax.swing.JTable(); | |
54 | |
55 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); | |
56 setTitle("Select a connection"); | |
57 | |
58 cancelButton.setText("Cancel"); | |
59 cancelButton.addActionListener(new java.awt.event.ActionListener() { | |
60 public void actionPerformed(java.awt.event.ActionEvent evt) { | |
61 cancelButtonActionPerformed(evt); | |
62 } | |
63 }); | |
64 | |
65 newButton.setText("New"); | |
66 newButton.addActionListener(new java.awt.event.ActionListener() { | |
67 public void actionPerformed(java.awt.event.ActionEvent evt) { | |
68 newButtonActionPerformed(evt); | |
69 } | |
70 }); | |
71 | |
72 selectButton.setText("Select"); | |
73 selectButton.addActionListener(new java.awt.event.ActionListener() { | |
74 public void actionPerformed(java.awt.event.ActionEvent evt) { | |
75 selectButtonActionPerformed(evt); | |
76 } | |
77 }); | |
78 | |
79 databasesTable.setModel(new javax.swing.table.DefaultTableModel( | |
80 new Object [][] { | |
81 | |
82 }, | |
83 new String [] { | |
84 "Hostname", "Database", "Username" | |
85 } | |
86 ) { | |
87 Class[] types = new Class [] { | |
88 java.lang.String.class, java.lang.String.class, java.lang.String.class | |
89 }; | |
90 boolean[] canEdit = new boolean [] { | |
91 false, false, false | |
92 }; | |
93 | |
94 public Class getColumnClass(int columnIndex) { | |
95 return types [columnIndex]; | |
96 } | |
97 | |
98 public boolean isCellEditable(int rowIndex, int columnIndex) { | |
99 return canEdit [columnIndex]; | |
100 } | |
101 }); | |
102 jScrollPane1.setViewportView(databasesTable); | |
103 | |
104 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); | |
105 getContentPane().setLayout(layout); | |
106 layout.setHorizontalGroup( | |
107 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
108 .add(layout.createSequentialGroup() | |
109 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
110 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() | |
111 .add(183, 183, 183) | |
112 .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
113 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
114 .add(newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
115 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
116 .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
117 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() | |
118 .addContainerGap() | |
119 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))) | |
120 .addContainerGap()) | |
121 ); | |
122 layout.setVerticalGroup( | |
123 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
124 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() | |
125 .addContainerGap(20, Short.MAX_VALUE) | |
126 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 225, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) | |
127 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
128 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER) | |
129 .add(selectButton) | |
130 .add(newButton) | |
131 .add(cancelButton)) | |
132 .add(207, 207, 207)) | |
133 ); | |
134 | |
135 pack(); | |
136 }// </editor-fold>//GEN-END:initComponents | |
137 | |
138 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed | |
139 cancelled = true; | |
140 doClose(); | |
141 }//GEN-LAST:event_cancelButtonActionPerformed | |
142 | |
143 private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed | |
144 cancelled = false; | |
145 int row = databasesTable.getSelectedRow(); | |
146 DefaultTableModel model = (DefaultTableModel)databasesTable.getModel(); | |
147 hostname = (String)model.getValueAt(row, 0); | |
148 database = (String)model.getValueAt(row, 1); | |
149 username = (String)model.getValueAt(row, 2); | |
150 doClose(); | |
151 }//GEN-LAST:event_selectButtonActionPerformed | |
152 | |
153 private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed | |
154 DatabaseDialog dialog = new DatabaseDialog(); | |
155 dialog.setVisible(true); | |
156 if (!dialog.cancelled) { | |
157 cancelled = false; | |
158 hostname = dialog.hostname; | |
159 database = dialog.database; | |
160 username = new String(""); | |
161 doClose(); | |
162 } | |
163 }//GEN-LAST:event_newButtonActionPerformed | |
164 | |
165 private void doClose() { | |
166 setVisible(false); | |
167 dispose(); | |
168 } | |
169 | |
170 public void add(String hostname, String database, String username) { | |
171 DefaultTableModel model = (DefaultTableModel)databasesTable.getModel(); | |
172 model.insertRow(model.getRowCount(), new Object[] { hostname, database, username }); | |
173 } | |
174 | |
175 // Variables declaration - do not modify//GEN-BEGIN:variables | |
176 private javax.swing.JButton cancelButton; | |
177 private javax.swing.JTable databasesTable; | |
178 private javax.swing.JScrollPane jScrollPane1; | |
179 private javax.swing.JButton newButton; | |
180 private javax.swing.JButton selectButton; | |
181 // End of variables declaration//GEN-END:variables | |
182 } |