KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.IDialogSettings;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27 import org.eclipse.jface.viewers.CheckboxTableViewer;
28 import org.eclipse.jface.viewers.ColumnWeightData;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.TableLayout;
33 import org.eclipse.jface.viewers.TreeViewer;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.jface.viewers.ViewerSorter;
36 import org.eclipse.jface.window.Window;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.events.SelectionAdapter;
39 import org.eclipse.swt.events.SelectionEvent;
40 import org.eclipse.swt.events.SelectionListener;
41 import org.eclipse.swt.graphics.Point;
42 import org.eclipse.swt.graphics.Rectangle;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Button;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.Event;
50 import org.eclipse.swt.widgets.Label;
51 import org.eclipse.swt.widgets.Listener;
52 import org.eclipse.swt.widgets.Shell;
53 import org.eclipse.swt.widgets.Table;
54 import org.eclipse.swt.widgets.TableColumn;
55 import org.eclipse.swt.widgets.Tree;
56 import org.eclipse.team.core.TeamException;
57 import org.eclipse.team.internal.ccvs.core.CVSException;
58 import org.eclipse.team.internal.ccvs.core.CVSTag;
59 import org.eclipse.team.internal.ccvs.core.ICVSFile;
60 import org.eclipse.team.internal.ccvs.core.ICVSFolder;
61 import org.eclipse.team.internal.ccvs.core.ILogEntry;
62 import org.eclipse.team.internal.ccvs.ui.merge.ProjectElement;
63 import org.eclipse.team.internal.ccvs.ui.merge.TagElement;
64 import org.eclipse.team.internal.ccvs.ui.merge.ProjectElement.ProjectElementSorter;
65 import org.eclipse.team.internal.ccvs.ui.model.CVSFileElement;
66 import org.eclipse.team.internal.ccvs.ui.model.CVSFolderElement;
67 import org.eclipse.team.internal.ccvs.ui.model.CVSRootFolderElement;
68 import org.eclipse.team.internal.ccvs.ui.model.RemoteContentProvider;
69 import org.eclipse.team.internal.ccvs.ui.repo.NewDateTagAction;
70 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryManager;
71 import org.eclipse.ui.PlatformUI;
72 import org.eclipse.ui.help.WorkbenchHelp;
73 import org.eclipse.ui.model.WorkbenchContentProvider;
74 import org.eclipse.ui.model.WorkbenchLabelProvider;
75
76 /**
77  * Allows configuration of the CVS tags that are shown within the workbench.
78  */

79 public class TagConfigurationDialog extends Dialog {
80     
81     // show the resource contained within the roots
82
private TreeViewer cvsResourceTree;
83     
84     // shows the tags found on the selected resources
85
private CheckboxTableViewer cvsTagTree;
86     
87     // shows the defined tags for the given root
88
private TreeViewer cvsDefinedTagsTree;
89     
90     // remember the root element in the defined tags tree
91
private ProjectElement cvsDefinedTagsRootElement;
92     
93     // list of auto-refresh files
94
private org.eclipse.swt.widgets.List autoRefreshFileList;
95     
96     // folders from which their children files can be examined for tags
97
private ICVSFolder[] roots;
98     private ICVSFolder root;
99     
100     // enable selecting auto-refresh files
101
private boolean allowSettingAutoRefreshFiles = true;
102     
103     // preference keys
104
private final String JavaDoc ALLOWREFRESH_WIDTH_KEY = "AllowRefreshWidth"; //$NON-NLS-1$
105
private final String JavaDoc ALLOWREFRESH_HEIGHT_KEY = "AllowRefreshHeight"; //$NON-NLS-1$
106
private final String JavaDoc NOREFRESH_WIDTH_KEY = "NoRefreshWidth"; //$NON-NLS-1$
107
private final String JavaDoc NOREFRESH_HEIGHT_KEY = "NoRefreshHeight"; //$NON-NLS-1$
108

109     // buttons
110
private Button addSelectedTagsButton;
111     private Button addSelectedFilesButton;
112     private Button removeFileButton;
113     private Button removeTagButton;
114     
115     // dialogs settings that are persistent between workbench sessions
116
private IDialogSettings settings;
117     
118     class FileSorter extends ViewerSorter {
119         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
120             boolean oneIsFile = e1 instanceof CVSFileElement;
121             boolean twoIsFile = e2 instanceof CVSFileElement;
122             if (oneIsFile != twoIsFile) {
123                 return oneIsFile ? 1 : -1;
124             }
125             return super.compare(viewer, e1, e2);
126         }
127     }
128     
129     public TagConfigurationDialog(Shell shell, ICVSFolder[] roots) {
130         super(shell);
131         setShellStyle(SWT.CLOSE|SWT.RESIZE|SWT.APPLICATION_MODAL);
132         this.roots = roots;
133         this.root = roots[0];
134         if(roots.length>1) {
135             allowSettingAutoRefreshFiles = false;
136         }
137         
138         IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
139         this.settings = workbenchSettings.getSection("TagConfigurationDialog");//$NON-NLS-1$
140
if (settings == null) {
141             this.settings = workbenchSettings.addNewSection("TagConfigurationDialog");//$NON-NLS-1$
142
}
143     }
144
145     /**
146      * @see Window#configureShell(Shell)
147      */

