01: package com.vividsolutions.jump.workbench.ui;
02:
03: import com.vividsolutions.jump.workbench.WorkbenchContext;
04: import com.vividsolutions.jump.workbench.model.*;
05: import com.vividsolutions.jump.workbench.ui.plugin.AddNewLayerPlugIn;
06:
07: import java.awt.BorderLayout;
08: import java.awt.Color;
09: import java.awt.event.ActionListener;
10:
11: import java.util.Iterator;
12:
13: import javax.swing.DefaultComboBoxModel;
14: import javax.swing.JComboBox;
15: import javax.swing.JPanel;
16: import javax.swing.event.InternalFrameAdapter;
17: import javax.swing.event.InternalFrameEvent;
18:
19: /**
20: * Stays in sync with the LayerManager.
21: */
22:
23: public class LayerComboBox extends JComboBox {
24: //For the layer name, use " " rather than "" so that the renderer is sized normally.
25: //See comments in #updateModel. [Jon Aquino]
26: private static final Layer DUMMY_LAYER = new Layer(" ", new Color(
27: 0, 0, 0, 0), AddNewLayerPlugIn
28: .createBlankFeatureCollection(), new LayerManager());
29: private LayerManager layerManager = new LayerManager();
30:
31: public Object getSelectedItem() {
32: return (super .getSelectedItem() != DUMMY_LAYER) ? super
33: .getSelectedItem() : null;
34: }
35:
36: private LayerListener listener = new LayerListener() {
37: public void featuresChanged(FeatureEvent e) {
38: }
39:
40: public void layerChanged(LayerEvent e) {
41: updateModel();
42: }
43:
44: public void categoryChanged(CategoryEvent e) {
45: }
46: };
47:
48: public LayerComboBox() {
49: super (new DefaultComboBoxModel());
50: setRenderer(new LayerNameRenderer());
51: }
52:
53: private DefaultComboBoxModel getMyModel() {
54: return (DefaultComboBoxModel) getModel();
55: }
56:
57: private void updateModel() {
58: Layer selectedLayer = getSelectedLayer();
59: getMyModel().removeAllElements();
60:
61: for (Iterator i = layerManager.iterator(); i.hasNext();) {
62: Layer layer = (Layer) i.next();
63: getMyModel().addElement(layer);
64: }
65:
66: if (getMyModel().getSize() == 0) {
67: //Don't leave the combobox empty -- an empty combobox is rendered a few
68: //pixels larger than a non-empty one. If it is in a packed frame's GridBagLayout,
69: //the GridBagLayout will collapse all components to minimum size
70: //(see Java bug 4247013). [Jon Aquino]
71: getMyModel().addElement(DUMMY_LAYER);
72: }
73:
74: if (-1 != getMyModel().getIndexOf(selectedLayer)) {
75: getMyModel().setSelectedItem(selectedLayer);
76: }
77: }
78:
79: public Layer getSelectedLayer() {
80: return (Layer) getSelectedItem();
81: }
82:
83: public void setLayerManager(LayerManager layerManager) {
84: this .layerManager.removeLayerListener(listener);
85: this .layerManager = layerManager;
86: this .layerManager.addLayerListener(listener);
87: updateModel();
88: }
89:
90: public LayerManager getLayerManager() {
91: return layerManager;
92: }
93:
94: public void setSelectedLayer(Layer candidateLayer) {
95: setSelectedItem(candidateLayer);
96: }
97:
98: }
|