Statistics
| Revision:

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

History | View | Annotate | Download (5.06 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.utils.swing.jcomboServer;
25

    
26
import javax.swing.ComboBoxModel;
27
import javax.swing.DefaultComboBoxModel;
28
import javax.swing.JComboBox;
29

    
30
/**
31
 * This class is a controller for a JComboBox component. It contains ServerData 
32
 * objects and it is able to show them in last access order.
33
 * It has methods to add ServerData objects and to retrieve them.
34
 *
35
 * @author gvSIG Team
36
 */
37
public class ComboServerController {
38

    
39
  private final JComboBox combo;
40

    
41
  public ComboServerController(JComboBox combo) {
42
    this.combo = combo;
43
  }
44

    
45
  public ComboServerController(JComboBox combo, ServerData[] servers) {
46
    this.combo = combo;
47
    this.combo.setModel(new DefaultComboBoxModel(setLastAccessOrder(servers)));
48
  }
49

    
50
  public JComboBox getCombo() {
51
    return this.combo;
52
  }
53
  
54
  /**
55
   * This method returns the selected server
56
   *
57
   * @return A Server
58
   */
59
  public ServerData getSelectedServer() {
60
    try {
61
      return (ServerData) this.combo.getSelectedItem();
62
    } catch (ClassCastException e) {
63
      return new ServerData((String) this.combo.getSelectedItem(), "", "");
64
    }
65
  }
66

    
67
  /**
68
   * This method adds a new Server in order
69
   *
70
   *
71
   * @param server New Server
72
   */
73
  public void addServer(ServerData server) {
74
    ServerData[] servers = getAllServers();
75
    ServerData[] newServers = new ServerData[servers.length + 1];
76
    System.arraycopy(servers, 0, newServers, 0, servers.length);
77
    newServers[servers.length] = server;
78
    newServers = setLastAccessOrder(newServers);
79
    setServerList(newServers);
80
  }
81

    
82
  /**
83
   * This method returns an array with all the servers that the combo contains
84
   *
85
   *
86
   * @return An array of servers
87
   */
88
  public ServerData[] getAllServers() {
89
    ServerData[] servers = new ServerData[this.combo.getItemCount()];
90
    for (int i = 0; i < this.combo.getItemCount(); i++) {
91
      servers[i] = (ServerData) this.combo.getItemAt(i);
92
    }
93
    return servers;
94
  }
95

    
96
  /**
97
   * It adds a server list
98
   *
99
   *
100
   * @param servers Array with servers
101
   */
102
  public void setServerList(ServerData[] servers) {
103
    this.combo.removeAllItems();
104
    servers = setLastAccessOrder(servers);
105
    for (int i = 0; i < servers.length; i++) {
106
      try {
107
        if (!(servers[i].getServerAddress().equals(""))) {
108
          this.combo.addItem(servers[i]);
109
        }
110
      } catch (java.lang.NullPointerException e) {
111
        //Server is null
112
      }
113
    }
114
  }
115

    
116
  /**
117
   * This method order all the servers in the combo
118
   *
119
   */
120
  public void setServersOrder() {
121
    ServerData[] servers = getAllServers();
122
    servers = setLastAccessOrder(servers);
123
    setServerList(servers);
124
  }
125

    
126
  /**
127
   * This static method ordered an array of servers by last access
128
   *
129
   *
130
   * @return A new array
131
   * @param servers Array of servers
132
   */
133
  private static ServerData[] setLastAccessOrder(ServerData[] servers) {
134
    ServerData[] orderedServerData = new ServerData[servers.length];
135

    
136
    for (int i = 0; i < servers.length; i++) {
137
      int pos = getServerPosition(servers, i);
138
      orderedServerData[pos] = servers[i];
139
    }
140
    return orderedServerData;
141
  }
142

    
143
  /**
144
   * This static function return the ordered position of a server. The algorithm
145
   * to order can be changed.
146
   *
147
   *
148
   * @return Number with the new position
149
   * @param servers Array that contains all the servers
150
   * @param serverPos Server position
151
   */
152
  private static int getServerPosition(ServerData[] servers, int serverPos) {
153
    int pos = 0;
154
    for (int i = 0; i < servers.length; i++) {
155
      if (!(servers[serverPos].getServerAddress().equals(servers[i].getServerAddress()))) {
156
        if (servers[serverPos].getLastAccess().before(servers[i].getLastAccess())) {
157
          pos++;
158
        }
159
        if (servers[serverPos].getLastAccess().equals(servers[i].getLastAccess())) {
160
          if (serverPos < i) {
161
            pos++;
162
          }
163
        }
164

    
165
      }
166
    }
167
    return pos;
168
  }
169

    
170
  public void setModel(ComboBoxModel model) {
171
    this.combo.setModel(model);
172
  }
173

    
174
  public ComboBoxModel getModel() {
175
    return this.combo.getModel();
176
  }
177

    
178
  public int getItemCount() {
179
    return this.combo.getItemCount();
180
  }
181

    
182
  public Object getItemAt(int i) {
183
    return this.combo.getItemAt(i);
184
  }
185
}