gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.app.wmsclient / src / main / java / org / gvsig / raster / wms / app / wmsclient / gui / wizard / ComboServerController.java @ 11260
History | View | Annotate | Download (5.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 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.raster.wms.app.wmsclient.gui.wizard; |
25 |
|
26 |
import javax.swing.ComboBoxModel; |
27 |
import javax.swing.DefaultComboBoxModel; |
28 |
import javax.swing.JComboBox; |
29 |
import org.gvsig.utils.swing.jcomboServer.ServerData; |
30 |
|
31 |
/**
|
32 |
* This class is a controller for a JComboBox component. It contains ServerData
|
33 |
* objects and it is able to show them in last access order.
|
34 |
* It has methods to add ServerData objects and to retrieve them.
|
35 |
*
|
36 |
* @author gvSIG Team
|
37 |
* @deprecated usar org.gvsig.utils.swing.jcomboServer.ComboServerController
|
38 |
*/
|
39 |
public class ComboServerController { |
40 |
|
41 |
private final JComboBox combo; |
42 |
|
43 |
public ComboServerController(JComboBox combo) { |
44 |
this.combo = combo;
|
45 |
} |
46 |
|
47 |
public ComboServerController(JComboBox combo, ServerData[] servers) { |
48 |
this.combo = combo;
|
49 |
this.combo.setModel(new DefaultComboBoxModel(setLastAccessOrder(servers))); |
50 |
} |
51 |
|
52 |
public JComboBox getCombo() { |
53 |
return this.combo; |
54 |
} |
55 |
|
56 |
/**
|
57 |
* This method returns the selected server
|
58 |
*
|
59 |
* @return A Server
|
60 |
*/
|
61 |
public ServerData getSelectedServer() {
|
62 |
try {
|
63 |
return (ServerData) this.combo.getSelectedItem(); |
64 |
} catch (ClassCastException e) { |
65 |
return new ServerData((String) this.combo.getSelectedItem(), "", ""); |
66 |
} |
67 |
} |
68 |
|
69 |
/**
|
70 |
* This method adds a new Server in order
|
71 |
*
|
72 |
*
|
73 |
* @param server New Server
|
74 |
*/
|
75 |
public void addServer(ServerData server) { |
76 |
ServerData[] servers = getAllServers();
|
77 |
ServerData[] newServers = new ServerData[servers.length + 1]; |
78 |
System.arraycopy(servers, 0, newServers, 0, servers.length); |
79 |
newServers[servers.length] = server; |
80 |
newServers = setLastAccessOrder(newServers); |
81 |
setServerList(newServers); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* This method returns an array with all the servers that the combo contains
|
86 |
*
|
87 |
*
|
88 |
* @return An array of servers
|
89 |
*/
|
90 |
public ServerData[] getAllServers() { |
91 |
ServerData[] servers = new ServerData[this.combo.getItemCount()]; |
92 |
for (int i = 0; i < this.combo.getItemCount(); i++) { |
93 |
servers[i] = (ServerData) this.combo.getItemAt(i);
|
94 |
} |
95 |
return servers;
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* It adds a server list
|
100 |
*
|
101 |
*
|
102 |
* @param servers Array with servers
|
103 |
*/
|
104 |
public void setServerList(ServerData[] servers) { |
105 |
this.combo.removeAllItems();
|
106 |
servers = setLastAccessOrder(servers); |
107 |
for (int i = 0; i < servers.length; i++) { |
108 |
try {
|
109 |
if (!(servers[i].getServerAddress().equals(""))) { |
110 |
this.combo.addItem(servers[i]);
|
111 |
} |
112 |
} catch (java.lang.NullPointerException e) {
|
113 |
//Server is null
|
114 |
} |
115 |
} |
116 |
} |
117 |
|
118 |
/**
|
119 |
* This method order all the servers in the combo
|
120 |
*
|
121 |
*/
|
122 |
public void setServersOrder() { |
123 |
ServerData[] servers = getAllServers();
|
124 |
servers = setLastAccessOrder(servers); |
125 |
setServerList(servers); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* This static method ordered an array of servers by last access
|
130 |
*
|
131 |
*
|
132 |
* @return A new array
|
133 |
* @param servers Array of servers
|
134 |
*/
|
135 |
private static ServerData[] setLastAccessOrder(ServerData[] servers) { |
136 |
ServerData[] orderedServerData = new ServerData[servers.length]; |
137 |
|
138 |
for (int i = 0; i < servers.length; i++) { |
139 |
int pos = getServerPosition(servers, i);
|
140 |
orderedServerData[pos] = servers[i]; |
141 |
} |
142 |
return orderedServerData;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* This static function return the ordered position of a server. The algorithm
|
147 |
* to order can be changed.
|
148 |
*
|
149 |
*
|
150 |
* @return Number with the new position
|
151 |
* @param servers Array that contains all the servers
|
152 |
* @param serverPos Server position
|
153 |
*/
|
154 |
private static int getServerPosition(ServerData[] servers, int serverPos) { |
155 |
int pos = 0; |
156 |
for (int i = 0; i < servers.length; i++) { |
157 |
if (!(servers[serverPos].getServerAddress().equals(servers[i].getServerAddress()))) {
|
158 |
if (servers[serverPos].getLastAccess().before(servers[i].getLastAccess())) {
|
159 |
pos++; |
160 |
} |
161 |
if (servers[serverPos].getLastAccess().equals(servers[i].getLastAccess())) {
|
162 |
if (serverPos < i) {
|
163 |
pos++; |
164 |
} |
165 |
} |
166 |
|
167 |
} |
168 |
} |
169 |
return pos;
|
170 |
} |
171 |
|
172 |
public void setModel(ComboBoxModel model) { |
173 |
this.combo.setModel(model);
|
174 |
} |
175 |
|
176 |
public ComboBoxModel getModel() { |
177 |
return this.combo.getModel(); |
178 |
} |
179 |
|
180 |
public int getItemCount() { |
181 |
return this.combo.getItemCount(); |
182 |
} |
183 |
|
184 |
public Object getItemAt(int i) { |
185 |
return this.combo.getItemAt(i); |
186 |
} |
187 |
} |