summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.console/src/org/gluster/storage/management/console/DeviceTableLabelProvider.java
blob: 073b022f9da608a4a7cbf3498d0b3a6bcd0c46c6 (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
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
/*******************************************************************************
 * Copyright (c) 2006-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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.console;

import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.gluster.storage.management.console.utils.GUIHelper;
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;


public class DeviceTableLabelProvider extends LabelProvider implements ITableLabelProvider {

	private GUIHelper guiHelper = GUIHelper.getInstance();
	private GlusterDataModelManager modelManager = GlusterDataModelManager.getInstance();
	public enum DEVICE_COLUMN_INDICES {
		DISK, PARTITION, FREE_SPACE, SPACE_IN_USE, STATUS
	};

	FontRegistry registry = new FontRegistry();

	public DeviceTableLabelProvider() {
	}

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

		Device device = (Device) element;
		if (columnIndex == DEVICE_COLUMN_INDICES.STATUS.ordinal()) {
			DEVICE_STATUS status = device.getStatus();
			
			if (status == null) {
				if (element instanceof Partition) {
					if (columnIndex == DEVICE_COLUMN_INDICES.STATUS.ordinal()) {
						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(modelManager.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;
	}

	@Override
	public String getText(Object element) {
		return super.getText(element);
	}
	
	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) {
		
		if (element == null) {
			return "";
		}
		
		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.getQualifiedName();
			} 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.getQualifiedName();
			} else {
				return "";
			}
		} else if (columnIndex == DEVICE_COLUMN_INDICES.STATUS.ordinal()) {
			if(device.isUninitialized()) {
				return "";
			}
			if(modelManager.isDeviceUsed(device)) {
				return "In Use";
			} else {
				return device.getStatusStr();
			}
		} else {
			return "";
		}
	}
}