001: //==============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.gui.panels.config.dbms;
025:
026: import java.util.StringTokenizer;
027: import javax.swing.JLabel;
028: import javax.swing.JTextField;
029: import org.dlib.gui.FlexLayout;
030: import org.fao.gast.lib.Lib;
031:
032: //==============================================================================
033:
034: public class PostgresPanel extends DbmsPanel {
035: //---------------------------------------------------------------------------
036: //---
037: //--- Constructor
038: //---
039: //---------------------------------------------------------------------------
040:
041: public PostgresPanel() {
042: FlexLayout fl = new FlexLayout(3, 5);
043: fl.setColProp(1, FlexLayout.EXPAND);
044: setLayout(fl);
045:
046: add("0,0", new JLabel("Server"));
047: add("0,1", new JLabel("Port"));
048: add("0,2", new JLabel("Database"));
049: add("0,3", new JLabel("Username"));
050: add("0,4", new JLabel("Password"));
051:
052: add("1,0", txtServer);
053: add("1,1", txtPort);
054: add("1,2", txtDatabase);
055: add("1,3", txtUser);
056: add("1,4", txtPass);
057:
058: add("2,2", new JLabel("<html><font color='red'>(REQ)</font>"));
059:
060: txtPort.setToolTipText("The default port is 5432");
061: }
062:
063: //---------------------------------------------------------------------------
064: //---
065: //--- DbmsPanel methods
066: //---
067: //---------------------------------------------------------------------------
068:
069: public String getLabel() {
070: return "PostgreSQL";
071: }
072:
073: //---------------------------------------------------------------------------
074:
075: public boolean matches(String url) {
076: return url.startsWith(PREFIX);
077: }
078:
079: //---------------------------------------------------------------------------
080: //--- jdbc:postgresql:<//host>:<port>/<database>
081: //--- jdbc:postgresql:<database>
082:
083: public void retrieve() {
084: String url = Lib.config.getDbmsURL();
085:
086: //--- cut prefix
087: url = url.substring(PREFIX.length());
088:
089: String server = "";
090: String port = "";
091: String database = url;
092:
093: if (url.startsWith("//") && url.length() > 2) {
094: StringTokenizer st = new StringTokenizer(url.substring(2),
095: "/");
096:
097: server = st.nextToken();
098: database = st.hasMoreTokens() ? st.nextToken() : "";
099:
100: int pos = server.indexOf(":");
101:
102: if (pos != -1) {
103: port = server.substring(pos + 1);
104: server = server.substring(0, pos);
105: }
106: }
107:
108: txtServer.setText(server);
109: txtPort.setText(port);
110: txtDatabase.setText(database);
111: txtUser.setText(Lib.config.getDbmsUser());
112: txtPass.setText(Lib.config.getDbmsPassword());
113: }
114:
115: //---------------------------------------------------------------------------
116:
117: public void save() throws Exception {
118: String server = txtServer.getText();
119: String port = txtPort.getText();
120: String database = txtDatabase.getText();
121:
122: if (database.equals(""))
123: throw new Exception("The database cannot be empty");
124:
125: if (!server.equals("") && !port.equals("")
126: && !Lib.type.isInteger(port))
127: throw new Exception("The port must be an integer");
128:
129: String url = server.equals("") ? PREFIX + database : port
130: .equals("") ? PREFIX + "//" + server + "/" + database
131: : PREFIX + "//" + server + ":" + port + "/" + database;
132:
133: Lib.config.setDbmsDriver("org.postgresql.Driver");
134: Lib.config.setDbmsURL(url);
135: Lib.config.setDbmsUser(txtUser.getText());
136: Lib.config.setDbmsPassword(txtPass.getText());
137: Lib.config.removeActivator();
138: Lib.config.save();
139: }
140:
141: //---------------------------------------------------------------------------
142: //---
143: //--- Variables
144: //---
145: //---------------------------------------------------------------------------
146:
147: private JTextField txtServer = new JTextField(15);
148: private JTextField txtPort = new JTextField(6);
149: private JTextField txtDatabase = new JTextField(12);
150: private JTextField txtUser = new JTextField(12);
151: private JTextField txtPass = new JTextField(12);
152:
153: //---------------------------------------------------------------------------
154:
155: private static final String PREFIX = "jdbc:postgresql:";
156: }
157:
158: //==============================================================================
|