001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.editor.feature;
011:
012: import java.util.StringTokenizer;
013: import java.util.Vector;
014:
015: import org.eclipse.jface.dialogs.Dialog;
016: import org.eclipse.jface.dialogs.IDialogConstants;
017: import org.eclipse.jface.dialogs.TrayDialog;
018: import org.eclipse.jface.viewers.CheckboxTableViewer;
019: import org.eclipse.jface.viewers.ITableLabelProvider;
020: import org.eclipse.jface.viewers.LabelProvider;
021: import org.eclipse.pde.internal.ui.IHelpContextIds;
022: import org.eclipse.pde.internal.ui.PDEPlugin;
023: import org.eclipse.pde.internal.ui.PDEUIMessages;
024: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
025: import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.graphics.Image;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.layout.GridLayout;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Shell;
033: import org.eclipse.ui.PlatformUI;
034:
035: public class PortabilityChoicesDialog extends TrayDialog {
036: private String value;
037: private Choice[] choices;
038: private CheckboxTableViewer choiceViewer;
039: private WizardCheckboxTablePart checkboxTablePart;
040:
041: class ContentProvider extends DefaultTableProvider {
042: public Object[] getElements(Object parent) {
043: return choices;
044: }
045: }
046:
047: class ChoiceLabelProvider extends LabelProvider implements
048: ITableLabelProvider {
049: public String getColumnText(Object obj, int index) {
050: return ((Choice) obj).getLabel();
051: }
052:
053: public Image getColumnImage(Object obj, int index) {
054: return null;
055: }
056: }
057:
058: public PortabilityChoicesDialog(Shell shell, Choice[] choices,
059: String value) {
060: super (shell);
061: this .value = value;
062: this .choices = choices;
063:
064: checkboxTablePart = new WizardCheckboxTablePart(
065: PDEUIMessages.FeatureEditor_PortabilityChoicesDialog_choices);
066: }
067:
068: protected void createButtonsForButtonBar(Composite parent) {
069: // create OK and Cancel buttons by default
070: createButton(parent, IDialogConstants.OK_ID,
071: IDialogConstants.OK_LABEL, true);
072: createButton(parent, IDialogConstants.CANCEL_ID,
073: IDialogConstants.CANCEL_LABEL, false);
074: }
075:
076: protected Control createDialogArea(Composite parent) {
077: Composite container = new Composite(parent, SWT.NULL);
078: GridLayout layout = new GridLayout();
079: layout.numColumns = 2;
080: layout.marginWidth = layout.marginHeight = 9;
081: container.setLayout(layout);
082: GridData gd = new GridData(GridData.FILL_BOTH);
083: container.setLayoutData(gd);
084:
085: checkboxTablePart.createControl(container);
086: choiceViewer = checkboxTablePart.getTableViewer();
087: choiceViewer.setContentProvider(new ContentProvider());
088: choiceViewer.setLabelProvider(new ChoiceLabelProvider());
089:
090: gd = (GridData) checkboxTablePart.getControl().getLayoutData();
091: gd.widthHint = 300;
092: gd.heightHint = 350;
093:
094: Dialog.applyDialogFont(parent);
095: initialize();
096: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
097: IHelpContextIds.FEATURE_PORTABILITY_WIZARD);
098: return container;
099: }
100:
101: public String getValue() {
102: return value;
103: }
104:
105: protected void initialize() {
106: choiceViewer.setInput(PDEPlugin.getDefault());
107:
108: if (value != null) {
109: Vector selected = new Vector();
110: StringTokenizer stok = new StringTokenizer(value, ","); //$NON-NLS-1$
111: while (stok.hasMoreElements()) {
112: String tok = stok.nextToken();
113: Choice choice = findChoice(tok);
114: if (choice != null)
115: selected.add(choice);
116: }
117: checkboxTablePart.setSelection(selected.toArray());
118: } else
119: checkboxTablePart.selectAll(false);
120: }
121:
122: private Choice findChoice(String value) {
123: for (int i = 0; i < choices.length; i++) {
124: Choice choice = choices[i];
125: if (choice.getValue().equalsIgnoreCase(value))
126: return choice;
127: }
128: return null;
129: }
130:
131: protected void okPressed() {
132: value = computeNewValue();
133: super .okPressed();
134: }
135:
136: private String computeNewValue() {
137: Object[] checked = checkboxTablePart.getSelection();
138: if (checked.length == 0)
139: return ""; //$NON-NLS-1$
140: StringBuffer buf = new StringBuffer();
141: for (int i = 0; i < checked.length; i++) {
142: Choice choice = (Choice) checked[i];
143: if (i > 0)
144: buf.append(","); //$NON-NLS-1$
145: buf.append(choice.getValue());
146: }
147: return buf.toString();
148: }
149: }
|