KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > provisioner > DirectorySelectionPage


1 /*******************************************************************************
2  * Copyright (c) 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.wizards.provisioner;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.Preferences;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.viewers.ArrayContentProvider;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.pde.internal.core.ICoreConstants;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.ui.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.internal.ui.util.SWTUtil;
29 import org.eclipse.pde.internal.ui.util.SharedLabelProvider;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.KeyAdapter;
32 import org.eclipse.swt.events.KeyEvent;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.DirectoryDialog;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Table;
43 import org.eclipse.swt.widgets.Text;
44 import org.eclipse.ui.ISharedImages;
45 import org.eclipse.ui.PlatformUI;
46
47 public class DirectorySelectionPage extends WizardPage {
48     
49     private static final Image fFolderImage = PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
50             ISharedImages.IMG_OBJ_FOLDER).createImage();
51     private static final String JavaDoc LAST_LOCATION = "last_location"; //$NON-NLS-1$
52

53     class FolderLabelProvider extends SharedLabelProvider {
54         
55         public Image getImage(Object JavaDoc obj) {
56             return fFolderImage;
57         }
58     }
59     
60     Text fDir = null;
61     private TableViewer fTableViewer = null;
62     private ArrayList JavaDoc fElements = new ArrayList JavaDoc();
63     private Button fAddButton = null;
64     private Button fRemoveButton = null;
65     private String JavaDoc fLastLocation = null;
66
67     protected DirectorySelectionPage(String JavaDoc pageName) {
68         super(pageName);
69         setTitle(PDEUIMessages.DirectorySelectionPage_title);
70         setDescription(PDEUIMessages.DirectorySelectionPage_description);
71         setPageComplete(false);
72         Preferences pref = PDECore.getDefault().getPluginPreferences();
73         fLastLocation = pref.getString(LAST_LOCATION);
74         if (fLastLocation.length() == 0)
75             fLastLocation = pref.getString(ICoreConstants.PLATFORM_PATH);
76     }
77
78     public void createControl(Composite parent) {
79         Composite client = new Composite(parent, SWT.NONE);
80         GridLayout layout = new GridLayout();
81         layout.marginWidth = 2;
82         layout.numColumns = 2;
83         client.setLayout(layout);
84         client.setLayoutData(new GridData(GridData.FILL_BOTH));
85         
86         Label label = new Label(client, SWT.None);
87         label.setText(PDEUIMessages.DirectorySelectionPage_label);
88         GridData gd = new GridData();
89         gd.horizontalSpan = 2;
90         label.setLayoutData(gd);
91         
92         fTableViewer = new TableViewer(client);
93         fTableViewer.setLabelProvider(new FolderLabelProvider());
94         fTableViewer.setContentProvider(new ArrayContentProvider());
95         fTableViewer.setInput(fElements);
96         gd = new GridData(GridData.FILL_BOTH);
97         gd.verticalSpan = 3;
98         fTableViewer.getControl().setLayoutData(gd);
99         
100         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
101
102             public void selectionChanged(SelectionChangedEvent event) {
103                 updateButtons();
104             }
105             
106         });
107         fTableViewer.getTable().addKeyListener(new KeyAdapter() {
108             public void keyPressed(KeyEvent event) {
109                 if (event.character == SWT.DEL && event.stateMask == 0) {
110                     handleRemove();
111                 }
112             }
113         });
114         Dialog.applyDialogFont(fTableViewer.getControl());
115         Dialog.applyDialogFont(label);
116         
117         createButtons(client);
118         PlatformUI.getWorkbench().getHelpSystem().setHelp(client,
119                             IHelpContextIds.FILE_SYSTEM_PROVISIONING_PAGE);
120         
121         setControl(client);
122     }
123     
124     protected void createButtons(Composite parent) {
125         fAddButton = new Button(parent, SWT.PUSH);
126         fAddButton.setText(PDEUIMessages.DirectorySelectionPage_add);
127         fAddButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
128         SWTUtil.setButtonDimensionHint(fAddButton);
129         fAddButton.addSelectionListener(new SelectionAdapter() {
130             public void widgetSelected(SelectionEvent e) {
131                 handleAdd();
132             }
133         });
134         
135         fRemoveButton = new Button(parent, SWT.PUSH);
136         fRemoveButton.setText(PDEUIMessages.DirectorySelectionPage_remove);
137         fRemoveButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
138         SWTUtil.setButtonDimensionHint(fRemoveButton);
139         fRemoveButton.addSelectionListener(new SelectionAdapter() {
140             public void widgetSelected(SelectionEvent e) {
141                 handleRemove();
142             }
143         });
144         updateButtons();
145     }
146     
147     private void handleAdd() {
148         DirectoryDialog dialog = new DirectoryDialog(getShell());
149         dialog.setMessage(PDEUIMessages.DirectorySelectionPage_message);
150         dialog.setFilterPath(fLastLocation);
151         String JavaDoc path = dialog.open();
152         if (path != null) {
153             fLastLocation = path;
154             File JavaDoc newDirectory = new File JavaDoc(path);
155             fElements.add(newDirectory);
156             fTableViewer.add(newDirectory);
157             setPageComplete(true);
158         }
159     }
160     
161     private void handleRemove() {
162         Object JavaDoc[] elements = ((IStructuredSelection)fTableViewer.getSelection()).toArray();
163         for (int i = 0; i < elements.length; i++)
164             fElements.remove(elements[i]);
165         
166         Table table = fTableViewer.getTable();
167         int index = table.getSelectionIndex() - fElements.size();
168         if (index > fElements.size())
169             index = fElements.size() - 1;
170         
171         fTableViewer.remove(elements);
172         table.setSelection(index);
173         
174         updateButtons();
175         setPageComplete(!fElements.isEmpty());
176     }
177         
178     public File JavaDoc[] getLocations() {
179         Preferences pref = PDECore.getDefault().getPluginPreferences();
180         pref.setValue(LAST_LOCATION, fLastLocation);
181         return (File JavaDoc[]) fElements.toArray(new File JavaDoc[fElements.size()]);
182     }
183     
184     protected void updateButtons() {
185         int num = fTableViewer.getTable().getSelectionCount();
186         fRemoveButton.setEnabled(num > 0);
187     }
188
189 }
190
Popular Tags