KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > TagSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13  
14 import java.util.*;
15 import java.util.List JavaDoc;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.jface.action.*;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.viewers.*;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.*;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.team.internal.ccvs.core.*;
27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
28 import org.eclipse.team.internal.ccvs.ui.actions.CVSAction;
29 import org.eclipse.team.internal.ccvs.ui.merge.ProjectElement;
30 import org.eclipse.team.internal.ccvs.ui.merge.TagElement;
31 import org.eclipse.team.internal.ccvs.ui.merge.ProjectElement.ProjectElementSorter;
32 import org.eclipse.team.internal.ccvs.ui.repo.*;
33 import org.eclipse.ui.help.WorkbenchHelp;
34 import org.eclipse.ui.model.WorkbenchContentProvider;
35 import org.eclipse.ui.model.WorkbenchLabelProvider;
36
37 /**
38  * Dialog to prompt the user to choose a tag for a selected resource
39  */

40 public class TagSelectionDialog extends Dialog{
41     private ICVSFolder[] folders;
42     private int includeFlags;
43     private CVSTag result;
44     private String JavaDoc helpContext;
45     private IStructuredSelection selection;
46     
47     public static final int INCLUDE_HEAD_TAG = ProjectElement.INCLUDE_HEAD_TAG;
48     public static final int INCLUDE_BASE_TAG = ProjectElement.INCLUDE_BASE_TAG;
49     public static final int INCLUDE_BRANCHES = ProjectElement.INCLUDE_BRANCHES;
50     public static final int INCLUDE_VERSIONS = ProjectElement.INCLUDE_VERSIONS;
51     public static final int INCLUDE_DATES = ProjectElement.INCLUDE_DATES;
52     public static final int INCLUDE_ALL_TAGS = ProjectElement.INCLUDE_ALL_TAGS;
53     
54     // widgets;
55
private TreeViewer tagTree;
56     private Button okButton;
57     
58     // dialog title, should indicate the action in which the tag selection
59
// dialog is being shown
60
private String JavaDoc title;
61     private String JavaDoc message;
62     
63     private boolean recurse = true;
64     private boolean showRecurse;
65     
66     // constants
67
private static final int SIZING_DIALOG_WIDTH = 400;
68     private static final int SIZING_DIALOG_HEIGHT = 250;
69     
70     public static CVSTag getTagToCompareWith(Shell shell, IProject[] projects) {
71         return getTagToCompareWith(shell, getCVSFoldersFor(projects));
72     }
73         
74     public static CVSTag getTagToCompareWith(Shell shell, ICVSFolder[] folders) {
75         TagSelectionDialog dialog = new TagSelectionDialog(shell, folders,
76             Policy.bind("CompareWithTagAction.message"), //$NON-NLS-1$
77
Policy.bind("TagSelectionDialog.Select_a_Tag_1"), //$NON-NLS-1$
78
TagSelectionDialog.INCLUDE_ALL_TAGS,
79             false, /* show recurse*/
80             IHelpContextIds.COMPARE_TAG_SELECTION_DIALOG);
81         dialog.setBlockOnOpen(true);
82         int result = dialog.open();
83         if (result == Dialog.CANCEL) {
84             return null;
85         }
86         return dialog.getResult();
87     }
88     /**
89      * Creates a new TagSelectionDialog.
90      * @param resource The resource to select a version for.
91      */

92     public TagSelectionDialog(Shell parentShell, IProject[] projects, String JavaDoc title, String JavaDoc message, int includeFlags, boolean showRecurse, String JavaDoc helpContext) {
93         this(parentShell, getCVSFoldersFor(projects), title, message, includeFlags, showRecurse, helpContext); //$NON-NLS-1$
94
}
95     
96     private static ICVSFolder[] getCVSFoldersFor(IProject[] projects) {
97         ICVSFolder[] folders = new ICVSFolder[projects.length];
98         for (int i = 0; i < projects.length; i++) {
99             folders[i] = CVSWorkspaceRoot.getCVSFolderFor(projects[i]);
100         }
101         return folders;
102     }
103     
104     /**
105      * Creates a new TagSelectionDialog.
106      * @param resource The resource to select a version for.
107      */

108     public TagSelectionDialog(Shell parentShell, ICVSFolder[] folders, String JavaDoc title, String JavaDoc message, int includeFlags, boolean showRecurse, String JavaDoc helpContext) {
109         super(parentShell);
110         this.folders = folders;
111         this.title = title;
112         this.message = message;
113         this.includeFlags = includeFlags;
114         this.showRecurse = showRecurse;
115         this.helpContext = helpContext;
116         setShellStyle(getShellStyle() | SWT.RESIZE);
117     }
118     
119     /* (non-Javadoc)
120      * Method declared on Window.
121      */

122     protected void configureShell(Shell newShell) {
123         super.configureShell(newShell);
124         newShell.setText(title);
125     }
126     
127     /**
128      * Creates this window's widgetry.
129      * <p>
130      * The default implementation of this framework method
131      * creates this window's shell (by calling <code>createShell</code>),
132      * its control (by calling <code>createContents</code>),
133      * and initializes this window's shell bounds
134      * (by calling <code>initializeBounds</code>).
135      * This framework method may be overridden; however,
136      * <code>super.create</code> must be called.
137      * </p>
138      */

139     public void create() {
140         super.create();
141         initialize();
142     }
143     
144     /**
145      * Add buttons to the dialog's button bar.
146      *
147      * @param parent the button bar composite
148      */

149     protected void createButtonsForButtonBar(Composite parent) {
150         // create OK and Cancel buttons by default
151
okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
152         okButton.setEnabled(false);
153         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
154     }
155     
156     /**
157      * Creates and returns the contents of the upper part
158      * of this dialog (above the button bar).
159      * <p>
160      * The default implementation of this framework method
161      * creates and returns a new <code>Composite</code> with
162      * standard margins and spacing.
163      * Subclasses should override.
164      * </p>
165      *
166      * @param the parent composite to contain the dialog area
167      * @return the dialog area control
168      */

169     protected Control createDialogArea(Composite parent) {
170         Composite top = (Composite)super.createDialogArea(parent);
171         // Add F1 help
172
if (helpContext != null) {
173             WorkbenchHelp.setHelp(top, helpContext);
174         }
175         Composite inner = new Composite(top, SWT.NULL);
176         GridData data = new GridData(GridData.FILL_BOTH);
177         data.widthHint = SIZING_DIALOG_WIDTH;
178         data.heightHint = SIZING_DIALOG_HEIGHT;
179         inner.setLayoutData(data);
180         GridLayout layout = new GridLayout();
181         inner.setLayout(layout);
182         
183         Label l = new Label (inner, SWT.NONE);
184         l.setText(message); //$NON-NLS-1$
185

186         tagTree = createTree(inner);
187         tagTree.setInput(new ProjectElement(folders[0], includeFlags));
188         tagTree.setSorter(new ProjectElementSorter());
189         Runnable JavaDoc refresh = new Runnable JavaDoc() {
190             public void run() {
191                 getShell().getDisplay().syncExec(new Runnable JavaDoc() {
192                     public void run() {
193                         tagTree.refresh();
194                     }
195                 });
196             }
197         };
198         
199         // Create the popup menu
200
MenuManager menuMgr = new MenuManager();
201         Tree tree = tagTree.getTree();
202         Menu menu = menuMgr.createContextMenu(tree);
203         menuMgr.addMenuListener(new IMenuListener() {
204             public void menuAboutToShow(IMenuManager manager) {
205                 addMenuItemActions(manager);
206             }
207
208         });
209         menuMgr.setRemoveAllWhenShown(true);
210         tree.setMenu(menu);
211         
212         if(showRecurse) {
213             final Button recurseCheck = new Button(top, SWT.CHECK);
214             recurseCheck.setText(Policy.bind("TagSelectionDialog.recurseOption")); //$NON-NLS-1$
215
recurseCheck.addListener(SWT.Selection, new Listener() {
216                 public void handleEvent(Event event) {
217                     recurse = recurseCheck.getSelection();
218                 }
219             });
220             recurseCheck.setSelection(true);
221         }
222
223         
224         TagConfigurationDialog.createTagDefinitionButtons(getShell(), top, folders,
225                                                           convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT),
226                                                           convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH),
227                                                           refresh, refresh);
228         
229         Label seperator = new Label(top, SWT.SEPARATOR | SWT.HORIZONTAL);
230         data = new GridData (GridData.FILL_BOTH);
231         data.horizontalSpan = 2;
232         seperator.setLayoutData(data);
233         
234         updateEnablement();
235         Dialog.applyDialogFont(parent);
236         
237         return top;
238     }
239     
240     
241     /**
242      * Utility method that creates a label instance
243      * and sets the default layout data.
244      *
245      * @param parent the parent for the new label
246      * @param text the text for the new label
247      * @return the new label
248      */

