|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2007 Mylyn project committers 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 |
|
| 9 |
package org.eclipse.mylyn.internal.tasks.ui.wizards; |
| 10 |
|
| 11 |
import java.util.HashMap; |
| 12 |
import java.util.Iterator; |
| 13 |
import java.util.LinkedList; |
| 14 |
import java.util.List; |
| 15 |
import java.util.Locale; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
import org.eclipse.jface.wizard.IWizardPage; |
| 19 |
import org.eclipse.jface.wizard.WizardPage; |
| 20 |
import org.eclipse.mylyn.internal.tasks.core.TaskAttachment; |
| 21 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiImages; |
| 22 |
import org.eclipse.swt.SWT; |
| 23 |
import org.eclipse.swt.events.ModifyEvent; |
| 24 |
import org.eclipse.swt.events.ModifyListener; |
| 25 |
import org.eclipse.swt.events.SelectionEvent; |
| 26 |
import org.eclipse.swt.events.SelectionListener; |
| 27 |
import org.eclipse.swt.layout.GridData; |
| 28 |
import org.eclipse.swt.layout.GridLayout; |
| 29 |
import org.eclipse.swt.widgets.Button; |
| 30 |
import org.eclipse.swt.widgets.Combo; |
| 31 |
import org.eclipse.swt.widgets.Composite; |
| 32 |
import org.eclipse.swt.widgets.Label; |
| 33 |
import org.eclipse.swt.widgets.Text; |
| 34 |
|
| 35 |
/** |
| 36 |
* A wizard page to enter details of a new attachment. |
| 37 |
* |
| 38 |
* @author Jeff Pound |
| 39 |
* @author Mik Kersten |
| 40 |
*/ |
| 41 |
public class TaskAttachmentPage extends WizardPage { |
| 42 |
|
| 43 |
private final TaskAttachment attachment; |
| 44 |
|
| 45 |
private Text filePath; |
| 46 |
|
| 47 |
private Text attachmentDesc; |
| 48 |
|
| 49 |
private Text attachmentComment; |
| 50 |
|
| 51 |
private Button isPatchButton; |
| 52 |
|
| 53 |
private Button attachContextButton; |
| 54 |
|
| 55 |
private Combo contentTypeList; |
| 56 |
|
| 57 |
private boolean supportsDescription = true; |
| 58 |
|
| 59 |
private static List<String> contentTypes; |
| 60 |
|
| 61 |
private static Map<String, String> extensions2Types; |
| 62 |
|
| 63 |
static { |
| 64 |
/* For UI */ |
| 65 |
contentTypes = new LinkedList<String>(); |
| 66 |
contentTypes.add("text/plain"); |
| 67 |
contentTypes.add("text/html"); |
| 68 |
contentTypes.add("application/xml"); |
| 69 |
contentTypes.add("image/gif"); |
| 70 |
contentTypes.add("image/jpeg"); |
| 71 |
contentTypes.add("image/png"); |
| 72 |
contentTypes.add("application/octet-stream"); |
| 73 |
|
| 74 |
/* For auto-detect */ |
| 75 |
extensions2Types = new HashMap<String, String>(); |
| 76 |
extensions2Types.put("txt", "text/plain"); |
| 77 |
extensions2Types.put("html", "text/html"); |
| 78 |
extensions2Types.put("htm", "text/html"); |
| 79 |
extensions2Types.put("jpg", "image/jpeg"); |
| 80 |
extensions2Types.put("jpeg", "image/jpeg"); |
| 81 |
extensions2Types.put("gif", "image/gif"); |
| 82 |
extensions2Types.put("png", "image/png"); |
| 83 |
extensions2Types.put("xml", "application/xml"); |
| 84 |
extensions2Types.put("zip", "application/octet-stream"); |
| 85 |
extensions2Types.put("tar", "application/octet-stream"); |
| 86 |
extensions2Types.put("gz", "application/octet-stream"); |
| 87 |
} |
| 88 |
|
| 89 |
protected TaskAttachmentPage(TaskAttachment attachment) { |
| 90 |
super("AttachmentDetails"); |
| 91 |
setTitle("Attachment Details"); |
| 92 |
setMessage("Enter a description and verify the content type of the attachment"); |
| 93 |
this.attachment = attachment; |
| 94 |
} |
| 95 |
|
| 96 |
public void createControl(Composite parent) { |
| 97 |
Composite composite = new Composite(parent, SWT.NONE); |
| 98 |
GridLayout gridLayout = new GridLayout(); |
| 99 |
gridLayout.numColumns = 3; |
| 100 |
composite.setLayout(gridLayout); |
| 101 |
setControl(composite); |
| 102 |
|
| 103 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 104 |
composite.setLayout(new GridLayout(3, false)); |
| 105 |
|
| 106 |
final TaskAttachmentPage thisPage = this; |
| 107 |
|
| 108 |
new Label(composite, SWT.NONE).setText("File"); |
| 109 |
filePath = new Text(composite, SWT.BORDER); |
| 110 |
filePath.setEditable(false); |
| 111 |
filePath.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1)); |
| 112 |
|
| 113 |
if (supportsDescription) { |
| 114 |
new Label(composite, SWT.NONE).setText("Description"); |
| 115 |
attachmentDesc = new Text(composite, SWT.BORDER); |
| 116 |
attachmentDesc.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1)); |
| 117 |
|
| 118 |
attachmentDesc.addModifyListener(new ModifyListener() { |
| 119 |
public void modifyText(ModifyEvent e) { |
| 120 |
if ("".equals(attachmentDesc.getText().trim())) { |
| 121 |
thisPage.setErrorMessage("Description required"); |
| 122 |
} else { |
| 123 |
if (!"".equals(filePath.getText())) { |
| 124 |
thisPage.setPageComplete(true); |
| 125 |
thisPage.setErrorMessage(null); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
Label label = new Label(composite, SWT.NONE); |
| 134 |
label.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false)); |
| 135 |
label.setText("Comment"); |
| 136 |
attachmentComment = new Text(composite, SWT.V_SCROLL | SWT.BORDER | SWT.WRAP); |
| 137 |
attachmentComment.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); |
| 138 |
|
| 139 |
new Label(composite, SWT.NONE).setText("Content Type");// .setBackground(parent.getBackground()); |
| 140 |
|
| 141 |
contentTypeList = new Combo(composite, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY); |
| 142 |
contentTypeList.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, false, false, 2, 1)); |
| 143 |
final HashMap<String, Integer> contentTypeIndices = new HashMap<String, Integer>(); |
| 144 |
Iterator<String> iter = contentTypes.iterator(); |
| 145 |
int i = 0; |
| 146 |
while (iter.hasNext()) { |
| 147 |
String next = iter.next(); |
| 148 |
contentTypeList.add(next); |
| 149 |
contentTypeIndices.put(next, new Integer(i)); |
| 150 |
i++; |
| 151 |
} |
| 152 |
|
| 153 |
/* Update attachment on select content type */ |
| 154 |
contentTypeList.addSelectionListener(new SelectionListener() { |
| 155 |
public void widgetDefaultSelected(SelectionEvent e) { |
| 156 |
// ignore |
| 157 |
} |
| 158 |
|
| 159 |
public void widgetSelected(SelectionEvent e) { |
| 160 |
attachment.setContentType(contentTypeList.getItem(contentTypeList.getSelectionIndex())); |
| 161 |
} |
| 162 |
}); |
| 163 |
contentTypeList.select(0); |
| 164 |
attachment.setContentType(contentTypeList.getItem(0)); |
| 165 |
|
| 166 |
// TODO: is there a better way to pad? |
| 167 |
new Label(composite, SWT.NONE); |
| 168 |
|
| 169 |
isPatchButton = new Button(composite, SWT.CHECK); |
| 170 |
isPatchButton.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, false, false, 2, 1)); |
| 171 |
isPatchButton.setText("Patch"); |
| 172 |
|
| 173 |
// TODO: is there a better way to pad? |
| 174 |
new Label(composite, SWT.NONE); |
| 175 |
|
| 176 |
attachContextButton = new Button(composite, SWT.CHECK); |
| 177 |
attachContextButton.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, false, false, 2, 1)); |
| 178 |
attachContextButton.setText("Attach Context"); |
| 179 |
attachContextButton.setImage(TasksUiImages.getImage(TasksUiImages.CONTEXT_ATTACH)); |
| 180 |
attachContextButton.setEnabled((((NewAttachmentWizard) getWizard()).hasContext())); |
| 181 |
|
| 182 |
/* |
| 183 |
* Attachment file name listener, update the local attachment |
| 184 |
* accordingly |
| 185 |
*/ |
| 186 |
filePath.addModifyListener(new ModifyListener() { |
| 187 |
public void modifyText(ModifyEvent e) { |
| 188 |
// Determine type by extension |
| 189 |
int index = filePath.getText().lastIndexOf("."); |
| 190 |
if (index > 0 && index < filePath.getText().length()) { |
| 191 |
String ext = filePath.getText().substring(index + 1); |
| 192 |
String type = extensions2Types.get(ext.toLowerCase(Locale.ENGLISH)); |
| 193 |
if (type != null) { |
| 194 |
contentTypeList.select(contentTypeIndices.get(type)); |
| 195 |
attachment.setContentType(type); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// check page completenes |
| 200 |
if (attachmentDesc != null && "".equals(attachmentDesc.getText())) { |
| 201 |
thisPage.setErrorMessage("Description required"); |
| 202 |
} else { |
| 203 |
if (!"".equals(filePath.getText())) { |
| 204 |
thisPage.setPageComplete(true); |
| 205 |
thisPage.setErrorMessage(null); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
}); |
| 210 |
|
| 211 |
filePath.setText(attachment.getName() == null ? "" : attachment.getName()); //$NON-NLS-1$ |
| 212 |
|
| 213 |
/* Listener for isPatch */ |
| 214 |
isPatchButton.addSelectionListener(new SelectionListener() { |
| 215 |
private int lastSelected; |
| 216 |
|
| 217 |
public void widgetDefaultSelected(SelectionEvent e) { |
| 218 |
// ignore |
| 219 |
} |
| 220 |
|
| 221 |
public void widgetSelected(SelectionEvent e) { |
| 222 |
attachment.setPatch(isPatchButton.getSelection()); |
| 223 |
if (isPatchButton.getSelection()) { |
| 224 |
lastSelected = contentTypeList.getSelectionIndex(); |
| 225 |
contentTypeList.select(0); |
| 226 |
contentTypeList.setEnabled(false); |
| 227 |
if (attachContextButton.isEnabled()) { |
| 228 |
attachContextButton.setSelection(true); |
| 229 |
} |
| 230 |
} else { |
| 231 |
contentTypeList.setEnabled(true); |
| 232 |
contentTypeList.select(lastSelected); |
| 233 |
} |
| 234 |
} |
| 235 |
}); |
| 236 |
|
| 237 |
thisPage.setErrorMessage(null); |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
public boolean isPageComplete() { |
| 242 |
return !"".equals(filePath.getText().trim()) |
| 243 |
&& (attachmentDesc == null || !"".equals(attachmentDesc.getText().trim())); |
| 244 |
} |
| 245 |
|
| 246 |
public void populateAttachment() { |
| 247 |
if (attachmentDesc != null) { |
| 248 |
attachment.setDescription(attachmentDesc.getText()); |
| 249 |
} |
| 250 |
attachment.setComment(attachmentComment.getText()); |
| 251 |
} |
| 252 |
|
| 253 |
public TaskAttachment getAttachment() { |
| 254 |
return attachment; |
| 255 |
} |
| 256 |
|
| 257 |
public void setFilePath(String path) { |
| 258 |
filePath.setText(path); |
| 259 |
if (path.endsWith(".patch")) { |
| 260 |
isPatchButton.setSelection(true); |
| 261 |
if (attachContextButton.isEnabled()) { |
| 262 |
attachContextButton.setSelection(true); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public IWizardPage getNextPage() { |
| 269 |
populateAttachment(); |
| 270 |
// TODO implement preview |
| 271 |
// PreviewAttachmentPage page = new PreviewAttachmentPage(getAttachment()); |
| 272 |
// page.setWizard(getWizard()); |
| 273 |
// return page; |
| 274 |
return null; |
| 275 |
} |
| 276 |
|
| 277 |
public boolean getAttachContext() { |
| 278 |
return attachContextButton.getSelection(); |
| 279 |
} |
| 280 |
|
| 281 |
public void setContentType() { |
| 282 |
String type = attachment.getContentType(); |
| 283 |
String[] typeList = contentTypeList.getItems(); |
| 284 |
for (int i = 0; i < typeList.length; i++) { |
| 285 |
if (typeList[i].equals(type)) { |
| 286 |
contentTypeList.select(i); |
| 287 |
contentTypeList.setEnabled(false); |
| 288 |
isPatchButton.setEnabled(false); |
| 289 |
return; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
public boolean supportsDescription() { |
| 295 |
return supportsDescription; |
| 296 |
} |
| 297 |
|
| 298 |
public void setSupportsDescription(boolean supportsDescription) { |
| 299 |
this.supportsDescription = supportsDescription; |
| 300 |
} |
| 301 |
|
| 302 |
} |