summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.console/src/org/gluster/storage/management/console/ServerDiskTableLabelProvider.java
blob: aa4c23c4582443e1c7fb4478cd980238217a28f6 (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * Gluster Management Console is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
 * for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.console;

import org.eclipse.swt.graphics.Image;
import org.gluster.storage.management.console.utils.GUIHelper;
import org.gluster.storage.management.console.views.pages.ServerDisksPage.SERVER_DISK_TABLE_COLUMN_INDICES;
import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import org.gluster.storage.management.core.model.Device;
import org.gluster.storage.management.core.model.Disk;
import org.gluster.storage.management.core.model.Partition;
import org.gluster.storage.management.core.model.Device.DEVICE_STATUS;
import org.gluster.storage.management.core.utils.NumberUtil;

import static org.gluster.storage.management.console.DeviceTableLabelProvider.DEVICE_COLUMN_INDICES;


public class ServerDiskTableLabelProvider extends TableLabelProviderAdapter {
	private GUIHelper guiHelper = GUIHelper.getInstance();
	private GlusterDataModelManager glusterDataModelManager = GlusterDataModelManager.getInstance();

	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		if (!(element instanceof Device)) {
			return null;
		}

		Device device = (Device) element;
		if (columnIndex == SERVER_DISK_TABLE_COLUMN_INDICES.STATUS.ordinal()) {
			DEVICE_STATUS status = device.getStatus();
			
			if (status == null) {
				return  null;
			}
			
			if(element instanceof Disk && ((Disk)element).hasPartitions()) {
				// disk has partitions. so don't show status image at disk level.
				return null;
			}

			switch (status) {
			case INITIALIZED:
				if(glusterDataModelManager.isDeviceUsed(device)) {
					return guiHelper.getImage(IImageKeys.DISK_IN_USE_16x16);
				} else {
					return guiHelper.getImage(IImageKeys.DISK_AVAILABLE_16x16);
				}
			case IO_ERROR:
				return guiHelper.getImage(IImageKeys.IO_ERROR_16x16);
			case UNINITIALIZED:
				return guiHelper.getImage(IImageKeys.DISK_UNINITIALIZED_16x16);
			case INITIALIZING:
				return guiHelper.getImage(IImageKeys.DISK_INITIALIZING_16x16);
			default:
				throw new GlusterRuntimeException("Invalid disk status [" + status + "]");
			}
		}

		return null;
	}

	private String getDeviceFreeSpace(Device device) {
		if (device.hasErrors() || device.isUninitialized()) {
			return "NA";
		} else {
			return NumberUtil.formatNumber((device.getFreeSpace() / 1024));
		}
	}

	private String getTotalDeviceSpace(Device device) {
		if (device.hasErrors() || device.isUninitialized()) {
			return "NA";
		} else {
			return NumberUtil.formatNumber((device.getSpace() / 1024));
		}
	}

	public String getColumnText(Object element, int columnIndex) {
		Device device = (Device) element;
		if (columnIndex == DEVICE_COLUMN_INDICES.DISK.ordinal()) {
			// show value in "disk" column only if it's a disk
			if (device instanceof Disk) {
				return device.getName();
			} else {
				return "";
			}
		} 
		
		if(element instanceof Disk && ((Disk)element).hasPartitions()) {
			// disk has partitions. so don't show any other details
			return "";
		}

		if (columnIndex == DEVICE_COLUMN_INDICES.FREE_SPACE.ordinal()) {
			return "" + getDeviceFreeSpace(device);
		} else if (columnIndex == DEVICE_COLUMN_INDICES.SPACE_IN_USE.ordinal()) {
			return "" + getTotalDeviceSpace(device);
		} else if (columnIndex == DEVICE_COLUMN_INDICES.PARTITION.ordinal()) {
			if (device instanceof Partition) {
				return device.getName();
			} else {
				return "";
			}
		} else if (columnIndex == DEVICE_COLUMN_INDICES.STATUS.ordinal()) {
			if(device.isUninitialized()) {
				return "";
			}
			if(glusterDataModelManager.isDeviceUsed(device)) {
				return "In Use";
			} else {
				return device.getStatusStr();
			}
		} else {
			return "";
		}
	}	
	
}