Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6894685ee8a7e9d98d3778eb1bf7bf56f4c694af (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2007, 2011 Intel Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Intel Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.newui;

import java.util.ArrayList;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.internal.ui.newui.Messages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * This class is intended to handle
 *
 * When new propertypage is created, it should request
 * project description by method
 * getProjectDescription()
 * This method, in addition, registers page in list.
 *
 * While page is active, it can change this description
 * but should not set it, to avoid inconsistency.
 *
 * When page's "performOK" called, it should call
 * manager's  method
 * performOk()
 *
 * Registered pages can call {@link CDTPropertyManager#remove(Object)}
 * to explicitly remove themselves from this manager.
 *
 * In addition, there are utility methods for pages:
 * getPagesCount()
 * getPage()
 * isSaveDone()
 *
 * @noextend This class is not intended to be subclassed by clients.
 */
public class CDTPropertyManager {

	private static ArrayList<Object> pages = new ArrayList<>();
	private static ICProjectDescription prjd = null;
	private static boolean saveDone = false;
	private static IProject project = null;
	private static DListener dListener = new DListener();

	public static ICProjectDescription getProjectDescription(PropertyPage p, IProject prj) {
		return get(p, prj);
	}

	public static ICProjectDescription getProjectDescription(Widget w, IProject prj) {
		return get(w, prj);
	}

	public static ICProjectDescription getProjectDescription(IProject prj) {
		return get(null, prj);
	}

	private static ICProjectDescription get(Object p, IProject prj) {
		// New session - clean static variables
		if (pages.size() == 0) {
			project = null;
			prjd = null;
			saveDone = false;
		}
		// Register new client
		if (p != null && !pages.contains(p)) {
			pages.add(p);
			if (p instanceof PropertyPage) {
				if (((PropertyPage) p).getControl() != null)
					((PropertyPage) p).getControl().addDisposeListener(dListener);
			} else if (p instanceof Widget) {
				((Widget) p).addDisposeListener(dListener);
			}
		}
		// Check that we are working with the same project
		if (project == null || !project.equals(prj)) {
			project = prj;
			prjd = null;
		}
		// obtain description if it's needed.
		if (prjd == null) {
			prjd = CoreModel.getDefault().getProjectDescription(prj);
		}
		return prjd;
	}

	/**
	 * Performs optimized (single-time) saving
	 * @param p - widget which calls this functionality
	 */
	public static void performOk(Object p) {
		if (saveDone)
			return;

		performOkForced(p);

		if (pages.size() == 0) {
			project = null;
			prjd = null;
			saveDone = false;
		}
	}

	public static void performCancel(Object p) {
		saveDone = true;

		if (pages.size() == 0) {
			project = null;
			prjd = null;
			saveDone = false;
		}
	}

	/**
	 * Explicitly remove the page from this CDTPropertyManager
	 * @param p
	 * @since 5.1
	 */
	public static void remove(Object p) {
		DListener.dispose(p);
	}

	/**
	 * Performs mandatory saving
	 * @param p
	 */
	public static void performOkForced(Object p) {
		saveDone = true;
		try {
			CoreModel.getDefault().setProjectDescription(project, prjd);
		} catch (CoreException e) {
			CUIPlugin.logError(Messages.AbstractPage_11 + e.getLocalizedMessage());
		}

		if (pages.size() == 0) {
			project = null;
			prjd = null;
			saveDone = false;
		}
	}

	// pages utilities
	public static boolean isSaveDone() {
		return saveDone;
	}

	public static int getPagesCount() {
		return pages.size();
	}

	public static Object getPage(int index) {
		return pages.get(index);
	}

	// Removes disposed items from list
	static class DListener implements DisposeListener {
		public static void dispose(Object w) {
			if (pages.contains(w)) { // Widget ?
				pages.remove(w);
			} else { // Property Page ?
				for (Object ob : pages) {
					if (ob != null && ob instanceof PropertyPage) {
						if (((PropertyPage) ob).getControl().equals(w)) {
							pages.remove(ob);
							break;
						}
					}
				}
			}

			if (pages.isEmpty()) {
				saveDone = true;
				project = null;
				prjd = null;
				saveDone = false;
			}
		}

		@Override
		public void widgetDisposed(DisposeEvent e) {
			dispose(e.widget);
		}
	}

}

Back to the top