Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / ThemeManagerWindow.java @ 38608

History | View | Annotate | Download (8.99 KB)

1 10679 jaume
/*
2
 *
3
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
4
 *
5
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
20
 *
21
 * For more information, contact:
22
 *
23
 *  Generalitat Valenciana
24
 *   Conselleria d'Infraestructures i Transport
25
 *   Av. Blasco Ib??ez, 50
26
 *   46010 VALENCIA
27
 *   SPAIN
28
 *
29
 *      +34 963862235
30
 *   gvsig@gva.es
31
 *      www.gvsig.gva.es
32
 *
33
 *    or
34
 *
35
 *   IVER T.I. S.A
36
 *   Salamanca 50
37
 *   46005 Valencia
38
 *   Spain
39
 *
40
 *   +34 963163400
41
 *   dac@iver.es
42
 */
43 29598 jpiera
package org.gvsig.app.project.documents.view.legend.gui;
44 10679 jaume
45
import java.awt.BorderLayout;
46 11734 jaume
import java.awt.Dimension;
47 10679 jaume
import java.awt.FlowLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.util.ArrayList;
51 16691 vcaballero
import java.util.Hashtable;
52 10679 jaume
53 38608 jjdelcerro
import javax.swing.JOptionPane;
54 10679 jaume
import javax.swing.JPanel;
55
import javax.swing.JTabbedPane;
56
import javax.swing.event.ChangeListener;
57
58 29598 jpiera
import org.gvsig.andami.PluginServices;
59
import org.gvsig.andami.messages.NotificationManager;
60 38608 jjdelcerro
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
61 29598 jpiera
import org.gvsig.andami.ui.mdiManager.IWindow;
62 38608 jjdelcerro
import org.gvsig.andami.ui.mdiManager.MDIManagerFactory;
63 29598 jpiera
import org.gvsig.andami.ui.mdiManager.WindowInfo;
64 38608 jjdelcerro
import org.gvsig.app.ApplicationLocator;
65
import org.gvsig.app.ApplicationManager;
66 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.FLayer;
67
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
68 10679 jaume
import org.gvsig.gui.beans.swing.JButton;
69 38608 jjdelcerro
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71 10679 jaume
72
73
/**
74 10844 jaume
 *
75 10679 jaume
 * @author jaume dominguez faus - jaume.dominguez@iver.es
76
 * @version 01-feb-2007
77
 */
