001: package guam;
002:
003: import java.awt.*;
004: import javax.swing.*;
005: import java.util.Hashtable;
006: import java.util.Iterator;
007: import java.awt.event.*;
008:
009: /**
010: * Title: GUAM
011: * Description:
012: * Copyright: Copyright (c) 2002
013: * Company:
014: * @author dkc
015: * @version 1.0
016: */
017:
018: public class databaseEdit extends JFrame {
019: JLabel NameLabel = new JLabel();
020: JLabel HostLabel = new JLabel();
021: JLabel UserLabel = new JLabel();
022: JLabel PassLabel = new JLabel();
023: JLabel PortLabel = new JLabel();
024: JTextField DBName = new JTextField();
025: JTextField DBHost = new JTextField();
026: JTextField DBUser = new JTextField();
027: JTextField DBPass = new JPasswordField();
028: JTextField DBPort = new JTextField();
029: JButton SaveButton = new JButton();
030: JButton CancelButton = new JButton();
031: Hashtable currentdata = null;
032: MainFrame mymainframe = null;
033: boolean isMod = false;
034: public boolean modified = false;
035:
036: private java.awt.event.ActionListener aListener = new java.awt.event.ActionListener() {
037: public void actionPerformed(ActionEvent e) {
038: modified = true;
039: }
040: };
041:
042: public databaseEdit(MainFrame mymainframe) {
043: this .mymainframe = mymainframe;
044: this .currentdata = new Hashtable();
045: try {
046: jbInit();
047: } catch (Exception e) {
048: e.printStackTrace();
049: }
050: }
051:
052: public databaseEdit(MainFrame mymainframe, Hashtable data) {
053: this .mymainframe = mymainframe;
054: this .currentdata = data;
055: this .isMod = true;
056: try {
057: jbInit();
058: String tmp;
059: tmp = (String) currentdata.get("DBName");
060: DBName.setText((tmp == null ? "" : tmp));
061: DBName.disable();
062: tmp = (String) currentdata.get("DBHost");
063: DBHost.setText((tmp == null ? "" : tmp));
064: tmp = (String) currentdata.get("DBUser");
065: DBUser.setText((tmp == null ? "" : tmp));
066: tmp = (String) currentdata.get("DBPass");
067: DBPass.setText((tmp == null ? "" : tmp));
068: tmp = (String) currentdata.get("DBPort");
069: DBPort.setText((tmp == null ? "" : tmp));
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073: }
074:
075: private void jbInit() throws Exception {
076: NameLabel.setText("Name:");
077: NameLabel.setBounds(new Rectangle(10, 10, 47, 23));
078: this .getContentPane().setLayout(null);
079: HostLabel.setText("Host:");
080: HostLabel.setBounds(new Rectangle(10, 32, 47, 23));
081: UserLabel.setText("User:");
082: UserLabel.setBounds(new Rectangle(10, 54, 47, 23));
083: PassLabel.setText("Password:");
084: PassLabel.setBounds(new Rectangle(10, 76, 68, 23));
085: PortLabel.setText("Port:");
086: PortLabel.setBounds(new Rectangle(10, 98, 47, 23));
087: DBName.setBounds(new Rectangle(77, 14, 125, 20));
088: DBHost.setBounds(new Rectangle(77, 35, 125, 20));
089: DBUser.setBounds(new Rectangle(77, 56, 125, 20));
090: DBPass.setBounds(new Rectangle(77, 76, 125, 20));
091: DBPort.setText("3306");
092: DBPort.setBounds(new Rectangle(77, 96, 125, 20));
093: DBName.addActionListener(aListener);
094: DBHost.addActionListener(aListener);
095: DBUser.addActionListener(aListener);
096: DBPass.addActionListener(aListener);
097: DBPort.addActionListener(aListener);
098: SaveButton.setText("Save");
099: SaveButton.setBounds(new Rectangle(10, 130, 78, 27));
100: SaveButton
101: .addActionListener(new java.awt.event.ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: SaveButton_actionPerformed(e);
104: }
105: });
106: CancelButton.setBounds(new Rectangle(124, 130, 78, 27));
107: CancelButton
108: .addActionListener(new java.awt.event.ActionListener() {
109: public void actionPerformed(ActionEvent e) {
110: CancelButton_actionPerformed(e);
111: }
112: });
113: CancelButton.setText("Cancel");
114: //this.setTitle("Add/Modify Database Record");
115: this
116: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
117: this .getContentPane().add(NameLabel, null);
118: this .getContentPane().add(PassLabel, null);
119: this .getContentPane().add(PortLabel, null);
120: this .getContentPane().add(HostLabel, null);
121: this .getContentPane().add(UserLabel, null);
122: this .getContentPane().add(DBName, null);
123: this .getContentPane().add(DBHost, null);
124: this .getContentPane().add(DBUser, null);
125: this .getContentPane().add(DBPass, null);
126: this .getContentPane().add(DBPort, null);
127: this .getContentPane().add(SaveButton, null);
128: this .getContentPane().add(CancelButton, null);
129: }
130:
131: void CancelButton_actionPerformed(ActionEvent e) {
132: int x = JOptionPane.showConfirmDialog(this ,
133: "Are you sure you want to lose changes?",
134: "Confirm cancel", JOptionPane.YES_NO_OPTION,
135: JOptionPane.WARNING_MESSAGE);
136: if (x == 0) {
137: mymainframe.reEnable();
138: this .dispose();
139: }
140: }
141:
142: void SaveButton_actionPerformed(ActionEvent e) {
143: currentdata.put("DBName", DBName.getText());
144: currentdata.put("DBHost", DBHost.getText());
145: currentdata.put("DBUser", DBUser.getText());
146: currentdata.put("DBPass", DBPass.getText());
147: currentdata.put("DBPort", DBPort.getText());
148: // replace entry if there is one with the current name
149: Iterator i = GUAM.databases.iterator();
150: while (i.hasNext()) {
151: try {
152: Hashtable h = (Hashtable) i.next();
153: if (h.get("DBName").equals(DBName.getText())) {
154: GUAM.databases.remove(h);
155: }
156: } catch (Exception E) {
157: // Weirdness in the Vector...
158: }
159: }
160: GUAM.databases.add(currentdata);
161: if (!isMod) {
162: mymainframe.populateDBList();
163: }
164: GUAM.saveDB();
165: mymainframe.reEnable();
166: this.dispose();
167: }
168: }
|