Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c634862bace6a0224487e4ad592c5a8e0d2c8dc0 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.utils;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.te.ui.forms.FormLayoutFactory;
import org.eclipse.tcf.te.ui.interfaces.IOptionListener;
import org.eclipse.tcf.te.ui.interfaces.ISearchMatcher;
import org.eclipse.tcf.te.ui.interfaces.ISearchable;
import org.eclipse.tcf.te.ui.nls.Messages;
import org.eclipse.tcf.te.ui.search.TreeViewerSearchDialog;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.events.IExpansionListener;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.Section;

/**
 * A utility searchable class that could combine several searchable objects and thus
 * divide a complex searchable into several simple ones.
 */
public abstract class CompositeSearchable implements ISearchable {
	// The delegating searchables
	private ISearchable[] searchables;

	/**
	 * Constructor.
	 */
	public CompositeSearchable() {
		super();
	}

	/**
	 * Set the delegating searchable objects.
	 * <p>
	 * This method needs to be called before {@link #createCommonPart(Composite)} is called!
	 *
	 * @param searchables Delegating searchable objects.
	 */
	public void setSearchables(ISearchable... searchables) {
		Assert.isNotNull(searchables);
		this.searchables = searchables;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#createCommonPart(org.eclipse.tcf.te.ui.search.TreeViewerSearchDialog, org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createCommonPart(TreeViewerSearchDialog dialog, Composite parent) {
		Assert.isNotNull(searchables, "setSearchables must be called before!"); //$NON-NLS-1$
		for (ISearchable searchable : searchables) {
			searchable.createCommonPart(dialog, parent);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#createAdvancedPart(org.eclipse.tcf.te.ui.search.TreeViewerSearchDialog, org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createAdvancedPart(TreeViewerSearchDialog dialog, Composite parent) {
		Section section = new Section(parent, ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT);
		section.setText(Messages.TreeViewerSearchDialog_AdvancedOptions);
		section.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		section.setLayoutData(layoutData);

		final Composite advancedPart = new Composite(section, SWT.NONE);
		advancedPart.setLayout(new GridLayout());
		advancedPart.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		advancedPart.setBackground(section.getBackground());
		section.setClient(advancedPart);

		section.addExpansionListener(new IExpansionListener(){
			@Override
            public void expansionStateChanging(ExpansionEvent e) {
            }

			@Override
            public void expansionStateChanged(ExpansionEvent e) {
				boolean state = e.getState();
				int client_height = advancedPart.getSize().y;
				Shell shell = advancedPart.getShell();
				Point p = shell.getSize();
				p.y = state ? p.y + client_height : p.y - client_height;
				shell.setSize(p.x, p.y);
            }});
		for(ISearchable searchable : searchables) {
			searchable.createAdvancedPart(dialog, advancedPart);
		}
	}

	/**
	 * A composite matcher that could combine several simple matchers
	 * into a complex matcher object.
	 */
	static class CompositeMatcher implements ISearchMatcher {
		// The delegating matchers.
		private ISearchMatcher[] matchers;
		/**
		 * The constructors.
		 */
		public CompositeMatcher(ISearchMatcher[] matchers) {
			Assert.isNotNull(matchers);
			this.matchers = matchers;
		}

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.tcf.te.ui.interfaces.ISearchMatcher#match(java.lang.Object)
		 */
		@Override
        public boolean match(Object element) {
			for(ISearchMatcher matcher : matchers) {
				if(!matcher.match(element)) {
					return false;
				}
			}
	        return true;
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#getMatcher()
	 */
	@Override
	public ISearchMatcher getMatcher() {
		ISearchMatcher[] matchers = new ISearchMatcher[searchables.length];
		for (int i = 0; i < searchables.length; i++) {
			matchers[i] = searchables[i].getMatcher();
		}
		return new CompositeMatcher(matchers);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#isInputValid()
	 */
	@Override
	public boolean isInputValid() {
		boolean valid = true;
		for(ISearchable searchable : searchables) {
			valid &= searchable.isInputValid();
		}
		return valid;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#addOptionListener(org.eclipse.tcf.te.ui.interfaces.IOptionListener)
	 */
	@Override
	public void addOptionListener(IOptionListener listener) {
		for(ISearchable searchable : searchables) {
			searchable.addOptionListener(listener);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#restoreValues(org.eclipse.jface.dialogs.IDialogSettings)
	 */
	@Override
    public void restoreValues(IDialogSettings settings) {
		for(ISearchable searchable : searchables) {
			searchable.restoreValues(settings);
		}
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#persistValues(org.eclipse.jface.dialogs.IDialogSettings)
	 */
	@Override
    public void persistValues(IDialogSettings settings) {
		for(ISearchable searchable : searchables) {
			searchable.persistValues(settings);
		}
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#removeOptionListener(org.eclipse.tcf.te.ui.interfaces.IOptionListener)
	 */
	@Override
	public void removeOptionListener(IOptionListener listener) {
		for(ISearchable searchable : searchables) {
			searchable.removeOptionListener(listener);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#getPreferredSize()
	 */
	@Override
    public Point getPreferredSize() {
		Point size = null;
		for(ISearchable searchable : searchables) {
			Point prefSize = searchable.getPreferredSize();
			if(prefSize != null) {
				if(size == null)
					size = new Point(0, 0);
				size.x = Math.max(size.x, prefSize.x);
				size.y = size.y + prefSize.y;
			}
		}
		return size;
    }
}

Back to the top