001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.util;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014:
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.Preferences;
017: import org.eclipse.jface.dialogs.Dialog;
018: import org.eclipse.jface.viewers.CheckStateChangedEvent;
019: import org.eclipse.jface.viewers.CheckboxTreeViewer;
020: import org.eclipse.jface.viewers.ICheckStateListener;
021: import org.eclipse.jface.viewers.IStructuredContentProvider;
022: import org.eclipse.jface.viewers.ITreeContentProvider;
023: import org.eclipse.jface.viewers.LabelProvider;
024: import org.eclipse.jface.viewers.TreeViewer;
025: import org.eclipse.jface.wizard.WizardPage;
026: import org.eclipse.pde.core.plugin.IPluginBase;
027: import org.eclipse.pde.core.plugin.IPluginModelBase;
028: import org.eclipse.pde.core.plugin.PluginRegistry;
029: import org.eclipse.pde.internal.core.PDECore;
030: import org.eclipse.pde.internal.ui.IHelpContextIds;
031: import org.eclipse.pde.internal.ui.IPreferenceConstants;
032: import org.eclipse.pde.internal.ui.PDEPlugin;
033: import org.eclipse.pde.internal.ui.PDEPluginImages;
034: import org.eclipse.pde.internal.ui.PDEUIMessages;
035: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
036: import org.eclipse.swt.SWT;
037: import org.eclipse.swt.events.ModifyEvent;
038: import org.eclipse.swt.events.ModifyListener;
039: import org.eclipse.swt.events.SelectionAdapter;
040: import org.eclipse.swt.events.SelectionEvent;
041: import org.eclipse.swt.graphics.Image;
042: import org.eclipse.swt.layout.GridData;
043: import org.eclipse.swt.layout.GridLayout;
044: import org.eclipse.swt.widgets.Button;
045: import org.eclipse.swt.widgets.Composite;
046: import org.eclipse.swt.widgets.Label;
047: import org.eclipse.swt.widgets.Text;
048: import org.eclipse.ui.IWorkingSet;
049: import org.eclipse.ui.IWorkingSetManager;
050: import org.eclipse.ui.PlatformUI;
051: import org.eclipse.ui.dialogs.FilteredTree;
052: import org.eclipse.ui.dialogs.IWorkingSetPage;
053: import org.eclipse.ui.dialogs.PatternFilter;
054:
055: public class PluginWorkingSet extends WizardPage implements
056: IWorkingSetPage {
057:
058: class ContentProvider extends DefaultContentProvider implements
059: ITreeContentProvider {
060: public Object[] getElements(Object inputElement) {
061: return PluginRegistry.getAllModels();
062: }
063:
064: public Object[] getChildren(Object parentElement) {
065: return null;
066: }
067:
068: public Object getParent(Object element) {
069: return null;
070: }
071:
072: public boolean hasChildren(Object element) {
073: return false;
074: }
075: }
076:
077: class WorkingSetLabelProvider extends LabelProvider {
078:
079: Preferences pref = PDEPlugin.getDefault()
080: .getPluginPreferences();
081:
082: public WorkingSetLabelProvider() {
083: PDEPlugin.getDefault().getLabelProvider().connect(this );
084: }
085:
086: /* (non-Javadoc)
087: * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
088: */
089: public String getText(Object element) {
090: if (element instanceof IPluginModelBase) {
091: IPluginBase plugin = ((IPluginModelBase) element)
092: .getPluginBase();
093: String showType = pref
094: .getString(IPreferenceConstants.PROP_SHOW_OBJECTS);
095: if (showType.equals(IPreferenceConstants.VALUE_USE_IDS))
096: return plugin.getId();
097: return plugin.getTranslatedName();
098: }
099: return super .getText(element);
100: }
101:
102: /* (non-Javadoc)
103: * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
104: */
105: public Image getImage(Object element) {
106: return PDEPlugin.getDefault().getLabelProvider().getImage(
107: element);
108: }
109:
110: /* (non-Javadoc)
111: * @see org.eclipse.jface.viewers.LabelProvider#dispose()
112: */
113: public void dispose() {
114: super .dispose();
115: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
116: }
117:
118: }
119:
120: class CheckboxFilteredTree extends FilteredTree {
121:
122: public CheckboxFilteredTree(Composite parent, int treeStyle,
123: PatternFilter filter) {
124: super (parent, treeStyle, filter);
125: }
126:
127: protected TreeViewer doCreateTreeViewer(Composite parent,
128: int style) {
129: return new CheckboxTreeViewer(parent, style);
130: }
131:
132: public CheckboxTreeViewer getCheckboxTreeViewer() {
133: return (CheckboxTreeViewer) getViewer();
134: }
135:
136: }
137:
138: private IWorkingSet fWorkingSet;
139: private Text fWorkingSetName;
140: private CheckboxFilteredTree fTree;
141: private boolean fFirstCheck;
142:
143: public PluginWorkingSet() {
144: super (
145: "page1", PDEUIMessages.PluginWorkingSet_title, PDEPluginImages.DESC_DEFCON_WIZ); //$NON-NLS-1$
146: PDEPlugin.getDefault().getLabelProvider().connect(this );
147: }
148:
149: /* (non-Javadoc)
150: * @see org.eclipse.ui.dialogs.IWorkingSetPage#finish()
151: */
152: public void finish() {
153: Object[] checked = fTree.getCheckboxTreeViewer()
154: .getCheckedElements();
155: ArrayList list = new ArrayList();
156: for (int i = 0; i < checked.length; i++) {
157: String id = ((IPluginModelBase) checked[i]).getPluginBase()
158: .getId();
159: if (id != null && id.length() > 0)
160: list.add(new PersistablePluginObject(id));
161: }
162: PersistablePluginObject[] objects = (PersistablePluginObject[]) list
163: .toArray(new PersistablePluginObject[list.size()]);
164:
165: String workingSetName = fWorkingSetName.getText().trim();
166: if (fWorkingSet == null) {
167: IWorkingSetManager workingSetManager = PlatformUI
168: .getWorkbench().getWorkingSetManager();
169: fWorkingSet = workingSetManager.createWorkingSet(
170: workingSetName, objects);
171: } else {
172: fWorkingSet.setName(workingSetName);
173: fWorkingSet.setElements(objects);
174: }
175: }
176:
177: /* (non-Javadoc)
178: * @see org.eclipse.ui.dialogs.IWorkingSetPage#getSelection()
179: */
180: public IWorkingSet getSelection() {
181: return fWorkingSet;
182: }
183:
184: /* (non-Javadoc)
185: * @see org.eclipse.ui.dialogs.IWorkingSetPage#setSelection(org.eclipse.ui.IWorkingSet)
186: */
187: public void setSelection(IWorkingSet workingSet) {
188: fWorkingSet = workingSet;
189: }
190:
191: /* (non-Javadoc)
192: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
193: */
194: public void createControl(Composite parent) {
195: Composite composite = new Composite(parent, SWT.NONE);
196: composite.setLayout(new GridLayout());
197: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
198: setControl(composite);
199:
200: Label label = new Label(composite, SWT.WRAP);
201: label.setText(PDEUIMessages.PluginWorkingSet_setName);
202: label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
203:
204: fWorkingSetName = new Text(composite, SWT.SINGLE | SWT.BORDER);
205: fWorkingSetName.setLayoutData(new GridData(
206: GridData.FILL_HORIZONTAL));
207: fWorkingSetName.addModifyListener(new ModifyListener() {
208: public void modifyText(ModifyEvent e) {
209: validatePage();
210: }
211: });
212: fWorkingSetName.setFocus();
213:
214: label = new Label(composite, SWT.WRAP);
215: label.setText(PDEUIMessages.PluginWorkingSet_setContent);
216: label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
217:
218: fTree = new CheckboxFilteredTree(composite, SWT.BORDER,
219: new PatternFilter());
220: GridData gd = new GridData(GridData.FILL_BOTH);
221: gd.heightHint = 250;
222: fTree.getViewer().getControl().setLayoutData(gd);
223: final IStructuredContentProvider fTableContentProvider = new ContentProvider();
224: fTree.getCheckboxTreeViewer().setContentProvider(
225: fTableContentProvider);
226: fTree.getCheckboxTreeViewer().setLabelProvider(
227: new WorkingSetLabelProvider());
228: fTree.getCheckboxTreeViewer().setUseHashlookup(true);
229: fTree.getCheckboxTreeViewer().setInput(PDECore.getDefault());
230:
231: fTree.getCheckboxTreeViewer().addCheckStateListener(
232: new ICheckStateListener() {
233: public void checkStateChanged(
234: CheckStateChangedEvent event) {
235: validatePage();
236: }
237: });
238:
239: // Add select / deselect all buttons for bug 46669
240: Composite buttonComposite = new Composite(composite, SWT.NONE);
241: buttonComposite.setLayout(new GridLayout(2, true));
242: buttonComposite.setLayoutData(new GridData(
243: GridData.HORIZONTAL_ALIGN_FILL));
244:
245: Button selectAllButton = new Button(buttonComposite, SWT.PUSH);
246: selectAllButton
247: .setText(PDEUIMessages.PluginWorkingSet_selectAll_label);
248: selectAllButton
249: .setToolTipText(PDEUIMessages.PluginWorkingSet_selectAll_toolTip);
250: selectAllButton.addSelectionListener(new SelectionAdapter() {
251: public void widgetSelected(SelectionEvent selectionEvent) {
252: fTree.getCheckboxTreeViewer().setCheckedElements(
253: fTableContentProvider.getElements(fTree
254: .getCheckboxTreeViewer().getInput()));
255: validatePage();
256: }
257: });
258: selectAllButton.setLayoutData(new GridData());
259: SWTUtil.setButtonDimensionHint(selectAllButton);
260:
261: Button deselectAllButton = new Button(buttonComposite, SWT.PUSH);
262: deselectAllButton
263: .setText(PDEUIMessages.PluginWorkingSet_deselectAll_label);
264: deselectAllButton
265: .setToolTipText(PDEUIMessages.PluginWorkingSet_deselectAll_toolTip);
266: deselectAllButton.addSelectionListener(new SelectionAdapter() {
267: public void widgetSelected(SelectionEvent selectionEvent) {
268: fTree.getCheckboxTreeViewer().setCheckedElements(
269: new Object[0]);
270: validatePage();
271: }
272: });
273: deselectAllButton.setLayoutData(new GridData());
274: SWTUtil.setButtonDimensionHint(deselectAllButton);
275: setPageComplete(false);
276: setMessage(PDEUIMessages.PluginWorkingSet_message);
277:
278: initialize();
279: Dialog.applyDialogFont(composite);
280:
281: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
282: IHelpContextIds.PLUGIN_WORKING_SET);
283: }
284:
285: /**
286: *
287: */
288: private void initialize() {
289: if (fWorkingSet != null) {
290: HashSet set = new HashSet();
291: IAdaptable[] elements = fWorkingSet.getElements();
292: for (int i = 0; i < elements.length; i++) {
293: if (elements[i] instanceof PersistablePluginObject)
294: set.add(((PersistablePluginObject) elements[i])
295: .getPluginID());
296: }
297:
298: IPluginModelBase[] bases = PluginRegistry.getAllModels();
299: for (int i = 0; i < bases.length; i++) {
300:
301: String id = bases[i].getPluginBase().getId();
302: if (id == null)
303: continue;
304: if (set.contains(id)) {
305: fTree.getCheckboxTreeViewer().setChecked(bases[i],
306: true);
307: set.remove(id);
308: }
309: if (set.isEmpty())
310: break;
311: }
312: fWorkingSetName.setText(fWorkingSet.getName());
313: }
314: }
315:
316: /* (non-Javadoc)
317: * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
318: */
319: public void dispose() {
320: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
321: }
322:
323: private void validatePage() {
324: String errorMessage = null;
325: String newText = fWorkingSetName.getText();
326:
327: if (newText.trim().length() == 0) {
328: errorMessage = PDEUIMessages.PluginWorkingSet_emptyName;
329: if (fFirstCheck) {
330: setPageComplete(false);
331: fFirstCheck = false;
332: return;
333: }
334: }
335: if (errorMessage == null
336: && fTree.getCheckboxTreeViewer().getCheckedElements().length == 0) {
337: errorMessage = PDEUIMessages.PluginWorkingSet_noPluginsChecked;
338: }
339:
340: if (errorMessage == null && fWorkingSet == null) {
341: IWorkingSet[] workingSets = PlatformUI.getWorkbench()
342: .getWorkingSetManager().getWorkingSets();
343: for (int i = 0; i < workingSets.length; i++) {
344: if (newText.equals(workingSets[i].getName())) {
345: errorMessage = PDEUIMessages.PluginWorkingSet_nameInUse;
346: break;
347: }
348: }
349: }
350: setErrorMessage(errorMessage);
351: setPageComplete(errorMessage == null);
352: }
353:
354: }
|