Skip to main content
summaryrefslogtreecommitdiffstats
blob: 993b8f88f4cb789a76377c08edb8d610785b08be (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
/*******************************************************************************
 * Copyright (c) 2006 Versant. 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: Versant. - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.ui.internal.details;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.PageBook;

/**
 * A wrapper for a <code>PageBook</code> with convenience methods.
 */
public class PageBookManager extends PageBook {

	private Map<Object, Composite> pageRecords;
	private Object active;
	private PageBook pageBook;
	private Composite defaultComposite;

	/**
	 * A <code>PageBookManager<code> is a wrapper for a <code>PageBook</code>. It provides
	 * convenience methods to add, remove, activate and inactive pages in the internal <code>PageBook</code>.
	 * If a page gets deactivated, the manager activates a default page.
	 *
	 * @param parent The parent composite to this manager
	 * @param aDefaultLabel The label on the the default page.
	 */
	public PageBookManager(Composite parent, String aDefaultLabel) {
		super(parent, SWT.NULL);
		this.setLayout(new FillLayout());
		this.pageRecords = new HashMap<Object, Composite>();
		this.pageBook = new PageBook(this, SWT.NONE);
		this.defaultComposite = new DefaultComposite(pageBook, aDefaultLabel);
		this.pageBook.showPage(this.defaultComposite);
	}

	/**
	 * @param anObj Activates (flips to top in the <code>PageBook</code>) the associated <code>Composite</code>
	 * for the given <code>Object</code>. Activates the <code>DefaultComposite</code> if there is no association
	 * for the given <code>Object</code>.
	 * @return Return false if there is no <code>Composite</code> association to the given <code>Object</code> or
	 * the associated <code>Composite</code> is already active.
	 */
	public boolean activate(Object anObj) {
		if(anObj != null && !anObj.equals(this.active) && this.pageRecords.containsKey(anObj)) {
			Composite composite = this.pageRecords.get(anObj);
			this.pageBook.showPage(composite);
			this.active = anObj;
			return true;
		} else if((anObj == null || !anObj.equals(this.active)) && !this.pageRecords.containsKey(anObj)) {
			this.pageBook.showPage(this.defaultComposite);
			this.active = null;
		}
		return false;
	}

	/**
	 * @param anObj
	 * @return Returns true if the associated <code>Composite</code> has been deactivated.
	 * Returns false if there is no <code>Composite</code> for the given <code>Object</code>
	 * or the <code>Composite</code> is already inactive.
	 */
	public boolean deactivate(Object anObj) {
		if(anObj.equals(this.active)) {
			this.pageBook.showPage(this.defaultComposite);
			this.active = null;
			return true;
		} else {
			return false;
		}
	}

	/**
     * Associates the specified <code>Object</code> with the specified <code>Composite</code>
     * in this <code>PageBookManager</code>.
     * If this manager previously contained a mapping for the <code>Object</code>, the old
     * <code>Composite</code> is replaced by the specified <code>Composite</code>.
     *
	 * @param anObj <code>Object</code> with which the specified <code>Composite</code> is to be associated.
	 * @param aComposite <code>Composite</code> to be associated with the specified <code>Object</code>.
	 *
     * @return previous <code>Composite</code> associated with specified <code>Object</code>,
     * 		   or <tt>null</tt> if there was no mapping for <code>Object</code>.
	 */
	public Composite put(Object anObj, Composite aComposite) {
		Composite composite = this.pageRecords.put(anObj, aComposite);
		this.activate(anObj);
		return composite;
	}

    /**
     * Removes the mapping for this <code>Object</code> from this pagebookmanager if it is present.
     *
     * @param anObj <code>Object</code> whose mapping is to be removed from this <code>PageBookManager</code>.
     * @return previous <code>Composite</code> associated with specified <code>Object</code>.
     */
	public Composite remove(Object anObj) {
		if(anObj.equals(this.active)) {
			this.active = null;
			this.pageBook.showPage(this.defaultComposite);
		}
		return this.pageRecords.remove(anObj);
	}

	/**
	 * @param anObj whose presence in this map is to be tested
	 * @return true if this <code>PageBookManager</code> contains a mapping for the
	 * 		   given <code>Object</code>. False otherwise.
	 */
	public boolean contains(Object anObj) {
		return this.pageRecords.containsKey(anObj);
	}

	public Composite get(Object key) {
		return pageRecords.get(key);
	}

	/**
	 * @return The currently active composite
	 */
	public Composite getActive() {
		if(this.active == null) {
			return this.defaultComposite;
		} else {
			return this.pageRecords.get(this.active);
		}
	}

	/**
	 * @return The internal <code>PageBook</code> of this <code>PageBookManager</code>.
	 * <code>Composite</code>s which should work with this manager needs to be created
	 * with this <code>Composite</code> as their parent composite.
	 */
	public Composite getComposite() {
		return this.pageBook;
	}

	// a default composite which is show if the active composite
	// gets deactivated
	private class DefaultComposite extends Composite {
		public DefaultComposite(Composite aParent, String aDefaultLabel) {
			super(aParent, SWT.NONE);
			setLayout(new FillLayout(SWT.VERTICAL));
			Label label = new Label(this, SWT.LEFT);
			label.setText(aDefaultLabel);
		}
	}

	@Override
	public void dispose() {
		super.dispose();
		for (Composite pane : this.pageRecords.values()) {
			pane.dispose();
		}
	}
}

Back to the top