Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / panelGroup / tabbedPanel / TabbedPanel.java @ 40561

History | View | Annotate | Download (9.62 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.gui.beans.panelGroup.tabbedPanel;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Component;
28
import java.awt.event.ContainerEvent;
29
import java.awt.event.ContainerListener;
30
import java.io.Serializable;
31
import java.util.Vector;
32

    
33
import javax.swing.JPanel;
34
import javax.swing.JTabbedPane;
35
import javax.swing.event.ChangeEvent;
36
import javax.swing.event.ChangeListener;
37

    
38
import org.gvsig.gui.beans.panelGroup.AbstractPanelGroup;
39
import org.gvsig.gui.beans.panelGroup.PanelGroupManager;
40
import org.gvsig.gui.beans.panelGroup.panels.AbstractPanel;
41
import org.gvsig.gui.beans.panelGroup.panels.IPanel;
42

    
43

    
44
/**
45
 * <p>Graphical interface that's a {@link JPanel JPanel} with an inner {@link JTabbedPane JTabbedPane} that
46
 * contains the {@link IPanel IPanel}'s of this group, and supports work with them.</p>
47
 * 
48
 * @see AbstractPanelGroup
49
 * 
50
 * @version 15/10/2007
51
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) 
52
 */
53
public class TabbedPanel extends AbstractPanelGroup implements Serializable {
54
        private static final long serialVersionUID = -7979646227156641693L;
55

    
56
        /**
57
         * <p>Graphical interface where the panels will be drawn.</p>
58
         * 
59
         * @see #getJTabbedPane()
60
         */
61
        private JTabbedPane jTabbedPane;
62

    
63
        static {
64
                // Registers this class as a "PanelGroup" type
65
                PanelGroupManager.getManager().registerPanelGroup(TabbedPanel.class);
66
                
67
                // Sets this class as the default "PanelGroup" type
68
                PanelGroupManager.getManager().setDefaultType(TabbedPanel.class);
69
        }
70

    
71
        /**
72
         * <p>Default constructor.</p>
73
         * 
74
         * @param reference object that is ''semantically' or 'contextually' related to the group of panels
75
         */
76
        public TabbedPanel(Object reference) {
77
                super(reference);
78
        }
79

    
80
        /*
81
         * (non-Javadoc)
82
         * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#initialize()
83
         */
84
        protected void initialize() {
85
                super.initialize();
86
                this.setLayout(new BorderLayout());
87
                this.add(getJTabbedPane(), BorderLayout.CENTER);
88
        }
89

    
90
        /**
91
         * This method initializes jTabbedPane
92
         *
93
         * @return JTabbedPane
94
         */
95
        protected JTabbedPane getJTabbedPane() {
96
                if (jTabbedPane == null) {
97
                        jTabbedPane = new JTabbedPane();
98
                        jTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
99

    
100
                        jTabbedPane.addContainerListener(new ContainerListener() {
101
                                /*
102
                                 * (non-Javadoc)
103
                                 * @see java.awt.event.ContainerListener#componentAdded(java.awt.event.ContainerEvent)
104
                                 */
105
                                public void componentAdded(ContainerEvent e) {
106
                                        // Propagates the event -> this allows notify that a component has been added into this TabbedPanel
107
                                        dispatchEvent(e);
108
                                }
109

    
110
                                /*
111
                                 * (non-Javadoc)
112
                                 * @see java.awt.event.ContainerListener#componentRemoved(java.awt.event.ContainerEvent)
113
                                 */
114
                                public void componentRemoved(ContainerEvent e) {
115
                                        // Propagates the event -> this allows notify that a component has been removed from this TabbedPanel
116
                                        dispatchEvent(e);
117
                                }
118
                        });
119

    
120
                        jTabbedPane.addChangeListener(new ChangeListener() {
121
                                /*
122
                                 * (non-Javadoc)
123
                                 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
124
                                 */
125
                                public void stateChanged(ChangeEvent e) {
126
                                        Object source = e.getSource();
127
                                        
128
                                        if (!((source != null) && (source instanceof JTabbedPane)))
129
                                                return;
130

    
131
                                        Object parent = ((JTabbedPane)source).getParent();
132
                                        
133
                                        if (!(parent instanceof TabbedPanel))
134
                                                return;
135
                                        
136
                                        Object object = ((JTabbedPane)source).getSelectedComponent();
137

    
138
                                        if ((object != null) && (object instanceof AbstractPanel)) {
139
                                        // Notifies the new panel selected
140
                                                ((TabbedPanel)parent).stateChanged(new ChangeEvent(object));
141
                                        }
142
                                }
143
                        });
144
                }
145

    
146
                return jTabbedPane;
147
        }
148

    
149
        /**
150
         * @see JTabbedPane#getSelectedIndex()
151
         * @see #setSelectedIndex(int)
152
         */
153
        public int getSelectedIndex() {
154
                return getJTabbedPane().getSelectedIndex();
155
        }
156

    
157
        /**
158
         * @see JTabbedPane#setSelectedIndex(int)
159
         * @see #getSelectedIndex()
160
         */
161
        public void setSelectedIndex(int position) {
162
                getJTabbedPane().setSelectedIndex(position);
163
        }
164

    
165
//        /*
166
//         * (non-Javadoc)
167
//         * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#addPanel(org.gvsig.gui.beans.panelGroup.panels.IPanel)
168
//         */
169
//        public void addPanel(IPanel panel) throws EmptyPanelGroupException, EmptyPanelGroupGUIException, PanelWithNoPreferredSizeDefinedException {
170
//                if ((belongsThisGroup(panel)) && (panel.getLabel() != null)) {
171
//                        super.loadPanel(panel);
172
//                        
173
//                        if (((AbstractPanel)panel).isVisible())
174
//                                getJTabbedPane().add(panel.getLabel(), (JPanel)panel);
175
//                }
176
//        }
177

    
178

    
179
        /*
180
         * (non-Javadoc)
181
         * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#loadPanel(org.gvsig.gui.beans.panelGroup.panels.IPanel)
182
         */
183
        protected void loadPanel(IPanel panel) {
184
                super.loadPanel(panel);
185
                
186
                if (((AbstractPanel)panel).isVisible()) {
187
                        getJTabbedPane().add(panel.getLabel(), (JPanel)panel);
188
                }
189
        }
190

    
191
        /*
192
         * (non-Javadoc)
193
         * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#unLoadPanel(org.gvsig.gui.beans.panelGroup.panels.IPanel)
194
         */
195
        protected void unLoadPanel(IPanel panel) {
196
                super.unLoadPanel(panel);
197

    
198
                getJTabbedPane().remove((Component) panel);
199
        }
200
        
201
//        /*
202
//         * (non-Javadoc)
203
//         * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#removePanel(org.gvsig.gui.beans.panelGroup.panels.AbstractPanel)
204
//         */
205
//        public void removePanel(IPanel panel) {
206
//                
207
//                if ((belongsThisGroup(panel)) && (panel.getLabel() != null)) {
208
//                        super.removePanel(panel);
209
//                        getJTabbedPane().remove((Component) panel);
210
//                }
211
//        }
212

    
213
        /*
214
         * (non-Javadoc)
215
         * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#getActivePanel()
216
         */
217
        public IPanel getActivePanel() {
218
                if (getJTabbedPane().getSelectedComponent() instanceof IPanel)
219
                        return (IPanel)getJTabbedPane().getSelectedComponent();                 
220
                else
221
                        return null;
222
        }
223

    
224
        /*
225
         * (non-Javadoc)
226
         * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#stateChanged(javax.swing.event.ChangeEvent)
227
         */
228
        public void stateChanged(ChangeEvent e) {
229
                if (registeredPanels.size() == 0)
230
                        return;
231

    
232
                Object object = e.getSource();
233

    
234
                if (object != null)
235
                        ((IPanel)object).selected();
236
        }
237

    
238
        /*
239
         * (non-Javadoc)
240
         * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#setPanelInGUI(org.gvsig.gui.beans.panelGroup.panels.IPanel, boolean)
241
         */
242
        public synchronized void setPanelInGUI(IPanel panel, boolean b) {
243
                if (registeredPanels.size() == 0)
244
                        return;
245

    
246
                if (registeredPanels.indexOf(panel) == -1)
247
                        return;
248
                
249
                // Remove all tabs and re-insert the necessary
250
                Vector<Component> tabPanels = new Vector<Component>(0, getJTabbedPane().getTabCount());
251

    
252
                for (int i = getJTabbedPane().getTabCount() -1; i >= 0; i--){
253
                        if (getJTabbedPane().getComponentAt(i) instanceof AbstractPanel) {
254
                                tabPanels.add(getJTabbedPane().getComponentAt(i));
255
                                getJTabbedPane().removeTabAt(i);
256
                        }
257
                }
258

    
259
                if (b == true) {
260
                        for (IPanel regPanel: registeredPanels) {
261
                                if (regPanel.equals(panel)) {
262
                                        getJTabbedPane().add(panel.getLabel(), (JPanel)panel);
263
                                }
264
                                else {
265
                                        if (tabPanels.indexOf((Component)regPanel) != -1) 
266
                                                getJTabbedPane().add(regPanel.getLabel(), (JPanel)regPanel);
267
                                }
268
                        }
269
                }
270
                else {
271
                        for (IPanel regPanel: registeredPanels) {
272
                                if (tabPanels.indexOf((Component)regPanel) != -1) {
273
                                        if (!(regPanel.equals(panel))) {
274
                                                getJTabbedPane().add(regPanel.getLabel(), (JPanel)regPanel);
275
                                        }
276
                                }
277
                        }
278
                }
279

    
280
                repaint();
281
        }
282

    
283
        /*
284
         * (non-Javadoc)
285
         * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#isPanelInGUI(org.gvsig.gui.beans.panelGroup.panels.IPanel)
286
         */
287
        public synchronized boolean isPanelInGUI(IPanel panel) {
288
                if (!registeredPanels.contains(panel))
289
                        return false;
290

    
291
                for (int i = getJTabbedPane().getTabCount() -1; i >= 0; i--){
292
                        Object object = getJTabbedPane().getComponentAt(i);
293
                        if (object instanceof AbstractPanel) {
294
                                if (object.equals(panel))
295
                                        return true;
296
                        }
297
                }
298
                
299
                return false;
300
        }
301

    
302
        /**
303
         * @see JTabbedPane#setEnabledAt(int, boolean)
304
         */
305
        public void setEnabledAt(int index, boolean enabled) {
306
                getJTabbedPane().setEnabledAt(index, enabled);
307
        }
308

    
309
        /**
310
         * @see JTabbedPane#isEnabledAt(int)
311
         */
312
        public boolean isEnabledAt(int index) {
313
                return getJTabbedPane().isEnabledAt(index);
314
        }
315

    
316
        /*
317
         * (non-Javadoc)
318
         * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#getPanelInGUICount()
319
         */
320
        public int getPanelInGUICount() {
321
                return getJTabbedPane().getTabCount();
322
        }
323

    
324
        /**
325
     * @see JTabbedPane#addChangeListener(ChangeListener)
326
     */
327
    public void addChangeListener(ChangeListener l) {
328
                getJTabbedPane().addChangeListener(l);
329
        }
330

    
331
    /**
332
     * @see JTabbedPane#removeChangeListener(ChangeListener)
333
     */
334
    public void removeChangeListener(ChangeListener l) {
335
        listenerList.remove(ChangeListener.class, l);
336
    }
337

    
338
    /**
339
     * @see JTabbedPane#getChangeListeners()
340
     */
341
    public ChangeListener[] getChangeListeners() {
342
        return (ChangeListener[])listenerList.getListeners(ChangeListener.class);
343
    }
344
}