Skip to main content
summaryrefslogtreecommitdiffstats
blob: 37d9bee62dc5b662df9b5e683ff2e627a3f1d2f9 (plain) (blame)
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
161
162
163
164
165
166
/*******************************************************************************
 * Copyright (c) 2009, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Mike Evans      - Fix for Bug 283136
 *******************************************************************************/

package org.eclipse.ui.intro.contentproviders;

import java.io.PrintWriter;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.internal.intro.impl.Messages;
import org.eclipse.ui.internal.intro.impl.util.ReopenUtil;
import org.eclipse.ui.intro.IIntroSite;
import org.eclipse.ui.intro.config.IIntroAction;
import org.eclipse.ui.intro.config.IIntroContentProvider;
import org.eclipse.ui.intro.config.IIntroContentProviderSite;
import org.eclipse.ui.PlatformUI;
/**
 *
 * Class which contributes a checkbox to an intro page which allows welcome to show 
 * on startup. If the checkbox is checked the home page of intro will be shown the next 
 * time the Eclipse application starts up. This class may be subclassed to override 
 * the text for the checkbox label.
 * 
 * Implements the IIntroContentProvider to create the checkbox ui, and the
 * org.eclipse.ui.intro.config.IIntroAction interface for handling checkbox click events.
 * 
 * @since 3.3
 */
public class AlwaysWelcomeCheckbox implements IIntroContentProvider,IIntroAction {

	public final static String ALWAYS_SHOW_INTRO = "alwaysShowIntro"; //$NON-NLS-1$
	
	private boolean disposed = false;
	
	/**
	 * Override this method to change the default text used for the checkbox
	 * 
	 * @return String label for the checkbox 
	 * 
	 * @since 3.3
	 * 
	 */
	protected String getText()
	{
		// Default text
		return Messages.AlwaysWelcomeCheckbox_Text;
	}
	

	@Override
	public void createContent(String id, PrintWriter out) {

		boolean alwaysShowIntro = getAlwaysShowIntroPref();

		// Use an IIntroAction url that points back to this class - 
		// particularly invoking run().
		// This url is 'activated' using the onClick event.
		out.print("<div id=\""+id+"\"><input type=\"checkbox\" "+  	//$NON-NLS-1$//$NON-NLS-2$
				"onClick=window.location="+ 						//$NON-NLS-1$
				"\"http://org.eclipse.ui.intro/runAction?"+ 		//$NON-NLS-1$
				"pluginId=org.eclipse.ui.intro&"+ 					//$NON-NLS-1$
				"class="+this.getClass().getName()+"\" ");			//$NON-NLS-1$ //$NON-NLS-2$
		
		if (alwaysShowIntro)
		{
			out.print(" checked=\"checked\""); //$NON-NLS-1$
			
	        PlatformUI.getPreferenceStore().setValue(
	        		IWorkbenchPreferenceConstants.SHOW_INTRO, alwaysShowIntro);	
		}
		
		out.println("/>"+getText()+"</div>");  //$NON-NLS-1$//$NON-NLS-2$
	}


	@Override
	public void createContent(String id, Composite parent, FormToolkit toolkit) {
		if (disposed)
			return;

        boolean alwaysShowIntro = getAlwaysShowIntroPref();

		Button checkBox = new Button(parent,SWT.CHECK);
		toolkit.adapt((Control)checkBox,false,false);
		
		checkBox.setText(getText());
		checkBox.setSelection(alwaysShowIntro);
		checkBox.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent e){
				reverseShowIntroState();
			}
		});
		
		if (alwaysShowIntro)
			PlatformUI.getPreferenceStore().setValue(
        		IWorkbenchPreferenceConstants.SHOW_INTRO, alwaysShowIntro);
	}

	@Override
	public void dispose() {
		disposed = true;
	}

	@Override
	public void init(IIntroContentProviderSite site) {
	}

	/**
	 * Method called when box is clicked in html (swt is handled with a
	 * SelectionAdapter - both methods call reverseShowIntroState())
	 */
	@Override
	public void run(IIntroSite site, Properties params) {
		reverseShowIntroState();
	}
	
	/**
	 * Method reverses preference ALWAYS_SHOW_INTRO due to checkbox selection change
	 * 
	 */
	private void reverseShowIntroState()
	{
		// Retrieve current state of IUniversalIntroConst.ALWAYS_SHOW_INTRO, change it, and save it back
		// to both ALWAYS_SHOW_INTRO and SHOW_INTRO
        boolean alwaysShowIntro = !getAlwaysShowIntroPref();
        
        // local preference store
        setAlwaysShowIntroPref(alwaysShowIntro);
        
        // workbench preference store
        PlatformUI.getPreferenceStore().setValue(
        		IWorkbenchPreferenceConstants.SHOW_INTRO, alwaysShowIntro);	

	}
	
	public boolean getAlwaysShowIntroPref()
	{
		// If uninitialized, we will default to true (box will be checked)
		if (!ReopenUtil.isReopenPreferenceInitialized()) {
			setAlwaysShowIntroPref(true);
		}
		return ReopenUtil.isReopenPreference();
	}
	
	public void setAlwaysShowIntroPref(boolean val)
	{
		ReopenUtil.setReopenPreference(val);
	}
}

Back to the top