-
Notifications
You must be signed in to change notification settings - Fork 23
/
AddNewServerFrame.java
328 lines (276 loc) · 9.5 KB
/
AddNewServerFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Copyright (C) 2001-2005 Central Laboratory of the Research Councils
* Copyright (C) 2008 Science and Technology Facilities Council
*
* History:
* 22-NOV-2011 (Margarida Castro Neves mcneves@ari.uni-heidelberg.de)
* Original version.
*/
package uk.ac.starlink.splat.vo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.net.MalformedURLException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
//import uk.ac.starlink.util.gui.ErrorDialog;
import uk.ac.starlink.table.DescribedValue;
import uk.ac.starlink.table.StarTable;
import uk.ac.starlink.vo.RegResource;
import uk.ac.starlink.votable.VOStarTable;
/**
* Class AddNewServerFrame
*
* This class allows manually adding a SSAP server to the server list.
* The AddServerFrame object is used by the SSAServerFrame class.
*
* @author Margarida Castro Neves
*/
public class AddNewServerFrame
extends JFrame
implements ActionListener
{
/**
* Menu components to ask the user for new server information
*/
private JTextField shortNameField;
private JTextField descriptionField;
private JTextField titleField;
private JTextField accessURLField;
private JLabel statusLabel;
private int status = 0;
private JPanel centrePanel = null;
private JPanel buttonsPanel = null;
private String type="";
/**
* The server list to be updated
*/
private PropertyChangeSupport statusChange;
private SSAPRegResource newResource;
/**
* Create an instance.
*/
public AddNewServerFrame()
{
initUI();
initMenus();
initFrame();
status=0;
statusChange= new PropertyChangeSupport(this);
}
public AddNewServerFrame( String type ) {
this.type = type;
initUI();
initMenus();
initFrame();
status=0;
statusChange= new PropertyChangeSupport(this);
}
/**
* Initialise the main part of the user interface.
*/
private void initUI()
{
getContentPane().setLayout( new BorderLayout() );
centrePanel = new JPanel();
centrePanel.setLayout( new BoxLayout(centrePanel, BoxLayout.PAGE_AXIS ));
getContentPane().add( centrePanel, BorderLayout.CENTER );
centrePanel.setBorder( BorderFactory.createTitledBorder( "Add New Service" ) );
buttonsPanel = new JPanel( new BorderLayout() );
getContentPane().add( buttonsPanel, BorderLayout.PAGE_END );
}
/**
* Initialise frame properties (disposal, title, menus etc.).
*/
private void initFrame()
{
setTitle( "Add New SSAP Service" );
setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
setSize( new Dimension( 500, 200 ) );
setVisible( true );
}
/**
* Initialise the menu bar, action bar and related actions.
*/
private void initMenus()
{
// Add the menu and form items to the panels
// The formular
JPanel shortNamePanel= new JPanel( new BorderLayout() );
JLabel shortNameLabel = new JLabel( "Short name:" );
shortNameField = new JTextField( 30 );
shortNamePanel.add( shortNameLabel, BorderLayout.LINE_START );
shortNamePanel.add( shortNameField, BorderLayout.LINE_END );
centrePanel.add( shortNamePanel );
JPanel titlePanel= new JPanel( new BorderLayout() );
JLabel titleLabel = new JLabel( "Title:" );
titleField = new JTextField( 30 );
titlePanel.add( titleLabel, BorderLayout.LINE_START );
titlePanel.add( titleField, BorderLayout.LINE_END );
centrePanel.add( titlePanel );
JPanel descriptionPanel= new JPanel( new BorderLayout() );
JLabel descriptionLabel = new JLabel( "Description:" );
descriptionField = new JTextField(30);
descriptionPanel.add( descriptionLabel, BorderLayout.LINE_START );
descriptionPanel.add( descriptionField, BorderLayout.LINE_END );
centrePanel.add( descriptionPanel );
JPanel accessURLPanel = new JPanel( new BorderLayout() );
JLabel accessURLLabel = new JLabel( "Access URL:" );
accessURLField = new JTextField( 30 );
accessURLPanel.add( accessURLLabel, BorderLayout.LINE_START );
accessURLPanel.add( accessURLField, BorderLayout.LINE_END );
centrePanel.add( accessURLPanel );
// the status information line
JPanel statusPanel = new JPanel(new BorderLayout() );
statusLabel = new JLabel( "", JLabel.CENTER );
statusPanel.add( statusLabel );
centrePanel.add( statusPanel );
// the action buttons
// Add action to add the new server to the list
JButton addButton = new JButton( "Add New Service" );
addButton.setActionCommand( "add" );
//addButton.setToolTipText( "Add new server" );
addButton.addActionListener( this );
buttonsPanel.add( addButton, BorderLayout.LINE_START );
// Add action to do reset the form
JButton resetButton = new JButton( "Reset" );
resetButton.setActionCommand( "reset" );
resetButton.setToolTipText( "Clear all fields" );
resetButton.addActionListener( this );
buttonsPanel.add( resetButton, BorderLayout.CENTER );
// Add an action to close the window.
JButton closeButton = new JButton( "Close" );
centrePanel.add( closeButton );
closeButton.addActionListener( this );
closeButton.setActionCommand( "close" );
closeButton.setToolTipText( "Cancel and Close window" );
buttonsPanel.add( closeButton, BorderLayout.LINE_END );
}
/**
* action performed
* process the actions
*/
public void actionPerformed(ActionEvent e) {
Object command = e.getActionCommand();
int oldstatus = status; // save old status value
if ( command.equals( "add" ) ) // add new server to list
{
// first, validate the input
status = validateResource(shortNameField.getText(), accessURLField.getText());
if ( status==1) {
setResource(); // create a new resoure object
statusLabel.setText(new String( "Added "+shortNameField.getText()+ "("+ accessURLField.getText()+ ")") );
}
}
if ( command.equals( "reset" ) ) // reset text fields
{
statusLabel.setText(new String(""));
resetFields();
}
if ( command.equals( "close" ) ) // close window
{
closeWindowEvent();
}
// if everything OK (status changed) fire an event - this will cause the
// new resource to be added to the list
statusChange.firePropertyChange("status", oldstatus, status);
status=0;
}
/**
* Close (hide) the window.
*/
private void closeWindowEvent()
{
this.setVisible( false );
}
/**
* Reset all fields
*/
private void resetFields()
{
titleField.setText("");
descriptionField.setText("");
shortNameField.setText("");
accessURLField.setText("");
}
/**
* Sets the new resource to be added to the server list
*/
private void setResource()
{
newResource = new SSAPRegResource(shortNameField.getText(), titleField.getText(), descriptionField.getText(), accessURLField.getText());
}
/**
* Validate user's input.
* Only minimal validation is performed.
* Returns 1 if no error occurred, 0 otherwise.
*/
protected int validateResource( String shortName, String accessURL )
{
int ok = 0;
statusLabel.setForeground(Color.red);
if ( shortName.trim().length() == 0 ) {
// short name cannot be null
statusLabel.setText(new String( "Short Name cannot be empty!") );
} else if ( accessURL.trim().length() == 0 ) {
// access url cannot be null
statusLabel.setText(new String( "Access URL cannot be empty!") );
} else if ( validateURL(accessURL) == false ) {
// check URL syntax
statusLabel.setText(new String( "Malformed URL: "+accessURL));
} else {
// else OK
statusLabel.setForeground(Color.black);
ok=1;
}
return ok;
}
/**
* Returns the new resource to be added to the list
*/
public SSAPRegResource getResource()
{
return newResource;
}
public String getShortName()
{
return newResource.getShortName();
}
public String getAccessURL()
{
return newResource.getCapabilities()[0].getAccessUrl();
}
public String getDescription()
{
return newResource.getCapabilities()[0].getDescription();
}
/**
* Register new Property Change Listener
*/
public void addPropertyChangeListener(PropertyChangeListener l)
{
statusChange.addPropertyChangeListener(l);
}
/**
* URL Validation
*/
public boolean validateURL(String urlStr)
{
try {
new URL(urlStr);
return true;
} catch (MalformedURLException e) {
return false;
}
}
}