Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / gui / ThemeManagerWindow.java @ 40558

History | View | Annotate | Download (8.73 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.view.legend.gui;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Dimension;
28
import java.awt.FlowLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.util.ArrayList;
32
import java.util.Hashtable;
33

    
34
import javax.swing.JOptionPane;
35
import javax.swing.JPanel;
36
import javax.swing.JTabbedPane;
37
import javax.swing.event.ChangeListener;
38

    
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.messages.NotificationManager;
41
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
42
import org.gvsig.andami.ui.mdiManager.IWindow;
43
import org.gvsig.andami.ui.mdiManager.MDIManagerFactory;
44
import org.gvsig.andami.ui.mdiManager.WindowInfo;
45
import org.gvsig.app.ApplicationLocator;
46
import org.gvsig.app.ApplicationManager;
47
import org.gvsig.fmap.mapcontext.layers.FLayer;
48
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
49
import org.gvsig.gui.beans.swing.JButton;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53

    
54
/**
55
 *
56
 * @author jaume dominguez faus - jaume.dominguez@iver.es
57
 * @version 01-feb-2007
58
 */
59
public final class ThemeManagerWindow extends JPanel implements IWindow, ActionListener {
60
        private static final Logger logger = LoggerFactory.getLogger(ThemeManagerWindow.class);
61
                        
62
        private static final long serialVersionUID = 4650656815369149211L;
63
        private static int selectedTabIndex = 0;
64
        private static ArrayList<Class<? extends AbstractThemeManagerPage>> pages =
65
                        new ArrayList<Class<? extends AbstractThemeManagerPage>>();
66
        private FLayer layer;
67
        //private Legend legend; // Le asignaremos la leyenda del primer tema activo.
68
        private JTabbedPane tabs = new JTabbedPane();  //  @jve:decl-index=0:
69
        private JPanel panelButtons;
70
        private static Hashtable<Class<? extends AbstractThemeManagerPage>, ArrayList<Class<? extends FLyrDefault>>> s =
71
                new Hashtable<Class<? extends AbstractThemeManagerPage>, ArrayList<Class<? extends FLyrDefault>>>();
72

    
73

    
74
        /**
75
         * Sobrecarga del constructor. Este se utiliza cuando se llama a este
76
         * di?logo desde la barra de comandos.
77
         *
78
         */
79
        public ThemeManagerWindow(FLayer l) {
80
                this.layer = l;
81

    
82
                // TODO falta definir leyenda para cualquier tipo de capa
83
                // y decidir entonces qu? opciones soporta cada una para
84
                // que el di?logo se autorrellene
85
                initialize();
86
        }
87

    
88
        private  void initialize() {
89
                StringBuffer msgerr = new StringBuffer(); 
90
                for (int i = 0; i < pages.size(); i++) {
91
                        Class<? extends AbstractThemeManagerPage> pageClass = pages.get(i);
92
                        AbstractThemeManagerPage tab = null;
93
                        try {
94
                                tab = pageClass.newInstance();
95
                        } catch (Exception e) {
96
                                msgerr.append(translate("_Cant_add_property_page_related_to_{0}", pageClass.getName()))
97
                                        .append("\n");
98
                                logger.info("Can't instance propety page from class "+pageClass.getName(),e);
99
                                continue;
100
                        }
101
                        if (tab!=null){
102
                            if (s.get(tab.getClass()).contains(layer.getClass())){
103
                                    String tabName = null;
104
                                    try {
105
                                            tab.setModel(layer);
106
                                            tabName = tab.getName();
107
                                    } catch( Throwable ex) {
108
                                                msgerr.append(translate("_Cant_initialice_property_page_{0}", tabName))
109
                                                        .append("\n");
110
                                            logger.info("Can't set model of property page '"+tabName+"' from class '"+pageClass.getName()+"'.");
111
                                    }
112
                                tabs.addTab(tabName, tab);
113
                            }
114
                        }
115
                }
116

    
117
                if (tabs.getComponentCount()>selectedTabIndex) {
118
                        tabs.setSelectedIndex(selectedTabIndex);
119
                }
120
                tabs.setPreferredSize(new java.awt.Dimension(640,390));
121

    
122

    
123
                setLayout(new BorderLayout());
124
                add(tabs, java.awt.BorderLayout.CENTER);
125

    
126
                // The listener must be added after the tabs are added to the window
127
                tabs.addChangeListener(new ChangeListener() {
128
                        public void stateChanged(javax.swing.event.ChangeEvent e) {
129
                                //remember the visible tab
130
                                selectedTabIndex = tabs.getSelectedIndex();
131
                                if (selectedTabIndex<0) {
132
                                        selectedTabIndex = 0;
133
                                }
134
                        };
135
                });
136
                add(getPanelButtons(), java.awt.BorderLayout.SOUTH);
137
                setSize(new Dimension(780, 540));
138
                if( msgerr.length()>0 ) {
139
                        PluginServices.getMainFrame().messageDialog(msgerr.toString(), "_Warning", JOptionPane.WARNING_MESSAGE);
140
                }
141
        }
142

    
143
        private String translate(String msg, String arg1) {
144
                String[] args = null;
145
                String translation = null;
146
        if (msg == null) {
147
            return "";
148
        }
149
        if( arg1 != null ) {
150
                args = new String[] { arg1 };
151
        }
152
        translation = org.gvsig.i18n.Messages.getText(msg, args);
153
        if (translation == null) {
154
                return "_"+msg.replace("_", " ");
155
        }
156
        return translation;
157
        }
158
        
159
        private JPanel getPanelButtons() {
160
                if (panelButtons == null) {
161
                        panelButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5,5));
162

    
163
                        JButton btnAceptar = new JButton(PluginServices.getText(this,"Aceptar"));
164
                        btnAceptar.setActionCommand("OK");
165
                        btnAceptar.addActionListener(this);
166

    
167
                        JButton btnApply = new JButton(PluginServices.getText(this,"Apply"));
168
                        btnApply.setActionCommand("APPLY");
169
                        btnApply.addActionListener(this);
170

    
171

    
172
                        JButton btnCancelar = new JButton(PluginServices.getText(this,"Cerrar"));
173
                        btnCancelar.setActionCommand("CANCEL");
174
                        btnCancelar.addActionListener(this);
175
                        panelButtons.setPreferredSize(new java.awt.Dimension(493,33));
176
                        panelButtons.add(btnCancelar);
177
                        panelButtons.add(btnApply);
178
                        panelButtons.add(btnAceptar);
179
                }
180
                return panelButtons;
181
        }
