Package org.rssowl.ui.internal.dialogs

Source Code of org.rssowl.ui.internal.dialogs.SearchMarkWizard$NewSearchMarkWizardPage

/*   **********************************************************************  **
**   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.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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.rssowl.core.Owl;
import org.rssowl.core.persist.IEntity;
import org.rssowl.core.persist.IFolder;
import org.rssowl.core.persist.IMark;
import org.rssowl.core.persist.IModelFactory;
import org.rssowl.core.persist.INews;
import org.rssowl.core.persist.ISearchCondition;
import org.rssowl.core.persist.ISearchField;
import org.rssowl.core.persist.ISearchMark;
import org.rssowl.core.persist.SearchSpecifier;
import org.rssowl.core.persist.dao.DynamicDAO;
import org.rssowl.core.persist.dao.IPreferenceDAO;
import org.rssowl.core.persist.reference.FolderReference;
import org.rssowl.core.persist.service.PersistenceException;
import org.rssowl.core.util.StringUtils;
import org.rssowl.ui.internal.Controller;
import org.rssowl.ui.internal.FolderChooser;
import org.rssowl.ui.internal.OwlUI;
import org.rssowl.ui.internal.search.SearchConditionList;
import org.rssowl.ui.internal.util.LayoutUtils;
import org.rssowl.ui.internal.views.explorer.BookMarkExplorer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* A <code>Wizard</code> that allows to create a new Saved Search.
*
* @author bpasero
*/
public class SearchMarkWizard extends Wizard implements INewWizard {
  private IStructuredSelection fSelection;
  private NewSearchMarkWizardPage fPage;

  /* Page for Wizard */
  private class NewSearchMarkWizardPage extends WizardPage {
    private Text fNameInput;
    private Button fMatchAllRadio;
    private Button fMatchAnyRadio;
    private SearchConditionList fSearchConditionList;
    private FolderChooser fFolderChooser;

    NewSearchMarkWizardPage(String pageName) {
      super(pageName, pageName, OwlUI.getImageDescriptor("icons/elcl16/search.gif"));
      setMessage("Create a new saved search to find those news that are of interest for you.\nYou can use \'?\' for any character and \'*\' for any word in your search.");
    }

    /*
     * @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 nameLabel = new Label(control, SWT.NONE);
      nameLabel.setText("Name: ");

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

      Label folderLabel = new Label(control, SWT.NONE);
      folderLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
      folderLabel.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));

      Composite radioContainer = new Composite(control, SWT.None);
      radioContainer.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
      radioContainer.setLayout(LayoutUtils.createGridLayout(2, 5, 0));
      ((GridLayout) radioContainer.getLayout()).marginTop = 10;

      fMatchAllRadio = new Button(radioContainer, SWT.RADIO);
      fMatchAllRadio.setText("Match all conditions");

      fMatchAnyRadio = new Button(radioContainer, SWT.RADIO);
      fMatchAnyRadio.setText("Match any condition");
      fMatchAnyRadio.setSelection(true);

      Composite conditionsContainer = new Composite(control, SWT.BORDER);
      conditionsContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
      conditionsContainer.setLayout(LayoutUtils.createGridLayout(2));
      conditionsContainer.setBackground(control.getDisplay().getSystemColor(SWT.COLOR_WHITE));
      conditionsContainer.setBackgroundMode(SWT.INHERIT_FORCE);

      /* Search Conditions List */
      fSearchConditionList = new SearchConditionList(conditionsContainer, SWT.None, getDefaultConditions());
      fSearchConditionList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
      fSearchConditionList.setVisibleItemCount(3);

      setControl(control);
    }

    private List<ISearchCondition> getDefaultConditions() {
      List<ISearchCondition> conditions = new ArrayList<ISearchCondition>(1);
      IModelFactory factory = Owl.getModelFactory();

      ISearchField field = factory.createSearchField(IEntity.ALL_FIELDS, INews.class.getName());
      ISearchCondition condition = factory.createSearchCondition(field, SearchSpecifier.CONTAINS, "");

      conditions.add(condition);

      return conditions;
    }

    String getFolderName() {
      return fNameInput.getText();
    }

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

    IFolder getFolder() {
      return fFolderChooser.getFolder();
    }
  }

  /*
   * @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 NewSearchMarkWizardPage("New Saved Search");
    setWindowTitle("Saved Search");
    setHelpAvailable(false);
    addPage(fPage);
  }

  /*
   * @see org.eclipse.jface.wizard.Wizard#performFinish()
   */
  @Override
  public boolean performFinish() {
    String name = fPage.getFolderName();
    IModelFactory factory = Owl.getModelFactory();

    /* Require Name */
    if (!StringUtils.isSet(name)) {
      fPage.setErrorMessage("Please enter a name for the saved search");
      fPage.focusInput();
      return false;
    }

    /* Make sure Conditions are provided */
    if (fPage.fSearchConditionList.isEmpty()) {
      fPage.setErrorMessage("Please specify your search by defining some conditions below.");
      return false;
    }

    IFolder fParent = fPage.getFolder();

    ISearchMark searchMark = factory.createSearchMark(null, fParent, name);
    searchMark.setMatchAllConditions(fPage.fMatchAllRadio.getSelection());

    fPage.fSearchConditionList.createConditions(searchMark);

    DynamicDAO.save(fParent);

    /* Update the Search */
    Controller.getDefault().getSavedSearchService().updateSavedSearches(Collections.singleton(searchMark));

    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();
  }
}
TOP

Related Classes of org.rssowl.ui.internal.dialogs.SearchMarkWizard$NewSearchMarkWizardPage

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.