Source code: com/port80/eclipse/jdt/annotation/EditAnnotationDialog.java
1 package com.port80.eclipse.jdt.annotation;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.eclipse.jface.dialogs.Dialog;
8 import org.eclipse.jface.dialogs.IDialogConstants;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.events.TraverseEvent;
15 import org.eclipse.swt.events.TraverseListener;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.RGB;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Combo;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Text;
27
28 /**
29 * Prompts the user for a multi-line text.
30 *
31 * @see org.eclipse.team.internal.ccvs.ui.ReleaseCommentDialog
32 * @author chrisl
33 */
34 public class EditAnnotationDialog extends Dialog {
35
36 private static final String NAME = "EditAnnotationDialog";
37 private static final int WIDTH_HINT = 512;
38 private static final int HEIGHT_HINT = 64;
39 private static final RGB COLOR_BLACK = new RGB(0, 0, 0);
40 private static final RGB COLOR_RED = new RGB(0xff, 0x20, 0x20);
41
42 private String fTitle = "";
43 private String fLabel = "";
44 private String fText = "";
45 private String fFolder = "";
46 private List fFolders;
47 private boolean fValid;
48 private Set fFolderSet;
49 private Color fColorBlack;
50 private Color fColorRed;
51
52 private Text fTextArea;
53 private Combo fFolderCombo;
54 private Label fFolderLabel;
55
56 ////////////////////////////////////////////////////////////////////////
57
58 /**
59 * TextEdit constructor.
60 *
61 * @param parentShell the parent of this dialog
62 */
63 public EditAnnotationDialog(
64 Shell parentShell,
65 String title,
66 String label,
67 String folder,
68 List folders,
69 String content) {
70 super(parentShell);
71 if (title != null)
72 fTitle = title;
73 if (label != null)
74 fLabel = label;
75 if (content != null)
76 fText = content;
77 if (folder != null)
78 fFolder = folder;
79 fFolders = folders;
80 fFolderSet = new HashSet();
81 }
82
83 /**
84 * @return The edited comment
85 */
86 public String getText() {
87 return fText;
88 }
89
90 /**
91 * @return A selected existing folder name or a new valid folder name.
92 * A valid folder name is not null or "".
93 */
94 public String getFolder() {
95 return fFolder;
96 }
97
98 ////////////////////////////////////////////////////////////////////////
99
100 /*
101 * @see Dialog#createDialogArea(Composite)
102 */
103 protected Control createDialogArea(Composite parent) {
104
105 fColorBlack = new Color(parent.getDisplay(), COLOR_BLACK);
106 fColorRed = new Color(parent.getDisplay(), COLOR_RED);
107
108 getShell().setText(fTitle);
109 GridData data;
110 Composite composite = new Composite(parent, SWT.NULL);
111 composite.setLayout(new GridLayout(4, true));
112 data = new GridData(GridData.FILL_BOTH);
113 data.horizontalSpan = 4;
114 composite.setLayoutData(data);
115
116 Label label = new Label(composite, SWT.NULL);
117 data = new GridData();
118 data.horizontalSpan = 4;
119 label.setLayoutData(data);
120 label.setText(fLabel);
121
122 Label labeltext = new Label(composite, SWT.NULL);
123 data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
124 labeltext.setLayoutData(data);
125 labeltext.setText("Annotation:");
126
127 fFolderLabel = new Label(composite, SWT.NULL);
128 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL);
129 data.horizontalSpan = 2;
130 fFolderLabel.setLayoutData(data);
131 fFolderLabel.setText("Folder: ");
132
133 fFolderCombo = new Combo(composite, SWT.NONE);
134 int width = 32;
135 if (fFolders != null) {
136 for (int i = 0; i < fFolders.size(); ++i) {
137 String s = (String) fFolders.get(i);
138 fFolderCombo.add(s);
139 fFolderSet.add(s);
140 if (s.length() * 2 > width)
141 width = s.length() * 2;
142 }
143 }
144 fFolderCombo.setText(fFolder);
145 data = new GridData(GridData.HORIZONTAL_ALIGN_END);
146 data.widthHint = convertWidthInCharsToPixels(width);
147 fFolderCombo.setLayoutData(data);
148 fFolderCombo.addSelectionListener(new SelectionAdapter() {
149 public void widgetSelected(SelectionEvent e) {
150 validCheck();
151 }
152 });
153 fFolderCombo.addModifyListener(new ModifyListener() {
154 public void modifyText(ModifyEvent e) {
155 validCheck();
156 }
157 });
158 fFolderCombo.setEnabled(true);
159 fValid = true;
160
161 fTextArea = new Text(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
162 data = new GridData(GridData.FILL_BOTH);
163 data.horizontalSpan = 4;
164 data.widthHint = WIDTH_HINT;
165 data.heightHint = HEIGHT_HINT;
166 fTextArea.setLayoutData(data);
167 fTextArea.setText(fText);
168 fTextArea.selectAll();
169 fTextArea.addTraverseListener(new TraverseListener() {
170 public void keyTraversed(TraverseEvent e) {
171 if (e.detail == SWT.TRAVERSE_RETURN && (e.stateMask & SWT.CTRL) != 0) {
172 e.doit = false;
173 okPressed();
174 }
175 }
176 });
177 // set F1 help
178 //WorkbenchHelp.setHelp(composite, IHelpContextIds.RELEASE_COMMENT_DIALOG);
179 return composite;
180 }
181
182 /*
183 * @see Dialog#okPressed
184 */
185 protected void okPressed() {
186 fText = fTextArea.getText();
187 fFolder = fFolderCombo.getText();
188 super.okPressed();
189 }
190
191 ////////////////////////////////////////////////////////////////////////
192
193 void validCheck() {
194 String folder = fFolderCombo.getText();
195 boolean valid = (folder.length() > 0);
196 if (valid != fValid) {
197 fValid = valid;
198 Button ok = getButton(IDialogConstants.OK_ID);
199 if (ok != null)
200 ok.setEnabled(fValid);
201 if (fFolderSet.contains(folder)) {
202 fFolderLabel.setForeground(fColorBlack);
203 fFolderLabel.setText("Folder:");
204 } else {
205 fFolderLabel.setForeground(fColorRed);
206 fFolderLabel.setText("Create Folder:");
207 }
208 }
209 }
210
211 ////////////////////////////////////////////////////////////////////////
212 }