Package org.rssowl.ui.internal.dialogs

Source Code of org.rssowl.ui.internal.dialogs.ImportOPMLWizard$ImportOPMLWizardPage

/*   **********************************************************************  **
**   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.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
import org.rssowl.core.interpreter.InterpreterException;
import org.rssowl.core.interpreter.ParserException;
import org.rssowl.core.util.StringUtils;
import org.rssowl.ui.internal.Activator;
import org.rssowl.ui.internal.Controller;
import org.rssowl.ui.internal.OwlUI;
import org.rssowl.ui.internal.util.JobRunner;

import java.io.File;
import java.io.FileNotFoundException;

/**
* The <code>ImportOPMLWizard</code> provides an input field to select a OPML
* file in order to import Newsfeeds.
*
* @author bpasero
*/
public class ImportOPMLWizard extends Wizard implements IImportWizard {
  private ImportOPMLWizardPage fPage;

  /* Page for Wizard */
  private static class ImportOPMLWizardPage extends WizardPage {
    private Text fInput;

    ImportOPMLWizardPage(String pageName) {
      super(pageName, pageName, OwlUI.getImageDescriptor("icons/obj16/bkmrk_wiz.gif"));
      setMessage("Please select the OPML file to import newsfeeds from.");
    }

    /*
     * @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(3, false));

      Label label = new Label(control, SWT.None);
      label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
      label.setText("File: ");

      fInput = new Text(control, SWT.BORDER);
      fInput.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
      fInput.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
          setErrorMessage(null);
        }
      });

      Button search = new Button(control, SWT.PUSH);
      search.setText("Search...");
      search.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
      search.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
          FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
          dialog.setFilterExtensions(new String[] { "*.opml", "*.xml", "*.*" });
          String file = dialog.open();
          if (file != null)
            fInput.setText(file);
        }
      });

      setControl(control);
    }

    String getFile() {
      return fInput.getText();
    }

    void focusInput() {
      fInput.setFocus();
      fInput.selectAll();
    }
  }

  /*
   * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
   * org.eclipse.jface.viewers.IStructuredSelection)
   */
  public void init(IWorkbench workbench, IStructuredSelection selection) {}

  /*
   * @see org.eclipse.jface.wizard.Wizard#addPages()
   */
  @Override
  public void addPages() {
    fPage = new ImportOPMLWizardPage("Import Newsfeeds");
    setWindowTitle("Newsfeeds");
    setHelpAvailable(false);
    addPage(fPage);
  }

  /*
   * @see org.eclipse.jface.wizard.Wizard#performFinish()
   */
  @Override
  public boolean performFinish() {
    String file = fPage.getFile();
    if (!StringUtils.isSet(file)) {
      fPage.setErrorMessage("Please enter the path to the OPML file.");
      return false;
    }

    if (!new File(file).exists()) {
      fPage.setErrorMessage("The given path does not point to an existing file.");
      return false;
    }

    try {
      Controller.getDefault().importFeeds(file);
    } catch (FileNotFoundException e) {
      Activator.getDefault().logError(e.getMessage(), e);
    } catch (InterpreterException e) {
      Activator.getDefault().logError(e.getMessage(), e);
    } catch (ParserException e) {
      Activator.getDefault().logError(e.getMessage(), e);
    }

    /* Force to rerun saved searches */
    JobRunner.runDelayedInBackgroundThread(new Runnable() {
      public void run() {
        Controller.getDefault().getSavedSearchService().updateSavedSearches(true);
      }
    });

    return true;
  }
}
TOP

Related Classes of org.rssowl.ui.internal.dialogs.ImportOPMLWizard$ImportOPMLWizardPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.