/**
* Copyright 2009 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.safehaus.penrose.studio.mapping.editor;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.safehaus.penrose.client.PenroseClient;
import org.safehaus.penrose.mapping.MappingClient;
import org.safehaus.penrose.partition.PartitionClient;
import org.safehaus.penrose.partition.PartitionManagerClient;
import org.safehaus.penrose.mapping.MappingConfig;
import org.safehaus.penrose.mapping.MappingManagerClient;
import org.safehaus.penrose.studio.config.editor.ParametersPage;
import org.safehaus.penrose.studio.server.Server;
public class MappingEditor extends FormEditor implements ModifyListener {
Logger log = Logger.getLogger(getClass());
Server server;
String partitionName;
String mappingName;
MappingConfig origMappingConfig;
MappingConfig mappingConfig;
boolean dirty;
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
super.init(site, input);
MappingEditorInput ei = (MappingEditorInput)input;
server = ei.getServer();
partitionName = ei.getPartitionName();
mappingName = ei.getMappingName();
try {
PenroseClient client = server.getClient();
PartitionManagerClient partitionManagerClient = client.getPartitionManagerClient();
PartitionClient partitionClient = partitionManagerClient.getPartitionClient(partitionName);
MappingManagerClient mappingManagerClient = partitionClient.getMappingManagerClient();
MappingClient mappingClient = mappingManagerClient.getMappingClient(mappingName);
origMappingConfig = mappingClient.getMappingConfig();
mappingConfig = (MappingConfig) origMappingConfig.clone();
} catch (Exception e) {
throw new PartInitException(e.getMessage(), e);
}
setPartName(ei.getName());
}
protected void addPages() {
try {
addPage(new MappingPropertiesPage(this));
addPage(new MappingRulesPage(this));
addPage(new MappingScriptsPage(this));
ParametersPage parametersPage = new MappingParametersPage(this);
parametersPage.setParameters(mappingConfig.getParameters());
addPage(parametersPage);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
public void doSave(IProgressMonitor iProgressMonitor) {
try {
store();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
public void doSaveAs() {
}
public void store() throws Exception {
PenroseClient client = server.getClient();
PartitionManagerClient partitionManagerClient = client.getPartitionManagerClient();
PartitionClient partitionClient = partitionManagerClient.getPartitionClient(partitionName);
MappingManagerClient mappingManagerClient = partitionClient.getMappingManagerClient();
mappingManagerClient.updateMapping(origMappingConfig.getName(), mappingConfig);
partitionClient.store();
}
public boolean isDirty() {
return dirty;
}
public boolean isSaveAsAllowed() {
return false;
}
public void checkDirty() {
try {
dirty = false;
/*
if (!origMappingConfig.equals(mappingConfig)) {
dirty = true;
return;
}
*/
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
} finally {
firePropertyChange(PROP_DIRTY);
}
}
public void modifyText(ModifyEvent event) {
checkDirty();
}
public String getPartitionName() {
return partitionName;
}
public void setPartitionName(String partitionName) {
this.partitionName = partitionName;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public MappingConfig getMappingConfig() {
return mappingConfig;
}
public void setMappingConfig(MappingConfig mappingConfig) {
this.mappingConfig = mappingConfig;
}
}