249     protected Label createLabel(Composite parent, String JavaDoc text) {
250         Label label = new Label(parent, SWT.LEFT);
251         label.setText(text);
252         GridData data = new GridData();
253         data.horizontalSpan = 1;
254         data.horizontalAlignment = GridData.FILL;
255         label.setLayoutData(data);
256         return label;
257     }
258     
259     protected TreeViewer createTree(Composite parent) {
260         Tree tree = new Tree(parent, SWT.MULTI | SWT.BORDER);
261         tree.setLayoutData(new GridData(GridData.FILL_BOTH));
262         TreeViewer result = new TreeViewer(tree);
263         result.setContentProvider(new WorkbenchContentProvider());
264         result.setLabelProvider(new WorkbenchLabelProvider());
265         result.addSelectionChangedListener(new ISelectionChangedListener() {
266             public void selectionChanged(SelectionChangedEvent event) {
267                 updateEnablement();
268             }
269         });
270         // select and close on double click
271
// To do: use defaultselection instead of double click
272
result.getTree().addMouseListener(new MouseAdapter() {
273             public void mouseDoubleClick(MouseEvent e) {
274                 IStructuredSelection selection = (IStructuredSelection)tagTree.getSelection();
275                 if (!selection.isEmpty() && (selection.getFirstElement() instanceof TagElement)) {
276                     okPressed();
277                 }
278             }
279         });
280         result.getControl().addKeyListener(new KeyListener() {
281             public void keyPressed(KeyEvent event) {
282                 handleKeyPressed(event);
283             }
284             public void keyReleased(KeyEvent event) {
285                 handleKeyReleased(event);
286             }
287         });
288         result.setSorter(new RepositorySorter());
289         return result;
290     }
291     
292     /**
293      * Returns the selected tag.
294      */

