KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > wizards > WorkingSetsDialog


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.team.internal.ui.wizards;
12
13 import java.util.*;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.TitleAreaDialog;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.ModifyEvent;
22 import org.eclipse.swt.events.ModifyListener;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.*;
27 import org.eclipse.team.internal.ui.TeamUIMessages;
28 import org.eclipse.team.internal.ui.TeamUIPlugin;
29 import org.eclipse.ui.IWorkingSet;
30
31 public class WorkingSetsDialog extends TitleAreaDialog {
32
33     protected TableViewer wsTableViewer;
34     protected Text wsNameText;
35     protected Image dlgTitleImage;
36
37     private String JavaDoc selectedWorkingSet;
38     
39     public static final String JavaDoc resourceWorkingSetId = "org.eclipse.ui.resourceWorkingSetPage"; //$NON-NLS-1$
40

41     public WorkingSetsDialog(Shell shell) {
42         super(shell);
43         setShellStyle(getShellStyle() | SWT.RESIZE);
44     }
45
46     protected Control createDialogArea(Composite parent) {
47         setTitle(TeamUIMessages.WorkingSetsDialog_Title);
48         setMessage(TeamUIMessages.WorkingSetsDialog_Message);
49         Composite composite = (Composite) super.createDialogArea(parent);
50         getShell().setText(TeamUIMessages.WorkingSetsDialog_TitleBar);
51
52         final Composite group = new Composite(composite, SWT.NONE);
53         GridLayout layout = new GridLayout(2, false);
54         layout.marginWidth = 0;
55         group.setLayout(layout);
56         group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
57
58         final Label label = new Label(group, SWT.NONE);
59         label.setLayoutData(new GridData());
60         label.setText(TeamUIMessages.WorkingSetsDialog_Label);
61
62         wsNameText = new Text(group, SWT.BORDER);
63         wsNameText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
64
65         layout = new GridLayout();
66         layout.numColumns = 1;
67         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
68         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
69         composite.setLayout(layout);
70         final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
71         composite.setLayoutData(data);
72
73         wsTableViewer = new TableViewer(composite, SWT.BORDER);
74         final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
75         gd.widthHint = 450;
76         gd.heightHint = 250;
77         wsTableViewer.getTable().setLayoutData(gd);
78
79         wsTableViewer.setContentProvider(new ArrayContentProvider());
80         wsTableViewer.setLabelProvider(new WorkingSetLabelProvider());
81         wsTableViewer.setInput(TeamUIPlugin.getPlugin().getWorkbench().getWorkingSetManager().getWorkingSets());
82
83         setupListeners();
84
85         return parent;
86     }
87
88     protected void okPressed() {
89         selectedWorkingSet = wsNameText.getText();
90
91         if (selectedWorkingSet.equals("")) { //$NON-NLS-1$
92
setErrorMessage(TeamUIMessages.WorkingSetsDialog_ErrorMessage);
93             return;
94         }
95
96         super.okPressed();
97     }
98
99     protected void cancelPressed() {
100         super.cancelPressed();
101     }
102
103     public boolean close() {
104         if (dlgTitleImage != null)
105             dlgTitleImage.dispose();
106         return super.close();
107     }
108
109     void setupListeners() {
110         wsTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
111             public void selectionChanged(SelectionChangedEvent event) {
112                 IStructuredSelection s = (IStructuredSelection) event.getSelection();
113                 Object JavaDoc obj = s.getFirstElement();
114                 if (obj instanceof IWorkingSet)
115                     wsNameText.setText(((IWorkingSet) obj).getName());
116             }
117         });
118
119         wsNameText.addModifyListener(new ModifyListener() {
120             public void modifyText(ModifyEvent e) {
121                 setErrorMessage(null);
122             }
123         });
124     }
125     
126     class WorkingSetLabelProvider extends LabelProvider {
127           private Map icons;
128           
129             public WorkingSetLabelProvider() {
130                 icons = new Hashtable();
131             }
132
133             public void dispose() {
134                 Iterator iterator = icons.values().iterator();
135
136                 while (iterator.hasNext()) {
137                     Image icon = (Image) iterator.next();
138                     icon.dispose();
139                 }
140                 super.dispose();
141             }
142
143             public Image getImage(Object JavaDoc object) {
144                 Assert.isTrue(object instanceof IWorkingSet);
145                 IWorkingSet workingSet = (IWorkingSet) object;
146                 ImageDescriptor imageDescriptor = workingSet.getImage();
147
148                 if (imageDescriptor == null) {
149                     return null;
150                 }
151
152                 Image icon = (Image) icons.get(imageDescriptor);
153                 if (icon == null) {
154                     icon = imageDescriptor.createImage();
155                     icons.put(imageDescriptor, icon);
156                 }
157                 return icon;
158             }
159                 
160             public String JavaDoc getText(Object JavaDoc object) {
161                 Assert.isTrue(object instanceof IWorkingSet);
162                 IWorkingSet workingSet = (IWorkingSet) object;
163                 return workingSet.getLabel();
164             }
165         }
166     
167     public String JavaDoc getSelectedWorkingSet(){
168         return selectedWorkingSet;
169     }
170 }
171
Popular Tags