arcgissamples\scenario\ConnectionDialog.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.scenario; /**Dialog to connect to mapservers running on a machine. * Input parameters are the webservice url (WebService created using webcatalog) and connection type. * In case of Lan connection input parameter will be machine name * Using gisclient libraries, you can connect to mapserver either by LAN or Internet. * After connection is established, map configurations that currently available are listed in the map names dropdown. * Based on the configuration selected, map is displayed on the map component. */ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Frame; import java.util.Iterator; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JTextField; import arcgissamples.scenario.map.MapInfo; public class ConnectionDialog extends javax.swing.JDialog { private static final long serialVersionUID = 1L; static ConnectionDialog connectionDialog; Object connectionTypeValues[] = {"Internet", "LAN"}; String connectionLabelString[] = {"Catalog URL: ", "Machine Name: "}; JLabel connectionURLLabel = null; JTextField connectionURLText = null; JLabel connectionTypeLabel = null; JComboBox connectionTypeCombo = null; JLabel mapNamesDropdownLabel = null; JComboBox mapNamesDropdown = null; JPanel leftPanel = null; JPanel rightPanel = null; JPanel centerPanel = null; JPanel bottomPanel = null; JSplitPane splitPane = null; JButton connectButton = null; JButton addMapButton = null; String connectionURL = ""; String connectionType = ""; String mapConfiguartionName; public ConnectionDialog() { this(null); } public ConnectionDialog(Frame owner) { super(owner, "Server Connection Dialog"); init(); setSize(400, 300); setModal(true); setResizable(true); } public static ConnectionDialog getConnectionDialog(Frame owner) { if(connectionDialog == null) connectionDialog = new ConnectionDialog(owner); return connectionDialog; } public static void disposeDialog() { if(connectionDialog != null) connectionDialog.dispose(); } public void init() { connectionURLLabel = new JLabel(connectionLabelString[0]); connectionURLText = new javax.swing.JTextField(); connectionTypeLabel = new JLabel("Connection Type: "); connectionTypeCombo = new JComboBox(connectionTypeValues); connectionTypeCombo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent event) { if(connectionTypeCombo.getSelectedItem().toString().equalsIgnoreCase("internet")) connectionURLLabel.setText(connectionLabelString[0]); else connectionURLLabel.setText(connectionLabelString[1]); } }); mapNamesDropdownLabel = new JLabel("Map Server Objects: "); mapNamesDropdown = new JComboBox(); leftPanel = new JPanel(); rightPanel = new JPanel(); centerPanel = new JPanel(); BoxLayout boxLayout1 = new BoxLayout(leftPanel, BoxLayout.Y_AXIS); leftPanel.setLayout(boxLayout1); leftPanel.add(Box.createVerticalStrut(15)); leftPanel.add(connectionTypeLabel); leftPanel.add(Box.createVerticalStrut(10)); leftPanel.add(connectionURLLabel); leftPanel.add(Box.createVerticalStrut(10)); BoxLayout boxLayout2 = new BoxLayout(rightPanel, BoxLayout.Y_AXIS); rightPanel.setLayout(boxLayout2); rightPanel.add(Box.createVerticalStrut(15)); rightPanel.add(connectionTypeCombo); rightPanel.add(Box.createVerticalStrut(5)); rightPanel.add(connectionURLText); rightPanel.add(Box.createVerticalStrut(5)); JPanel centerPanel = new JPanel(); BoxLayout boxLayout3 = new BoxLayout(centerPanel, BoxLayout.X_AXIS); centerPanel.setLayout(boxLayout3); centerPanel.add(Box.createHorizontalStrut(30)); centerPanel.add(leftPanel); centerPanel.add(Box.createHorizontalStrut(10)); centerPanel.add(rightPanel); centerPanel.add(Box.createHorizontalStrut(30)); centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); JPanel connectPanel = new JPanel(); connectButton = new JButton("Connect"); connectPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); connectPanel.add(this.connectButton); JPanel centerPanel1 = new JPanel(); centerPanel1.setLayout(new BorderLayout()); centerPanel1.add(centerPanel, BorderLayout.CENTER); centerPanel1.add(connectPanel, BorderLayout.SOUTH); bottomPanel = new JPanel(); BoxLayout boxLayout4 = new BoxLayout(bottomPanel,BoxLayout.Y_AXIS); bottomPanel.setLayout(boxLayout4); mapNamesDropdownLabel.setPreferredSize(new Dimension(400, 25)); mapNamesDropdownLabel.setMaximumSize(new Dimension(400, 25)); mapNamesDropdownLabel.setMinimumSize(new Dimension(400, 25)); mapNamesDropdown.setPreferredSize(new Dimension(400, 25)); mapNamesDropdown.setMaximumSize(new Dimension(400, 25)); mapNamesDropdown.setMinimumSize(new Dimension(400, 25)); JPanel mapButtonPanel = new JPanel(); mapButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); addMapButton = new JButton("Add Map"); mapButtonPanel.add(this.addMapButton); bottomPanel.add(Box.createVerticalStrut(10)); bottomPanel.add(mapNamesDropdownLabel); bottomPanel.add(mapNamesDropdown); bottomPanel.add(Box.createVerticalStrut(10)); bottomPanel.add(mapButtonPanel); bottomPanel.add(Box.createVerticalGlue()); bottomPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 20)); splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setDividerSize(10); splitPane.setTopComponent(centerPanel1); splitPane.setBottomComponent(bottomPanel); this.getContentPane().add(splitPane, java.awt.BorderLayout.CENTER); connectButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent event) { ConnectionDialog.this.connectionButtonactionPerformed(); } }); addMapButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent event) { ConnectionDialog.this.addMapButtonActionPerformed(); } }); } private void connectionButtonactionPerformed() { //Get all the text field values.. connectionURL = this.connectionURLText.getText().trim(); connectionType = this.connectionTypeCombo.getSelectedItem().toString().trim(); //Get the configuration list MapInfo.setConnectionType(connectionType); java.util.Vector configurationNames = MapInfo.getMapInfoInstance().getConfigurationList(connectionURL, connectionType); //Fill the combobox with the available configurations DefaultComboBoxModel model = (DefaultComboBoxModel)mapNamesDropdown.getModel(); model.removeAllElements(); for(Iterator i = configurationNames.iterator(); i.hasNext(); ) { Object data = i.next(); model.addElement(data); } if(configurationNames.size() != 0) mapNamesDropdown.setSelectedIndex(0); } private void addMapButtonActionPerformed() { Object selectedValue = this.mapNamesDropdown.getSelectedItem(); if(selectedValue == null || selectedValue.toString().equalsIgnoreCase("")) return; mapConfiguartionName = selectedValue.toString(); setVisible(false); } /**Returns the selected map configuration */ public String getConfigurationName() { return mapConfiguartionName; } }