1
package edu.ucsb.eucalyptus.admin.client;
3
import com.google.gwt.user.client.rpc.AsyncCallback;
4
import com.google.gwt.user.client.ui.*;
6
import java.util.ArrayList;
9
public class VmTypeTable extends VerticalPanel {
11
private static Label statusLabel = new Label();
12
private Grid grid = new Grid ();
13
private List<VmTypeWeb> VmTypeList = new ArrayList<VmTypeWeb>();
14
private static String sessionId;
16
public VmTypeTable(String sessionId)
18
this.sessionId = sessionId;
20
this.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
21
this.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
22
Label VmTypesHeader = new Label( "VM Types:" );
23
VmTypesHeader.setStyleName ( "euca-section-header" );
24
this.add ( VmTypesHeader );
25
this.add ( this.grid );
26
HorizontalPanel hpanel = new HorizontalPanel ();
27
hpanel.setSpacing (5);
28
hpanel.add ( new Button( "Save VmTypes", new SaveCallback( this ) ) );
29
hpanel.add ( this.statusLabel );
30
this.statusLabel.setWidth ("250");
31
this.statusLabel.setText ("");
32
this.statusLabel.setStyleName ("euca-greeting-pending");
34
EucalyptusWebBackend.App.getInstance().getVmTypes(
35
this.sessionId, new GetCallback( this ) );
38
private void rebuildTable()
40
int rows = this.VmTypeList.size() + 1;
42
this.grid.resize ( rows, 5 );
43
this.grid.setVisible (true);
44
this.grid.setStyleName( "euca-table" );
45
this.grid.setCellPadding( 6 );
46
//this.grid.setWidget( 0, 0, new Label( "Enabled" ) );
47
this.grid.setWidget( 0, 1, new Label( "Name" ) );
48
this.grid.setWidget( 0, 2, new Label( "CPUs" ) );
49
this.grid.setWidget( 0, 3, new Label( "Memory (MB)" ) );
50
this.grid.setWidget( 0, 4, new Label( "Disk (GB)" ) );
51
this.grid.getRowFormatter().setStyleName( 0, "euca-table-heading-row" );
53
for ( VmTypeWeb VmType : this.VmTypeList ) {
54
addVmTypeEntry (row++, VmType);
58
private void addVmTypeEntry( int row, VmTypeWeb VmType )
60
if ( ( row % 2 ) == 1 ) {
61
this.grid.getRowFormatter().setStyleName( row, "euca-table-odd-row" );
63
this.grid.getRowFormatter().setStyleName( row, "euca-table-even-row" );
66
final CheckBox cb = new CheckBox();
67
cb.addClickListener (new ChangeCallback (this, row));
68
cb.setChecked( true ); // TODO: get this from server
69
//this.grid.setWidget( row, 0, cb );
71
final Label name_b = new Label ();
72
name_b.setText( VmType.getName() );
73
this.grid.setWidget( row, 1, name_b );
75
final TextBox cpu_b = new TextBox();
76
cpu_b.addChangeListener (new ChangeCallback (this, row));
77
cpu_b.setVisibleLength( 2 );
78
cpu_b.setText( "" + VmType.getCpu() );
79
this.grid.setWidget( row, 2, cpu_b );
80
this.grid.getCellFormatter().setHorizontalAlignment(row, 2, HasHorizontalAlignment.ALIGN_CENTER);
82
final TextBox mem_b = new TextBox();
83
mem_b.addChangeListener (new ChangeCallback (this, row));
84
mem_b.setVisibleLength( 4 );
85
mem_b.setText( "" + VmType.getMemory() );
86
this.grid.setWidget( row, 3, mem_b );
87
this.grid.getCellFormatter().setHorizontalAlignment(row, 3, HasHorizontalAlignment.ALIGN_CENTER);
90
final TextBox disk_b = new TextBox();
91
disk_b.addChangeListener (new ChangeCallback (this, row));
92
disk_b.setVisibleLength( 4 );
93
disk_b.setText( "" + VmType.getDisk() );
94
this.grid.setWidget( row, 4, disk_b );
95
this.grid.getCellFormatter().setHorizontalAlignment(row, 4, HasHorizontalAlignment.ALIGN_CENTER);
99
public List<VmTypeWeb> getVmTypeList()
104
public void setVmTypeList( final List<VmTypeWeb> VmTypeList )
106
this.VmTypeList = VmTypeList;
109
public void updateRow (int row)
111
VmTypeWeb VmType = this.VmTypeList.get (row-1); // table has a header row
112
VmType.setCpu (Integer.parseInt(((TextBox)this.grid.getWidget(row, 2)).getText()));
113
VmType.setMemory (Integer.parseInt(((TextBox)this.grid.getWidget(row, 3)).getText()));
114
VmType.setDisk (Integer.parseInt(((TextBox)this.grid.getWidget(row, 4)).getText()));
117
class ChangeCallback implements ChangeListener, ClickListener {
118
private VmTypeTable parent;
121
ChangeCallback ( final VmTypeTable parent, final int row )
123
this.parent = parent;
127
public void onChange (Widget sender)
129
this.parent.updateRow (this.row);
130
this.parent.statusLabel.setText ("Unsaved changes");
131
this.parent.statusLabel.setStyleName ("euca-greeting-warning");
134
public void onClick (Widget sender)
136
this.parent.updateRow (this.row);
137
this.parent.statusLabel.setText ("Unsaved changes");
138
this.parent.statusLabel.setStyleName ("euca-greeting-warning");
142
class GetCallback implements AsyncCallback {
144
private VmTypeTable parent;
146
GetCallback( final VmTypeTable parent )
148
this.parent = parent;
151
public void onFailure( final Throwable throwable )
153
this.parent.statusLabel.setText ("Failed to contact server!");
154
this.parent.statusLabel.setStyleName ("euca-greeting-error");
157
public void onSuccess( final Object o )
159
List<VmTypeWeb> newVmTypeList = (List<VmTypeWeb>) o;
160
this.parent.VmTypeList = newVmTypeList;
161
this.parent.rebuildTable();
165
class SaveCallback implements AsyncCallback, ClickListener {
167
private VmTypeTable parent;
169
SaveCallback( final VmTypeTable parent )
171
this.parent = parent;
174
public void onClick( final Widget widget )
176
this.parent.statusLabel.setText ("Saving...");
177
this.parent.statusLabel.setStyleName ("euca-greeting-pending");
178
EucalyptusWebBackend.App.getInstance().setVmTypes(
179
this.parent.sessionId, this.parent.VmTypeList, this );
182
public void onFailure( final Throwable throwable )
184
this.parent.statusLabel.setText ("Failed to save! (Make sure values in each column are ordered.)");
185
this.parent.statusLabel.setStyleName ("euca-greeting-error");
188
public void onSuccess( final Object o )
190
this.parent.statusLabel.setText ("Saved VM Types to server");
191
this.parent.statusLabel.setStyleName ("euca-greeting-disabled");
192
EucalyptusWebBackend.App.getInstance().getVmTypes(
193
this.parent.sessionId, new GetCallback( this.parent ) ); // so the order will refresh