svn-gvsig-desktop / branches / v05 / libraries / libIverUtiles / src / com / iver / utiles / swing / jcomboServer / JComboServer.java @ 4464
History | View | Annotate | Download (5.21 KB)
1 | 4303 | jorpiell | |
---|---|---|---|
2 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
3 | *
|
||
4 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
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 2
|
||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * Generalitat Valenciana
|
||
23 | * Conselleria d'Infraestructures i Transport
|
||
24 | * Av. Blasco Ib??ez, 50
|
||
25 | * 46010 VALENCIA
|
||
26 | * SPAIN
|
||
27 | *
|
||
28 | * +34 963862235
|
||
29 | * gvsig@gva.es
|
||
30 | * www.gvsig.gva.es
|
||
31 | *
|
||
32 | * or
|
||
33 | *
|
||
34 | * IVER T.I. S.A
|
||
35 | * Salamanca 50
|
||
36 | * 46005 Valencia
|
||
37 | * Spain
|
||
38 | *
|
||
39 | * +34 963163400
|
||
40 | * dac@iver.es
|
||
41 | */
|
||
42 | package com.iver.utiles.swing.jcomboServer; |
||
43 | import javax.swing.JComboBox; |
||
44 | |||
45 | /**
|
||
46 | * This class is a user interface component that looks equal to
|
||
47 | * a JComboBox component, but it contains ServerData objects and
|
||
48 | * it is able to show them in last access order.It has methods to
|
||
49 | * add ServerData objects and to retrieve them.
|
||
50 | *
|
||
51 | * @see java.swing.JComboBox
|
||
52 | * @see es.gva.cit.catalogClient.utils.comboserver.ServerData
|
||
53 | * @author Jorge Piera Llodra (piera_jor@gva.es)
|
||
54 | */
|
||
55 | public class JComboServer extends JComboBox { |
||
56 | |||
57 | /**
|
||
58 | * Just a simple constructor
|
||
59 | *
|
||
60 | */
|
||
61 | public JComboServer() {
|
||
62 | super();
|
||
63 | } |
||
64 | |||
65 | /**
|
||
66 | * A constructor
|
||
67 | *
|
||
68 | *
|
||
69 | * @param servers Array with all the rervers to show
|
||
70 | */
|
||
71 | public JComboServer(ServerData[] servers) { |
||
72 | super(setLastAccessOrder(servers));
|
||
73 | } |
||
74 | |||
75 | /**
|
||
76 | * This method returns the selected server
|
||
77 | *
|
||
78 | *
|
||
79 | * @return A Server
|
||
80 | */
|
||
81 | public ServerData getSelectedServer() {
|
||
82 | try {
|
||
83 | return (ServerData) getSelectedItem();
|
||
84 | }catch(ClassCastException e){ |
||
85 | return new ServerData((String)getSelectedItem(),"",""); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /**
|
||
90 | * This method adds a new Server in order
|
||
91 | *
|
||
92 | *
|
||
93 | * @param server New Server
|
||
94 | */
|
||
95 | public void addServer(ServerData server) { |
||
96 | ServerData[] servers = getAllServers();
|
||
97 | ServerData[] newServers = new ServerData[servers.length + 1]; |
||
98 | System.arraycopy(servers, 0, newServers, 0, servers.length); |
||
99 | newServers[servers.length] = server; |
||
100 | newServers = setLastAccessOrder(newServers); |
||
101 | setServerList(newServers); |
||
102 | } |
||
103 | |||
104 | /**
|
||
105 | * This method returns an array with all the servers that the
|
||
106 | * combo contains
|
||
107 | *
|
||
108 | *
|
||
109 | * @return An array of servers
|
||
110 | */
|
||
111 | public ServerData[] getAllServers() { |
||
112 | ServerData[] servers = new ServerData[getItemCount()]; |
||
113 | for (int i=0 ; i<getItemCount() ; i++){ |
||
114 | servers[i] = (ServerData) this.getItemAt(i);
|
||
115 | } |
||
116 | return servers;
|
||
117 | } |
||
118 | |||
119 | /**
|
||
120 | * It adds a server list
|
||
121 | *
|
||
122 | *
|
||
123 | * @param servers Array with servers
|
||
124 | */
|
||
125 | public void setServerList(ServerData[] servers) { |
||
126 | removeAllItems(); |
||
127 | servers = setLastAccessOrder(servers); |
||
128 | for (int i=0 ; i<servers.length ; i++){ |
||
129 | try{
|
||
130 | if (!(servers[i].getServerAddress().equals(""))){ |
||
131 | addItem(servers[i]); |
||
132 | } |
||
133 | }catch(java.lang.NullPointerException e){
|
||
134 | //Server is null
|
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * This method order all the servers in the combo
|
||
141 | *
|
||
142 | */
|
||
143 | public void setServersOrder() { |
||
144 | ServerData[] servers = getAllServers();
|
||
145 | servers = setLastAccessOrder(servers); |
||
146 | setServerList(servers); |
||
147 | } |
||
148 | |||
149 | /**
|
||
150 | * This static method ordered an array of servers by last access
|
||
151 | *
|
||
152 | *
|
||
153 | * @return A new array
|
||
154 | * @param servers Array of servers
|
||
155 | */
|
||
156 | private static ServerData[] setLastAccessOrder(ServerData[] servers) { |
||
157 | ServerData[] orderedServerData = new ServerData[servers.length]; |
||
158 | |||
159 | for (int i=0 ; i<servers.length ; i++){ |
||
160 | int pos = getServerPosition(servers, i);
|
||
161 | orderedServerData[pos] = servers[i]; |
||
162 | } |
||
163 | return orderedServerData;
|
||
164 | } |
||
165 | |||
166 | /**
|
||
167 | * This static function return the ordered position of a server. The algorithm to
|
||
168 | * order can be changed.
|
||
169 | *
|
||
170 | *
|
||
171 | * @return Number with the new position
|
||
172 | * @param servers Array that contains all the servers
|
||
173 | * @param serverPos Server position
|
||
174 | */
|
||
175 | private static int getServerPosition(ServerData[] servers, int serverPos) { |
||
176 | int pos=0; |
||
177 | for (int i=0 ; i<servers.length ; i++){ |
||
178 | if (!(servers[serverPos].getServerAddress().equals(servers[i].getServerAddress()))){
|
||
179 | if (servers[serverPos].getLastAccess().before(servers[i].getLastAccess())){
|
||
180 | pos++; |
||
181 | } |
||
182 | if (servers[serverPos].getLastAccess().equals(servers[i].getLastAccess())){
|
||
183 | if (serverPos < i){
|
||
184 | pos++; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | } |
||
189 | } |
||
190 | return pos;
|
||
191 | } |
||
192 | } |