/* ********************************************************************** **
** Copyright notice **
** **
** (c) 2005-2006 RSSOwl Development Team **
** http://www.rssowl.org/ **
** **
** 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.rssowl.org/legal/epl-v10.html **
** **
** A copy is found in the file epl-v10.html and important notices to the **
** license from the team is found in the textfile LICENSE.txt distributed **
** in this package. **
** **
** This copyright notice MUST APPEAR in all copies of the file! **
** **
** Contributors: **
** RSSOwl Development Team - initial API and implementation **
** **
** ********************************************************************** */
package org.rssowl.ui.internal.dialogs;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.rssowl.core.Owl;
import org.rssowl.core.connection.ConnectionException;
import org.rssowl.core.persist.IBookMark;
import org.rssowl.core.persist.IFeed;
import org.rssowl.core.persist.IFolder;
import org.rssowl.core.persist.IMark;
import org.rssowl.core.persist.IModelFactory;
import org.rssowl.core.persist.dao.DynamicDAO;
import org.rssowl.core.persist.dao.IFeedDAO;
import org.rssowl.core.persist.dao.IFolderDAO;
import org.rssowl.core.persist.dao.IPreferenceDAO;
import org.rssowl.core.persist.reference.FeedLinkReference;
import org.rssowl.core.persist.reference.FeedReference;
import org.rssowl.core.persist.reference.FolderReference;
import org.rssowl.core.persist.service.PersistenceException;
import org.rssowl.core.util.StringUtils;
import org.rssowl.core.util.URIUtils;
import org.rssowl.ui.internal.Controller;
import org.rssowl.ui.internal.FolderChooser;
import org.rssowl.ui.internal.OwlUI;
import org.rssowl.ui.internal.actions.ReloadTypesAction;
import org.rssowl.ui.internal.util.JobRunner;
import org.rssowl.ui.internal.util.LayoutUtils;
import org.rssowl.ui.internal.util.UIBackgroundJob;
import org.rssowl.ui.internal.views.explorer.BookMarkExplorer;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
/**
* A <code>Wizard</code> that allows to create new bookmarks.
*
* @author bpasero
*/
public class BookmarkWizard extends Wizard implements INewWizard {
private NewBookMarkWizardPage fPage;
private IStructuredSelection fSelection;
/* Page for Wizard */
private class NewBookMarkWizardPage extends WizardPage {
private static final String HTTP = "http://";
private Text fLinkInput;
private Text fNameInput;
private FolderChooser fFolderChooser;
private LocalResourceManager fResources;
NewBookMarkWizardPage(String pageName) {
super(pageName, pageName, OwlUI.getImageDescriptor("icons/obj16/bkmrk_wiz.gif"));
setMessage("Create a new bookmark to a feed of your choice.");
fResources = new LocalResourceManager(JFaceResources.getResources());
}
/*
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
*/
public void createControl(Composite parent) {
Composite control = new Composite(parent, SWT.NONE);
control.setLayout(new GridLayout(2, false));
Label linkLabel = new Label(control, SWT.NONE);
linkLabel.setText("Link: ");
String initialLink = getInitialLink();
fLinkInput = new Text(control, SWT.SINGLE | SWT.BORDER);
fLinkInput.setText(initialLink);
fLinkInput.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
fLinkInput.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
setErrorMessage(null);
}
});
if (StringUtils.isSet(initialLink) && !initialLink.equals(HTTP)) {
fLinkInput.setText(initialLink);
fLinkInput.selectAll();
} else {
fLinkInput.setText(HTTP);
fLinkInput.setSelection(HTTP.length());
}
Label nameLabel = new Label(control, SWT.NONE);
nameLabel.setText("Name: ");
Composite nameContainer = new Composite(control, SWT.BORDER);
nameContainer.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
nameContainer.setLayout(LayoutUtils.createGridLayout(2, 0, 0));
nameContainer.setBackground(control.getDisplay().getSystemColor(SWT.COLOR_WHITE));
fNameInput = new Text(nameContainer, SWT.SINGLE);
fNameInput.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
fNameInput.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
setErrorMessage(null);
}
});
ToolBar grabTitleBar = new ToolBar(nameContainer, SWT.FLAT);
grabTitleBar.setBackground(control.getDisplay().getSystemColor(SWT.COLOR_WHITE));
ToolItem grabTitleItem = new ToolItem(grabTitleBar, SWT.PUSH);
grabTitleItem.setImage(OwlUI.getImage(fResources, "icons/etool16/info.gif"));
grabTitleItem.setToolTipText("Load name from feed");
grabTitleItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
onGrabTitle();
}
});
Label l3 = new Label(control, SWT.NONE);
l3.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
l3.setText("Location: ");
/* Folder Chooser */
fFolderChooser = new FolderChooser(control, getParent(), SWT.BORDER);
fFolderChooser.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
fFolderChooser.setLayout(LayoutUtils.createGridLayout(1, 0, 0, 2, 5, false));
fFolderChooser.setBackground(control.getDisplay().getSystemColor(SWT.COLOR_WHITE));
setControl(control);
}
private void onGrabTitle() {
if (StringUtils.isSet(fLinkInput.getText())) {
getShell().setCursor(getShell().getDisplay().getSystemCursor(SWT.CURSOR_APPSTARTING));
final String linkText = fLinkInput.getText();
JobRunner.runUIUpdater(new UIBackgroundJob(getShell()) {
private String fLabel;
@Override
protected void runInBackground(IProgressMonitor monitor) {
try {
URI link = new URI(linkText);
fLabel = Owl.getConnectionService().getLabel(link);
} catch (ConnectionException e) {
/* Ignore */
} catch (URISyntaxException e) {
/* Ignore */
}
}
@Override
protected void runInUI(IProgressMonitor monitor) {
if (StringUtils.isSet(fLabel))
fNameInput.setText(fLabel);
getShell().setCursor(null);
}
});
}
}
private String getInitialLink() {
Clipboard cb = new Clipboard(getShell().getDisplay());
TextTransfer transfer = TextTransfer.getInstance();
String data = (String) cb.getContents(transfer);
data = (data != null) ? data.trim() : null;
cb.dispose();
if (data != null && URIUtils.looksLikeLink(data)) {
if (!data.contains("://"))
data = HTTP + data;
} else
data = HTTP;
return data;
}
String getLink() {
return fLinkInput.getText();
}
String getBookmarkName() {
return fNameInput.getText();
}
/*
* @see org.eclipse.jface.dialogs.DialogPage#dispose()
*/
@Override
public void dispose() {
super.dispose();
fResources.dispose();
}
IFolder getFolder() {
return fFolderChooser.getFolder();
}
void focusInput(boolean link) {
if (link) {
fLinkInput.setFocus();
fLinkInput.selectAll();
} else {
fNameInput.setFocus();
fNameInput.selectAll();
}
}
}
/*
* @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
* org.eclipse.jface.viewers.IStructuredSelection)
*/
public void init(IWorkbench workbench, IStructuredSelection selection) {
fSelection = selection;
}
/*
* @see org.eclipse.jface.wizard.Wizard#addPages()
*/
@Override
public void addPages() {
fPage = new NewBookMarkWizardPage("New Bookmark");
setWindowTitle("Bookmark");
setHelpAvailable(false);
addPage(fPage);
}
/*
* @see org.eclipse.jface.wizard.Wizard#performFinish()
*/
@Override
public boolean performFinish() {
String link = fPage.getLink();
String name = fPage.getBookmarkName();
IFolder parent = fPage.getFolder();
IModelFactory factory = Owl.getModelFactory();
if (!StringUtils.isSet(link)) {
fPage.setErrorMessage("Please enter the link to the feed.");
fPage.focusInput(true);
return false;
}
if (!StringUtils.isSet(name)) {
fPage.setErrorMessage("Please enter a name for the new bookmark.");
fPage.focusInput(false);
return false;
}
URI uriObj = null;
try {
uriObj = new URI(link);
} catch (URISyntaxException e) {
fPage.setErrorMessage("Please enter a valid link.");
fPage.focusInput(true);
return false;
}
/* Check if a Feed with the URL already exists */
FeedReference feedRef = DynamicDAO.getDAO(IFeedDAO.class).loadReference(uriObj);
/* Create a new Feed then */
if (feedRef == null) {
IFeed feed = factory.createFeed(null, uriObj);
feed = DynamicDAO.getDAO(IFeedDAO.class).save(feed);
}
/* Create the BookMark */
FeedLinkReference feedLinkRef = new FeedLinkReference(uriObj);
IBookMark bookmark = factory.createBookMark(null, parent, feedLinkRef, name);
/* Copy all Properties from Parent into this Mark */
Map<String, ?> properties = parent.getProperties();
for (Map.Entry<String, ?> property : properties.entrySet())
bookmark.setProperty(property.getKey(), property.getValue());
parent = DynamicDAO.getDAO(IFolderDAO.class).save(parent);
/* Auto-Reload added BookMark */
for (IMark mark : parent.getMarks()) {
if (mark.equals(bookmark)) {
new ReloadTypesAction(new StructuredSelection(mark), (Shell) getShell().getParent()).run();
break;
}
}
return true;
}
private IFolder getParent() throws PersistenceException {
/* Check Selection */
if (fSelection != null && !fSelection.isEmpty()) {
Object element = fSelection.getFirstElement();
if (element instanceof IFolder)
return (IFolder) element;
else if (element instanceof IMark)
return ((IMark) element).getParent();
}
/* Otherwise return visible root-folder */
String selectedBookMarkSetPref = BookMarkExplorer.getSelectedBookMarkSetPref(OwlUI.getWindow());
Long selectedRootFolderID = DynamicDAO.getDAO(IPreferenceDAO.class).load(selectedBookMarkSetPref).getLong();
if (selectedRootFolderID != null)
return new FolderReference(selectedRootFolderID).resolve();
/* Finally return the first root folder */
return Controller.getDefault().getCacheService().getRootFolders().iterator().next();
}
}