148     protected void configureShell(Shell newShell) {
149         super.configureShell(newShell);
150         if(roots.length==1) {
151             newShell.setText(Policy.bind("TagConfigurationDialog.1", roots[0].getName())); //$NON-NLS-1$
152
} else {
153             newShell.setText(Policy.bind("TagConfigurationDialog.2", Integer.toString(roots.length))); //$NON-NLS-1$ //$NON-NLS-2$
154
}
155     }
156
157     /**
158      * @see Dialog#createDialogArea(Composite)
159      */

160     protected Control createDialogArea(Composite parent) {
161         Composite shell = new Composite(parent, SWT.NONE);
162         GridData data = new GridData (GridData.FILL_BOTH);
163         shell.setLayoutData(data);
164         GridLayout gridLayout = new GridLayout();
165         gridLayout.numColumns = 2;
166         gridLayout.makeColumnsEqualWidth = true;
167         gridLayout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
168         gridLayout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
169         shell.setLayout (gridLayout);
170         
171         Composite comp = new Composite(shell, SWT.NULL);
172         gridLayout = new GridLayout();
173         gridLayout.numColumns = 1;
174         gridLayout.marginWidth = 0;
175         gridLayout.marginHeight = 0;
176         comp.setLayout(gridLayout);
177         comp.setLayoutData(new GridData(GridData.FILL_BOTH));
178         
179         Label cvsResourceTreeLabel = new Label(comp, SWT.NONE);
180         cvsResourceTreeLabel.setText(Policy.bind("TagConfigurationDialog.5")); //$NON-NLS-1$
181
data = new GridData();
182         data.horizontalSpan = 1;
183         cvsResourceTreeLabel.setLayoutData(data);
184     
185         Tree tree = new Tree(comp, SWT.BORDER | SWT.MULTI);
186         cvsResourceTree = new TreeViewer (tree);
187         cvsResourceTree.setContentProvider(new RemoteContentProvider());
188         cvsResourceTree.setLabelProvider(new WorkbenchLabelProvider());
189         data = new GridData (GridData.FILL_BOTH);
190         data.heightHint = 150;
191         data.horizontalSpan = 1;
192         cvsResourceTree.getTree().setLayoutData(data);
193         if(roots.length==1) {
194             cvsResourceTree.setInput(new CVSFolderElement(roots[0], false /*don't include unmanaged resources*/));
195         } else {
196             cvsResourceTree.setInput(new CVSRootFolderElement(roots));
197         }
198         cvsResourceTree.setSorter(new FileSorter());
199         cvsResourceTree.addSelectionChangedListener(new ISelectionChangedListener() {
200             public void selectionChanged(SelectionChangedEvent event) {
201                 updateShownTags();
202                 updateEnablements();
203             }
204         });
205     
206     
207         comp = new Composite(shell, SWT.NULL);
208         gridLayout = new GridLayout();
209         gridLayout.numColumns = 1;
210         gridLayout.marginWidth = 0;
211         gridLayout.marginHeight = 0;
212         comp.setLayout(gridLayout);
213         comp.setLayoutData(new GridData(GridData.FILL_BOTH));
214     
215         Label cvsTagTreeLabel = new Label(comp, SWT.NONE);
216         cvsTagTreeLabel.setText(Policy.bind("TagConfigurationDialog.6")); //$NON-NLS-1$
217
data = new GridData();
218         data.horizontalSpan = 1;
219         cvsTagTreeLabel.setLayoutData(data);
220         
221         final Table table = new Table(comp, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.CHECK);
222         data = new GridData(GridData.FILL_BOTH);
223         data.heightHint = 150;
224         data.horizontalSpan = 1;
225         table.setLayoutData(data);
226         TableLayout layout = new TableLayout();
227         layout.addColumnData(new ColumnWeightData(60, true));
228         table.setLayout(layout);
229         TableColumn col = new TableColumn(table, SWT.NONE);
230         col.setResizable(true);
231         cvsTagTree = new CheckboxTableViewer(table);
232         cvsTagTree.setContentProvider(new WorkbenchContentProvider());
233         cvsTagTree.setLabelProvider(new WorkbenchLabelProvider());
234         cvsTagTree.addSelectionChangedListener(new ISelectionChangedListener() {
235             public void selectionChanged(SelectionChangedEvent event) {
236                 updateEnablements();
237             }
238         });
239         
240         Composite selectComp = new Composite(comp, SWT.NONE);
241         GridLayout selectLayout = new GridLayout(2, true);
242         selectLayout.marginHeight = selectLayout.marginWidth = 0;
243         selectComp.setLayout(selectLayout);
244         selectComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
245         Button selectAllButton = new Button(selectComp, SWT.PUSH);
246         selectAllButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
247         selectAllButton.setText(Policy.bind("ReleaseCommentDialog.selectAll")); //$NON-NLS-1$
248
selectAllButton.addSelectionListener(new SelectionAdapter() {
249             public void widgetSelected(SelectionEvent e) {
250                 int nItems = table.getItemCount();
251                 for (int j=0; j<nItems; j++)
252                     table.getItem(j).setChecked(true);
253             }
254         });
255         Button deselectAllButton = new Button(selectComp, SWT.PUSH);
256         deselectAllButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
257         deselectAllButton.setText(Policy.bind("ReleaseCommentDialog.deselectAll")); //$NON-NLS-1$
258
deselectAllButton.addSelectionListener(new SelectionAdapter() {
259             public void widgetSelected(SelectionEvent e) {
260                 int nItems = table.getItemCount();
261                 for (int j=0; j<nItems; j++)
262                     table.getItem(j).setChecked(false);
263             }
264         });
265         
266         cvsTagTree.setSorter(new ViewerSorter() {
267             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
268                 if (!(e1 instanceof TagElement) || !(e2 instanceof TagElement)) return super.compare(viewer, e1, e2);
269                 CVSTag tag1 = ((TagElement)e1).getTag();
270                 CVSTag tag2 = ((TagElement)e2).getTag();
271                 int type1 = tag1.getType();
272                 int type2 = tag2.getType();
273                 if (type1 != type2) {
274                     return type1 - type2;
275                 }
276                 // Sort in reverse order so larger numbered versions are at the top
277
return -tag1.compareTo(tag2);
278             }
279         });
280         
281         Composite rememberedTags = new Composite(shell, SWT.NONE);
282         data = new GridData (GridData.FILL_BOTH);
283         data.horizontalSpan = 2;
284         rememberedTags.setLayoutData(data);
285         gridLayout = new GridLayout();
286         gridLayout.numColumns = 2;
287         gridLayout.marginHeight = 0;
288         gridLayout.marginWidth = 0;
289         rememberedTags.setLayout (gridLayout);
290     
291         Label rememberedTagsLabel = new Label (rememberedTags, SWT.NONE);
292         rememberedTagsLabel.setText (Policy.bind("TagConfigurationDialog.7")); //$NON-NLS-1$
293
data = new GridData ();
294         data.horizontalSpan = 2;
295         rememberedTagsLabel.setLayoutData (data);
296         
297         tree = new Tree(rememberedTags, SWT.BORDER | SWT.MULTI);
298         cvsDefinedTagsTree = new TreeViewer (tree);
299         cvsDefinedTagsTree.setContentProvider(new WorkbenchContentProvider());
300         cvsDefinedTagsTree.setLabelProvider(new WorkbenchLabelProvider());
301         data = new GridData (GridData.FILL_BOTH);
302         data.heightHint = 100;
303         data.horizontalAlignment = GridData.FILL;
304         data.grabExcessHorizontalSpace = true;
305         cvsDefinedTagsTree.getTree().setLayoutData(data);
306         cvsDefinedTagsRootElement = new ProjectElement(roots[0], ProjectElement.INCLUDE_BRANCHES | ProjectElement.INCLUDE_VERSIONS |ProjectElement.INCLUDE_DATES);
307         cvsDefinedTagsRootElement.getBranches().add(CVSUIPlugin.getPlugin().getRepositoryManager().getKnownTags(root, CVSTag.BRANCH));
308         cvsDefinedTagsRootElement.getVersions().add(CVSUIPlugin.getPlugin().getRepositoryManager().getKnownTags(root, CVSTag.VERSION));
309         cvsDefinedTagsRootElement.getDates().add(CVSUIPlugin.getPlugin().getRepositoryManager().getKnownTags(root, CVSTag.DATE));
310         cvsDefinedTagsTree.setInput(cvsDefinedTagsRootElement);
311         cvsDefinedTagsTree.addSelectionChangedListener(new ISelectionChangedListener() {
312             public void selectionChanged(SelectionChangedEvent event) {
313                 updateEnablements();
314             }
315         });
316         cvsDefinedTagsTree.setSorter(new ProjectElementSorter());
317     
318         Composite buttonComposite = new Composite(rememberedTags, SWT.NONE);
319         data = new GridData ();
320         data.verticalAlignment = GridData.BEGINNING;
321         buttonComposite.setLayoutData(data);
322         gridLayout = new GridLayout();
323         gridLayout.marginHeight = 0;
324         gridLayout.marginWidth = 0;
325         buttonComposite.setLayout (gridLayout);
326         
327         addSelectedTagsButton = new Button (buttonComposite, SWT.PUSH);
328         addSelectedTagsButton.setText (Policy.bind("TagConfigurationDialog.8")); //$NON-NLS-1$
329
data = getStandardButtonData(addSelectedTagsButton);
330         data.horizontalAlignment = GridData.FILL;
331         addSelectedTagsButton.setLayoutData(data);
332         addSelectedTagsButton.addListener(SWT.Selection, new Listener() {
333                 public void handleEvent(Event event) {
334                     rememberCheckedTags();
335                     updateShownTags();
336                     updateEnablements();
337                 }
338             });
339         Button addDatesButton = new Button(buttonComposite, SWT.PUSH);
340         addDatesButton.setText(Policy.bind("TagConfigurationDialog.0")); //$NON-NLS-1$
341
data = getStandardButtonData(addDatesButton);
342         data.horizontalAlignment = GridData.FILL;
343         addDatesButton.setLayoutData(data);
344         addDatesButton.addListener(SWT.Selection, new Listener(){
345             public void handleEvent(Event event){
346                 CVSTag dateTag = NewDateTagAction.getDateTag(getShell(), CVSUIPlugin.getPlugin().getRepositoryManager().getRepositoryLocationFor(root));
347                 addDateTagsSelected(dateTag);
348                 updateShownTags();
349                 updateEnablements();
350             }
351         });
352         removeTagButton = new Button (buttonComposite, SWT.PUSH);
353         removeTagButton.setText (Policy.bind("TagConfigurationDialog.9")); //$NON-NLS-1$
354
data = getStandardButtonData(removeTagButton);
355         data.horizontalAlignment = GridData.FILL;
356         removeTagButton.setLayoutData(data);
357         removeTagButton.addListener(SWT.Selection, new Listener() {
358                 public void handleEvent(Event event) {
359                     deleteSelected();
360                     updateShownTags();
361                     updateEnablements();
362                 }
363             });
364             
365         Button removeAllTags = new Button (buttonComposite, SWT.PUSH);
366         removeAllTags.setText (Policy.bind("TagConfigurationDialog.10")); //$NON-NLS-1$
367
data = getStandardButtonData(removeAllTags);
368         data.horizontalAlignment = GridData.FILL;
369         removeAllTags.setLayoutData(data);
370         removeAllTags.addListener(SWT.Selection, new Listener() {
371                 public void handleEvent(Event event) {
372                     removeAllKnownTags();
373                     updateShownTags();
374                     updateEnablements();
375                 }
376             });
377         
378         if(allowSettingAutoRefreshFiles) {
379             Label explanation = new Label(rememberedTags, SWT.WRAP);
380             explanation.setText(Policy.bind("TagConfigurationDialog.11")); //$NON-NLS-1$
381
data = new GridData ();
382             data.horizontalSpan = 2;
383             //data.widthHint = 300;
384
explanation.setLayoutData(data);
385             
386             autoRefreshFileList = new org.eclipse.swt.widgets.List(rememberedTags, SWT.BORDER | SWT.MULTI);
387             data = new GridData ();
388             data.heightHint = 45;
389             data.horizontalAlignment = GridData.FILL;
390             data.grabExcessHorizontalSpace = true;
391             autoRefreshFileList.setLayoutData(data);
392             try {
393                 autoRefreshFileList.setItems(CVSUIPlugin.getPlugin().getRepositoryManager().getAutoRefreshFiles(roots[0]));
394             } catch (CVSException e) {
395                 autoRefreshFileList.setItems(new String JavaDoc[0]);
396                 CVSUIPlugin.log(e);
397             }
398             autoRefreshFileList.addSelectionListener(new SelectionListener() {
399                 public void widgetSelected(SelectionEvent e) {
400                     updateEnablements();
401                 }
402                 public void widgetDefaultSelected(SelectionEvent e) {
403                     updateEnablements();
404                 }
405             });
406     
407             Composite buttonComposite2 = new Composite(rememberedTags, SWT.NONE);
408             data = new GridData ();
409             data.verticalAlignment = GridData.BEGINNING;
410             buttonComposite2.setLayoutData(data);
411             gridLayout = new GridLayout();
412             gridLayout.marginHeight = 0;
413             gridLayout.marginWidth = 0;
414             buttonComposite2.setLayout (gridLayout);
415     
416             addSelectedFilesButton = new Button (buttonComposite2, SWT.PUSH);
417             addSelectedFilesButton.setText (Policy.bind("TagConfigurationDialog.12")); //$NON-NLS-1$
418
data = getStandardButtonData(addSelectedFilesButton);
419             data.horizontalAlignment = GridData.FILL;
420             addSelectedFilesButton.setLayoutData(data);
421             addSelectedFilesButton.addListener(SWT.Selection, new Listener() {
422                     public void handleEvent(Event event) {
423                         addSelectionToAutoRefreshList();
424                     }
425                 });
426                 
427             removeFileButton = new Button (buttonComposite2, SWT.PUSH);
428             removeFileButton.setText (Policy.bind("TagConfigurationDialog.13")); //$NON-NLS-1$
429
data = getStandardButtonData(removeFileButton);
430             data.horizontalAlignment = GridData.FILL;
431             removeFileButton.setLayoutData(data);
432             removeFileButton.addListener(SWT.Selection, new Listener() {
433                     public void handleEvent(Event event) {
434                         String JavaDoc[] selected = autoRefreshFileList.getSelection();
435                         for (int i = 0; i < selected.length; i++) {
436                             autoRefreshFileList.remove(selected[i]);
437                             autoRefreshFileList.setFocus();
438                         }
439                     }
440                 });
441             WorkbenchHelp.setHelp(autoRefreshFileList, IHelpContextIds.TAG_CONFIGURATION_REFRESHLIST);
442         }
443             
444         Label seperator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
445         data = new GridData (GridData.FILL_BOTH);
446         data.horizontalSpan = 2;
447         seperator.setLayoutData(data);
448     
449         WorkbenchHelp.setHelp(shell, IHelpContextIds.TAG_CONFIGURATION_OVERVIEW);
450     
451         updateEnablements();
452         Dialog.applyDialogFont(parent);
453         return shell;
454     }
455
456     private void updateShownTags() {
457         final CVSFileElement[] filesSelection = getSelectedFiles();
458         final Set JavaDoc tags = new HashSet JavaDoc();
459         if(filesSelection.length!=0) {
460             try {
461                 CVSUIPlugin.runWithProgress(getShell(), true /*cancelable*/, new IRunnableWithProgress() {
462                     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
463                         monitor.beginTask(Policy.bind("TagConfigurationDialog.22"), filesSelection.length); //$NON-NLS-1$
464
try {
465                             for (int i = 0; i < filesSelection.length; i++) {
466                                 ICVSFile file = filesSelection[i].getCVSFile();
467                                 tags.addAll(Arrays.asList(getTagsFor(file, Policy.subMonitorFor(monitor, 1))));
468                             }
469                         } catch (TeamException e) {
470                             // ignore the exception
471
} finally {
472                             monitor.done();
473                         }
474                     }
475                 });
476             } catch (InterruptedException JavaDoc e) {
477                 // operation cancelled
478
} catch (InvocationTargetException JavaDoc e) {
479                 // can't happen since we're ignoring all possible exceptions
480
}
481             cvsTagTree.getTable().removeAll();
482             for (Iterator JavaDoc it = tags.iterator(); it.hasNext();) {
483                 CVSTag tag = (CVSTag) it.next();
484                 List JavaDoc knownTags = new ArrayList JavaDoc();
485                 knownTags.addAll(Arrays.asList(cvsDefinedTagsRootElement.getBranches().getTags()));
486                 knownTags.addAll(Arrays.asList(cvsDefinedTagsRootElement.getVersions().getTags()));
487                 knownTags.addAll(Arrays.asList(cvsDefinedTagsRootElement.getDates().getTags()));
488                 if(!knownTags.contains(tag)) {
489                     TagElement tagElem = new TagElement(tag);
490                     cvsTagTree.add(tagElem);
491                     cvsTagTree.setChecked(tagElem, true);
492                 }
493             }
494         }
495     }
496     
497     private CVSFileElement[] getSelectedFiles() {
498         IStructuredSelection selection = (IStructuredSelection)cvsResourceTree.getSelection();
499         if (!selection.isEmpty()) {
500             final List JavaDoc filesSelection = new ArrayList JavaDoc();
501             Iterator JavaDoc it = selection.iterator();
502             while(it.hasNext()) {
503                 Object JavaDoc o = it.next();
504                 if(o instanceof CVSFileElement) {
505                     filesSelection.add(o);
506                 }
507             }
508             return (CVSFileElement[]) filesSelection.toArray(new CVSFileElement[filesSelection.size()]);
509         }
510         return new CVSFileElement[0];
511     }
512     
513     private void addSelectionToAutoRefreshList() {
514         IStructuredSelection selection = (IStructuredSelection)cvsResourceTree.getSelection();
515         if (!selection.isEmpty()) {
516             final List JavaDoc filesSelection = new ArrayList JavaDoc();
517             Iterator JavaDoc it = selection.iterator();
518             while(it.hasNext()) {
519                 Object JavaDoc o = it.next();
520                 if(o instanceof CVSFileElement) {
521                     filesSelection.add(o);
522                 }
523             }
524             if(!filesSelection.isEmpty()) {
525                 for (it = filesSelection.iterator(); it.hasNext();) {
526                     try {
527                         ICVSFile file = ((CVSFileElement)it.next()).getCVSFile();
528                         ICVSFolder fileParent = file.getParent();
529                         String JavaDoc filePath = new Path(fileParent.getFolderSyncInfo().getRepository())
530                             .append(file.getRelativePath(fileParent)).toString();
531                         if(autoRefreshFileList.indexOf(filePath)==-1) {
532                             autoRefreshFileList.add(filePath);
533                         }
534                     } catch(CVSException e) {
535                         CVSUIPlugin.openError(getShell(), null, null, e);
536                     }
537                 }
538             }
539         }
540     }
541     
542     private CVSTag[] getTagsFor(ICVSFile file, IProgressMonitor monitor) throws TeamException {
543         Set JavaDoc tagSet = new HashSet JavaDoc();
544         ILogEntry[] entries = file.getLogEntries(monitor);
545         for (int j = 0; j < entries.length; j++) {
546             CVSTag[] tags = entries[j].getTags();
547             for (int k = 0; k < tags.length; k++) {
548                 tagSet.add(tags[k]);
549             }
550         }
551         return (CVSTag[])tagSet.toArray(new CVSTag[tagSet.size()]);
552     }
553     
554     private void rememberCheckedTags() {
555         Object JavaDoc[] checked = cvsTagTree.getCheckedElements();
556         for (int i = 0; i < checked.length; i++) {
557             CVSTag tag = ((TagElement)checked[i]).getTag();
558             if(tag.getType() == CVSTag.BRANCH) {
559                 cvsDefinedTagsRootElement.getBranches().add(new CVSTag[] {tag});
560             }else if(tag.getType() == CVSTag.DATE){
561                 cvsDefinedTagsRootElement.getDates().add(new CVSTag[] {tag});
562             }else {
563                 cvsDefinedTagsRootElement.getVersions().add(new CVSTag[] {tag});
564             }
565         }
566         cvsDefinedTagsTree.refresh();
567     }
568     
569     private void deleteSelected() {
570         IStructuredSelection selection = (IStructuredSelection)cvsDefinedTagsTree.getSelection();
571         if (!selection.isEmpty()) {
572             Iterator JavaDoc it = selection.iterator();
573             while(it.hasNext()) {
574                 Object JavaDoc o = it.next();
575                 if(o instanceof TagElement) {
576                     CVSTag tag = ((TagElement)o).getTag();
577                     if(tag.getType() == CVSTag.BRANCH) {
578                         cvsDefinedTagsRootElement.getBranches().remove(tag);
579                     } else if(tag.getType()==CVSTag.VERSION) {
580                         cvsDefinedTagsRootElement.getVersions().remove(tag);
581                     } else if(tag.getType() == CVSTag.DATE){
582                         cvsDefinedTagsRootElement.getDates().remove(tag);
583                     }
584                 }
585             }
586         }
587         cvsDefinedTagsTree.refresh();
588         cvsDefinedTagsTree.getTree().setFocus();
589     }
590     private void addDateTagsSelected(CVSTag tag){
591         if(tag == null) return;
592         List JavaDoc knownTags = new ArrayList JavaDoc();
593         knownTags.addAll(Arrays.asList(cvsDefinedTagsRootElement.getDates().getTags()));
594         if(!knownTags.contains( tag)){
595             cvsDefinedTagsRootElement.getDates().add(tag);
596         }
597         cvsDefinedTagsTree.refresh();
598         cvsDefinedTagsTree.getTree().setFocus();
599     }
600     private boolean isTagSelectedInKnownTagTree() {
601         IStructuredSelection selection = (IStructuredSelection)cvsDefinedTagsTree.getSelection();
602         if (!selection.isEmpty()) {
603             final List JavaDoc versions = new ArrayList JavaDoc();
604             final List JavaDoc branches = new ArrayList JavaDoc();
605             Iterator JavaDoc it = selection.iterator();
606             while(it.hasNext()) {
607                 Object JavaDoc o = it.next();
608                 if(o instanceof TagElement) {
609                     return true;
610                 }
611             }
612         }
613         return false;
614     }
615
616     private void removeAllKnownTags() {
617         cvsDefinedTagsRootElement.getBranches().removeAll();
618         cvsDefinedTagsRootElement.getVersions().removeAll();
619         cvsDefinedTagsRootElement.getDates().removeAll();
620         cvsDefinedTagsTree.refresh();
621     }
622     
623     private void updateEnablements() {
624         // add checked tags
625
Object JavaDoc[] checked = cvsTagTree.getCheckedElements();
626         addSelectedTagsButton.setEnabled(checked.length!=0?true:false);
627         
628         // Remove known tags
629
removeTagButton.setEnabled(isTagSelectedInKnownTagTree()?true:false);
630         
631         if(allowSettingAutoRefreshFiles) {
632             // add selected files
633
addSelectedFilesButton.setEnabled(getSelectedFiles().length!=0?true:false);
634             
635             // remove auto refresh files
636
removeFileButton.setEnabled(autoRefreshFileList.getSelection().length!=0?true:false);
637         }
638     }
639     
640     /**
641      * @see Dialog#okPressed()
642      */

