blob: d1475cd3af534822568ebc7d482334fa403bfe4a [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.authoring.ui.forms;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import org.eclipse.epf.authoring.ui.AuthoringUIPlugin;
import org.eclipse.epf.authoring.ui.AuthoringUIResources;
import org.eclipse.epf.authoring.ui.AuthoringUIText;
import org.eclipse.epf.authoring.ui.editors.MethodElementEditor;
import org.eclipse.epf.library.LibraryService;
import org.eclipse.epf.library.edit.command.IActionManager;
import org.eclipse.epf.library.edit.util.TngUtil;
import org.eclipse.epf.library.util.ResourceHelper;
import org.eclipse.epf.uma.ContentDescription;
import org.eclipse.epf.uma.UmaPackage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.FormToolkit;
/**
* The Description page in a Guidance (with attachments) editor.
*
* @author Shashidhar Kannoori
* @author Kelvin Low
* @since 1.0
*/
public class GuidanceWithAttachmentsDescriptionPage extends GuidanceDescriptionPage {
private Button ctrl_attach, ctrl_detach; // , ctrl_remove;
private List ctrl_attached_files;
private static final String GUIDANCE_ATTACHMENT_LAST_DIRECTORY_BROWSED_PREF = "guidance_attachment_last_dir_browsed"; //$NON-NLS-1$
/**
* Creates a new instance.
*/
public GuidanceWithAttachmentsDescriptionPage(FormEditor editor) {
super(editor);
}
/**
* @see org.eclipse.epf.authoring.ui.forms.GuidanceDescriptionPage#createEditorContent(org.eclipse.ui.forms.widgets.FormToolkit)
*/
protected void createEditorContent(FormToolkit toolkit) {
super.createEditorContent(toolkit);
createLabel(toolkit, generalComposite,
AuthoringUIText.ATTACHED_FILE_TEXT, 2);
ctrl_attached_files = new List(generalComposite, SWT.SINGLE
| SWT.V_SCROLL | SWT.H_SCROLL);
GridData listGridData = new GridData(SWT.BEGINNING, SWT.BEGINNING,
true, true);
listGridData.heightHint = 30;
listGridData.widthHint = 300;
ctrl_attached_files.setLayoutData(listGridData);
ctrl_attached_files.setData(FormToolkit.KEY_DRAW_BORDER,
FormToolkit.TEXT_BORDER);
Composite buttonComposite = createComposite(toolkit, generalComposite,
1);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
gridData.verticalSpan = 1;
gridData.horizontalSpan = 3;
buttonComposite.setLayoutData(gridData);
buttonComposite.setLayout(new GridLayout(3, false));
ctrl_attach = createButton(toolkit, buttonComposite, ATTACH_BUTTON);
ctrl_detach = createButton(toolkit, buttonComposite, DETACH_BUTTON);
// ctrl_remove = createButton(toolkit, buttonComposite, REMOVE_BUTTON);
label_base.setText(AuthoringUIText.BASE_ELEMENT_TEXT);
}
/**
* Add listeners
*
* @see org.eclipse.epf.authoring.ui.forms.GuidanceDescriptionPage#addListeners()
*/
protected void addListeners() {
super.addListeners();
final IActionManager actionMgr = ((MethodElementEditor) getEditor())
.getActionManager();
ctrl_attach.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell());
dialog
.setText(AuthoringUIResources.attachGuidanceFileDialog_title);
String lastDir = AuthoringUIPlugin
.getDefault()
.getPreferenceStore()
.getString(
GUIDANCE_ATTACHMENT_LAST_DIRECTORY_BROWSED_PREF);
if (lastDir != null && lastDir.trim().length() > 0) {
dialog.setFilterPath(lastDir);
} else {
dialog.setFilterPath(LibraryService.getInstance()
.getCurrentMethodLibraryPath()
+ File.separator
+ ResourceHelper.getElementPath(guidance));
}
dialog.open();
if (dialog.getFileName() != null && dialog.getFileName() != "") { //$NON-NLS-1$
try {
File fileToAttach = new File(dialog.getFilterPath(),
dialog.getFileName());
String fileUrl = ResourceHelper.getURLForAttachment(
fileToAttach, guidance, true);
if (ctrl_attached_files.indexOf(fileUrl) != -1) {
// file is already an attachment
AuthoringUIPlugin
.getDefault()
.getMsgDialog()
.displayInfo(
AuthoringUIResources.AttachmentDialogattachFile_text,
AuthoringUIResources.forms_GuidanceWithAttachmentsDescriptionPage_alreadyAttached_msg);
return;
}
ctrl_attached_files.add(fileUrl);
boolean status = actionMgr.doAction(IActionManager.SET,
guidance.getPresentation(),
UmaPackage.eINSTANCE
.getGuidanceDescription_Attachments(),
getFilesFromListCtrl(), -1);
if (!status) {
return;
} else {
AuthoringUIPlugin
.getDefault()
.getPreferenceStore()
.setValue(
GUIDANCE_ATTACHMENT_LAST_DIRECTORY_BROWSED_PREF,
dialog.getFilterPath());
}
} catch (IOException ex) {
// TODO: Display an error dialog.
ex.printStackTrace();
}
}
}
});
ctrl_detach.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ctrl_attached_files.remove(ctrl_attached_files
.getSelectionIndex());
boolean status = actionMgr.doAction(IActionManager.SET,
guidance.getPresentation(), UmaPackage.eINSTANCE
.getGuidanceDescription_Attachments(),
getFilesFromListCtrl(), -1);
if (!status) {
// ctrl_attached_files.setText(oldContent);
return;
}
}
});
// ctrl_remove.addSelectionListener(new SelectionAdapter(){
// public void widgetSelected(SelectionEvent e) {
// //ctrl_attached_files.setText(""); //$NON-NLS-1$
// String oldContent =
// ((GuidanceDescription)guidance.getPresentation()).getAttachments();
// ctrl_attached_files.setText(""); //$NON-NLS-1$
// boolean status = actionMgr.doAction(IActionManager.SET,
// guidance.getPresentation(),
// UmaPackage.eINSTANCE.getGuidanceDescription_Attachments(),
// ctrl_attached_files.getText(), -1);
// if(!status){
// //ctrl_attached_files.setText(oldContent);
// return;
// }
// }
// });
}
/**
* @see org.eclipse.epf.authoring.ui.forms.GuidanceDescriptionPage#refresh(boolean)
*/
protected void refresh(boolean editable) {
super.refresh(editable);
ctrl_attach.setEnabled(editable);
ctrl_detach.setEnabled(editable);
// ctrl_remove.setEnabled(editable);
}
/**
* Load initial data from model
*
* @see org.eclipse.epf.authoring.ui.forms.GuidanceDescriptionPage#loadData()
*/
protected void loadData() {
super.loadData();
if (guidance != null) {
ContentDescription content = (ContentDescription) guidance
.getPresentation();
String thirdpartyformat = ((org.eclipse.epf.uma.GuidanceDescription) content)
.getAttachments();
setFilesToListCtrl(thirdpartyformat);
}
}
/**
* @see org.eclipse.epf.authoring.ui.forms.GuidanceDescriptionPage#getContentElement()
*/
protected Object getContentElement() {
return contentElement;
}
/**
* Given a string of files, add the files to the List control
*
* @param files
*/
private void setFilesToListCtrl(String fileString) {
java.util.List attachmentList = TngUtil
.convertGuidanceAttachmentsToList(fileString);
ctrl_attached_files.removeAll();
for (Iterator iter = attachmentList.iterator(); iter.hasNext();) {
String attachment = (String) iter.next();
ctrl_attached_files.add(attachment);
}
}
/**
* returns the list of files as a string
*
* @return
*/
private String getFilesFromListCtrl() {
String files[] = ctrl_attached_files.getItems();
return TngUtil.convertGuidanceAttachmentsToString(Arrays.asList(files));
}
}