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 @ 41575

History | View | Annotate | Download (11 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.Collections;
33
import java.util.Comparator;
34
import java.util.Date;
35
import java.util.Hashtable;
36
import java.util.List;
37

    
38
import javax.swing.JOptionPane;
39
import javax.swing.JPanel;
40
import javax.swing.JTabbedPane;
41
import javax.swing.event.ChangeListener;
42

    
43
import org.gvsig.andami.PluginServices;
44
import org.gvsig.andami.ui.mdiManager.IWindow;
45
import org.gvsig.andami.ui.mdiManager.WindowInfo;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.gui.beans.swing.JButton;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51

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

    
71

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

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

    
86
        private  void initialize() {
87
                
88
                StringBuffer msgerr = new StringBuffer(); 
89
                List<AbstractThemeManagerPage> activePages = this.getActivePages(layer, msgerr);
90

    
91
                for (int i = 0; i < activePages.size(); i++) {
92
                    AbstractThemeManagerPage tab = activePages.get(i);
93
                    String tabName = null;
94
                    try {
95
                            tab.setModel(layer);
96
                            tabName = tab.getName();
97
                    } catch( Throwable ex) {
98
                            msgerr.append(translate("_Cant_initialice_property_page_{0}", tabName))
99
                                    .append("\n");
100
                            logger.warn("Can't set model of property page '"+tabName+"' from class '"+tab.getClass().getName()+"'.", ex);
101
                    }
102
                    tabs.addTab(tabName, tab);
103
                }
104

    
105
                if (tabs.getComponentCount()>selectedTabIndex) {
106
                        tabs.setSelectedIndex(selectedTabIndex);
107
                }
108
                tabs.setPreferredSize(new java.awt.Dimension(640,390));
109

    
110

    
111
                setLayout(new BorderLayout());
112
                add(tabs, java.awt.BorderLayout.CENTER);
113

    
114
                // The listener must be added after the tabs are added to the window
115
                tabs.addChangeListener(new ChangeListener() {
116
                        public void stateChanged(javax.swing.event.ChangeEvent e) {
117
                                //remember the visible tab
118
                                selectedTabIndex = tabs.getSelectedIndex();
119
                                if (selectedTabIndex<0) {
120
                                        selectedTabIndex = 0;
121
                                }
122
                        };
123
                });
124
                add(getPanelButtons(), java.awt.BorderLayout.SOUTH);
125
                setSize(new Dimension(780, 540));
126
                if( msgerr.length()>0 ) {
127
                        PluginServices.getMainFrame().messageDialog(msgerr.toString(), "_Warning", JOptionPane.WARNING_MESSAGE);
128
                }
129
        }
130

    
131
        private List<AbstractThemeManagerPage> getActivePages(FLayer layer, StringBuffer msgerr) {
132
            List<AbstractThemeManagerPage> activePages = new ArrayList();
133
            for (int i = 0; i < pages.size(); i++) {
134
                Date t1 = new Date();
135
                String pageName = "(unknow)";
136
                Class<? extends AbstractThemeManagerPage> pageClass = this.pages.get(i);
137
                AbstractThemeManagerPage page = null;
138
                try {
139
                        page = pageClass.newInstance();
140
                        if( page.isTabEnabledForLayer(layer) ) {
141
                            activePages.add(page);
142
                        }
143
                        pageName = page.getName();
144
                } catch (Exception e) {
145
                    if( msgerr!=null ) {
146
                        msgerr.append(translate("_Cant_add_property_page_related_to_{0}", pageClass.getName()))
147
                                .append("\n");
148
                    }
149
                    logger.warn("Can't initialize propety page from class "+pageClass.getName(),e);
150
                }
151
                Date t2 = new Date();
152
                logger.info("Page "+i+", "+pageName+" loaded in "+ (t2.getTime()-t1.getTime())+" seconds.");
153
            }
154
            Collections.sort(activePages, new Comparator<AbstractThemeManagerPage>() {
155
                public int compare(AbstractThemeManagerPage o1, AbstractThemeManagerPage o2) {
156
                    try {
157
                        // int n = Integer.compare(o1.getPriority(), o2.getPriority());
158
                        //
159
                        // Integer.compare not is compatible with ava 1.5 use next line for
160
                        // compatibility
161
                        int n = Integer.valueOf(o2.getPriority()).compareTo(Integer.valueOf(o1.getPriority()));
162
                        
163
                        if( n!=0 ) {
164
                            return n;
165
                        }
166
                        return o1.getName().compareToIgnoreCase(o2.getName());
167
                    } catch(Exception ex) {
168
                        logger.warn("Problems ordering properies page, assume pages have same order.",ex);
169
                        return 0;
170
                    }
171
                }
172
            });
173
            return activePages;    
174
        }
175
        
176
        private String translate(String msg, String arg1) {
177
                String[] args = null;
178
                String translation = null;
179
        if (msg == null) {
180
            return "";
181
        }
182
        if( arg1 != null ) {
183
                args = new String[] { arg1 };
184
        }
185
        translation = org.gvsig.i18n.Messages.getText(msg, args);
186
        if (translation == null) {
187
                return "_"+msg.replace("_", " ");
188
        }
189
        return translation;
190
        }
191
        
192
        private JPanel getPanelButtons() {
193
                if (panelButtons == null) {
194
                        panelButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5,5));
195

    
196
                        JButton btnAceptar = new JButton(PluginServices.getText(this,"Aceptar"));
197
                        btnAceptar.setActionCommand("OK");
198
                        btnAceptar.addActionListener(this);
199

    
200
                        JButton btnApply = new JButton(PluginServices.getText(this,"Apply"));
201
                        btnApply.setActionCommand("APPLY");
202
                        btnApply.addActionListener(this);
203

    
204

    
205
                        JButton btnCancelar = new JButton(PluginServices.getText(this,"Cerrar"));
206
                        btnCancelar.setActionCommand("CANCEL");
207
                        btnCancelar.addActionListener(this);
208
                        panelButtons.setPreferredSize(new java.awt.Dimension(493,33));
209
                        panelButtons.add(btnCancelar);
210
                        panelButtons.add(btnApply);
211
                        panelButtons.add(btnAceptar);
212
                }
