summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.console/src/com/gluster/storage/management/console/ApplicationWorkbenchAdvisor.java
blob: 8d6c81dabe51e5d49f8afbd11decaedd8e2400f5 (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
/*******************************************************************************
 * 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 com.gluster.storage.management.console;

import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import com.gluster.storage.management.console.jobs.DataSyncJob;
import com.gluster.storage.management.console.preferences.PreferenceConstants;

/**
 * This workbench advisor creates the window advisor, and specifies
 * the perspective id for the initial window.
 */
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
	private Job syncJob;
	private static final IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
	private long JOB_INTERVAL = preferenceStore.getLong(PreferenceConstants.P_DATA_SYNC_INTERVAL) * 1000;
	private IPropertyChangeListener propertyChangeListener;
	
    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        return new ApplicationWorkbenchWindowAdvisor(configurer);
    }

	public String getInitialWindowPerspectiveId() {
		return Perspective.ID;
	} 
	
	@Override
	public void initialize(IWorkbenchConfigurer configurer) {
		super.initialize(configurer);
		configurer.setSaveAndRestore(false); // we don't need save/restore as of now
		
		createPropertyChangeListener();
		preferenceStore.addPropertyChangeListener(propertyChangeListener);
	}
	
	private void createPropertyChangeListener() {
		propertyChangeListener = new IPropertyChangeListener() {
			
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if(event.getProperty().equals(PreferenceConstants.P_DATA_SYNC_INTERVAL)) {
					JOB_INTERVAL = (Integer)event.getNewValue() * 1000L;
				}
			}
		};
	}

	@Override
	public void postStartup() {
		super.postStartup();
		setupBackgroundJobs();
	}
	
	private void setupBackgroundJobs() {
		syncJob = new DataSyncJob("Retrieving Management Info");
		syncJob.schedule(JOB_INTERVAL);
		syncJob.addJobChangeListener(new JobChangeAdapter() {
			@Override
			public void done(IJobChangeEvent event) {
				super.done(event);
				
				// job done. schedule again after the pre-defined interval
				syncJob.schedule(JOB_INTERVAL);
			}
		});
	}
	
	@Override
	public boolean preShutdown() {
		return syncJob.cancel();
	}
}