78
public final class ThemeManagerWindow extends JPanel implements IWindow, ActionListener {
79 38608 jjdelcerro
        private static final Logger logger = LoggerFactory.getLogger(ThemeManagerWindow.class);
80
81 20779 vcaballero
        private static final long serialVersionUID = 4650656815369149211L;
82 10679 jaume
        private static int selectedTabIndex = 0;
83 20779 vcaballero
        private static ArrayList<Class<? extends AbstractThemeManagerPage>> pages =
84
                        new ArrayList<Class<? extends AbstractThemeManagerPage>>();
85 10679 jaume
        private FLayer layer;
86
        //private Legend legend; // Le asignaremos la leyenda del primer tema activo.
87
        private JTabbedPane tabs = new JTabbedPane();  //  @jve:decl-index=0:
88
        private JPanel panelButtons;
89 20779 vcaballero
        private static Hashtable<Class<? extends AbstractThemeManagerPage>, ArrayList<Class<? extends FLyrDefault>>> s =
90
                new Hashtable<Class<? extends AbstractThemeManagerPage>, ArrayList<Class<? extends FLyrDefault>>>();
91 10679 jaume
92
93
        /**
94
         * Sobrecarga del constructor. Este se utiliza cuando se llama a este
95
         * di?logo desde la barra de comandos.
96
         *
97
         */
98
        public ThemeManagerWindow(FLayer l) {
99
                this.layer = l;
100
101
                // TODO falta definir leyenda para cualquier tipo de capa
102
                // y decidir entonces qu? opciones soporta cada una para
103
                // que el di?logo se autorrellene
104
                initialize();
105
        }
106 10844 jaume
107 10679 jaume
        private  void initialize() {
108 38608 jjdelcerro
                StringBuffer msgerr = new StringBuffer();
109 10679 jaume
                for (int i = 0; i < pages.size(); i++) {
110 20779 vcaballero
                        Class<? extends AbstractThemeManagerPage> pageClass = pages.get(i);
111 34114 fdiaz
                        AbstractThemeManagerPage tab = null;
112 10679 jaume
                        try {
113 26449 jmvivo
                                tab = pageClass.newInstance();
114 38608 jjdelcerro
                        } catch (Exception e) {
115
                                msgerr.append(translate("_Cant_add_property_page_related_to_{0}", pageClass.getName()))
116
                                        .append("\n");
117
                                logger.info("Can't instance propety page from class "+pageClass.getName(),e);
118
                                continue;
119 10679 jaume
                        }
120 34114 fdiaz
                        if (tab!=null){
121
                            if (s.get(tab.getClass()).contains(layer.getClass())){
122 38608 jjdelcerro
                                    String tabName = null;
123
                                    try {
124
                                            tab.setModel(layer);
125
                                            tabName = tab.getName();
126
                                    } catch( Throwable ex) {
127
                                                msgerr.append(translate("_Cant_initialice_property_page_{0}", tabName))
128
                                                        .append("\n");
129
                                            logger.info("Can't set model of property page '"+tabName+"' from class '"+pageClass.getName()+"'.");
130
                                    }
131
                                tabs.addTab(tabName, tab);
132 34114 fdiaz
                            }
133
                        }
134 10679 jaume
                }
135
136 13513 caballero
                if (tabs.getComponentCount()>selectedTabIndex) {
137
                        tabs.setSelectedIndex(selectedTabIndex);
138
                }
139 10679 jaume
                tabs.setPreferredSize(new java.awt.Dimension(640,390));
140
141
142
                setLayout(new BorderLayout());
143
                add(tabs, java.awt.BorderLayout.CENTER);
144
145
                // The listener must be added after the tabs are added to the window
146
                tabs.addChangeListener(new ChangeListener() {
147
                        public void stateChanged(javax.swing.event.ChangeEvent e) {
148
                                //remember the visible tab
149
                                selectedTabIndex = tabs.getSelectedIndex();
150 26449 jmvivo
                                if (selectedTabIndex<0) {
151 10679 jaume
                                        selectedTabIndex = 0;
152 26449 jmvivo
                                }
153 10679 jaume
                        };
154
                });
155
                add(getPanelButtons(), java.awt.BorderLayout.SOUTH);
156 22752 vcaballero
                setSize(new Dimension(780, 540));
157 38608 jjdelcerro
                if( msgerr.length()>0 ) {
158
                        PluginServices.getMainFrame().messageDialog(msgerr.toString(), "_Warning", JOptionPane.WARNING_MESSAGE);
159
                }
160 10679 jaume
        }
161
162 38608 jjdelcerro
        private String translate(String msg, String arg1) {
163
                String[] args = null;
164
                String translation = null;
165
        if (msg == null) {
166
            return "";
167
        }
168
        if( arg1 != null ) {
169
                args = new String[] { arg1 };
170
        }
171
        translation = org.gvsig.i18n.Messages.getText(msg, args);
172
        if (translation == null) {
173
                return "_"+msg.replace("_", " ");
174
        }
175
        return translation;
176
        }
177
178 10679 jaume
        private JPanel getPanelButtons() {
179
                if (panelButtons == null) {
180
                        panelButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5,5));
181
182
                        JButton btnAceptar = new JButton(PluginServices.getText(this,"Aceptar"));
183
                        btnAceptar.setActionCommand("OK");
184
                        btnAceptar.addActionListener(this);
185
186 13558 evercher
                        JButton btnApply = new JButton(PluginServices.getText(this,"Apply"));
187 10679 jaume
                        btnApply.setActionCommand("APPLY");
188
                        btnApply.addActionListener(this);
189
190
191
                        JButton btnCancelar = new JButton(PluginServices.getText(this,"Cerrar"));
192
                        btnCancelar.setActionCommand("CANCEL");
193
                        btnCancelar.addActionListener(this);
194
                        panelButtons.setPreferredSize(new java.awt.Dimension(493,33));
195
                        panelButtons.add(btnCancelar);
196
                        panelButtons.add(btnApply);
197
                        panelButtons.add(btnAceptar);
198
                }