643     protected void okPressed() {
644         try {
645             // save auto refresh file names
646
if(allowSettingAutoRefreshFiles) {
647                 RepositoryManager manager = CVSUIPlugin.getPlugin().getRepositoryManager();
648                 manager.setAutoRefreshFiles(root, autoRefreshFileList.getItems());
649             }
650             
651             // save defined tags and update all project with the same version tags
652
final RepositoryManager manager = CVSUIPlugin.getPlugin().getRepositoryManager();
653             manager.run(new IRunnableWithProgress() {
654                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
655                     CVSTag[] branches = cvsDefinedTagsRootElement.getBranches().getTags();
656                     CVSTag[] versions = cvsDefinedTagsRootElement.getVersions().getTags();
657                     CVSTag[] dates = cvsDefinedTagsRootElement.getDates().getTags();
658                     try {
659                         for(int i = 0; i < roots.length; i++) {
660                             CVSTag[] oldTags = manager.getKnownTags(roots[i]);
661                             manager.removeTags(roots[i], oldTags);
662                             if(branches.length > 0) {
663                                 manager.addTags(roots[i], branches);
664                             }
665                             if(versions.length>0) {
666                                 manager.addTags(roots[i], versions);
667                             }
668                             if(dates.length>0) {
669                                 manager.addTags(roots[i], dates);
670                             }
671                         }
672                     } catch (CVSException e) {
673                         throw new InvocationTargetException JavaDoc(e);
674                     }
675                 }
676             }, null);
677             
678             super.okPressed();
679         } catch (CVSException e) {
680             CVSUIPlugin.openError(getShell(), null, null, e);
681         } catch (InvocationTargetException JavaDoc e) {
682             CVSUIPlugin.openError(getShell(), null, null, e);
683         } catch (InterruptedException JavaDoc e) {
684         }
685     }
686     
687     /*
688      * Returns a button that implements the standard refresh tags operation. The runnable is run immediatly after
689      * the tags are fetched from the server. A client should refresh their widgets that show tags because they
690      * may of changed.
691      */

