// ********************************************************************** // ********************************************************************** package com.obrctm.openmap.editor; import java.awt.*; import java.awt.event.*; import java.beans.*; import java.beans.beancontext.*; import java.io.Serializable; import java.net.URL; import java.util.*; import javax.swing.*; import javax.accessibility.*; import com.bbn.openmap.*; import com.bbn.openmap.gui.*; import com.bbn.openmap.event.LayerEvent; import com.bbn.openmap.event.LayerListener; import com.bbn.openmap.event.LayerSupport; import com.bbn.openmap.plugin.*; import com.bbn.openmap.util.ComponentFactory; import com.bbn.openmap.util.Debug; import com.bbn.openmap.util.PropUtils; import com.bbn.openmap.util.propertyEditor.*; /** * Class to interactively add a Layer to the map. A LayerAddPanel * utilizes the bean context mechanisms to keep up to date about the * applications LayerHandler and PropertyHandler (see findAndInit * method). A property is used to determine objects of which Layer * subclasses can be instantiated (see static String layerTypes), for * configuration of a layer-to-be an Inspector is invoked to inspect * and configure a Layer object through the PropertyConsumer * interface. */ public class LayerAddFromFile extends JPanel implements Serializable, ActionListener { /** * Holds the PropertyHandler. */ protected PropertyHandler propertyHandler = null; /** * Holds the LayerHandler. */ protected LayerHandler layerHandler = null; /** The String to use as a prefix for the new Layer's properties. */ protected JTextField prefixTextField = null; /** The LayerAddPanel GUI. */ protected JFrame window = null; /** Action command String for JButton. */ protected final String configureActionCommand = "configureActionCommand"; /** The Inspector to handle the configuration of the new Layer. */ protected Inspector inspector = null; /** The layer to configure and add. */ protected Object layer; protected String layerClassesName = null; protected boolean sourceOfData; // true - new file, false - from file /** * Creates the LayerPanel. */ public LayerAddFromFile() { super(); inspector = new Inspector(); inspector.addActionListener((java.awt.event.ActionListener)this); } /** * Creates the LayerPanel. * @param l the LayerHandler controlling the layers. */ public LayerAddFromFile(PropertyHandler p, LayerHandler l) { this(); propertyHandler = p; layerHandler = l; } public final static String DefaultLayerName = "Layer Name"; /** * Produces a dialog panel to add a layer. If the layers haven't * been manually added through createPanel(layers), then the * PropertyHandler is consulted and the layer list is built from * the layerTypes property. */ public void createPanel() { window = new JFrame("Add a Layer"); JPanel panel = new JPanel(); JButton configureButton = new JButton("Configure"); configureButton.addActionListener(this); configureButton.setActionCommand(configureActionCommand); prefixTextField = new JTextField(DefaultLayerName, 12); panel.add(prefixTextField); panel.add(configureButton); window.getContentPane().add(panel); window.pack();// window.setVisible(true); } /** * Method associated with the ActionListener interface. */ public void actionPerformed(java.awt.event.ActionEvent e) { if (e.getActionCommand()==configureActionCommand) { String prefix = prefixTextField.getText().trim(); layer = ComponentFactory.create(layerClassesName, null, null); if (layer instanceof PropertyConsumer) { if (layer instanceof PlugIn) { PlugInLayer pil = new PlugInLayer(); pil.setPlugIn((PlugIn)layer); pil.setName(prefix); layer = pil; } if (layer instanceof Layer) { // Set the pretty name to what the user chose. ((Layer)layer).setName(prefix); } // Set the prefix to a modified version of the pretty name. prefix = propertyHandler.getUniquePrefix(prefix); ((PropertyConsumer)layer).setPropertyPrefix(prefix); inspector.inspectPropertyConsumer((PropertyConsumer)layer); if (isNewFile()) { ActionEvent ee = new ActionEvent(this,0,inspector.doneCommand); inspector.actionPerformed(ee); } } hidePanel(); } else if (e.getActionCommand() == inspector.doneCommand) { // the confirmation button of the Inspector panel was pressed // find the beancontext and add the layer at hand (var. layer) if (layer != null && layerHandler != null) { if (layer instanceof Layer) { // Let's add it on top, so the user can find it // easier, instead of adding it to the bottom and // having it lost behind some other layers. layerHandler.addLayer((Layer)layer, 0); } else if (layer instanceof PlugIn) { PlugInLayer pil = (PlugInLayer)((PlugIn)layer).getComponent(); layerHandler.addLayer(pil, 0); } // prefixTextField.setText(DefaultLayerName); } else if (layerHandler != null) { JOptionPane.showMessageDialog(window, "Layer Handler not found.\nCan't find anything to add the layer to."); } else { JOptionPane.showMessageDialog(window, "No Layer instantiated"); } } else if (e.getActionCommand() == inspector.cancelCommand) { if (layer != null && propertyHandler != null) { propertyHandler.removeUsedPrefix(((PropertyConsumer)layer).getPropertyPrefix()); } } } public void showPanel() { if (window == null) { createPanel(); } prefixTextField.setText(DefaultLayerName); window.setVisible(true); } public void hidePanel() { window.setVisible(false); } public boolean isNewFile() { return sourceOfData; } public void addLayerToHandler(String aClassName) { addLayerToHandler(aClassName, false); } public void addLayerToHandler(String aClassName, boolean aNewFile) { layerClassesName = aClassName; sourceOfData = aNewFile; showPanel(); } }