199
                return panelButtons;
200
        }
201
202
        public void actionPerformed(ActionEvent e) {
203
                if (e.getActionCommand() == "OK") {
204
                        /* Causes all the tabs in the ThemeManagerWindow to perform THEIR apply-action
205
                         * then fires the LegendChanged event that causes the view to be refreshed.
206
                         * After that, it closes the window.
207
                         */
208 22752 vcaballero
                        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "APPLY"));
209 10679 jaume
210 14522 jdominguez
                        close();
211 10679 jaume
                } else if (e.getActionCommand() == "CANCEL") {
212
                        /* Causes all the tabs in the ThemeManagerWindow to perform THEIR cancel-action
213
                         * then closes the window.
214
                         */
215
                        for (int i = 0; i < tabs.getTabCount(); i++) {
216
                                AbstractThemeManagerPage tab = (AbstractThemeManagerPage) tabs.getComponentAt(i);
217
                                tab.cancelAction();
218
                        }
219 14522 jdominguez
                        close();
220 10679 jaume
                } else if (e.getActionCommand() == "APPLY") {
221
                        /* Causes the current visible tab in the ThemeManagerWindow to perform
222
                         * ITS specific apply-action.
223
                         */
224 22752 vcaballero
                        for (int i = 0; i < tabs.getTabCount(); i++) {
225
                                AbstractThemeManagerPage tab = (AbstractThemeManagerPage) tabs.getComponentAt(i);
226
                                tab.applyAction();
227
                        }
228 10679 jaume
                        layer.getMapContext().callLegendChanged();
229
                } else {}
230 22752 vcaballero
//                 Lots of temporary objects were create.
231
                // let's try some memory cleanup
232
                System.gc();
233 10679 jaume
        }
234 22752 vcaballero
235 14522 jdominguez
        private void close() {
236
                PluginServices.getMDIManager().closeWindow(ThemeManagerWindow.this);
237
        }
238 10679 jaume
239
        public WindowInfo getWindowInfo() {
240
                WindowInfo viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG|WindowInfo.RESIZABLE);
241
                viewInfo.setWidth(getWidth());
242
                viewInfo.setHeight(getHeight());
243
                viewInfo.setTitle(PluginServices.getText(this,"propiedades_de_la_capa"));
244
                return viewInfo;
245
        }
246
247 20779 vcaballero
        public static void addPage(Class<? extends AbstractThemeManagerPage> abstractThemeManagerPageClass) {
248 10679 jaume
                pages.add(abstractThemeManagerPageClass);
249
        }
250 22752 vcaballero
251 20779 vcaballero
        public static void setTabEnabledForLayer(Class<? extends AbstractThemeManagerPage> abstractThemeManagerPageClass, Class<? extends FLyrDefault> fLayerClazz, boolean enabled) {
252
                ArrayList<Class<? extends FLyrDefault>> enabledLayers;
253 18623 jdominguez
                if (enabled == true) {
254
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
255 20779 vcaballero
                                enabledLayers = new ArrayList<Class<? extends FLyrDefault>> ();
256 18623 jdominguez
                                enabledLayers.add(fLayerClazz);
257
                                s.put(abstractThemeManagerPageClass, enabledLayers);
258 16691 vcaballero
                        } else {
259 20779 vcaballero
                                enabledLayers = s.get(abstractThemeManagerPageClass);
260 18623 jdominguez
                                enabledLayers.add(fLayerClazz);
261 16691 vcaballero
                        }
262 18623 jdominguez
                } else {
263 26449 jmvivo
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
264
                                return;
265
                        }
266 20779 vcaballero
                                enabledLayers = s.get(abstractThemeManagerPageClass);
267 18623 jdominguez
                        enabledLayers.remove(fLayerClazz);
268
                }
269 16691 vcaballero
        };
270 26449 jmvivo
        public Object getWindowProfile() {
271
                return WindowInfo.TOOL_PROFILE;
272
        };
273 10679 jaume
}  //  @jve:decl-index=0:visual-constraint="10,10"