001: //==============================================================================
002: //===
003: //=== MainFrame
004: //===
005: //==============================================================================
006: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
007: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
008: //=== and United Nations Environment Programme (UNEP)
009: //===
010: //=== This program is free software; you can redistribute it and/or modify
011: //=== it under the terms of the GNU General Public License as published by
012: //=== the Free Software Foundation; either version 2 of the License, or (at
013: //=== your option) any later version.
014: //===
015: //=== This program is distributed in the hope that it will be useful, but
016: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
017: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: //=== General Public License for more details.
019: //===
020: //=== You should have received a copy of the GNU General Public License
021: //=== along with this program; if not, write to the Free Software
022: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
023: //===
024: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
025: //=== Rome - Italy. email: geonetwork@osgeo.org
026: //==============================================================================
027:
028: package org.fao.geonet.csw.client;
029:
030: import java.awt.BorderLayout;
031: import java.awt.Font;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.awt.event.ItemEvent;
035: import java.awt.event.ItemListener;
036: import java.io.PrintWriter;
037: import java.io.StringWriter;
038: import java.io.UnsupportedEncodingException;
039: import java.util.List;
040: import javax.swing.JButton;
041: import javax.swing.JComboBox;
042: import javax.swing.JFrame;
043: import javax.swing.JLabel;
044: import javax.swing.JPanel;
045: import javax.swing.JPasswordField;
046: import javax.swing.JScrollPane;
047: import javax.swing.JTextArea;
048: import javax.swing.JTextField;
049: import org.dlib.gui.FlexLayout;
050: import org.dlib.gui.MultiPanel;
051: import org.dlib.gui.TPanel;
052: import org.fao.geonet.csw.common.exceptions.CatalogException;
053: import org.fao.geonet.csw.common.requests.CatalogRequest;
054: import org.jdom.Element;
055: import javax.swing.JCheckBox;
056:
057: //==============================================================================
058:
059: public class MainFrame extends JFrame implements ActionListener,
060: ItemListener {
061: private JTextField txtHost = new JTextField("localhost", 15);
062: private JTextField txtPort = new JTextField("8080");
063: private JTextField txtSrvAddr = new JTextField(
064: "/geonetwork/srv/en/csw");
065: private JTextField txtLogAddr = new JTextField(
066: "/geonetwork/srv/en/xml.user.login");
067: private JTextField txtUser = new JTextField("a");
068: private JPasswordField txtPass = new JPasswordField("a");
069: private JComboBox cmbOperat = new JComboBox();
070: private JComboBox cmbMethod = new JComboBox();
071: private JButton btnSend = new JButton("Send");
072: private JTextArea txaLog = new JTextArea(40, 100);
073: private JCheckBox chbSoap = new JCheckBox("Use SOAP");
074:
075: private MultiPanel paramsPanel = new MultiPanel();
076:
077: private GetCapabilitiesPanel getCapPanel = new GetCapabilitiesPanel();
078: private DescribeRecordPanel desRecPanel = new DescribeRecordPanel();
079: private GetRecordByIdPanel getByIdPanel = new GetRecordByIdPanel();
080: private GetRecordsPanel getRecsPanel = new GetRecordsPanel();
081:
082: private enum Operation {
083: GetCapabilities, DescribeRecord, GetRecordById, GetRecords
084: }
085:
086: //---------------------------------------------------------------------------
087: //---
088: //--- Constructor
089: //---
090: //---------------------------------------------------------------------------
091:
092: public MainFrame() {
093: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
094: setTitle("GeoNetwork CSW 2.0.1 test application");
095:
096: //------------------------------------------------------------------------
097:
098: btnSend.addActionListener(this );
099: btnSend.setActionCommand("send");
100:
101: txaLog.setFont(new Font("Monospaced", Font.PLAIN, 12));
102: txaLog.setEditable(false);
103:
104: for (Operation oper : Operation.values())
105: cmbOperat.addItem(oper);
106:
107: // cmbOperat.addItem("DescribeRecord");
108: // tcbOperat.addItem("GetDomain");
109: // cmbOperat.addItem("GetRecordById");
110: // cmbOperat.addItem("GetRecords");
111: // tcbOperat.addItem("Transaction");
112: // tcbOperat.addItem("Harvest");
113:
114: cmbMethod.addItem("GET");
115: cmbMethod.addItem("POST");
116:
117: cmbOperat.addItemListener(this );
118:
119: //------------------------------------------------------------------------
120:
121: paramsPanel.add(Operation.GetCapabilities.toString(),
122: getCapPanel);
123: paramsPanel.add(Operation.DescribeRecord.toString(),
124: desRecPanel);
125: paramsPanel.add(Operation.GetRecordById.toString(),
126: getByIdPanel);
127: paramsPanel.add(Operation.GetRecords.toString(), getRecsPanel);
128:
129: //------------------------------------------------------------------------
130:
131: getContentPane().add(buildLeftPanel(), BorderLayout.WEST);
132: getContentPane().add(buildLogPanel(), BorderLayout.CENTER);
133:
134: pack();
135: setVisible(true);
136: }
137:
138: //---------------------------------------------------------------------------
139: //---
140: //--- ActionListener
141: //---
142: //---------------------------------------------------------------------------
143:
144: public void actionPerformed(ActionEvent e) {
145: String cmd = e.getActionCommand();
146:
147: if (cmd.equals("send"))
148: handleSend();
149: }
150:
151: //---------------------------------------------------------------------------
152:
153: private void handleSend() {
154: clearLog();
155: CatalogRequest req = buildRequest();
156:
157: if (req == null) {
158: log("Invalid operation");
159: return;
160: }
161:
162: req.setHost(txtHost.getText());
163:
164: String port = txtPort.getText();
165:
166: if (!port.equals(""))
167: req.setPort(Integer.parseInt(port));
168:
169: req.setAddress(txtSrvAddr.getText());
170: req.setLoginAddress(txtLogAddr.getText());
171:
172: try {
173: if (txtUser.getText().trim().length() != 0) {
174: req.login(txtUser.getText(), txtPass.getText());
175: log(req.getSentData());
176: logLine();
177: log(req.getReceivedData());
178: }
179:
180: Element response = req.execute();
181: } catch (Exception e) {
182: log("Exception : " + e);
183: }
184:
185: logLine();
186: log(req.getSentData());
187: logLine();
188: log(req.getReceivedData());
189: }
190:
191: //---------------------------------------------------------------------------
192:
193: private CatalogRequest buildRequest() {
194: CatalogRequest request;
195:
196: Operation oper = (Operation) cmbOperat.getSelectedItem();
197:
198: switch (oper) {
199: case GetCapabilities:
200: request = getCapPanel.createRequest();
201: break;
202:
203: case DescribeRecord:
204: request = desRecPanel.createRequest();
205: break;
206:
207: // case GetDomain: request = getDomPanel.createRequest();
208: // break;
209:
210: case GetRecordById:
211: request = getByIdPanel.createRequest();
212: break;
213:
214: case GetRecords:
215: request = getRecsPanel.createRequest();
216: break;
217:
218: default:
219: return null;
220: }
221:
222: //--- set request method
223:
224: CatalogRequest.Method method = (cmbMethod.getSelectedIndex() == 0) ? CatalogRequest.Method.GET
225: : CatalogRequest.Method.POST;
226:
227: request.setMethod(method);
228: request.setUseSOAP(chbSoap.isSelected());
229:
230: return request;
231: }
232:
233: //---------------------------------------------------------------------------
234: //---
235: //--- ItemListener
236: //---
237: //---------------------------------------------------------------------------
238:
239: public void itemStateChanged(ItemEvent e) {
240: paramsPanel.show(cmbOperat.getSelectedItem().toString());
241: }
242:
243: //---------------------------------------------------------------------------
244: //---
245: //--- Private methods
246: //---
247: //---------------------------------------------------------------------------
248:
249: private JPanel buildLeftPanel() {
250: JPanel p = new JPanel();
251:
252: FlexLayout flexL = new FlexLayout(1, 5);
253: flexL.setColProp(0, FlexLayout.EXPAND);
254: flexL.setRowProp(4, FlexLayout.EXPAND);
255: p.setLayout(flexL);
256:
257: p.add("0,0,x", buildServerPanel());
258: p.add("0,1,x", buildLoginPanel());
259: p.add("0,2,x", buildRequestPanel());
260: p.add("0,3,x", btnSend);
261: p.add("0,4,x,x", paramsPanel);
262:
263: return p;
264: }
265:
266: //---------------------------------------------------------------------------
267:
268: private JPanel buildServerPanel() {
269: JPanel p = new TPanel("Server");
270:
271: FlexLayout flexL = new FlexLayout(2, 4);
272: flexL.setColProp(1, FlexLayout.EXPAND);
273: p.setLayout(flexL);
274:
275: p.add("0,0", new JLabel("Host"));
276: p.add("0,1", new JLabel("Port"));
277: p.add("0,2", new JLabel("CSW service"));
278: p.add("0,3", new JLabel("Login service"));
279:
280: p.add("1,0,x", txtHost);
281: p.add("1,1,x", txtPort);
282: p.add("1,2,x", txtSrvAddr);
283: p.add("1,3,x", txtLogAddr);
284:
285: return p;
286: }
287:
288: //---------------------------------------------------------------------------
289:
290: private JPanel buildLoginPanel() {
291: JPanel p = new TPanel("Login");
292:
293: FlexLayout flexL = new FlexLayout(2, 2);
294: flexL.setColProp(1, FlexLayout.EXPAND);
295: p.setLayout(flexL);
296:
297: p.add("0,0", new JLabel("Username"));
298: p.add("0,1", new JLabel("Password"));
299:
300: p.add("1,0,x", txtUser);
301: p.add("1,1,x", txtPass);
302:
303: return p;
304: }
305:
306: //---------------------------------------------------------------------------
307:
308: private JPanel buildRequestPanel() {
309: JPanel p = new TPanel("Request");
310:
311: FlexLayout flexL = new FlexLayout(2, 3);
312: flexL.setColProp(1, FlexLayout.EXPAND);
313: p.setLayout(flexL);
314:
315: p.add("0,0", new JLabel("Operation"));
316: p.add("0,1", new JLabel("Method"));
317:
318: p.add("1,0,x", cmbOperat);
319: p.add("1,1,x", cmbMethod);
320:
321: p.add("0,2,x,c,2", chbSoap);
322:
323: return p;
324: }
325:
326: //---------------------------------------------------------------------------
327:
328: private JPanel buildLogPanel() {
329: JPanel p = new TPanel("Communication log");
330:
331: FlexLayout flexL = new FlexLayout(1, 1);
332: flexL.setColProp(0, FlexLayout.EXPAND);
333: flexL.setRowProp(0, FlexLayout.EXPAND);
334: p.setLayout(flexL);
335:
336: p.add("0,0,x,x", new JScrollPane(txaLog));
337:
338: return p;
339: }
340:
341: //---------------------------------------------------------------------------
342: //---
343: //--- Logging facility
344: //---
345: //---------------------------------------------------------------------------
346:
347: private void clearLog() {
348: txaLog.setText("");
349: }
350:
351: //---------------------------------------------------------------------------
352:
353: private void log(List list) {
354: for (int i = 0; i < list.size(); i++) {
355: if (list.get(i) instanceof String)
356: txaLog.append((String) list.get(i) + "\n");
357: else {
358: try {
359: String text = new String((byte[]) list.get(i),
360: "UTF-8");
361: txaLog.append(text);
362: } catch (UnsupportedEncodingException e) {
363: e.printStackTrace();
364: }
365: }
366: }
367: }
368:
369: //---------------------------------------------------------------------------
370:
371: private void log(String text) {
372: txaLog.append(text + "\n");
373: }
374:
375: //---------------------------------------------------------------------------
376:
377: private void logLine() {
378: txaLog
379: .append("==============================================================\n");
380: }
381: }
382:
383: //==============================================================================
|