svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / generictoolbar / GenericToolBarPanel.java @ 18824
History | View | Annotate | Download (7.38 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.rastertools.generictoolbar; |
20 |
|
21 |
import java.awt.event.ActionEvent; |
22 |
import java.awt.event.ActionListener; |
23 |
import java.util.Iterator; |
24 |
|
25 |
import javax.swing.BoxLayout; |
26 |
import javax.swing.JButton; |
27 |
import javax.swing.JToolBar; |
28 |
|
29 |
import org.gvsig.gui.beans.comboboxconfigurablelookup.DefaultComboBoxConfigurableLookUpModel; |
30 |
import org.gvsig.gui.beans.comboboxconfigurablelookup.JComboBoxConfigurableLookUp; |
31 |
import org.gvsig.gui.beans.controls.combobutton.ComboButton; |
32 |
import org.gvsig.raster.gui.IGenericToolBarMenuItem; |
33 |
|
34 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
35 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
36 |
import com.iver.utiles.extensionPoints.ExtensionPoint; |
37 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
38 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
39 |
/**
|
40 |
* Componente que contiene los objetos visuales de la barra de herramientas
|
41 |
* generica
|
42 |
*
|
43 |
* @version 13/02/2008
|
44 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
45 |
*/
|
46 |
public class GenericToolBarPanel extends JToolBar implements ActionListener { |
47 |
private JComboBoxConfigurableLookUp jCBCLU;
|
48 |
private ComboButton buttonGroup = new ComboButton(); |
49 |
private ComboButton buttonMenu = new ComboButton(); |
50 |
private FLayers lastLayers = null; |
51 |
|
52 |
public GenericToolBarPanel() {
|
53 |
super("GenericToolBarPanel"); |
54 |
initialize(); |
55 |
} |
56 |
|
57 |
/**
|
58 |
* Especifica que las capas de la vista han cambiado.
|
59 |
* @param layers
|
60 |
*/
|
61 |
public void setLayers(FLayers layers) { |
62 |
lastLayers = layers; |
63 |
|
64 |
getComboBoxConfigurableLookUp().removeAllItems(); |
65 |
for (int i = 0; i < layers.getLayersCount(); i++) |
66 |
getComboBoxConfigurableLookUp().addItem(layers.getLayer(i).getName()); |
67 |
|
68 |
getComboBoxConfigurableLookUp().setSelectedItem(layers.getActives()[0].getName());
|
69 |
getComboBoxConfigurableLookUp().hidePopup(); |
70 |
|
71 |
reloadSubMenu(); |
72 |
} |
73 |
|
74 |
/**
|
75 |
* Devuelve un combo de busqueda de items.
|
76 |
* @return
|
77 |
*/
|
78 |
private JComboBoxConfigurableLookUp getComboBoxConfigurableLookUp() {
|
79 |
if (jCBCLU == null) { |
80 |
jCBCLU = new JComboBoxConfigurableLookUp();
|
81 |
((DefaultComboBoxConfigurableLookUpModel) jCBCLU.getModel()).setShowAllItemsInListBox(false);
|
82 |
if (jCBCLU.getModel() instanceof DefaultComboBoxConfigurableLookUpModel) { |
83 |
((DefaultComboBoxConfigurableLookUpModel) jCBCLU.getModel()).setLookUpAgent(new BinarySearch());
|
84 |
jCBCLU.setOnlyOneColorOnText(true);
|
85 |
} |
86 |
} |
87 |
return jCBCLU;
|
88 |
} |
89 |
|
90 |
private void initialize() { |
91 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
92 |
|
93 |
buttonGroup.addActionListener(this);
|
94 |
buttonGroup.setName("Menu principal");
|
95 |
reloadMenuGroup(); |
96 |
add(buttonGroup); |
97 |
|
98 |
buttonMenu.addActionListener(this);
|
99 |
buttonMenu.setName("Submenus");
|
100 |
reloadMenuGroup(); |
101 |
add(buttonMenu); |
102 |
|
103 |
add(getComboBoxConfigurableLookUp()); |
104 |
} |
105 |
|
106 |
/**
|
107 |
* Recarga los items del menu global dejando seleccionado el item que habia
|
108 |
* previamente, en caso de que exista
|
109 |
*/
|
110 |
public void reloadMenuGroup() { |
111 |
String actionCommand = buttonGroup.getActionCommand();
|
112 |
buttonGroup.clearButtons(); |
113 |
buttonMenu.clearButtons(); |
114 |
Iterator iterator = null; |
115 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
116 |
ExtensionPoint extensionPoint = (ExtensionPoint) extensionPoints.get("GenericToolBarGroup");
|
117 |
if (extensionPoint == null) |
118 |
return;
|
119 |
iterator = extensionPoint.keySet().iterator(); |
120 |
while (iterator.hasNext()) {
|
121 |
String key = (String) iterator.next(); |
122 |
Object object = ((Object) extensionPoint.get(key)); |
123 |
if (object instanceof IGenericToolBarMenuItem) { |
124 |
IGenericToolBarMenuItem item = (IGenericToolBarMenuItem) object; |
125 |
JButton button2 = new JButton(item.getText(), item.getIcon()); |
126 |
button2.setActionCommand(key); |
127 |
buttonGroup.addButton(button2); |
128 |
} |
129 |
} |
130 |
buttonGroup.setSelectedItem(actionCommand); |
131 |
reloadSubMenu(); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Recarga los items del submenu dejando seleccionado el item que habia
|
136 |
* previamente, en caso de que exista
|
137 |
*/
|
138 |
public void reloadSubMenu() { |
139 |
String actionCommand = buttonMenu.getActionCommand();
|
140 |
buttonMenu.clearButtons(); |
141 |
Iterator iterator = null; |
142 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
143 |
ExtensionPoint extensionPoint = (ExtensionPoint) extensionPoints.get("GenericToolBarMenu");
|
144 |
if (extensionPoint == null) |
145 |
return;
|
146 |
iterator = extensionPoint.keySet().iterator(); |
147 |
FLayer[] layers = new FLayer[] {getLayerSelected()}; |
148 |
while (iterator.hasNext()) {
|
149 |
String key = (String) iterator.next(); |
150 |
Object object = ((Object) extensionPoint.get(key)); |
151 |
if (object instanceof IGenericToolBarMenuItem) { |
152 |
IGenericToolBarMenuItem item = (IGenericToolBarMenuItem) object; |
153 |
if (!buttonGroup.getActionCommand().equals(item.getGroup()))
|
154 |
continue;
|
155 |
if (!item.isVisible(null, layers)) |
156 |
continue;
|
157 |
// Mirar pq no se puede poner disabled un item del menu
|
158 |
if (!item.isEnabled(null, layers)) |
159 |
continue;
|
160 |
JButton button2 = new JButton(item.getText(), item.getIcon()); |
161 |
button2.setActionCommand(key); |
162 |
buttonMenu.addButton(button2); |
163 |
} |
164 |
} |
165 |
buttonMenu.setSelectedItem(actionCommand); |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Devuelve el layer seleccionado en el combobox
|
170 |
* @return
|
171 |
*/
|
172 |
public FLayer getLayerSelected() {
|
173 |
if (lastLayers == null) |
174 |
return null; |
175 |
for (int i = 0; i < lastLayers.getLayersCount(); i++) { |
176 |
if (lastLayers.getLayer(i).getName().equals(getComboBoxConfigurableLookUp().getEditor().getItem())) {
|
177 |
return lastLayers.getLayer(i);
|
178 |
} |
179 |
} |
180 |
return null; |
181 |
} |
182 |
|
183 |
/*
|
184 |
* (non-Javadoc)
|
185 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
186 |
*/
|
187 |
public void actionPerformed(ActionEvent e) { |
188 |
if (e.getSource() == buttonGroup) {
|
189 |
reloadSubMenu(); |
190 |
return;
|
191 |
} |
192 |
if (e.getSource() == buttonMenu) {
|
193 |
Iterator iterator = null; |
194 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
195 |
ExtensionPoint extensionPoint = (ExtensionPoint) extensionPoints.get("GenericToolBarMenu");
|
196 |
if (extensionPoint == null) |
197 |
return;
|
198 |
iterator = extensionPoint.keySet().iterator(); |
199 |
while (iterator.hasNext()) {
|
200 |
String key = (String) iterator.next(); |
201 |
if (!key.equals(e.getActionCommand()))
|
202 |
continue;
|
203 |
Object object = ((Object) extensionPoint.get(key)); |
204 |
if (object instanceof IGenericToolBarMenuItem) { |
205 |
IGenericToolBarMenuItem item = (IGenericToolBarMenuItem) object; |
206 |
|
207 |
if (getLayerSelected() != null) { |
208 |
item.execute(null, new FLayer[] {getLayerSelected()}); |
209 |
reloadSubMenu(); |
210 |
} |
211 |
return;
|
212 |
} |
213 |
} |
214 |
return;
|
215 |
} |
216 |
} |
217 |
} |