KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AntPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.preferences;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import org.eclipse.ant.internal.core.AntObject;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.viewers.ColumnLayoutData;
20 import org.eclipse.jface.viewers.ColumnWeightData;
21 import org.eclipse.jface.viewers.DoubleClickEvent;
22 import org.eclipse.jface.viewers.IDoubleClickListener;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.TableLayout;
27 import org.eclipse.jface.viewers.TableViewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.KeyAdapter;
30 import org.eclipse.swt.events.KeyEvent;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.TabFolder;
39 import org.eclipse.swt.widgets.TabItem;
40 import org.eclipse.swt.widgets.Table;
41 import org.eclipse.swt.widgets.TableColumn;
42 import org.eclipse.ui.PlatformUI;
43
44 /**
45  * Provides the generic implementation for a sub-page in the
46  * Ant preference page.
47  */

48 public abstract class AntPage {
49     
50     protected static final int ADD_BUTTON = IDialogConstants.CLIENT_ID + 1;
51     protected static final int EDIT_BUTTON = IDialogConstants.CLIENT_ID + 2;
52     protected static final int REMOVE_BUTTON = IDialogConstants.CLIENT_ID + 3;
53     
54     protected SelectionAdapter selectionAdapter = new SelectionAdapter() {
55         public void widgetSelected(SelectionEvent e) {
56             buttonPressed(((Integer JavaDoc) e.widget.getData()).intValue());
57         }
58     };
59     private AntRuntimePreferencePage preferencePage;
60     private TableViewer tableViewer;
61     private AntContentProvider contentProvider;
62     
63     protected Button editButton;
64     protected Button removeButton;
65     
66     private final String JavaDoc[] fTableColumnHeaders= {
67             AntPreferencesMessages.AntPage_0, AntPreferencesMessages.AntPage_1, AntPreferencesMessages.AntPage_2, AntPreferencesMessages.AntPage_3
68     };
69     private final ColumnLayoutData[] fTableColumnLayouts= {
70             new ColumnWeightData(40),
71             new ColumnWeightData(20),
72             new ColumnWeightData(20),
73             new ColumnWeightData(20)
74     };
75
76     /**
77      * Creates an instance of this page.
78      */

79     public AntPage(AntRuntimePreferencePage preferencePage) {
80         super();
81         this.preferencePage = preferencePage;
82     }
83     
84     /**
85      * Adds buttons specific to the page.
86      */

87     protected abstract void addButtonsToButtonGroup(Composite parent);
88     
89     /**
90      * Give this page a chance to initialize itself
91      */

92     protected abstract void initialize();
93     
94     /**
95      * Adds an object to the contents
96      */

97     protected void addContent(Object JavaDoc o) {
98         if (contentProvider != null) {
99             contentProvider.add(o);
100         }
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.ant.internal.ui.preferences.AntPage#buttonPressed(int)
105      */

106     private void buttonPressed(int buttonId) {
107         switch (buttonId) {
108             case ADD_BUTTON :
109                 add();
110                 break;
111             case EDIT_BUTTON :
112                 edit(getSelection());
113                 break;
114             case REMOVE_BUTTON :
115                 remove();
116                 break;
117         }
118     }
119
120     /**
121      * Creates and returns a button with appropriate size and layout.
122      *
123      * @param parent the control to create the button on
124      * @param labelKey the key to lookup the button's label
125      * @param buttonId the id to assign to this button
126      * @return a new and initialized button
127      */

128     protected Button createPushButton(Composite parent, String JavaDoc buttonText, int buttonId) {
129         Button button = new Button(parent, SWT.PUSH);
130         button.setFont(parent.getFont());
131         button.setText(buttonText);
132         button.setData(new Integer JavaDoc(buttonId));
133         button.addSelectionListener(selectionAdapter);
134         preferencePage.setButtonLayoutData(button);
135         return button;
136     }
137     
138     /**
139      * Creates the group which will contain the buttons.
140      */

141     protected void createButtonGroup(Composite top) {
142         Composite buttonGroup = new Composite(top, SWT.NONE);
143         GridLayout layout = new GridLayout();
144         layout.marginHeight = 0;
145         layout.marginWidth = 0;
146         buttonGroup.setLayout(layout);
147         buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL));
148         buttonGroup.setFont(top.getFont());
149         
150         addButtonsToButtonGroup(buttonGroup);
151     }
152     
153     /**
154      * Creates the table viewer.
155      */