182

    
183
        public void actionPerformed(ActionEvent e) {
184
                if (e.getActionCommand() == "OK") {
185
                        /* Causes all the tabs in the ThemeManagerWindow to perform THEIR apply-action
186
                         * then fires the LegendChanged event that causes the view to be refreshed.
187
                         * After that, it closes the window.
188
                         */
189
                        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "APPLY"));
190

    
191
                        close();
192
                } else if (e.getActionCommand() == "CANCEL") {
193
                        /* Causes all the tabs in the ThemeManagerWindow to perform THEIR cancel-action
194
                         * then closes the window.
195
                         */
196
                        for (int i = 0; i < tabs.getTabCount(); i++) {
197
                                AbstractThemeManagerPage tab = (AbstractThemeManagerPage) tabs.getComponentAt(i);
198
                                tab.cancelAction();
199
                        }
200
                        close();
201
                } else if (e.getActionCommand() == "APPLY") {
202
                        /* Causes the current visible tab in the ThemeManagerWindow to perform
203
                         * ITS specific apply-action.
204
                         */
205
                        for (int i = 0; i < tabs.getTabCount(); i++) {
206
                                AbstractThemeManagerPage tab = (AbstractThemeManagerPage) tabs.getComponentAt(i);
207
                                tab.applyAction();
208
                        }
209
                        layer.getMapContext().callLegendChanged();
210
                } else {}
211
//                 Lots of temporary objects were create.
212
                // let's try some memory cleanup
213
                System.gc();
214
        }
215

    
216
        private void close() {
217
                PluginServices.getMDIManager().closeWindow(ThemeManagerWindow.this);
218
        }
219

    
220
        public WindowInfo getWindowInfo() {
221
                WindowInfo viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG|WindowInfo.RESIZABLE);
222
                viewInfo.setWidth(getWidth());
223
                viewInfo.setHeight(getHeight());
224
                viewInfo.setTitle(PluginServices.getText(this,"propiedades_de_la_capa"));
225
                return viewInfo;
226
        }
227

    
228
        public static void addPage(Class<? extends AbstractThemeManagerPage> abstractThemeManagerPageClass) {
229
                pages.add(abstractThemeManagerPageClass);
230
        }
231

    
232
        public static void setTabEnabledForLayer(Class<? extends AbstractThemeManagerPage> abstractThemeManagerPageClass, Class<? extends FLyrDefault> fLayerClazz, boolean enabled) {
233
                ArrayList<Class<? extends FLyrDefault>> enabledLayers;
234
                if (enabled == true) {
235
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
236
                                enabledLayers = new ArrayList<Class<? extends FLyrDefault>> ();
237
                                enabledLayers.add(fLayerClazz);
238
                                s.put(abstractThemeManagerPageClass, enabledLayers);
239
                        } else {
240
                                enabledLayers = s.get(abstractThemeManagerPageClass);
241
                                enabledLayers.add(fLayerClazz);
242
                        }
243
                } else {
244
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
245
                                return;
246
                        }
247
                                enabledLayers = s.get(abstractThemeManagerPageClass);
248
                        enabledLayers.remove(fLayerClazz);
249
                }
250
        };
251
        public Object getWindowProfile() {
252
                return WindowInfo.TOOL_PROFILE;
253
        };
254
}  //  @jve:decl-index=0:visual-constraint="10,10"