// *********************************************************************************** // $Source: /home/cvsroot/nodus/src/com/bbn/openmap/plugin/wms/gui/WMSLayersChooserDlg.java,v $ // $RCSfile: WMSLayersChooserDlg.java,v $ // $Revision: 1.1 $ // $Date: 2004/12/03 16:56:24 $ // $Author: jourquin $ // *********************************************************************************** package com.bbn.openmap.plugin.wms.gui; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.mattwelsh.framework.gui.*; import org.fucam.gtm.nodus.*; /** * *

Title: Nodus 5.0

*

Description: This dialog displays a "checkable" list of layer names fetched from a properties * file. When closing it, the properties are updated with a string representing the layers to pass to * a WMS server (a '+' separated list of layer names) *

*

Copyright : Copyright (c) 2004

*

Société : FUCaM-GTM

* @author Bart Jourquin * @version 1.0 */ public class WMSLayersChooserDlg extends JDialog implements INodusConstants { // GUI components private JPanel panel = new JPanel(); private GridBagLayout gridBagLayout = new GridBagLayout(); private JScrollPane scrollPane = new JScrollPane(); private CheckList layerCheckList = new CheckList(); private JButton okButton = new JButton(); private Properties properties; private String pathPrefix; JButton selectAllButton = new JButton(); JButton deselectAllButton = new JButton(); JButton cancelButton = new JButton(); /** * Initialises a dialog box that will display the list of available and selected * WMS layers. Selection/deselection can be done in a list containing checkboxes. * The list is created by means of the information stored in a properties file p, * in which all the relevant data ist prepended with the given prefix. * @param frame Frame * @param prefix String * @param p Properties */ public WMSLayersChooserDlg(Frame frame, String prefix, Properties p) { super(frame, "", true); pathPrefix = prefix; properties = p; setTitle("Available layers"); try { jbInit(); pack(); setLocationRelativeTo(frame); } catch(Exception ex) { ex.printStackTrace(); } } /** * Called by the constructor. Fills the "checkable list" with the available layer names and their * state (selected or not). * @throws Exception */ void jbInit() throws Exception { panel.setLayout(gridBagLayout); okButton.setText("Ok"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { okButton_actionPerformed(e); } }); layerCheckList.setMaximumSize(new Dimension(200, 300)); selectAllButton.setText("Select all"); selectAllButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { selectAllButton_actionPerformed(e); } }); deselectAllButton.setText("Deselect all"); deselectAllButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { deselectAllButton_actionPerformed(e); } }); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cancelButton_actionPerformed(e); } }); panel.setBorder(BorderFactory.createRaisedBevelBorder()); getContentPane().add(panel); panel.add(scrollPane, new GridBagConstraints(0, 0, 4, 1, 0.5, 0.5 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); panel.add(okButton, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 5, 5), 0, 0)); panel.add(selectAllButton, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); panel.add(deselectAllButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); panel.add(cancelButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); scrollPane.getViewport().add(layerCheckList, null); layerCheckList.setModel(getDefaultListModel()); } /** * List model used by the dialog's checkable listbox. Filled with info stored in the * properties * @return ListModel */ protected ListModel getDefaultListModel() { DefaultListModel model = new DefaultListModel(); String availableLayers = properties.getProperty(pathPrefix + PROP_AVAILABLE_LAYERS); String layers = properties.getProperty(pathPrefix + PROP_DOTLAYERS); if (availableLayers != null) { // Build a list of current selected layers LinkedList ll = new LinkedList(); if (layers != null) { // if layers are currently selected StringTokenizer st = new StringTokenizer(layers, ","); while (st.hasMoreTokens()) { String currentName = st.nextToken(); // currentName = currentName.replace('+', ' '); ll.add(currentName); } } // Fill listbox and check the currently selected layers StringTokenizer st = new StringTokenizer(availableLayers, ","); while (st.hasMoreTokens()) { String currentName = st.nextToken(); model.addElement(new CheckableItem(currentName.replace('_', ' '), ll.contains(currentName))); } } return model; } /** * Creates a WMS compliant string of layer names ('+' separated list of names) and * put it in the WMS.Layer entry of the properties. Closes the dialog when done. * @param e ActionEvent */ void okButton_actionPerformed(ActionEvent e) { // Create a string that represent the selected layers StringBuffer layers = new StringBuffer(""); // Create a string to remember wich layers are selected StringBuffer selectedLayers = new StringBuffer(""); int n = layerCheckList.getModel().getSize(); for (int i = 0 ; i < n ; i++) { CheckableItem sel = (CheckableItem) layerCheckList.getModel().getElementAt(i); if (selectedLayers.length() > 0) { selectedLayers.append(','); } if (sel.isChecked()) { if (layers.length() > 0) { layers.append(','); } layers.append(sel.toString().replace(' ', '_')); selectedLayers.append('1'); } else { selectedLayers.append('0'); } } // Replace spaces with + signs for (int i = 0 ; i < layers.length() ; i++) { if (layers.charAt(i) == ' ') { layers.setCharAt(i, '+'); } } // Put the result in the properties properties.put(pathPrefix + PROP_DOTLAYERS, layers.toString()); // Close dialog setVisible(false); } /** * Select all the available layers * @param e ActionEvent */ void selectAllButton_actionPerformed(ActionEvent e) { selectAll(true); } /** * Deselect all the available layers * @param e ActionEvent */ void deselectAllButton_actionPerformed(ActionEvent e) { selectAll(false); } /** * Select/deselect the available layers in the list * @param flag boolean */ void selectAll(boolean flag) { int n = layerCheckList.getModel().getSize(); for (int i = 0 ; i < n ; i++) { CheckableItem sel = (CheckableItem) layerCheckList.getModel().getElementAt(i); sel.setChecked(flag); } layerCheckList.repaint(); } /** * Just closes the dialog box * @param e ActionEvent */ void cancelButton_actionPerformed(ActionEvent e) { setVisible(false); } } /** * *

Title: Nodus 5.0

*

Description: Private class used to select/deselect a given checkbox * in the list

*

Copyright : Copyright (c) 2004

*

Société : FUCaM-GTM

* @author Bart Jourquin * @version 1.0 */ class CheckableItem implements Checkable { private boolean sel; private String name; public CheckableItem(String s, boolean b) { sel = b; name = s; } public boolean isChecked() { return sel; } public void setChecked(boolean v) { sel = v; } public String toString() { return name; } }