156     protected void createTable(Composite parent) {
157         Table table = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
158         GridData data= new GridData(GridData.FILL_BOTH);
159         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
160         data.heightHint = table.getItemHeight();
161         data.horizontalSpan= 1;
162         table.setLayoutData(data);
163         table.setFont(parent.getFont());
164         
165         TableLayout tableLayout = new TableLayout();
166         table.setLayout(tableLayout);
167         table.setHeaderVisible(true);
168         table.setLinesVisible(true);
169         
170         // tableViewer.setColumnProperties(fTableColumnProperties);
171
for (int i = 0; i < fTableColumnHeaders.length; i++) {
172             tableLayout.addColumnData(fTableColumnLayouts[i]);
173             TableColumn column = new TableColumn(table, SWT.NONE, i);
174             column.setResizable(fTableColumnLayouts[i].resizable);
175             column.setText(fTableColumnHeaders[i]);
176         }
177         
178         contentProvider = getContentProvider();
179         tableViewer = new TableViewer(table);
180         tableViewer.setContentProvider(contentProvider);
181         tableViewer.setLabelProvider(new AntObjectLabelProvider());
182         tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
183             public void selectionChanged(SelectionChangedEvent event) {
184                 tableSelectionChanged((IStructuredSelection) event.getSelection());
185             }
186         });
187         
188         tableViewer.addDoubleClickListener(new IDoubleClickListener() {
189             public void doubleClick(DoubleClickEvent event) {
190                 if (!event.getSelection().isEmpty() && editButton.isEnabled()) {
191                     edit((IStructuredSelection)event.getSelection());
192                 }
193             }
194         });
195         
196         table.addKeyListener(new KeyAdapter() {
197             public void keyPressed(KeyEvent event) {
198                 if (editButton.isEnabled() && event.character == SWT.DEL && event.stateMask == 0) {
199                     remove(tableViewer);
200                 }
201             }
202         });
203     }
204
205     /**
206      * Returns the content provider to use for the table viewer
207      *
208      * @return AntPageContentProvider
209      */

210     protected AntContentProvider getContentProvider() {
211         return new AntContentProvider();
212     }
213
214     /**
215      * Returns the currently listed objects in the table if the library
216      * for that entry is still included in the preferences. Default objects
217      * are included depending on the value of the <code>forDisplay</code> parameter.
218      * Returns <code>null</code> if this widget has not yet been created
219      * or has been disposed.
220      * @param forDisplay Whether the result is to be displayed in the UI or stored in the preferences
221      * @return The list of objects currently displayed in the table
222      */

223     protected List JavaDoc getContents(boolean forDisplay) {
224         if (tableViewer == null || tableViewer.getControl().isDisposed()) {
225             return null;
226         }
227         List JavaDoc entries= getPreferencePage().getLibraryEntries();
228         
229         Object JavaDoc[] elements = contentProvider.getElements(tableViewer.getInput());
230         List JavaDoc contents= new ArrayList JavaDoc(elements.length);
231         Object JavaDoc element;
232         AntObject antObject;
233         for (int i = 0; i < elements.length; i++) {
234             element= elements[i];
235             if (element instanceof AntObject) {
236                 antObject= (AntObject)element;
237                 if (forDisplay) {
238                     if (!antObject.isDefault() && !entries.contains(antObject.getLibraryEntry())) {
239                         continue;
240                     }
241                 } else if (antObject.isDefault() || !entries.contains(antObject.getLibraryEntry())) {
242                     continue;
243                 }
244             }
245             contents.add(element);
246         }
247         return contents;
248     }
249     
250     /**
251      * Returns the selection in the viewer, or <code>null</code> if none.
252      */

