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 / ThemeManagerWindowNew.java @ 43556

History | View | Annotate | Download (10.1 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
import javax.swing.JComponent;
34

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

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

    
50

    
51
public final class ThemeManagerWindowNew extends JPanel implements IWindow, ActionListener {
52
        private static final Logger logger = LoggerFactory.getLogger(ThemeManagerWindow.class);
53
                        
54
        private static final long serialVersionUID = 4650656815369149211L;
55
        private static int selectedTabIndex = 0;
56
        private static ArrayList<Class<? extends AbstractThemeManagerPage>> pages =
57
                        new ArrayList<Class<? extends AbstractThemeManagerPage>>();
58
        private FLayer layer;
59
        //private Legend legend; // Le asignaremos la leyenda del primer tema activo.
60
        private JTabbedPane tabs = new JTabbedPane();  //  @jve:decl-index=0:
61
        private JPanel panelButtons;
62
        private static Hashtable<Class<? extends AbstractThemeManagerPage>, ArrayList<Class<? extends FLayer>>> s =
63
                new Hashtable<Class<? extends AbstractThemeManagerPage>, ArrayList<Class<? extends FLayer>>>();
64

    
65

    
66
        /**
67
         * Sobrecarga del constructor. Este se utiliza cuando se llama a este
68
         * di?logo desde la barra de comandos.
69
         *
70
         */
71
        public ThemeManagerWindowNew(FLayer l) {
72
                this.layer = l;
73

    
74
                // TODO falta definir leyenda para cualquier tipo de capa
75
                // y decidir entonces qu? opciones soporta cada una para
76
                // que el di?logo se autorrellene
77
                initialize();
78
        }
79

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

    
109
                if (tabs.getComponentCount()>selectedTabIndex) {
110
                        tabs.setSelectedIndex(selectedTabIndex);
111
                }
112
                tabs.setPreferredSize(new java.awt.Dimension(640,390));
113

    
114

    
115
                setLayout(new BorderLayout());
116
                add(tabs, java.awt.BorderLayout.CENTER);
117

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

    
135
        private String translate(String msg, String arg1) {
136
                String[] args = null;
137
                String translation = null;
138
        if (msg == null) {
139
            return "";
140
        }
141
        if( arg1 != null ) {
142
                args = new String[] { arg1 };
143
        }
144
        translation = org.gvsig.i18n.Messages.getText(msg, args);
145
        if (translation == null) {
146
                return "_"+msg.replace("_", " ");
147
        }
148
        return translation;
149
        }
150
        
151
        private JPanel getPanelButtons() {
152
                if (panelButtons == null) {
153
                        panelButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5,5));
154

    
155
                        JButton btnAceptar = new JButton(PluginServices.getText(this,"Aceptar"));
156
                        btnAceptar.setActionCommand("OK");
157
                        btnAceptar.addActionListener(this);
158

    
159
                        JButton btnApply = new JButton(PluginServices.getText(this,"Apply"));
160
                        btnApply.setActionCommand("APPLY");
161
                        btnApply.addActionListener(this);
162

    
163

    
164
                        JButton btnCancelar = new JButton(PluginServices.getText(this,"Cerrar"));
165
                        btnCancelar.setActionCommand("CANCEL");
166
                        btnCancelar.addActionListener(this);
167
                        panelButtons.setPreferredSize(new java.awt.Dimension(493,33));
168
                        panelButtons.add(btnCancelar);
169
                        panelButtons.add(btnApply);
170
                        panelButtons.add(btnAceptar);
171
                }
172
                return panelButtons;
173
        }
174

    
175
        public void actionPerformed(ActionEvent e) {
176
                if (e.getActionCommand() == "OK") {
177
                        /* Causes all the tabs in the ThemeManagerWindow to perform THEIR apply-action
178
                         * then fires the LegendChanged event that causes the view to be refreshed.
179
                         * After that, it closes the window.
180
                         */
181
                        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "APPLY"));
182

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

    
208
        private void close() {
209
                PluginServices.getMDIManager().closeWindow(this);
210
        }
211

    
212
        public WindowInfo getWindowInfo() {
213
                WindowInfo viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG|WindowInfo.RESIZABLE);
214
                viewInfo.setWidth(getWidth());
215
                viewInfo.setHeight(getHeight());
216
                viewInfo.setTitle(PluginServices.getText(this,"propiedades_de_la_capa"));
217
                return viewInfo;
218
        }
219

    
220
//        private static class AbstractThemeManagerPageAdapter implements PropertiesPage, PropertiesPageFactory {
221
//
222
//            AbstractThemeManagerPage x;
223
//            
224
//            AbstractThemeManagerPageAdapter(AbstractThemeManagerPage x) {
225
//                this.x = x;
226
//            }
227
//            
228
//            public String getTitle() {
229
//                return this.x.getName();
230
//            }
231
//
232
//            public int getPriority() {
233
//                return 100;
234
//            }
235
//
236
//            public boolean whenAccept() {
237
//                this.x.acceptAction();
238
//                return true;
239
//            }
240
//
241
//            public boolean whenApply() {
242
//                this.x.applyAction();
243
//                return true;
244
//            }
245
//
246
//            public boolean whenCancel() {
247
//                this.x.cancelAction();
248
//                return true;
249
//            }
250
//
251
//            public JComponent asJComponent() {
252
//                return this.x;
253
//            }
254
//
255
//            public String getGroupID() {
256
//                return "Layer";
257
//            }
258
//
259
//            public boolean isVisible(Object obj) {
260
//                return this.x.isTabEnabledForLayer((FLayer)obj);
261
//            }
262
//
263
//            public PropertiesPage create(Object obj) {
264
//                this.x.setModel((FLayer)obj);
265
//                return this;
266
//            }
267
//
268
//        }
269
        
270
        public static void addPage(Class<? extends AbstractThemeManagerPage> themeManagerPageClass) {
271
            
272
                pages.add(themeManagerPageClass);
273
        }
274

    
275
        public static void setTabEnabledForLayer(Class<? extends AbstractThemeManagerPage> abstractThemeManagerPageClass, Class<? extends FLayer> fLayerClazz, boolean enabled) {
276
                ArrayList<Class<? extends FLayer>> enabledLayers;
277
                if (enabled == true) {
278
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
279
                                enabledLayers = new ArrayList<Class<? extends FLayer>> ();
280
                                enabledLayers.add(fLayerClazz);
281
                                s.put(abstractThemeManagerPageClass, enabledLayers);
282
                        } else {
283
                                enabledLayers = s.get(abstractThemeManagerPageClass);
284
                                enabledLayers.add(fLayerClazz);
285
                        }
286
                } else {
287
                        if (!s.containsKey(abstractThemeManagerPageClass)) {
288
                                return;
289
                        }
290
                                enabledLayers = s.get(abstractThemeManagerPageClass);
291
                        enabledLayers.remove(fLayerClazz);
292
                }
293
        };
294

    
295
        public static boolean isTabEnabledForLayer(AbstractThemeManagerPage page, FLayer layer) {
296
            try {
297
                return s.get(page.getClass()).contains(layer.getClass());            
298
            } catch( Exception ex) {
299
                return false;
300
            }
301
        }
302
        
303
        public Object getWindowProfile() {
304
                return WindowInfo.TOOL_PROFILE;
305
        };
306
}