295     public CVSTag getResult() {
296         return result;
297     }
298     
299     public boolean getRecursive() {
300         return recurse;
301     }
302
303     /**
304      * Initializes the dialog contents.
305      */

306     protected void initialize() {
307         okButton.setEnabled(false);
308     }
309     
310     /**
311      * Notifies that the ok button of this dialog has been pressed.
312      * <p>
313      * The default implementation of this framework method sets
314      * this dialog's return code to <code>Window.OK</code>
315      * and closes the dialog. Subclasses may override.
316      * </p>
317      */

318     protected void okPressed() {
319         IStructuredSelection selection = (IStructuredSelection)tagTree.getSelection();
320         Object JavaDoc o = selection.getFirstElement();
321         TagElement element = (TagElement)o;
322         result = element.getTag();
323         super.okPressed();
324     }
325
326     
327     /**
328      * Updates the dialog enablement.
329      */

330     protected void updateEnablement() {
331         selection = (IStructuredSelection)tagTree.getSelection();
332         if(okButton!=null) {
333             if (selection.isEmpty() || selection.size() != 1 || !(selection.getFirstElement() instanceof TagElement)) {
334                 okButton.setEnabled(false);
335             } else {
336                 okButton.setEnabled(true);
337             }
338         }
339     }
340
341     public void handleKeyPressed(KeyEvent event) {
342         if (event.character == SWT.DEL && event.stateMask == 0) {
343             deleteDateTag();
344         }
345     }
346     private void deleteDateTag() {
347         TagElement[] selectedDateTagElements = getSelectedDateTagElement();
348         if (selectedDateTagElements.length == 0) return;
349         for(int i = 0; i < selectedDateTagElements.length; i++){
350             RepositoryManager mgr = CVSUIPlugin.getPlugin().getRepositoryManager();
351             CVSTag tag = selectedDateTagElements[i].getTag();
352             if(tag.getType() == CVSTag.DATE){
353                 mgr.removeDateTag(getLocation(),tag);
354             }
355         }
356         tagTree.refresh();
357         updateEnablement();
358     }
359
360     protected void handleKeyReleased(KeyEvent event) {
361     }
362     
363     private ICVSRepositoryLocation getLocation(){
364         RepositoryManager mgr = CVSUIPlugin.getPlugin().getRepositoryManager();
365         ICVSRepositoryLocation location = mgr.getRepositoryLocationFor( folders[0]);
366         return location;
367     }
368     
369     /**
370      * Returns the selected date tag elements
371      */