253     protected final IStructuredSelection getSelection() {
254         if (tableViewer == null || tableViewer.getControl().isDisposed()) {
255             return null;
256         }
257         return ((IStructuredSelection) tableViewer.getSelection());
258     }
259     
260     /**
261      * Returns the shell of the sub-page.
262      */

263     protected final Shell getShell() {
264         if (tableViewer == null || tableViewer.getControl().isDisposed()) {
265             return null;
266         }
267         return tableViewer.getControl().getShell();
268     }
269     
270     /**
271      * Handles the remove button pressed event
272      */

273     protected void remove() {
274         remove(tableViewer);
275     }
276     
277     protected void remove(TableViewer viewer) {
278         AntContentProvider antContentProvider= (AntContentProvider)viewer.getContentProvider();
279         IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
280         Iterator JavaDoc itr = sel.iterator();
281         while (itr.hasNext()) {
282             antContentProvider.remove(itr.next());
283         }
284     }
285     
286     /**
287      * Sets the contents of the table on this page. Has no effect
288      * if this widget has not yet been created or has been disposed.
289      */

290     protected void setInput(List JavaDoc inputs) {
291         if (tableViewer == null || tableViewer.getControl().isDisposed()) {
292             return;
293         }
294         tableViewer.setInput(inputs);
295         tableSelectionChanged((IStructuredSelection) tableViewer.getSelection());
296     }
297
298     /**
299      * Updates the content element in the table viewer.
300      */

301     protected final void updateContent(Object JavaDoc element) {
302         if (tableViewer == null || tableViewer.getControl().isDisposed()) {
303             return;
304         }
305         tableViewer.update(element, null);
306     }
307     
308     /**
309      * Creates the default contents of this page
310      */

311     protected Composite createContents(Composite top) {
312         PlatformUI.getWorkbench().getHelpSystem().setHelp(top, getHelpContextId());
313         GridLayout layout = new GridLayout();
314         layout.numColumns = 2;
315         layout.marginHeight = 2;
316         layout.marginWidth = 2;
317         top.setLayout(layout);
318         
319         top.setLayoutData(new GridData(GridData.FILL_BOTH));
320
321         createTable(top);
322         createButtonGroup(top);
323         
324         return top;
325     }
326     
327     protected AntRuntimePreferencePage getPreferencePage() {
328         return preferencePage;
329     }
330     
331     protected TableViewer getTableViewer() {
332         return tableViewer;
333     }
334
335     /**
336      * Handles selection changes in the table viewer.
337      */

338     protected void tableSelectionChanged(IStructuredSelection newSelection) {
339         int size = newSelection.size();
340         boolean enabled= true;
341         
342         Iterator JavaDoc itr= newSelection.iterator();
343         while (itr.hasNext()) {
344             Object JavaDoc element = itr.next();
345             if (element instanceof AntObject) {
346                 AntObject antObject= (AntObject)element;
347                 if (antObject.isDefault()) {
348                     enabled= false;
349                     break;
350                 }
351             }
352         }
353         editButton.setEnabled(enabled && size == 1);
354         removeButton.setEnabled(enabled && size > 0);
355     }
356     
357     /**
358      * Allows the user to edit a custom Ant object.
359      *
360      * @param selection The selection containing the object to edit
361      */

362     protected abstract void edit(IStructuredSelection selection);
363     
364     /**
365      * Allows the user to add a custom Ant object.
366      *
367      */

368     protected abstract void add();
369     
370     /**
371      * Returns this page's help context id, which is hooked
372      * to this page on creation.
373      *
374      * @return help context id
375      */

376     protected abstract String JavaDoc getHelpContextId();
377
378     protected void connectToFolder(final TabItem item, TabFolder folder) {
379         folder.addSelectionListener(new SelectionAdapter() {
380             public void widgetSelected(SelectionEvent e) {
381                 if (e.item == item) {
382                     //remove ant objects whose library has been removed
383
setInput(getContents(true));
384                 }
385             }
386         });
387     }
388 }
389
Popular Tags