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.awt.event.ActionEvent;
027: import java.awt.event.ItemEvent;
028: import java.awt.event.ItemListener;
029: import java.io.IOException;
030: import javax.swing.JComboBox;
031: import javax.swing.JComponent;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import org.dlib.gui.FlexLayout;
035: import org.dlib.gui.MultiPanel;
036: import org.fao.gast.gui.panels.FormPanel;
037: import org.fao.gast.lib.Lib;
038:
039: //==============================================================================
040:
041: public class MainPanel extends FormPanel {
042: //---------------------------------------------------------------------------
043: //---
044: //--- Constructor
045: //---
046: //---------------------------------------------------------------------------
047:
048: public MainPanel() {
049: for (DbmsPanel p : panels) {
050: multiPanel.add(p.getLabel(), p);
051: cmbDbms.addItem(p.getLabel());
052: }
053:
054: multiPanel.show(panels[0].getLabel());
055:
056: //--- show proper panel
057:
058: String url = Lib.config.getDbmsURL();
059:
060: for (DbmsPanel p : panels)
061: if (p.matches(url)) {
062: p.retrieve();
063: multiPanel.show(p.getLabel());
064: cmbDbms.setSelectedItem(p.getLabel());
065: break;
066: }
067:
068: //--- setup combobox
069:
070: cmbDbms.addItemListener(itemList);
071: }
072:
073: //---------------------------------------------------------------------------
074: //---
075: //--- ActionListener
076: //---
077: //---------------------------------------------------------------------------
078:
079: public void actionPerformed(ActionEvent e) {
080: DbmsPanel p = panels[cmbDbms.getSelectedIndex()];
081:
082: try {
083: p.save();
084: Lib.gui.showInfo(this , "Configuration saved");
085: }
086:
087: catch (IOException ex) {
088: Lib.gui.showError(this ,
089: "Raised an exception while saving the configuration:\n"
090: + ex.getMessage());
091:
092: ex.printStackTrace();
093: }
094:
095: catch (Exception ex) {
096: Lib.gui.showError(this , ex.getMessage());
097: }
098: }
099:
100: //---------------------------------------------------------------------------
101: //---
102: //--- Protected methods
103: //---
104: //---------------------------------------------------------------------------
105:
106: protected JComponent buildInnerPanel() {
107: //--- setup container
108:
109: JPanel p = new JPanel();
110:
111: FlexLayout fl = new FlexLayout(2, 3);
112: fl.setColProp(1, FlexLayout.EXPAND);
113: fl.setRowProp(1, FlexLayout.EXPAND);
114: p.setLayout(fl);
115:
116: p.add("0,0", new JLabel("DBMS"));
117: p.add("1,0,x", cmbDbms);
118: p.add("0,2,x,x,2", multiPanel);
119:
120: return p;
121: }
122:
123: //---------------------------------------------------------------------------
124: //---
125: //--- Variables
126: //---
127: //---------------------------------------------------------------------------
128:
129: private JComboBox cmbDbms = new JComboBox();
130: private MultiPanel multiPanel = new MultiPanel();
131:
132: //---------------------------------------------------------------------------
133:
134: private static final DbmsPanel panels[] = { new EmbeddedPanel(),
135: new OraclePanel(), new MySQLPanel(), new PostgresPanel(),
136: new GenericPanel() //--- this must be the last one
137: };
138:
139: //---------------------------------------------------------------------------
140:
141: private ItemListener itemList = new ItemListener() {
142: public void itemStateChanged(ItemEvent e) {
143: if (e.getStateChange() == ItemEvent.SELECTED)
144: multiPanel.show(e.getItem().toString());
145: }
146: };
147: }
148:
149: //==============================================================================
150:
151: abstract class DbmsPanel extends JPanel {
152: public abstract String getLabel();
153:
154: public abstract boolean matches(String url);
155:
156: public abstract void retrieve();
157:
158: public abstract void save() throws Exception;
159: }
160:
161: //==============================================================================
|