/******************************************************************************* * Copyright (c) 2008 Pierre-Antoine Grégoire. * 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: * Pierre-Antoine Grégoire - initial API and implementation *******************************************************************************/ package org.org.eclipse.cheatsheet.catalog.internal.views.actions; import java.util.Map; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.window.Window; import org.org.eclipse.cheatsheet.catalog.internal.dialogs.CheatSheetCategoryDialog; import org.org.eclipse.cheatsheet.catalog.internal.model.CheatSheetCatalog; import org.org.eclipse.cheatsheet.catalog.internal.model.CheatSheetCategory; import org.org.eclipse.cheatsheet.catalog.internal.model.ModelPersistence; import org.org.eclipse.cheatsheet.catalog.internal.views.CheatSheetCatalogView; import org.org.eclipse.cheatsheet.catalog.internal.views.actions.enabling.EnabledForWritableCheatSheetCatalogAction; import org.org.eclipse.core.utils.platform.dialogs.input.IFieldIdentifier; import org.org.eclipse.core.utils.platform.dialogs.input.IFieldValueHolder; import org.org.eclipse.core.utils.platform.dialogs.input.IValidationResult; import org.org.eclipse.core.utils.platform.dialogs.input.StringBuilderValidationResult; public class AddCategoryAction extends EnabledForWritableCheatSheetCatalogAction { /** * */ private final CheatSheetCatalogView cheatSheetCatalogView; public AddCategoryAction(CheatSheetCatalogView cheatSheetCatalogView, CheatSheetCatalogView actionHost) { super(actionHost); this.cheatSheetCatalogView = cheatSheetCatalogView; } public void run() { IStructuredSelection structuredSelection = (IStructuredSelection) this.cheatSheetCatalogView.getViewer().getSelection(); CheatSheetCatalog catalog = (CheatSheetCatalog) structuredSelection.getFirstElement(); CheatSheetCategoryDialog inputDialog = new CheatSheetCategoryDialog(this.cheatSheetCatalogView.getViewSite().getShell()); inputDialog.setValidator(new CatalogAwareFieldsValidator(catalog) { @SuppressWarnings("rawtypes") public IValidationResult validate(Map<IFieldIdentifier, IFieldValueHolder> fieldValueHolders) { StringBuilderValidationResult validationResult = new StringBuilderValidationResult(); String uid = (String) fieldValueHolders.get(CheatSheetCategoryDialog.UID_FIELD).getValue(); if (uid.trim().length() == 0) { validationResult.append("Uid should not be empty\n"); } else if (!(uid).matches("[\\p{L}\\- 0-9]+")) { validationResult.append("Uid is not valid\n"); } else if (getCatalog().hasChild(uid)) { validationResult.append("Category's uid must not already exist in the same category.\n"); } String name = (String) fieldValueHolders.get(CheatSheetCategoryDialog.NAME_FIELD).getValue(); if (name.trim().length() == 0) { validationResult.append("Name should not be empty\n"); } else if (!(name).matches("[\\p{L}\\- 0-9]+")) { validationResult.append("Name is not valid\n"); } return validationResult; } }); if (inputDialog.open() == Window.OK) { CheatSheetCategory cheatSheetCategory = new CheatSheetCategory(inputDialog.getName()); catalog.addChild(cheatSheetCategory); this.cheatSheetCatalogView.refreshViewerKeepFolding(); ModelPersistence.saveCheatSheetCatalogs(); } } }