Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / swing / jcomboServer / JComboServer.java @ 40561

History | View | Annotate | Download (6.14 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

    
25
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
26
*
27
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
28
*
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
*
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
*
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
*
43
* For more information, contact:
44
*
45
*  Generalitat Valenciana
46
*   Conselleria d'Infraestructures i Transport
47
*   Av. Blasco Ib??ez, 50
48
*   46010 VALENCIA
49
*   SPAIN
50
*
51
*      +34 963862235
52
*   gvsig@gva.es
53
*      www.gvsig.gva.es
54
*
55
*    or
56
*
57
*   IVER T.I. S.A
58
*   Salamanca 50
59
*   46005 Valencia
60
*   Spain
61
*
62
*   +34 963163400
63
*   dac@iver.es
64
*/
65
package org.gvsig.utils.swing.jcomboServer;
66
import javax.swing.JComboBox;
67

    
68
/**
69
 * This class is a user interface component that looks equal to
70
 * a JComboBox component, but it contains ServerData objects and 
71
 * it is able to show them in last access order.It has methods to 
72
 * add ServerData objects and to retrieve them.
73
 * 
74
 * @see java.swing.JComboBox
75
 * @see es.gva.cit.catalogClient.utils.comboserver.ServerData
76
 * @author Jorge Piera Llodra (piera_jor@gva.es)
77
 */
78
public class JComboServer extends JComboBox {
79

    
80
/**
81
 * Just a simple constructor
82
 * 
83
 */
84
    public  JComboServer() {        
85
        super();
86
    } 
87

    
88
/**
89
 * A constructor
90
 * 
91
 * 
92
 * @param servers Array with all the rervers to show
93
 */
94
    public  JComboServer(ServerData[] servers) {        
95
        super(setLastAccessOrder(servers));
96
    } 
97

    
98
/**
99
 * This method returns the selected server
100
 * 
101
 * 
102
 * @return A Server
103
 */
104
    public ServerData getSelectedServer() {        
105
        try {
106
            return (ServerData) getSelectedItem();
107
        }catch(ClassCastException e){
108
            return new ServerData((String)getSelectedItem(),"","");
109
        }
110
    } 
111

    
112
/**
113
 * This method adds a new Server in order
114
 * 
115
 * 
116
 * @param server New Server
117
 */
118
    public void addServer(ServerData server) {        
119
        ServerData[] servers = getAllServers();
120
        ServerData[] newServers = new ServerData[servers.length + 1];
121
        System.arraycopy(servers, 0, newServers, 0, servers.length);
122
        newServers[servers.length] = server;
123
        newServers = setLastAccessOrder(newServers);
124
        setServerList(newServers);
125
    } 
126

    
127
/**
128
 * This method returns an array with all the servers that the
129
 * combo contains
130
 * 
131
 * 
132
 * @return An array of servers
133
 */
134
    public ServerData[] getAllServers() {        
135
        ServerData[] servers = new ServerData[getItemCount()];
136
        for (int i=0 ; i<getItemCount() ; i++){
137
            servers[i] = (ServerData) this.getItemAt(i);
138
        }
139
        return servers;
140
    } 
141

    
142
/**
143
 * It adds a server list
144
 * 
145
 * 
146
 * @param servers Array with servers
147
 */
148
    public void setServerList(ServerData[] servers) {        
149
        removeAllItems();
150
        servers = setLastAccessOrder(servers);
151
        for (int i=0 ; i<servers.length ; i++){
152
           try{
153
                   if (!(servers[i].getServerAddress().equals(""))){
154
                           addItem(servers[i]);
155
                   }
156
           }catch(java.lang.NullPointerException e){
157
                   //Server is null
158
           }
159
        }
160
    } 
161

    
162
/**
163
 * This method order all the servers in the combo
164
 * 
165
 */
166
    public void setServersOrder() {        
167
        ServerData[] servers = getAllServers();
168
        servers = setLastAccessOrder(servers);
169
        setServerList(servers);
170
    } 
171

    
172
/**
173
 * This static method ordered an array of servers by last access
174
 * 
175
 * 
176
 * @return A new array
177
 * @param servers Array of servers
178
 */
179
    private static ServerData[] setLastAccessOrder(ServerData[] servers) {        
180
            ServerData[] orderedServerData = new ServerData[servers.length];
181
                
182
            for (int i=0 ; i<servers.length ; i++){
183
                int pos = getServerPosition(servers, i);
184
                orderedServerData[pos] = servers[i];
185
        }
186
            return orderedServerData;
187
    } 
188

    
189
/**
190
 * This static function return the ordered position of a server. The algorithm to
191
 * order can be changed.
192
 * 
193
 * 
194
 * @return Number with the new position
195
 * @param servers Array that contains all the servers
196
 * @param serverPos Server position
197
 */
198
    private static int getServerPosition(ServerData[] servers, int serverPos) {        
199
        int pos=0;
200
        for (int i=0 ; i<servers.length ; i++){
201
            if (!(servers[serverPos].getServerAddress().equals(servers[i].getServerAddress()))){
202
                if (servers[serverPos].getLastAccess().before(servers[i].getLastAccess())){
203
                    pos++;
204
                }
205
                if (servers[serverPos].getLastAccess().equals(servers[i].getLastAccess())){
206
                    if (serverPos < i){
207
                        pos++;
208
                    }
209
                }
210
                
211
            }
212
        }
213
        return pos;
214
    } 
215
 }