692     private static Button createTagRefreshButton(final Shell shell, Composite composite, String JavaDoc title, final ICVSFolder folder, final Runnable JavaDoc runnable) {
693         Button refreshButton = new Button(composite, SWT.PUSH);
694         refreshButton.setText (title);
695         refreshButton.addListener(SWT.Selection, new Listener() {
696                 public void handleEvent(Event event) {
697                     try {
698                         PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
699                             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
700                                 try {
701                                     CVSUIPlugin.getPlugin().getRepositoryManager().refreshDefinedTags(folder, false /* replace */, true, monitor);
702                                     Display.getDefault().asyncExec(runnable);
703                                 } catch (TeamException e) {
704                                     throw new InvocationTargetException JavaDoc(e);
705                                 }
706                             }
707                         });
708                     } catch (InterruptedException JavaDoc e) {
709                         // operation cancelled
710
} catch (InvocationTargetException JavaDoc e) {
711                         CVSUIPlugin.openError(shell, Policy.bind("TagConfigurationDialog.14"), null, e); //$NON-NLS-1$
712
}
713                 }
714             });
715         updateEnablementOnRefreshButton(refreshButton, folder);
716         return refreshButton;
717      }
718      
719      public static Control createTagDefinitionButtons(final Shell shell, Composite composite, final ICVSFolder[] folders, int hHint, int wHint, final Runnable JavaDoc afterRefresh, final Runnable JavaDoc afterConfigure) {
720         Composite buttonComp = new Composite(composite, SWT.NONE);
721         GridData data = new GridData ();
722         data.horizontalAlignment = GridData.END;
723         buttonComp.setLayoutData(data);
724         GridLayout layout = new GridLayout();
725         layout.numColumns = 2;
726         layout.marginHeight = 0;
727         layout.marginWidth = 0;
728         buttonComp.setLayout (layout);
729         
730         final Button refreshButton = TagConfigurationDialog.createTagRefreshButton(shell, buttonComp, Policy.bind("TagConfigurationDialog.20"), folders[0], afterRefresh); //$NON-NLS-1$
731
data = new GridData();
732         if(hHint!=0 && wHint!=0) {
733             data.heightHint = hHint;
734             //don't crop labels with large font
735
//int widthHint = wHint;
736
//data.widthHint = Math.max(widthHint, refreshButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
737
}
738         data.horizontalAlignment = GridData.END;
739         data.horizontalSpan = 1;
740         refreshButton.setLayoutData (data);
741
742         Button addButton = new Button(buttonComp, SWT.PUSH);
743         addButton.setText (Policy.bind("TagConfigurationDialog.21")); //$NON-NLS-1$
744
data = new GridData ();
745         if(hHint!=0 && wHint!=0) {
746             data.heightHint = hHint;
747             //don't crop labels with large font
748
//int widthHint = wHint;
749
//data.widthHint = Math.max(widthHint, addButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
750
}
751         data.horizontalAlignment = GridData.END;
752         data.horizontalSpan = 1;
753         addButton.setLayoutData (data);
754         addButton.addListener(SWT.Selection, new Listener() {
755                 public void handleEvent(Event event) {
756                     TagConfigurationDialog d = new TagConfigurationDialog(shell, folders);
757                     d.open();
758                     updateEnablementOnRefreshButton(refreshButton, folders[0]);
759                     afterConfigure.run();
760                 }
761             });
762         
763         WorkbenchHelp.setHelp(refreshButton, IHelpContextIds.TAG_CONFIGURATION_REFRESHACTION);
764         WorkbenchHelp.setHelp(addButton, IHelpContextIds.TAG_CONFIGURATION_OVERVIEW);
765         return buttonComp;
766      }
767      
768      private static void updateEnablementOnRefreshButton(Button refreshButton, ICVSFolder project) {
769         try {
770             String JavaDoc[] files = CVSUIPlugin.getPlugin().getRepositoryManager().getAutoRefreshFiles(project);
771             refreshButton.setEnabled(files.length != 0);
772         } catch (CVSException e) {
773             refreshButton.setEnabled(false);
774             CVSUIPlugin.log(e);
775         }
776         
777      }
778      
779     /**
780      * @see Window#getInitialSize()
781      */