213
                return panelButtons;
214
        }
215

    
216
        public void actionPerformed(ActionEvent e) {
217
                if (e.getActionCommand() == "OK") {
218
                        /* Causes all the tabs in the ThemeManagerWindow to perform THEIR apply-action
219
                         * then fires the LegendChanged event that causes the view to be refreshed.
220
                         * After that, it closes the window.
221
                         */
222
                        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "APPLY"));
223

    
224
                        close();
225
                } else if (e.getActionCommand() == "CANCEL") {
226
                        /* Causes all the tabs in the ThemeManagerWindow to perform THEIR cancel-action
227
                         * then closes the window.
228
                         */
229
                        for (int i = 0; i < tabs.getTabCount(); i++) {
230
                                AbstractThemeManagerPage tab = (AbstractThemeManagerPage) tabs.getComponentAt(i);
231
                                tab.cancelAction();
232
                        }
233
                        close();
234
                } else if (e.getActionCommand() == "APPLY") {
235
                        /* Causes the current visible tab in the ThemeManagerWindow to perform
236
                         * ITS specific apply-action.
237
                         */
238
                        for (int i = 0; i < tabs.getTabCount(); i++) {
239
                                AbstractThemeManagerPage tab = (AbstractThemeManagerPage) tabs.getComponentAt(i);
240
                                tab.applyAction();
241
                        }
242
                        layer.getMapContext().callLegendChanged();
243
                } else {}
244
//                 Lots of temporary objects were create.
245
                // let's try some memory cleanup
246
                System.gc();
247
        }
248

    
249
        private void close() {
250
                PluginServices.getMDIManager().closeWindow(ThemeManagerWindow.this);
251
        }
252

    
253
        public WindowInfo getWindowInfo() {
254
                WindowInfo viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG|WindowInfo.RESIZABLE);
255
                viewInfo.setWidth(getWidth());
256
                viewInfo.setHeight(getHeight());
257
                viewInfo.setTitle(PluginServices.getText(this,"propiedades_de_la_capa"));
258
                return viewInfo;
259
        }
260

    
261
        public static void addPage(Class<? extends AbstractThemeManagerPage> abstractThemeManagerPageClass) {
262
                pages.add(abstractThemeManagerPageClass);
263
        }
264

    
265
        public static void setTabEnabledForLayer(Class<? extends AbstractThemeManagerPage> abstractThemeManagerPageClass, Class<? extends FLayer> fLayerClazz, boolean enabled) {
266
                ArrayList<Class<? extends FLayer>> enabledLayers;
267
                if (enabled == true) {
268
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
269
                                enabledLayers = new ArrayList<Class<? extends FLayer>> ();
270
                                enabledLayers.add(fLayerClazz);
271
                                s.put(abstractThemeManagerPageClass, enabledLayers);
272
                        } else {
273
                                enabledLayers = s.get(abstractThemeManagerPageClass);
274
                                enabledLayers.add(fLayerClazz);
275
                        }
276
                } else {
277
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
278
                                return;
279
                        }
280
                                enabledLayers = s.get(abstractThemeManagerPageClass);
281
                        enabledLayers.remove(fLayerClazz);
282
                }
283
        };
284

    
285
        public static boolean isTabEnabledForLayer(AbstractThemeManagerPage page, FLayer layer) {
286
            try {
287
                return s.get(page.getClass()).contains(layer.getClass());            
288
            } catch( Exception ex) {
289
                return false;
290
            }
291
        }
292
        
293
        public Object getWindowProfile() {
294
                return WindowInfo.TOOL_PROFILE;
295
        };
296
}  //  @jve:decl-index=0:visual-constraint="10,10"