/*******************************************************************************
* 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 java.util.Set;
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.CheatSheetReference;
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.EnabledForWritableCheatSheetCategoryAction;
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 EditCategoryAction extends EnabledForWritableCheatSheetCategoryAction {
/**
*
*/
private final CheatSheetCatalogView cheatSheetCatalogView;
public EditCategoryAction(CheatSheetCatalogView cheatSheetCatalogView, CheatSheetCatalogView actionHost) {
super(actionHost);
this.cheatSheetCatalogView = cheatSheetCatalogView;
}
public void run() {
IStructuredSelection structuredSelection = (IStructuredSelection) this.cheatSheetCatalogView.getViewer().getSelection();
CheatSheetCategory category = (CheatSheetCategory) structuredSelection.getFirstElement();
CheatSheetCatalog catalog = category.getParent();
Set<CheatSheetReference> children = category.getChildren();
CheatSheetCategoryDialog inputDialog = new CheatSheetCategoryDialog(this.cheatSheetCatalogView.getViewSite().getShell());
inputDialog.initEdit(category);
inputDialog.setValidator(new CatalogAwareFieldsValidator(category.getParent()) {
@SuppressWarnings("rawtypes")
public IValidationResult validate(Map<IFieldIdentifier, IFieldValueHolder> fieldValueHolders) {
StringBuilderValidationResult validationResult = new StringBuilderValidationResult();
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) {
category = new CheatSheetCategory(category.getUID(), inputDialog.getName());
for (CheatSheetReference child : children) {
category.addChild(child);
}
catalog.addChild(category);
this.cheatSheetCatalogView.refreshViewerKeepFolding();
ModelPersistence.saveCheatSheetCatalogs();
}
}
}