KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > build > AddLibraryDialog


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.build;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.ITableLabelProvider;
20 import org.eclipse.jface.viewers.LabelProvider;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.pde.core.plugin.IPluginLibrary;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.ui.PDELabelProvider;
26 import org.eclipse.pde.internal.ui.PDEPlugin;
27 import org.eclipse.pde.internal.ui.PDEPluginImages;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Table;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.dialogs.SelectionStatusDialog;
43
44 public class AddLibraryDialog extends SelectionStatusDialog {
45     private String JavaDoc newName;
46     private String JavaDoc[] libraries;
47     private IPluginModelBase model;
48     private static String JavaDoc init = "library.jar"; //$NON-NLS-1$
49
private Text text;
50     private Image libImage;
51     private TableViewer libraryViewer;
52     private DuplicateStatusValidator validator;
53
54     class DuplicateStatusValidator {
55         public IStatus validate(String JavaDoc text) {
56             if (text.length() == 0)
57                 return new Status(IStatus.ERROR, PDEPlugin.getPluginId(), IStatus.ERROR, PDEUIMessages.AddLibraryDialog_emptyLibraries, null);
58             
59             if (text.indexOf(' ') != -1)
60                 return new Status(IStatus.ERROR, PDEPlugin.getPluginId(), IStatus.ERROR, PDEUIMessages.AddLibraryDialog_nospaces, null);
61             
62             if (libraries == null || libraries.length == 0)
63                 return new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
64

65             if (!text.endsWith(".jar") && !text.endsWith("/") && !text.equals(".")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
66
text += "/"; //$NON-NLS-1$
67

68             for (int i = 0; i < libraries.length; i++) {
69                 if (libraries[i].equals(text))
70                     return new Status(
71                             IStatus.ERROR,
72                             PDEPlugin.getPluginId(),
73                             IStatus.ERROR,
74                             PDEUIMessages.BuildEditor_RuntimeInfoSection_duplicateLibrary,
75                             null);
76             }
77             return new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
78
}
79     }
80     class TableContentProvider extends DefaultContentProvider implements IStructuredContentProvider{
81         public Object JavaDoc[] getElements(Object JavaDoc input){
82             if (input instanceof IPluginModelBase){
83                 return ((IPluginModelBase)input).getPluginBase().getLibraries();
84             }
85             return new Object JavaDoc[0];
86         }
87     }
88     
89     class TableLabelProvider extends LabelProvider implements ITableLabelProvider{
90         public String JavaDoc getColumnText(Object JavaDoc obj, int index){
91             return ((IPluginLibrary)obj).getName();
92         }
93         
94         public Image getColumnImage(Object JavaDoc obj, int index){
95             return libImage;
96         }
97     }
98     
99     public AddLibraryDialog(Shell shell, String JavaDoc[] libraries, IPluginModelBase model) {
100         super(shell);
101         setShellStyle(getShellStyle() | SWT.RESIZE);
102         setLibraryNames(libraries);
103         setPluginModel(model);
104         initializeImages();
105         initializeValidator();
106         setStatusLineAboveButtons(true);
107     }
108     
109     public void setPluginModel(IPluginModelBase model){
110         this.model = model;
111     }
112     
113     private void initializeImages(){
114         PDELabelProvider provider = PDEPlugin.getDefault().getLabelProvider();
115         libImage= provider.get(PDEPluginImages.DESC_JAVA_LIB_OBJ);
116     }
117     
118     public void setLibraryNames(String JavaDoc[] libraries) {
119         this.libraries = libraries;
120     }
121     
122     protected Control createDialogArea(Composite parent) {
123         Composite container = new Composite(parent, SWT.NULL);
124         GridLayout layout = new GridLayout();
125         layout.numColumns = 1;
126         layout.marginWidth = 10;
127         layout.marginHeight = 10;
128         container.setLayout(layout);
129         
130         container.setLayoutData(new GridData(GridData.FILL_BOTH));
131         
132         Label label = new Label(container, SWT.NULL);
133         label.setText(PDEUIMessages.BuildEditor_AddLibraryDialog_label);
134         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
135         
136         text = new Text(container, SWT.SINGLE|SWT.BORDER);
137         text.addModifyListener(new ModifyListener() {
138         public void modifyText(ModifyEvent e) {
139             updateStatus(validator.validate(text.getText()));
140             }
141         });
142         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
143
144         Table table = new Table(container, SWT.FULL_SELECTION | SWT.BORDER);
145         GridData gd = new GridData(GridData.FILL_BOTH);
146         gd.heightHint = 125;
147         table.setLayoutData(gd);
148         
149         libraryViewer = new TableViewer(table);
150         libraryViewer.setContentProvider(new TableContentProvider());
151         libraryViewer.setLabelProvider(new TableLabelProvider());
152         libraryViewer.addSelectionChangedListener(new ISelectionChangedListener(){
153             public void selectionChanged(SelectionChangedEvent e){
154                 ISelection sel = e.getSelection();
155                 IPluginLibrary obj = (IPluginLibrary)((IStructuredSelection)sel).getFirstElement();
156                 text.setText(obj!=null ? obj.getName() : ""); //$NON-NLS-1$
157
}
158         });
159         libraryViewer.setInput(model);
160         applyDialogFont(container);
161         return container;
162     }
163     
164     public int open() {
165         text.setText(init);
166         text.selectAll();
167         return super.open();
168     }
169
170     protected void computeResult(){
171         
172     }
173       
174     public String JavaDoc getNewName() {
175         return newName;
176     }
177
178     /* (non-Javadoc)
179      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
180      */

181     protected void okPressed() {
182         newName = text.getText();
183         super.okPressed();
184     }
185     
186     private void initializeValidator(){
187         this.validator = new DuplicateStatusValidator();
188     }
189
190 }
191
Popular Tags