372     private TagElement[] getSelectedDateTagElement() {
373         ArrayList dateTagElements = null;
374         if (selection!=null && !selection.isEmpty()) {
375             dateTagElements = new ArrayList();
376             Iterator elements = selection.iterator();
377             while (elements.hasNext()) {
378                 Object JavaDoc next = CVSAction.getAdapter(elements.next(), TagElement.class);
379                 if (next instanceof TagElement) {
380                     if(((TagElement)next).getTag().getType() == CVSTag.DATE){
381                         dateTagElements.add(next);
382                     }
383                 }
384             }
385         }
386         if (dateTagElements != null && !dateTagElements.isEmpty()) {
387             TagElement[] result = new TagElement[dateTagElements.size()];
388             dateTagElements.toArray(result);
389             return result;
390         }
391         return new TagElement[0];
392     }
393     private void addDateTag(CVSTag tag){
394         if(tag == null) return;
395         List JavaDoc dateTags = new ArrayList();
396         dateTags.addAll(Arrays.asList(CVSUIPlugin.getPlugin().getRepositoryManager().getKnownTags(folders[0],CVSTag.DATE)));
397         if(!dateTags.contains( tag)){
398             CVSUIPlugin.getPlugin().getRepositoryManager().addDateTag(getLocation(),tag);
399         }
400         try {
401             tagTree.getControl().setRedraw(false);
402             tagTree.refresh();
403             // TODO: Hack to instantiate the model before revealing the selection
404
Object JavaDoc[] expanded = tagTree.getExpandedElements();
405             tagTree.expandToLevel(2);
406             tagTree.collapseAll();
407             for (int i = 0; i < expanded.length; i++) {
408                 Object JavaDoc object = expanded[i];
409                 tagTree.expandToLevel(object, 1);
410             }
411             // Reveal the selection
412
tagTree.reveal(new TagElement(tag));
413             tagTree.setSelection(new StructuredSelection(new TagElement(tag)));
414         } finally {
415             tagTree.getControl().setRedraw(true);
416         }
417         updateEnablement();
418     }
419     private void addMenuItemActions(IMenuManager manager) {
420         manager.add(new Action(Policy.bind("TagSelectionDialog.0")) { //$NON-NLS-1$
421
public void run() {
422                 CVSTag dateTag = NewDateTagAction.getDateTag(getShell(), CVSUIPlugin.getPlugin().getRepositoryManager().getRepositoryLocationFor(folders[0]));
423                 addDateTag(dateTag);
424             }
425         });
426         if(getSelectedDateTagElement().length > 0){
427             manager.add(new Action(Policy.bind("TagSelectionDialog.1")) { //$NON-NLS-1$
428
public void run() {
429                     deleteDateTag();
430                 }
431             });
432         }
433
434     }
435
436 }
437
Popular Tags