~ccheney/ubuntu/lucid/eucalyptus/lucid-sru

« back to all changes in this revision

Viewing changes to clc/modules/www/src/edu/ucsb/eucalyptus/admin/client/VmTypeTable.java

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2009-02-11 02:45:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090211024539-0jhzbpg3hk6nu1yg
Tags: upstream-1.5~bzr139
ImportĀ upstreamĀ versionĀ 1.5~bzr139

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package edu.ucsb.eucalyptus.admin.client;
 
2
 
 
3
import com.google.gwt.user.client.rpc.AsyncCallback;
 
4
import com.google.gwt.user.client.ui.*;
 
5
 
 
6
import java.util.ArrayList;
 
7
import java.util.List;
 
8
 
 
9
public class VmTypeTable extends VerticalPanel {
 
10
 
 
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;
 
15
        
 
16
        public VmTypeTable(String sessionId)
 
17
        {
 
18
                this.sessionId = sessionId;
 
19
                this.setSpacing (10);
 
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");
 
33
                this.add ( hpanel );
 
34
                EucalyptusWebBackend.App.getInstance().getVmTypes( 
 
35
                        this.sessionId, new GetCallback( this ) );
 
36
        }
 
37
 
 
38
        private void rebuildTable()
 
39
        {
 
40
                int rows = this.VmTypeList.size() + 1;
 
41
                this.grid.clear ();
 
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" );
 
52
                int row = 1;
 
53
                for ( VmTypeWeb VmType : this.VmTypeList ) {
 
54
                        addVmTypeEntry (row++, VmType);
 
55
                }
 
56
        }
 
57
 
 
58
        private void addVmTypeEntry( int row, VmTypeWeb VmType )
 
59
        {                       
 
60
                if ( ( row % 2 ) == 1 ) {
 
61
                        this.grid.getRowFormatter().setStyleName( row, "euca-table-odd-row" );
 
62
                } else {
 
63
                        this.grid.getRowFormatter().setStyleName( row, "euca-table-even-row" );
 
64
                }
 
65
                                
 
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 );
 
70
                                
 
71
                final Label name_b = new Label ();
 
72
                name_b.setText( VmType.getName() );
 
73
                this.grid.setWidget( row, 1, name_b );
 
74
                
 
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);
 
81
 
 
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);
 
88
                
 
89
                
 
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);
 
96
                                        
 
97
        }
 
98
 
 
99
        public List<VmTypeWeb> getVmTypeList()
 
100
        {
 
101
                return VmTypeList;
 
102
        }
 
103
 
 
104
        public void setVmTypeList( final List<VmTypeWeb> VmTypeList )
 
105
        {
 
106
                this.VmTypeList = VmTypeList;
 
107
        }
 
108
 
 
109
        public void updateRow (int row)
 
110
        {
 
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()));
 
115
        }
 
116
        
 
117
        class ChangeCallback implements ChangeListener, ClickListener {
 
118
                private VmTypeTable parent;
 
119
                private int row;
 
120
                
 
121
                ChangeCallback ( final VmTypeTable parent, final int row )
 
122
                {
 
123
                        this.parent = parent;
 
124
                        this.row = row;
 
125
                }
 
126
                
 
127
                public void onChange (Widget sender) 
 
128
                {
 
129
                        this.parent.updateRow (this.row);
 
130
                        this.parent.statusLabel.setText ("Unsaved changes");
 
131
                        this.parent.statusLabel.setStyleName ("euca-greeting-warning");
 
132
                }
 
133
                
 
134
                public void onClick (Widget sender) 
 
135
                {
 
136
                        this.parent.updateRow (this.row);
 
137
                        this.parent.statusLabel.setText ("Unsaved changes");
 
138
                        this.parent.statusLabel.setStyleName ("euca-greeting-warning");
 
139
                }
 
140
        }
 
141
        
 
142
        class GetCallback implements AsyncCallback {
 
143
 
 
144
                private VmTypeTable parent;
 
145
 
 
146
                GetCallback( final VmTypeTable parent )
 
147
                {
 
148
                        this.parent = parent;
 
149
                }
 
150
 
 
151
                public void onFailure( final Throwable throwable )
 
152
                {
 
153
                        this.parent.statusLabel.setText ("Failed to contact server!");
 
154
                        this.parent.statusLabel.setStyleName ("euca-greeting-error");
 
155
                }
 
156
 
 
157
                public void onSuccess( final Object o )
 
158
                {
 
159
                        List<VmTypeWeb> newVmTypeList = (List<VmTypeWeb>) o;
 
160
                        this.parent.VmTypeList = newVmTypeList;
 
161
                        this.parent.rebuildTable(); 
 
162
                }
 
163
        }
 
164
 
 
165
        class SaveCallback implements AsyncCallback, ClickListener {
 
166
 
 
167
                private VmTypeTable parent;
 
168
 
 
169
                SaveCallback( final VmTypeTable parent )
 
170
                {
 
171
                        this.parent = parent;
 
172
                }
 
173
 
 
174
                public void onClick( final Widget widget )
 
175
                {
 
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 );
 
180
                }
 
181
 
 
182
                public void onFailure( final Throwable throwable )
 
183
                {
 
184
                        this.parent.statusLabel.setText ("Failed to save! (Make sure values in each column are ordered.)");
 
185
                        this.parent.statusLabel.setStyleName ("euca-greeting-error");
 
186
                }
 
187
 
 
188
                public void onSuccess( final Object o )
 
189
                {
 
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
 
194
                }
 
195
        }
 
196
}