001: package guam;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import java.util.Vector;
007: import java.util.Hashtable;
008: import java.util.Iterator;
009: import javax.swing.event.*;
010: import java.beans.*;
011:
012: public class MainFrame extends JFrame {
013: JPanel contentPane;
014: JMenuBar jMenuBar = new JMenuBar();
015: JMenu jMenuFile = new JMenu();
016: JMenuItem jMenuFileExit = new JMenuItem();
017: JMenu jMenuHelp = new JMenu();
018: JMenuItem jMenuHelpAbout = new JMenuItem();
019: JMenuItem jMenuItemConnect = new JMenuItem();
020: JMenuItem jMenuItemDisconn = new JMenuItem();
021: JMenuItem jMenuItemHelpTopics = new JMenuItem();
022: JMenu jMenuConfig = new JMenu();
023: JMenuItem jMenuItemAddDB = new JMenuItem();
024: JMenuItem jMenuItemModDB = new JMenuItem();
025: JMenuItem jMenuItemDelDB = new JMenuItem();
026: JMenuItem jMenuItemOptions = new JMenuItem();
027: JButton ConnectToggle = new JButton();
028: JComboBox DBCombo = new JComboBox();
029: JLabel DBComboName = new JLabel();
030: JLabel UsersLabel = new JLabel();
031: JList UsersList = new JList();
032: JLabel StatusBar = new JLabel();
033: JButton NewUser = new JButton();
034: JButton ModUser = new JButton();
035: JButton DelUser = new JButton();
036: JScrollPane UserScroll = new JScrollPane(UsersList);
037: Vector databases = null;
038:
039: MouseAdapter UserMouseListener = new java.awt.event.MouseAdapter() {
040: public void mouseClicked(MouseEvent e) {
041: UserButtonMonitor();
042: }
043: };
044:
045: ActionListener menuListener = new ActionListener() {
046: public void actionPerformed(ActionEvent e) {
047: //jMenuFileExit_actionPerformed(e);
048: if (e.getActionCommand().equals("Connect")
049: || e.getActionCommand().equals("Disconnect")) {
050: ToggleConnection();
051: } else if (e.getActionCommand().equals("Add Database")) {
052: DoAddDB();
053: } else if (e.getActionCommand().equals("Modify Database")) {
054: DoModDB();
055: } else if (e.getActionCommand().equals("Delete Database")) {
056: DoDelDB();
057: } else if (e.getActionCommand().equals("About")) {
058: DoAbout();
059: } else if (e.getActionCommand().equals("Exit")) {
060: ExitApp();
061: }
062: }
063: };
064:
065: ActionListener buttonListener = new ActionListener() {
066: public void actionPerformed(ActionEvent e) {
067: if (e.getActionCommand().equals("Connect")
068: || e.getActionCommand().equals("Disconnect")) {
069: ToggleConnection();
070: } else if (e.getActionCommand().equals("New")) {
071: AddUser();
072: } else if (e.getActionCommand().equals("Modify")) {
073: ModifyUser();
074: } else if (e.getActionCommand().equals("Delete")) {
075: DeleteUser();
076: }
077: }
078: };
079:
080: /**Construct the frame*/
081: public MainFrame(Vector databases) {
082: this .databases = databases;
083: enableEvents(AWTEvent.WINDOW_EVENT_MASK);
084: try {
085: jbInit();
086: } catch (Exception e) {
087: e.printStackTrace();
088: }
089: }
090:
091: /**Component initialization*/
092: private void jbInit() throws Exception {
093: //setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
094: contentPane = (JPanel) this .getContentPane();
095: contentPane.setLayout(null);
096: this .setSize(new Dimension(352, 386));
097: this .setTitle("GUAM v0.1");
098: jMenuFile.setText("File");
099: jMenuFileExit.setText("Exit");
100: jMenuFileExit.addActionListener(menuListener);
101: jMenuHelp.setText("Help");
102: jMenuHelpAbout.setText("About");
103: jMenuHelpAbout.addActionListener(menuListener);
104: jMenuItemConnect.setText("Connect");
105: jMenuItemConnect.addActionListener(menuListener);
106: jMenuItemDisconn.setText("Disconnect");
107: jMenuItemDisconn.addActionListener(menuListener);
108: jMenuItemDisconn.setEnabled(false);
109: jMenuItemHelpTopics.setText("Help Topics");
110: jMenuConfig.setText("Configure");
111: jMenuItemAddDB.setText("Add Database");
112: jMenuItemAddDB.addActionListener(menuListener);
113: jMenuItemModDB.setText("Modify Database");
114: jMenuItemModDB.addActionListener(menuListener);
115: if (this .databases == null) {
116: jMenuItemModDB.setEnabled(false);
117: jMenuItemDelDB.setEnabled(false);
118: jMenuItemConnect.setEnabled(false);
119: ConnectToggle.setEnabled(false);
120: }
121: jMenuItemDelDB.setText("Delete Database");
122: jMenuItemDelDB.addActionListener(menuListener);
123: jMenuItemOptions.setText("Options");
124: ConnectToggle.setText("Connect");
125: ConnectToggle.setBounds(new Rectangle(213, 10, 102, 25));
126: ConnectToggle.addActionListener(buttonListener);
127: DBCombo.setBounds(new Rectangle(81, 10, 125, 24));
128: DBComboName.setLabelFor(DBCombo);
129: DBComboName.setText("Database:");
130: DBComboName.setBounds(new Rectangle(16, 10, 64, 24));
131: UsersLabel.setLabelFor(UsersList);
132: UsersLabel.setText("Users:");
133: UsersLabel.setBounds(new Rectangle(15, 40, 58, 26));
134: UsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
135: UserScroll.setBounds(new Rectangle(15, 64, 301, 202));
136: UsersList.addMouseListener(UserMouseListener);
137: StatusBar.setBorder(BorderFactory.createLoweredBevelBorder());
138: StatusBar.setText("Ready");
139: StatusBar.setBounds(new Rectangle(7, 311, 318, 21));
140: NewUser.setText("New");
141: NewUser.setBounds(new Rectangle(65, 273, 80, 25));
142: NewUser.addActionListener(buttonListener);
143: NewUser.setEnabled(false);
144: ModUser.setBounds(new Rectangle(151, 273, 80, 25));
145: ModUser.setText("Modify");
146: ModUser.addActionListener(buttonListener);
147: ModUser.setEnabled(false);
148: DelUser.setBounds(new Rectangle(236, 273, 80, 25));
149: DelUser.setText("Delete");
150: DelUser.addActionListener(buttonListener);
151: DelUser.setEnabled(false);
152: jMenuFile.add(jMenuItemConnect);
153: jMenuFile.add(jMenuItemDisconn);
154: jMenuFile.add(jMenuFileExit);
155: jMenuFile.addSeparator();
156: jMenuHelp.add(jMenuItemHelpTopics);
157: jMenuHelp.add(jMenuHelpAbout);
158: jMenuBar.add(jMenuFile);
159: jMenuBar.add(jMenuConfig);
160: jMenuBar.add(jMenuHelp);
161: this .setJMenuBar(jMenuBar);
162: jMenuConfig.add(jMenuItemAddDB);
163: jMenuConfig.add(jMenuItemModDB);
164: jMenuConfig.add(jMenuItemDelDB);
165: jMenuConfig.addSeparator();
166: jMenuConfig.add(jMenuItemOptions);
167: contentPane.add(ConnectToggle, null);
168: contentPane.add(DBComboName, null);
169: contentPane.add(DBCombo, null);
170: contentPane.add(StatusBar, null);
171: contentPane.add(NewUser, null);
172: contentPane.add(ModUser, null);
173: contentPane.add(DelUser, null);
174: contentPane.add(UsersLabel, null);
175: contentPane.add(UserScroll, null);
176: populateDBList();
177: }
178:
179: /**File | Exit action performed*/
180: public void ExitApp() {
181: System.exit(0);
182: }
183:
184: /**Help | About action performed*/
185: public void DoAbout() {
186: JOptionPane.showMessageDialog(null,
187: "GUAM (Gui User Administration for MySQL) v0.1",
188: "About GUAM", JOptionPane.INFORMATION_MESSAGE, null);
189: }
190:
191: /**Overridden so we can exit when window is closed*/
192: protected void processWindowEvent(WindowEvent e) {
193: super .processWindowEvent(e);
194: if (e.getID() == WindowEvent.WINDOW_CLOSING) {
195: ExitApp();
196: }
197: }
198:
199: void ToggleConnection() {
200: if (GUAM.dbi.isConnected()) {
201: GUAM.dbi.disconnect();
202: UsersList.setListData(new Vector());
203: StatusBar.setText("Ready");
204: ConnectToggle.setText("Connect");
205: jMenuItemDisconn.setEnabled(false);
206: jMenuItemConnect.setEnabled(true);
207: NewUser.setEnabled(false);
208: ModUser.setEnabled(false);
209: DelUser.setEnabled(false);
210: } else {
211: StatusBar.setText("Connecting...");
212: int i = this .DBCombo.getSelectedIndex();
213: Hashtable t = (Hashtable) databases.get(i);
214: if (GUAM.dbi.connect((String) t.get("DBHost"), (String) t
215: .get("DBUser"), (String) t.get("DBPass"),
216: (String) t.get("DBPort"))) {
217: ConnectToggle.setText("Disconnect");
218: StatusBar.setText("Retrieving User Info");
219: this .refreshUserList();
220: StatusBar.setText("Connected");
221: jMenuItemDisconn.setEnabled(true);
222: jMenuItemConnect.setEnabled(false);
223: NewUser.setEnabled(true);
224: } else {
225: StatusBar.setText("Error connecting.");
226: }
227: }
228: }
229:
230: void DoAddDB() {
231: databaseEdit dbe = new databaseEdit(this );
232: Dimension size = dbe.getPreferredSize();
233: Dimension frmSize = getSize();
234: Point loc = getLocation();
235: int x = (frmSize.width - size.width) / 2 + loc.x;
236: int y = (frmSize.height - size.height) / 2 + loc.y;
237: dbe.setBounds(new Rectangle(x, y, 220, 200));
238: dbe.setTitle("Add database");
239: this .setEnabled(false);
240: dbe.show();
241: }
242:
243: void reEnable() {
244: this .setEnabled(true);
245: }
246:
247: public void populateDBList() {
248: this .DBCombo.removeAllItems();
249: if (this .databases == null || this .databases.size() == 0) {
250: jMenuItemModDB.setEnabled(false);
251: jMenuItemDelDB.setEnabled(false);
252: ConnectToggle.setEnabled(false);
253: jMenuItemConnect.setEnabled(false);
254: } else {
255: jMenuItemModDB.setEnabled(true);
256: jMenuItemDelDB.setEnabled(true);
257: jMenuItemConnect.setEnabled(true);
258: ConnectToggle.setEnabled(true);
259: Iterator i = databases.iterator();
260: while (i.hasNext()) {
261: Hashtable t = (Hashtable) i.next();
262: String ts = (String) t.get("DBName");
263: this .DBCombo.addItem(ts);
264: }
265: }
266: }
267:
268: void DoModDB() {
269: int i = this .DBCombo.getSelectedIndex();
270: Hashtable t = (Hashtable) databases.get(i);
271: databaseEdit dbe = new databaseEdit(this , t);
272: Dimension size = dbe.getPreferredSize();
273: Dimension frmSize = getSize();
274: Point loc = getLocation();
275: int x = (frmSize.width - size.width) / 2 + loc.x;
276: int y = (frmSize.height - size.height) / 2 + loc.y;
277: dbe.setBounds(new Rectangle(x, y, 220, 200));
278: dbe.setTitle("Add database");
279: this .setEnabled(false);
280: dbe.show();
281: }
282:
283: void DeleteUser() {
284: String x = (String) UsersList.getSelectedValue();
285: GUAM.dbi.deleteUser(x);
286: UsersList.setListData(GUAM.dbi.getUserList());
287: UserButtonMonitor();
288: }
289:
290: void ModifyUser() {
291: if (GUAM.dbi.isConnected()) {
292: String un = (String) UsersList.getSelectedValue();
293: this .openUserEdit(un);
294: }
295: }
296:
297: void openUserEdit(String uname) {
298: StatusBar.setText("Getting DB structure...");
299: Hashtable h = GUAM.dbi.getDBStructure();
300: StatusBar.setText("Modifying user");
301: UserMain u = new UserMain(this , uname, h);
302: Dimension size = u.getPreferredSize();
303: Dimension frmSize = getSize();
304: Point loc = getLocation();
305: int x = 10 + loc.x;
306: int y = 10 + loc.y;
307: u.setBounds(new Rectangle(x, y, 400, 500));
308: u.setTitle("Properties for user " + uname);
309: this .setEnabled(false);
310: u.show();
311: }
312:
313: void DoDelDB() {
314: int x = JOptionPane
315: .showConfirmDialog(
316: this ,
317: "Are you sure you want to delete this database record?",
318: "Confirm deletion", JOptionPane.YES_NO_OPTION,
319: JOptionPane.WARNING_MESSAGE);
320: if (x == 0) {
321: int y = this .DBCombo.getSelectedIndex();
322: databases.remove(y);
323: this .populateDBList();
324: GUAM.saveDB();
325: }
326: }
327:
328: void AddUser() {
329: if (GUAM.dbi.isConnected()) {
330: UserCreate u = new UserCreate(this );
331: Dimension size = u.getPreferredSize();
332: Dimension frmSize = getSize();
333: Point loc = getLocation();
334: int x = 10 + loc.x;
335: int y = 10 + loc.y;
336: u.setBounds(new Rectangle(x, y, 320, 150));
337: u.setTitle("Add User");
338: this .setEnabled(false);
339: u.show();
340: } else {
341: }
342: }
343:
344: public void refreshUserList() {
345: if (GUAM.dbi.isConnected()) {
346: UsersList.setListData(GUAM.dbi.getUserList());
347: UserButtonMonitor();
348: }
349: }
350:
351: void UserButtonMonitor() {
352: if (UsersList.getSelectedIndex() > -1) {
353: ModUser.setEnabled(true);
354: DelUser.setEnabled(true);
355: } else {
356: ModUser.setEnabled(false);
357: DelUser.setEnabled(false);
358: }
359: }
360:
361: }
|