Statistics
| Revision:

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

History | View | Annotate | Download (4.98 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;
25

    
26
import java.io.Serializable;
27
import java.util.ArrayList;
28

    
29
import org.gvsig.gui.beans.panelGroup.panels.AbstractPanel;
30

    
31
/**
32
 * <p>Implemented to use the {@link AbstractPanelGroup AbstractPanelGroup} type desired.</p>
33
 * <p>This class allows having different implementations of {@link AbstractPanelGroup AbstractPanelGroup}
34
 * and only load in memory the class of some of them, whom have previously been imported to future use. Then, when
35
 * a graphical interface would need use a <code>AbstractPanelGroup</code> instance, will instance an object of the 
36
 * class <i>selected</i> (set previously as <i>default</i>) of the registered types in this manager.</p> 
37
 * 
38
 * @version 18/10/2007
39
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) 
40
 */
41
public class PanelGroupManager implements Serializable {
42
        private static final long serialVersionUID = 2059085515913026734L;
43

    
44
        /**
45
         * Array list with the {@link AbstractPanelGroup AbstractPanelGroup} registered.
46
         * 
47
         * @see #registerPanelGroup(Class)
48
         * @see #deregisterPanelGroup(Class)
49
         */
50
        private ArrayList<Class> registeredPanelGroups = null;
51
        
52
        /**
53
         * The {@link AbstractPanelGroup AbstractPanelGroup} type set as default.
54
         * 
55
         * @see #getPanelGroup()
56
         * @see #setDefaultType(Class)
57
         */
58
        private Class defaultPanelGroup = null;
59

    
60
        /**
61
         * <p>Reference to the current manager with the <i>PanelGroup</i> registered.</p>
62
         */
63
        private static PanelGroupManager manager = null;
64

    
65
        /**
66
         * <p>Default constructor.</p>
67
         */
68
        protected PanelGroupManager() {
69
                 registeredPanelGroups = new ArrayList<Class>();
70
        }
71
        
72
        /**
73
         * <p>The singleton <code>PanelGroupManager</code> instance.</p>
74
         * 
75
         * @return current manager used, or if didn't exist any, a new one
76
         */
77
        public static synchronized PanelGroupManager getManager()
78
        {
79
                if( manager == null ) {
80
                        manager = new PanelGroupManager ();
81
                }
82

    
83
                return manager;
84
        }
85
        
86
        /**
87
         * <p>Registers a new {@link AbstractPanelGroup AbstractPanelGroup} class type if it
88
         * wasn't already done.</p>
89
         * 
90
         * @param panelGroup the new <code>AbstractPanelGroup</code> class type to register
91
         * 
92
         * @see #deregisterPanelGroup(Class)
93
         */
94
        public synchronized void registerPanelGroup(Class panelGroup) {
95
                // Only adds if isn't already registered
96
                if (!registeredPanelGroups.contains(panelGroup))
97
                        registeredPanelGroups.add(panelGroup);
98
        }
99

    
100
        /**
101
         * <p>Unregisters a {@link AbstractPanelGroup AbstractPanelGroup} class type if it
102
         * was previously registered.</p>
103
         * <p>If the type to unregister is the <i>default</i>, sets <i>default</i> to <code>null</code>.</p>
104
         * 
105
         * @param panelGroup the <code>AbstractPanelGroup</code> class type to unregister
106
         * 
107
         * @see #registerPanelGroup(Class)
108
         */
109
        public synchronized void deregisterPanelGroup(Class panelGroup) {
110
                // Only removes if already registered
111
                if (registeredPanelGroups.contains(panelGroup)) {
112
                        registeredPanelGroups.remove(panelGroup);
113

    
114
                        if (defaultPanelGroup == null)
115
                                return;
116

    
117
                        if (defaultPanelGroup.equals(panelGroup)) {
118
                                defaultPanelGroup = null;
119
                        }
120
                }
121
        }
122
        
123
        /**
124
         * <p>Sets as <i>default</i> one of the {@link AbstractPanelGroup AbstractPanelGroup} class type
125
         *  previously registered.</p>
126
         * 
127
         * @param panelGroup a <code>AbstractPanelGroup</code> class type
128
         * 
129
         * @see #getPanelGroup()
130
         */
131
        public synchronized void setDefaultType(Class panelGroup) {
132
                if (registeredPanelGroups.contains(panelGroup))
133
                        defaultPanelGroup = panelGroup;
134
        }
135
        
136
        /**
137
         * <p>Gets the <i>default</i> {@link AbstractPanelGroup AbstractPanelGroup} class type.</p>
138
         * 
139
         * return the <i>default</i> <code>AbstractPanelGroup</code> class type or <code>null</code> if isn't any
140
         * 
141
         * @throws Exception any exception produced loading the {@link AbstractPanel AbstractPanel}
142
         * 
143
         * @see #setDefaultType(Class)
144
         */        
145
        public synchronized AbstractPanelGroup getPanelGroup(Object reference) throws Exception {
146
                try {
147
                        if( defaultPanelGroup != null ) {
148
                                return (AbstractPanelGroup) defaultPanelGroup.getConstructor(Object.class).newInstance(reference);
149
                        }
150
                }
151
                catch (Exception e) {
152
                        throw e;
153
                }
154

    
155
                return null;
156
        }
157
}