1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workspace.tracker.internal.contributions;
14 import java.io.IOException;
15 import java.nio.file.FileStore;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
19 import org.eclipse.e4.ui.model.application.ui.menu.MToolControl;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.util.IPropertyChangeListener;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.graphics.Color;
28 import org.eclipse.swt.graphics.GC;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.graphics.Rectangle;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.widgets.Menu;
36 import org.eclipse.swt.widgets.Shell;
37 import org.simantics.filesystem.services.sizetracker.SizeTracker;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The Heap Status control, which shows the heap usage statistics in the window trim.
46 public class WorkspaceSizeTrackerTrim extends Composite {
48 private Logger logger = LoggerFactory.getLogger(WorkspaceSizeTrackerTrim.class);
50 private MToolControl toolControl;
51 private SizeTracker sizeTracker;
52 private IPreferenceStore prefStore;
55 * How many MB of free disk space means we are low on disk space?
57 private long lowSpaceThreshold = IWorkspaceSizeTrackerConstants.DEFAULT_LOW_SPACE_THRESHOLD;
58 private boolean highlightLowSpace = IWorkspaceSizeTrackerConstants.DEFAULT_HIGHLIGHT_LOW_SPACE;
59 private int updateInterval = IWorkspaceSizeTrackerConstants.DEFAULT_UPDATE_INTERVAL;
61 private Color bgCol, usedSpaceCol, lowSpaceCol, topLeftCol, bottomRightCol, sepCol, textCol;
62 @SuppressWarnings("unused")
63 private Color markCol;
65 private String storeName;
66 private long totalSpace;
67 private long availableSpace;
68 private long usedSpace;
69 private long prevTotalSpace = -1L;
70 private long prevAvailableSpace = -1L;
71 private long prevUsedSpace = -1L;
73 private boolean hasChanged;
74 private long mark = -1;
76 private boolean updateTooltip = false;
78 private final Runnable timer = new Runnable() {
90 getDisplay().timerExec(updateInterval, this);
95 private final IPropertyChangeListener prefListener = event -> {
96 if (IWorkspaceSizeTrackerConstants.PREF_UPDATE_INTERVAL.equals(event.getProperty())) {
97 setUpdateIntervalInMS(prefStore.getInt(IWorkspaceSizeTrackerConstants.PREF_UPDATE_INTERVAL));
98 } else if (IWorkspaceSizeTrackerConstants.PREF_HIGHLIGHT_LOW_SPACE.equals(event.getProperty())) {
99 highlightLowSpace = prefStore.getBoolean(IWorkspaceSizeTrackerConstants.PREF_HIGHLIGHT_LOW_SPACE);
101 } else if (IWorkspaceSizeTrackerConstants.PREF_LOW_SPACE_THRESHOLD.equals(event.getProperty())) {
102 lowSpaceThreshold = prefStore.getLong(IWorkspaceSizeTrackerConstants.PREF_LOW_SPACE_THRESHOLD);
104 } else if (IWorkspaceSizeTrackerConstants.PREF_SHOW_MONITOR.equals(event.getProperty())) {
105 boolean show = prefStore.getBoolean(IWorkspaceSizeTrackerConstants.PREF_SHOW_MONITOR);
112 * Creates a new heap status control with the given parent, and using
113 * the given preference store to obtain settings such as the refresh
117 * @param parent the parent composite
118 * @param sizeTracker the workspace sizeTracker service
119 * @param prefStore the preference store
121 public WorkspaceSizeTrackerTrim(Composite parent, MToolControl toolControl, SizeTracker sizeTracker, IPreferenceStore prefStore) {
122 super(parent, SWT.NONE);
123 this.toolControl = toolControl;
124 this.sizeTracker = sizeTracker;
126 this.prefStore = prefStore;
127 prefStore.addPropertyChangeListener(prefListener);
129 setUpdateIntervalInMS(prefStore.getInt(IWorkspaceSizeTrackerConstants.PREF_UPDATE_INTERVAL));
130 highlightLowSpace = prefStore.getBoolean(IWorkspaceSizeTrackerConstants.PREF_HIGHLIGHT_LOW_SPACE);
131 lowSpaceThreshold = prefStore.getLong(IWorkspaceSizeTrackerConstants.PREF_LOW_SPACE_THRESHOLD);
133 Display display = getDisplay();
134 usedSpaceCol = display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
135 lowSpaceCol = new Color(display, 255, 70, 70); // medium red
136 bgCol = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
137 sepCol = topLeftCol = display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
138 bottomRightCol = display.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);
139 markCol = textCol = display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND);
143 Listener listener = event -> {
144 switch (event.type) {
149 if (event.widget == WorkspaceSizeTrackerTrim.this) {
150 paintComposite(event.gc);
154 if (event.button == 1) {
155 if (event.widget == WorkspaceSizeTrackerTrim.this) {
161 WorkspaceSizeTrackerTrim.this.updateTooltip = true;
165 if (event.widget == WorkspaceSizeTrackerTrim.this) {
166 WorkspaceSizeTrackerTrim.this.updateTooltip = false;
171 addListener(SWT.Dispose, listener);
172 addListener(SWT.MouseDown, listener);
173 addListener(SWT.Paint, listener);
174 addListener(SWT.MouseEnter, listener);
175 addListener(SWT.MouseExit, listener);
177 // make sure stats are updated before first paint
180 getDisplay().asyncExec(() -> {
182 getDisplay().timerExec(updateInterval, timer);
188 public void setBackground(Color color) {
193 public void setForeground(Color color) {
195 markCol = textCol = getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
197 markCol = textCol = color;
202 public Color getForeground() {
203 if (usedSpaceCol != null) {
206 return getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
209 private void setUpdateIntervalInMS(int interval) {
210 updateInterval = Math.max(100, interval);
213 private void doDispose() {
214 prefStore.removePropertyChangeListener(prefListener);
215 if (lowSpaceCol != null) {
216 lowSpaceCol.dispose();
221 public Point computeSize(int wHint, int hHint, boolean changed) {
222 GC gc = new GC(this);
223 Point p = gc.textExtent(Messages.WorkspaceSizeTrackerTrim_widthStr);
224 int height = p.y + 4;
226 return new Point(p.x, height);
230 * Creates the context menu
232 private void createContextMenu() {
233 MenuManager menuMgr = new MenuManager();
234 menuMgr.setRemoveAllWhenShown(true);
235 menuMgr.addMenuListener(menuMgr1 -> fillMenu(menuMgr1));
236 Menu menu = menuMgr.createContextMenu(this);
240 private void fillMenu(IMenuManager menuMgr) {
241 menuMgr.add(new SetMarkAction());
242 menuMgr.add(new ClearMarkAction());
243 menuMgr.add(new CloseSizeTrackerTrimAction());
247 * Sets the mark to the current usedMem level.
249 private void setMark() {
250 safeUpdateStats(); // get up-to-date stats before taking the mark
259 private void clearMark() {
265 private void paintComposite(GC gc) {
266 paintCompositeMaxUnknown(gc);
269 private void paintCompositeMaxUnknown(GC gc) {
270 Rectangle rect = getClientArea();
275 int sw = w - 1; // status width
276 long storeUsedSpace = totalSpace - availableSpace;
277 int uw = (int) (sw * storeUsedSpace / totalSpace); // used space width
278 int ux = x + 1 + uw; // used space right edge
280 gc.setBackground(bgCol);
282 gc.fillRectangle(rect);
284 boolean lowOnSpace = false;
285 if (highlightLowSpace) {
286 lowOnSpace = (1024L*1024L*lowSpaceThreshold) >= availableSpace;
289 gc.setForeground(sepCol);
290 gc.drawLine(ux, y, ux, y + h);
291 gc.setForeground(topLeftCol);
292 gc.drawLine(x, y, x+w, y);
293 gc.drawLine(x, y, x, y+h);
294 gc.setForeground(bottomRightCol);
295 gc.drawLine(x+w-1, y, x+w-1, y+h);
296 gc.drawLine(x, y+h-1, x+w, y+h-1);
298 gc.setBackground(lowOnSpace ? lowSpaceCol : usedSpaceCol);
299 gc.fillRectangle(x + 1, y + 1, uw, h - 2);
301 String s = NLS.bind(Messages.WorkspaceSizeTrackerTrim_status, convertToSizeString(usedSpace), convertToSizeString(availableSpace));
302 Point p = gc.textExtent(s);
303 int sx = (rect.width - 15 - p.x) / 2 + rect.x + 1;
304 int sy = (rect.height - 2 - p.y) / 2 + rect.y + 1;
305 gc.setForeground(textCol);
306 gc.drawString(s, sx, sy, true);
309 private void safeUpdateStats() {
312 } catch (IOException e) {
313 logger.error("Failed to update workspace size statistics.", e);
317 private void updateStats() throws IOException {
318 Path path = sizeTracker.path();
319 FileStore store = Files.getFileStore(path);
321 storeName = store.toString();
322 totalSpace = store.getTotalSpace();
323 availableSpace = store.getUsableSpace();
324 usedSpace = sizeTracker.size();
326 if (convertToMeg(prevTotalSpace) != convertToMeg(totalSpace)) {
327 prevTotalSpace = totalSpace;
328 this.hasChanged = true;
330 if (prevAvailableSpace != availableSpace) {
331 prevAvailableSpace = availableSpace;
332 this.hasChanged = true;
334 if (convertToMeg(prevUsedSpace) != convertToMeg(usedSpace)) {
335 prevUsedSpace = usedSpace;
336 this.hasChanged = true;
340 private void updateToolTip() {
341 String usedStr = convertToSizeString(usedSpace);
342 String availableStr = convertToSizeString(availableSpace);
343 String totalStr = convertToSizeString(totalSpace);
344 String markStr = mark == -1 ? Messages.WorkspaceSizeTrackerTrim_noMark : convertToSizeString(mark);
345 String toolTip = NLS.bind(Messages.WorkspaceSizeTrackerTrim_memoryToolTip, new Object[] { usedStr, storeName, availableStr, totalStr, markStr });
346 if (!toolTip.equals(getToolTipText())) {
347 setToolTipText(toolTip);
352 * Converts the given number of bytes to a printable number of megabytes (rounded up).
354 private String convertToSizeString(long numBytes) {
355 long megs = convertToMeg(numBytes);
357 double megsd = (double) megs;
358 long gigs = (long) Math.floor(megsd / 1024.0);
359 long decimals = (long) (megsd - gigs*1024);
360 decimals = (decimals + 5) / 10;
361 return NLS.bind(Messages.WorkspaceSizeTrackerTrim_gig, new Long(gigs), new Long(decimals));
363 return NLS.bind(Messages.WorkspaceSizeTrackerTrim_meg, new Long(megs));
368 * Converts the given number of bytes to the corresponding number of megabytes (rounded up).
370 private long convertToMeg(long numBytes) {
371 return (numBytes + (512 * 1024)) / (1024 * 1024);
374 class SetMarkAction extends Action {
376 super(Messages.SetMarkAction_text);
385 class ClearMarkAction extends Action {
387 super(Messages.ClearMarkAction_text);
396 class CloseSizeTrackerTrimAction extends Action{
397 CloseSizeTrackerTrimAction(){
398 super(Messages.WorkspaceSizeTrackerTrim_close);
407 private void showTracker(boolean show) {
408 if (toolControl.isToBeRendered() != show) {
409 Object widget = toolControl.getWidget();
410 Shell shell = widget instanceof Control ? ((Control) widget).getShell() : null;
411 toolControl.setToBeRendered(show);
413 shell.layout(null, SWT.ALL | SWT.CHANGED | SWT.DEFER);
414 prefStore.setValue(IWorkspaceSizeTrackerConstants.PREF_SHOW_MONITOR, show);