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.build;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Status;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.ISelectionChangedListener;
016: import org.eclipse.jface.viewers.IStructuredContentProvider;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.viewers.ITableLabelProvider;
019: import org.eclipse.jface.viewers.LabelProvider;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.TableViewer;
022: import org.eclipse.pde.core.plugin.IPluginLibrary;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.internal.ui.PDELabelProvider;
025: import org.eclipse.pde.internal.ui.PDEPlugin;
026: import org.eclipse.pde.internal.ui.PDEPluginImages;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.events.ModifyEvent;
031: import org.eclipse.swt.events.ModifyListener;
032: import org.eclipse.swt.graphics.Image;
033: import org.eclipse.swt.layout.GridData;
034: import org.eclipse.swt.layout.GridLayout;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.swt.widgets.Control;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Shell;
039: import org.eclipse.swt.widgets.Table;
040: import org.eclipse.swt.widgets.Text;
041: import org.eclipse.ui.dialogs.SelectionStatusDialog;
042:
043: public class AddLibraryDialog extends SelectionStatusDialog {
044: private String newName;
045: private String[] libraries;
046: private IPluginModelBase model;
047: private static String init = "library.jar"; //$NON-NLS-1$
048: private Text text;
049: private Image libImage;
050: private TableViewer libraryViewer;
051: private DuplicateStatusValidator validator;
052:
053: class DuplicateStatusValidator {
054: public IStatus validate(String text) {
055: if (text.length() == 0)
056: return new Status(IStatus.ERROR, PDEPlugin
057: .getPluginId(), IStatus.ERROR,
058: PDEUIMessages.AddLibraryDialog_emptyLibraries,
059: null);
060:
061: if (text.indexOf(' ') != -1)
062: return new Status(IStatus.ERROR, PDEPlugin
063: .getPluginId(), IStatus.ERROR,
064: PDEUIMessages.AddLibraryDialog_nospaces, null);
065:
066: if (libraries == null || libraries.length == 0)
067: return new Status(IStatus.OK, PDEPlugin.getPluginId(),
068: IStatus.OK, "", null); //$NON-NLS-1$
069:
070: if (!text.endsWith(".jar") && !text.endsWith("/") && !text.equals(".")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
071: text += "/"; //$NON-NLS-1$
072:
073: for (int i = 0; i < libraries.length; i++) {
074: if (libraries[i].equals(text))
075: return new Status(
076: IStatus.ERROR,
077: PDEPlugin.getPluginId(),
078: IStatus.ERROR,
079: PDEUIMessages.BuildEditor_RuntimeInfoSection_duplicateLibrary,
080: null);
081: }
082: return new Status(IStatus.OK, PDEPlugin.getPluginId(),
083: IStatus.OK, "", null); //$NON-NLS-1$
084: }
085: }
086:
087: class TableContentProvider extends DefaultContentProvider implements
088: IStructuredContentProvider {
089: public Object[] getElements(Object input) {
090: if (input instanceof IPluginModelBase) {
091: return ((IPluginModelBase) input).getPluginBase()
092: .getLibraries();
093: }
094: return new Object[0];
095: }
096: }
097:
098: class TableLabelProvider extends LabelProvider implements
099: ITableLabelProvider {
100: public String getColumnText(Object obj, int index) {
101: return ((IPluginLibrary) obj).getName();
102: }
103:
104: public Image getColumnImage(Object obj, int index) {
105: return libImage;
106: }
107: }
108:
109: public AddLibraryDialog(Shell shell, String[] libraries,
110: IPluginModelBase model) {
111: super (shell);
112: setShellStyle(getShellStyle() | SWT.RESIZE);
113: setLibraryNames(libraries);
114: setPluginModel(model);
115: initializeImages();
116: initializeValidator();
117: setStatusLineAboveButtons(true);
118: }
119:
120: public void setPluginModel(IPluginModelBase model) {
121: this .model = model;
122: }
123:
124: private void initializeImages() {
125: PDELabelProvider provider = PDEPlugin.getDefault()
126: .getLabelProvider();
127: libImage = provider.get(PDEPluginImages.DESC_JAVA_LIB_OBJ);
128: }
129:
130: public void setLibraryNames(String[] libraries) {
131: this .libraries = libraries;
132: }
133:
134: protected Control createDialogArea(Composite parent) {
135: Composite container = new Composite(parent, SWT.NULL);
136: GridLayout layout = new GridLayout();
137: layout.numColumns = 1;
138: layout.marginWidth = 10;
139: layout.marginHeight = 10;
140: container.setLayout(layout);
141:
142: container.setLayoutData(new GridData(GridData.FILL_BOTH));
143:
144: Label label = new Label(container, SWT.NULL);
145: label.setText(PDEUIMessages.BuildEditor_AddLibraryDialog_label);
146: label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
147:
148: text = new Text(container, SWT.SINGLE | SWT.BORDER);
149: text.addModifyListener(new ModifyListener() {
150: public void modifyText(ModifyEvent e) {
151: updateStatus(validator.validate(text.getText()));
152: }
153: });
154: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
155:
156: Table table = new Table(container, SWT.FULL_SELECTION
157: | SWT.BORDER);
158: GridData gd = new GridData(GridData.FILL_BOTH);
159: gd.heightHint = 125;
160: table.setLayoutData(gd);
161:
162: libraryViewer = new TableViewer(table);
163: libraryViewer.setContentProvider(new TableContentProvider());
164: libraryViewer.setLabelProvider(new TableLabelProvider());
165: libraryViewer
166: .addSelectionChangedListener(new ISelectionChangedListener() {
167: public void selectionChanged(SelectionChangedEvent e) {
168: ISelection sel = e.getSelection();
169: IPluginLibrary obj = (IPluginLibrary) ((IStructuredSelection) sel)
170: .getFirstElement();
171: text.setText(obj != null ? obj.getName() : ""); //$NON-NLS-1$
172: }
173: });
174: libraryViewer.setInput(model);
175: applyDialogFont(container);
176: return container;
177: }
178:
179: public int open() {
180: text.setText(init);
181: text.selectAll();
182: return super .open();
183: }
184:
185: protected void computeResult() {
186:
187: }
188:
189: public String getNewName() {
190: return newName;
191: }
192:
193: /* (non-Javadoc)
194: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
195: */
196: protected void okPressed() {
197: newName = text.getText();
198: super .okPressed();
199: }
200:
201: private void initializeValidator() {
202: this .validator = new DuplicateStatusValidator();
203: }
204:
205: }
|