001: /*******************************************************************************
002: * Copyright (c) 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.wizards.provisioner;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014:
015: import org.eclipse.core.runtime.Preferences;
016: import org.eclipse.jface.dialogs.Dialog;
017: import org.eclipse.jface.viewers.ArrayContentProvider;
018: import org.eclipse.jface.viewers.ISelectionChangedListener;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.TableViewer;
022: import org.eclipse.jface.wizard.WizardPage;
023: import org.eclipse.pde.internal.core.ICoreConstants;
024: import org.eclipse.pde.internal.core.PDECore;
025: import org.eclipse.pde.internal.ui.IHelpContextIds;
026: import org.eclipse.pde.internal.ui.PDEUIMessages;
027: import org.eclipse.pde.internal.ui.util.SWTUtil;
028: import org.eclipse.pde.internal.ui.util.SharedLabelProvider;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.events.KeyAdapter;
031: import org.eclipse.swt.events.KeyEvent;
032: import org.eclipse.swt.events.SelectionAdapter;
033: import org.eclipse.swt.events.SelectionEvent;
034: import org.eclipse.swt.graphics.Image;
035: import org.eclipse.swt.layout.GridData;
036: import org.eclipse.swt.layout.GridLayout;
037: import org.eclipse.swt.widgets.Button;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.DirectoryDialog;
040: import org.eclipse.swt.widgets.Label;
041: import org.eclipse.swt.widgets.Table;
042: import org.eclipse.swt.widgets.Text;
043: import org.eclipse.ui.ISharedImages;
044: import org.eclipse.ui.PlatformUI;
045:
046: public class DirectorySelectionPage extends WizardPage {
047:
048: private static final Image fFolderImage = PlatformUI.getWorkbench()
049: .getSharedImages().getImageDescriptor(
050: ISharedImages.IMG_OBJ_FOLDER).createImage();
051: private static final String LAST_LOCATION = "last_location"; //$NON-NLS-1$
052:
053: class FolderLabelProvider extends SharedLabelProvider {
054:
055: public Image getImage(Object obj) {
056: return fFolderImage;
057: }
058: }
059:
060: Text fDir = null;
061: private TableViewer fTableViewer = null;
062: private ArrayList fElements = new ArrayList();
063: private Button fAddButton = null;
064: private Button fRemoveButton = null;
065: private String fLastLocation = null;
066:
067: protected DirectorySelectionPage(String pageName) {
068: super (pageName);
069: setTitle(PDEUIMessages.DirectorySelectionPage_title);
070: setDescription(PDEUIMessages.DirectorySelectionPage_description);
071: setPageComplete(false);
072: Preferences pref = PDECore.getDefault().getPluginPreferences();
073: fLastLocation = pref.getString(LAST_LOCATION);
074: if (fLastLocation.length() == 0)
075: fLastLocation = pref
076: .getString(ICoreConstants.PLATFORM_PATH);
077: }
078:
079: public void createControl(Composite parent) {
080: Composite client = new Composite(parent, SWT.NONE);
081: GridLayout layout = new GridLayout();
082: layout.marginWidth = 2;
083: layout.numColumns = 2;
084: client.setLayout(layout);
085: client.setLayoutData(new GridData(GridData.FILL_BOTH));
086:
087: Label label = new Label(client, SWT.None);
088: label.setText(PDEUIMessages.DirectorySelectionPage_label);
089: GridData gd = new GridData();
090: gd.horizontalSpan = 2;
091: label.setLayoutData(gd);
092:
093: fTableViewer = new TableViewer(client);
094: fTableViewer.setLabelProvider(new FolderLabelProvider());
095: fTableViewer.setContentProvider(new ArrayContentProvider());
096: fTableViewer.setInput(fElements);
097: gd = new GridData(GridData.FILL_BOTH);
098: gd.verticalSpan = 3;
099: fTableViewer.getControl().setLayoutData(gd);
100:
101: fTableViewer
102: .addSelectionChangedListener(new ISelectionChangedListener() {
103:
104: public void selectionChanged(
105: SelectionChangedEvent event) {
106: updateButtons();
107: }
108:
109: });
110: fTableViewer.getTable().addKeyListener(new KeyAdapter() {
111: public void keyPressed(KeyEvent event) {
112: if (event.character == SWT.DEL && event.stateMask == 0) {
113: handleRemove();
114: }
115: }
116: });
117: Dialog.applyDialogFont(fTableViewer.getControl());
118: Dialog.applyDialogFont(label);
119:
120: createButtons(client);
121: PlatformUI.getWorkbench().getHelpSystem().setHelp(client,
122: IHelpContextIds.FILE_SYSTEM_PROVISIONING_PAGE);
123:
124: setControl(client);
125: }
126:
127: protected void createButtons(Composite parent) {
128: fAddButton = new Button(parent, SWT.PUSH);
129: fAddButton.setText(PDEUIMessages.DirectorySelectionPage_add);
130: fAddButton.setLayoutData(new GridData(
131: GridData.VERTICAL_ALIGN_BEGINNING));
132: SWTUtil.setButtonDimensionHint(fAddButton);
133: fAddButton.addSelectionListener(new SelectionAdapter() {
134: public void widgetSelected(SelectionEvent e) {
135: handleAdd();
136: }
137: });
138:
139: fRemoveButton = new Button(parent, SWT.PUSH);
140: fRemoveButton
141: .setText(PDEUIMessages.DirectorySelectionPage_remove);
142: fRemoveButton.setLayoutData(new GridData(
143: GridData.VERTICAL_ALIGN_BEGINNING));
144: SWTUtil.setButtonDimensionHint(fRemoveButton);
145: fRemoveButton.addSelectionListener(new SelectionAdapter() {
146: public void widgetSelected(SelectionEvent e) {
147: handleRemove();
148: }
149: });
150: updateButtons();
151: }
152:
153: private void handleAdd() {
154: DirectoryDialog dialog = new DirectoryDialog(getShell());
155: dialog.setMessage(PDEUIMessages.DirectorySelectionPage_message);
156: dialog.setFilterPath(fLastLocation);
157: String path = dialog.open();
158: if (path != null) {
159: fLastLocation = path;
160: File newDirectory = new File(path);
161: fElements.add(newDirectory);
162: fTableViewer.add(newDirectory);
163: setPageComplete(true);
164: }
165: }
166:
167: private void handleRemove() {
168: Object[] elements = ((IStructuredSelection) fTableViewer
169: .getSelection()).toArray();
170: for (int i = 0; i < elements.length; i++)
171: fElements.remove(elements[i]);
172:
173: Table table = fTableViewer.getTable();
174: int index = table.getSelectionIndex() - fElements.size();
175: if (index > fElements.size())
176: index = fElements.size() - 1;
177:
178: fTableViewer.remove(elements);
179: table.setSelection(index);
180:
181: updateButtons();
182: setPageComplete(!fElements.isEmpty());
183: }
184:
185: public File[] getLocations() {
186: Preferences pref = PDECore.getDefault().getPluginPreferences();
187: pref.setValue(LAST_LOCATION, fLastLocation);
188: return (File[]) fElements.toArray(new File[fElements.size()]);
189: }
190:
191: protected void updateButtons() {
192: int num = fTableViewer.getTable().getSelectionCount();
193: fRemoveButton.setEnabled(num > 0);
194: }
195:
196: }
|