KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > PortabilityChoicesDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.feature;
12
13 import java.util.StringTokenizer JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.TrayDialog;
19 import org.eclipse.jface.viewers.CheckboxTableViewer;
20 import org.eclipse.jface.viewers.ITableLabelProvider;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.pde.internal.ui.IHelpContextIds;
23 import org.eclipse.pde.internal.ui.PDEPlugin;
24 import org.eclipse.pde.internal.ui.PDEUIMessages;
25 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
26 import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.PlatformUI;
35
36 public class PortabilityChoicesDialog extends TrayDialog {
37     private String JavaDoc value;
38     private Choice[] choices;
39     private CheckboxTableViewer choiceViewer;
40     private WizardCheckboxTablePart checkboxTablePart;
41
42     class ContentProvider extends DefaultTableProvider {
43         public Object JavaDoc[] getElements(Object JavaDoc parent) {
44             return choices;
45         }
46     }
47
48     class ChoiceLabelProvider
49         extends LabelProvider
50         implements ITableLabelProvider {
51         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
52             return ((Choice) obj).getLabel();
53         }
54
55         public Image getColumnImage(Object JavaDoc obj, int index) {
56             return null;
57         }
58     }
59
60     public PortabilityChoicesDialog(
61         Shell shell,
62         Choice[] choices,
63         String JavaDoc value) {
64         super(shell);
65         this.value = value;
66         this.choices = choices;
67         
68         checkboxTablePart = new WizardCheckboxTablePart(PDEUIMessages.FeatureEditor_PortabilityChoicesDialog_choices);
69     }
70
71     protected void createButtonsForButtonBar(Composite parent) {
72         // create OK and Cancel buttons by default
73
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
74         createButton(
75             parent,
76             IDialogConstants.CANCEL_ID,
77             IDialogConstants.CANCEL_LABEL,
78             false);
79     }
80
81     protected Control createDialogArea(Composite parent) {
82         Composite container = new Composite(parent, SWT.NULL);
83         GridLayout layout = new GridLayout();
84         layout.numColumns = 2;
85         layout.marginWidth = layout.marginHeight = 9;
86         container.setLayout(layout);
87         GridData gd = new GridData(GridData.FILL_BOTH);
88         container.setLayoutData(gd);
89         
90         checkboxTablePart.createControl(container);
91         choiceViewer = checkboxTablePart.getTableViewer();
92         choiceViewer.setContentProvider(new ContentProvider());
93         choiceViewer.setLabelProvider(new ChoiceLabelProvider());
94
95         gd = (GridData)checkboxTablePart.getControl().getLayoutData();
96         gd.widthHint = 300;
97         gd.heightHint = 350;
98
99         Dialog.applyDialogFont(parent);
100         initialize();
101         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.FEATURE_PORTABILITY_WIZARD);
102         return container;
103     }
104     public String JavaDoc getValue() {
105         return value;
106     }
107
108     protected void initialize() {
109         choiceViewer.setInput(PDEPlugin.getDefault());
110
111         if (value != null) {
112             Vector JavaDoc selected = new Vector JavaDoc();
113             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
114
while (stok.hasMoreElements()) {
115                 String JavaDoc tok = stok.nextToken();
116                 Choice choice = findChoice(tok);
117                 if (choice != null)
118                     selected.add(choice);
119             }
120             checkboxTablePart.setSelection(selected.toArray());
121         }
122         else
123             checkboxTablePart.selectAll(false);
124     }
125
126     private Choice findChoice(String JavaDoc value) {
127         for (int i = 0; i < choices.length; i++) {
128             Choice choice = choices[i];
129             if (choice.getValue().equalsIgnoreCase(value))
130                 return choice;
131         }
132         return null;
133     }
134
135     protected void okPressed() {
136         value = computeNewValue();
137         super.okPressed();
138     }
139
140     private String JavaDoc computeNewValue() {
141         Object JavaDoc[] checked = checkboxTablePart.getSelection();
142         if (checked.length == 0)
143             return ""; //$NON-NLS-1$
144
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
145         for (int i = 0; i < checked.length; i++) {
146             Choice choice = (Choice) checked[i];
147             if (i > 0)
148                 buf.append(","); //$NON-NLS-1$
149
buf.append(choice.getValue());
150         }
151         return buf.toString();
152     }
153 }
154
Popular Tags