/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.actions;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.browser.IWebBrowser;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.model.WGADesignConfigurationModelWrapper;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.natures.IncompatibleWGAConfigVersion;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
public class OpenModulInBrowser implements IActionDelegate {
public static final String BROWSER_ID = WGADesignerPlugin.PLUGIN_ID + ".browser.TMLModul";
private IFile _selectedFile;
private String _contentStore;
public static void openBrowser(IFile module) {
String dbkey = determineContentStoreOfFile(module);
if (dbkey != null) {
openBrowser(dbkey, module);
} else {
WorkbenchUtils.showErrorDialog(Display.getCurrent().getActiveShell(), "Unable to open tml module", "Unable to open tml module '" + module.getLocation() + "' in browser. Dbkey of content store could not be found.");
}
}
public static void openBrowser(String contentStore, IFile module) {
try {
WGARuntime currentRuntime = TomcatUtils.getInstance().getCurrentRuntime();
if (currentRuntime != null) {
URL rootURL = currentRuntime.getRootURL();
if (rootURL != null) {
IWebBrowser browser = WGADesignerPlugin.getDefault().createBrowser(BROWSER_ID);
URL url = WGADesignStructureHelper.generateHttpURL(rootURL, contentStore, module);
browser.openURL(url);
}
}
} catch (Exception e) {
WorkbenchUtils.showErrorDialog(WGADesignerPlugin.getDefault(), Display.getCurrent().getActiveShell(), "Unable to open tml module", "Unable to open tml module '" + module.getLocation() + "' in browser.", e);
}
}
public void run(IAction action) {
if (_contentStore!=null && !_contentStore.equals("")) {
openBrowser(_contentStore, _selectedFile);
}
}
public void selectionChanged(IAction action, ISelection selection) {
action.setEnabled(false);
if (selection != null && selection instanceof IStructuredSelection) {
IStructuredSelection structSelection = (IStructuredSelection) selection;
Object selectedElement = structSelection.getFirstElement();
if (selectedElement instanceof IFile) {
_selectedFile = (IFile) selectedElement;
if (_selectedFile.getFileExtension().equalsIgnoreCase("tml")) {
if (WGADesignStructureHelper.isDirectAccess(_selectedFile)) {
_contentStore = determineContentStoreOfFile(_selectedFile);
if (_contentStore != null) {
action.setEnabled(true);
}
}
}
}
}
}
public static String determineContentStoreOfFile(IFile tmlfile) {
WGARuntime currentRuntime = TomcatUtils.getInstance().getCurrentRuntime();
if (currentRuntime != null) {
try {
Map<String, IContainer> contentDesign = currentRuntime.getConnectedDesignContainers();
for(String key : contentDesign.keySet()){
IContainer currentDesignFolder = contentDesign.get(key);
WGADesignStructureHelper helper = new WGADesignStructureHelper(currentDesignFolder);
if(helper.isWGADesignResourceOfCurrentDesign(tmlfile)){
return key;
}
}
IFolder[] pluginFolders = currentRuntime.getPluginsAsFolder(true);
for(IFolder currentFolder : pluginFolders){
WGADesignStructureHelper helper = new WGADesignStructureHelper(currentFolder);
if(helper.isWGADesignResourceOfCurrentDesign(tmlfile)){
WGADesignConfigurationModelWrapper wrapper = new WGADesignConfigurationModelWrapper(helper.getSyncInfo());
return "plugin-"+wrapper.getDesignKey();
}
}
} catch (IncompatibleWGAConfigVersion e) {
WGADesignerPlugin.getDefault().logError("Incompatible WGAConfig version", e);
} catch (IOException e) {
WGADesignerPlugin.getDefault().logError("Can not read WGAConfig", e);
}
}
return null;
}
}