KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > ScopeSetDialog


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.help.ui.internal.views;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.help.ui.internal.*;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.preference.*;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.layout.*;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.ui.dialogs.ListDialog;
25
26 /**
27  * Scope dialog for federated search.
28  */

29 public class ScopeSetDialog extends ListDialog {
30     private ScopeSetManager manager;
31     private EngineDescriptorManager descManager;
32     private static final int NEW_ID = IDialogConstants.CLIENT_ID + 1;
33     private static final int EDIT_ID = IDialogConstants.CLIENT_ID + 2;
34     private static final int RENAME_ID = IDialogConstants.CLIENT_ID +3;
35     private static final int REMOVE_ID = IDialogConstants.CLIENT_ID + 4;
36     private Button editButton;
37     private Button renameButton;
38     private Button removeButton;
39     private ArrayList JavaDoc sets;
40     private ArrayList JavaDoc operations;
41     
42     private abstract class PendingOperation {
43         ScopeSet set;
44         public PendingOperation(ScopeSet set) {
45             this.set = set;
46         }
47         public abstract void commit();
48         public abstract void cancel();
49     }
50     
51     private class AddOperation extends PendingOperation {
52         public AddOperation(ScopeSet set) {
53             super(set);
54         }
55         public void commit() {
56             manager.add(set);
57         }
58         public void cancel() {
59             set.dispose();
60         }
61     }
62
63     private class RenameOperation extends PendingOperation {
64         private String JavaDoc newName;
65         public RenameOperation(ScopeSet set, String JavaDoc newName) {
66             super(set);
67             this.newName = newName;
68         }
69         public void commit() {
70             this.set.setName(newName);
71         }
72         public void cancel() {
73         }
74     }
75
76 /*
77     private class EditOperation extends PendingOperation {
78         public EditOperation(ScopeSet set) {
79             super(set);
80         }
81         public void commit() {
82         }
83         public void cancel() {
84         }
85     }
86 */

87
88     private class RemoveOperation extends PendingOperation {
89         public RemoveOperation(ScopeSet set) {
90             super(set);
91         }
92         public void commit() {
93             manager.remove(set);
94         }
95         public void cancel() {
96         }
97     }
98     
99     private class ScopeContentProvider implements IStructuredContentProvider {
100         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
101             return sets.toArray();
102         }
103
104         /* (non-Javadoc)
105          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
106          */

107         public void dispose() {
108         }
109
110         /* (non-Javadoc)
111          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
112          */

113         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
114         }
115     }
116     
117     private class ScopeLabelProvider extends LabelProvider {
118         public String JavaDoc getText(Object JavaDoc obj) {
119             String JavaDoc name = findNewName((ScopeSet)obj);
120             if (name!=null)
121                 return name;
122             return ((ScopeSet)obj).getName();
123         }
124         private String JavaDoc findNewName(ScopeSet set) {
125             PendingOperation op = findOperation(set, RenameOperation.class);
126             if (op!=null) {
127                 RenameOperation rop = (RenameOperation)op;
128                 return rop.newName;
129             }
130             return null;
131         }
132         public Image getImage(Object JavaDoc obj) {
133             return HelpUIResources.getImage(IHelpUIConstants.IMAGE_SCOPE_SET);
134         }
135     }
136
137     /**
138      * @param parent
139      */