782     protected Point getInitialSize() {
783         int width, height;
784         if(allowSettingAutoRefreshFiles) {
785             try {
786                 height = settings.getInt(ALLOWREFRESH_HEIGHT_KEY);
787                 width = settings.getInt(ALLOWREFRESH_WIDTH_KEY);
788             } catch(NumberFormatException JavaDoc e) {
789                 return super.getInitialSize();
790             }
791         } else {
792             try {
793                 height = settings.getInt(NOREFRESH_HEIGHT_KEY);
794                 width = settings.getInt(NOREFRESH_WIDTH_KEY);
795             } catch(NumberFormatException JavaDoc e) {
796                 return super.getInitialSize();
797             }
798         }
799         return new Point(width, height);
800     }
801     
802     /**
803      * @see Dialog#cancelPressed()
804      */

805     protected void cancelPressed() {
806         super.cancelPressed();
807     }
808     
809     private GridData getStandardButtonData(Button button) {
810         GridData data = new GridData();
811         data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
812         //don't crop labels with large font
813
//int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
814
//data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
815
return data;
816     }
817
818     /**
819      * @see Window#close()
820      */

821     public boolean close() {
822         Rectangle bounds = getShell().getBounds();
823         if(allowSettingAutoRefreshFiles) {
824             settings.put(ALLOWREFRESH_HEIGHT_KEY, bounds.height);
825             settings.put(ALLOWREFRESH_WIDTH_KEY, bounds.width);
826         } else {
827             settings.put(NOREFRESH_HEIGHT_KEY, bounds.height);
828             settings.put(NOREFRESH_WIDTH_KEY, bounds.width);
829         }
830         return super.close();
831     }
832 }
833
Popular Tags