140     public ScopeSetDialog(Shell parent, ScopeSetManager manager, EngineDescriptorManager descManager) {
141         super(parent);
142         this.manager = manager;
143         this.descManager = descManager;
144         this.sets = extractSets(manager.getScopeSets(false));
145         setContentProvider(new ScopeContentProvider());
146         setLabelProvider(new ScopeLabelProvider());
147         setInitialSelections(new Object JavaDoc[] { manager.getActiveSet() });
148     }
149     
150     private ArrayList JavaDoc extractSets(ScopeSet[] array) {
151         ArrayList JavaDoc list = new ArrayList JavaDoc();
152         for (int i=0; i<array.length; i++) {
153             list.add(array[i]);
154         }
155         return list;
156     }
157     
158     protected Control createDialogArea(Composite container) {
159         Composite listContainer = (Composite)super.createDialogArea(container);
160         createEditingButtons(listContainer);
161         getTableViewer().addSelectionChangedListener(new ISelectionChangedListener() {
162             public void selectionChanged(SelectionChangedEvent event) {
163                 updateButtons();
164             }
165         });
166         return listContainer;
167     }
168     
169     private void createEditingButtons(Composite composite) {
170         Composite buttonComposite= new Composite(composite, SWT.RIGHT);
171         GridLayout layout= new GridLayout();
172         layout.numColumns= 2;
173         buttonComposite.setLayout(layout);
174         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL);
175         data.grabExcessHorizontalSpace= true;
176         composite.setData(data);
177         createButton(buttonComposite, NEW_ID, Messages.ScopeSetDialog_new, false);
178         renameButton = createButton(buttonComposite, RENAME_ID, Messages.ScopeSetDialog_rename, false);
179         editButton = createButton(buttonComposite, EDIT_ID, Messages.ScopeSetDialog_edit, false);
180         removeButton = createButton(buttonComposite, REMOVE_ID, Messages.ScopeSetDialog_remove, false);
181         updateButtons();
182     }
183     
184     public ScopeSet getActiveSet() {
185         Object JavaDoc [] result = getResult();
186         if (result!=null && result.length>0)
187             return (ScopeSet)result[0];
188         return null;
189     }
190     protected void okPressed() {
191         if (operations!=null) {
192             for (int i=0; i<operations.size(); i++) {
193                 PendingOperation operation = (PendingOperation)operations.get(i);
194                 operation.commit();
195             }
196             operations = null;
197         }
198         super.okPressed();
199     }
200     
201     protected void cancelPressed() {
202         if (operations!=null) {
203             for (int i=0; i<operations.size(); i++) {
204                 PendingOperation operation = (PendingOperation)operations.get(i);
205                 operation.cancel();
206             }
207             operations = null;
208         }
209         super.cancelPressed();
210     }
211     
212     protected void buttonPressed(int buttonId) {
213         switch (buttonId) {
214         case NEW_ID:
215             doNew();
216             break;
217         case EDIT_ID:
218             doEdit();
219             break;
220         case RENAME_ID:
221             doRename();
222             break;
223         case REMOVE_ID:
224             doRemove();
225             break;
226         }
227         super.buttonPressed(buttonId);
228     }
229     
230     private void doNew() {
231         IStructuredSelection ssel = (IStructuredSelection)getTableViewer().getSelection();
232         ScopeSet set = (ScopeSet)ssel.getFirstElement();
233         ScopeSet newSet = new ScopeSet(set);
234         String JavaDoc name = getNewName(newSet.getName(), false);
235         if (name!=null) {
236             newSet.setName(name);
237             scheduleOperation(new AddOperation(newSet));
238             sets.add(newSet);
239             getTableViewer().refresh();
240             updateButtons();
241         }
242     }
243
244     private void doEdit() {
245         IStructuredSelection ssel = (IStructuredSelection)getTableViewer().getSelection();
246         ScopeSet set = (ScopeSet)ssel.getFirstElement();
247         if (set!=null) {
248             PreferenceManager manager = new ScopePreferenceManager(descManager, set);
249             PreferenceDialog dialog = new ScopePreferenceDialog(getShell(), manager, descManager, set.isEditable());
250             dialog.setPreferenceStore(set.getPreferenceStore());
251             dialog.create();
252             dialog.getShell().setText(NLS.bind(Messages.ScopePreferenceDialog_wtitle, set.getName()));
253             dialog.open();
254         }
255     }
256
257     private void doRename() {
258         IStructuredSelection ssel = (IStructuredSelection)getTableViewer().getSelection();
259         ScopeSet set = (ScopeSet)ssel.getFirstElement();
260         if (set!=null) {
261             RenameOperation rop = (RenameOperation)findOperation(set, RenameOperation.class);
262             String JavaDoc oldName = rop!=null?rop.newName:set.getName();
263             String JavaDoc newName = getNewName(oldName, true);
264             if (newName!=null) {
265                 if (rop!=null)
266                     rop.newName = newName;
267                 else
268                     scheduleOperation(new RenameOperation(set, newName));
269                 getTableViewer().update(set, null);
270                 updateButtons();
271             }
272         }
273     }
274
275     private String JavaDoc getNewName(String JavaDoc oldName, boolean isRename) {
276         RenameDialog dialog = new RenameDialog(getShell(), oldName);
277         for (int i=0; i<sets.size(); i++) {
278             ScopeSet set = (ScopeSet)sets.get(i);
279             dialog.addOldName(set.getName());
280         }
281         dialog.create();
282         String JavaDoc dialogTitle = isRename ?
283           Messages.RenameDialog_wtitle : Messages.NewDialog_wtitle;
284         dialog.getShell().setText(dialogTitle);
285         if (dialog.open()==RenameDialog.OK) {
286             return dialog.getNewName();
287         }
288         return null;
289     }
290     
291     private void doRemove() {
292         IStructuredSelection ssel = (IStructuredSelection)getTableViewer().getSelection();
293         ScopeSet set = (ScopeSet)ssel.getFirstElement();
294         if (set!=null) {
295             scheduleOperation(new RemoveOperation(set));
296             sets.remove(set);
297             getTableViewer().refresh();
298             updateButtons();
299         }
300     }
301     
302     private void scheduleOperation(PendingOperation op) {
303         if (operations==null)
304             operations = new ArrayList JavaDoc();
305         operations.add(op);
306     }
307     
308     private void updateButtons() {
309         IStructuredSelection ssel = (IStructuredSelection)getTableViewer().getSelection();
310         editButton.setEnabled(ssel.isEmpty()==false);
311         ScopeSet set = (ScopeSet)ssel.getFirstElement();
312         boolean editableSet = set!=null && set.isEditable() && !set.isImplicit();
313         removeButton.setEnabled(editableSet);
314         renameButton.setEnabled(editableSet);
315         Button okButton = getOkButton();
316         if (okButton!=null)
317             okButton.setEnabled(set!=null);
318     }
319     
320     private PendingOperation findOperation(ScopeSet set, Class JavaDoc type) {
321         if (operations!=null) {
322             for (int i=0; i<operations.size(); i++) {
323                 PendingOperation op = (PendingOperation)operations.get(i);
324                 if (op.getClass().equals(type)) {
325                     if (op.set.equals(set))
326                         return op;
327                 }
328             }
329         }
330         return null;
331     }
332 }
333
Popular Tags