Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved CreateResourceBundleAction for PortletApp and Portlet
  • Loading branch information
kameshsampath committed Sep 24, 2011
1 parent a585f91 commit 3c49d1d
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 268 deletions.
Expand Up @@ -80,18 +80,13 @@ public String read() {
*/
@Override
public void write( final String value ) {
String val = value;
IProject project = element().adapt( IProject.class );

// System.out.println( "VALUE ___________________ " + val );

if ( val != null ) {
val = PortletUtil.convertIOToJavaFileName( project, value.trim() );
}

// System.out.println( "TextNodeValueBinding.write() - Parent " + xml( true ).getParent() );
final XmlElement element = xml( false ).getChildElement( this.params[0], true );
element.setText( val );
if ( value != null && ( value.endsWith( ".properties" ) || value.indexOf( "/" ) != -1 ) ) {
IProject project = element().adapt( IProject.class );
element.setText( PortletUtil.convertIOToJavaFileName( project, value.trim() ) );
}
else {
element.setText( value );
}
}

}
@@ -0,0 +1,109 @@
/*******************************************************************************
* Copyright (c) 2000-2011 Accenture Services Pvt. Ltd., All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License for more
* details.
*
* Contributors:
* Kamesh Sampath - initial implementation
******************************************************************************/

package com.liferay.ide.eclipse.portlet.ui.editor.internal;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.sapphire.modeling.IModelElement;
import org.eclipse.sapphire.modeling.ModelProperty;
import org.eclipse.sapphire.modeling.ModelPropertyListener;
import org.eclipse.sapphire.modeling.ValueProperty;
import org.eclipse.sapphire.ui.SapphirePropertyEditorActionHandler;

import com.liferay.ide.eclipse.portlet.core.model.internal.ResourceBundleRelativePathService;
import com.liferay.ide.eclipse.portlet.core.util.PortletUtil;

/**
* @author <a href="mailto:kamesh.sampath@accenture.com">Kamesh Sampath</a>
*/
public abstract class AbstractResourceBundleActionHandler extends SapphirePropertyEditorActionHandler {

final IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IWorkspaceRoot wroot = workspace.getRoot();
protected ModelPropertyListener listener;

/*
* (non-Javadoc)
* @see org.eclipse.sapphire.ui.SapphirePropertyEditorActionHandler#computeEnablementState()
*/
@Override
protected boolean computeEnablementState() {
boolean isEnabled = super.computeEnablementState();
final IModelElement element = getModelElement();
final ModelProperty property = getProperty();
final IProject project = element.adapt( IProject.class );
String rbFile = element.read( (ValueProperty) property ).getText();
if ( rbFile != null ) {
String ioFileName =
PortletUtil.convertJavaToIoFileName( rbFile, ResourceBundleRelativePathService.RB_FILE_EXTENSION );
isEnabled = isEnabled && !getFileFromClasspath( project, ioFileName );
}
return isEnabled;
}

/**
* @param project
* @param ioFileName
* @return
*/
protected final boolean getFileFromClasspath( IProject project, String ioFileName ) {

IClasspathEntry[] cpEntries = PortletUtil.getClasspathEntries( project );
for ( IClasspathEntry iClasspathEntry : cpEntries ) {
if ( IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind() ) {
IPath entryPath = wroot.getFolder( iClasspathEntry.getPath() ).getLocation();
entryPath = entryPath.append( ioFileName );
IFile resourceBundleFile = wroot.getFileForLocation( entryPath );
if ( resourceBundleFile != null && resourceBundleFile.exists() ) {
return true;
}
else {
return false;
}
}
}
return false;
}

/**
* @param project
* @param ioFileName
* @return
*/
protected final IPath getResourceBundleFolderLocation( IProject project, String ioFileName ) {

IClasspathEntry[] cpEntries = PortletUtil.getClasspathEntries( project );
for ( IClasspathEntry iClasspathEntry : cpEntries ) {
if ( IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind() ) {
IPath srcFolder = wroot.getFolder( iClasspathEntry.getPath() ).getLocation();
IPath entryPath = wroot.getFolder( iClasspathEntry.getPath() ).getLocation();
entryPath = entryPath.append( ioFileName );
IFile resourceBundleFile = wroot.getFileForLocation( entryPath );
if ( resourceBundleFile != null ) {
return srcFolder;
}
}
}
return null;
}
}
@@ -0,0 +1,133 @@
/*******************************************************************************
* Copyright (c) 2000-2011 Accenture Services Pvt. Ltd., All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License for more
* details.
*
* Contributors:
* Kamesh Sampath - initial implementation
******************************************************************************/

package com.liferay.ide.eclipse.portlet.ui.editor.internal;

import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.sapphire.modeling.IModelElement;
import org.eclipse.sapphire.modeling.ModelProperty;
import org.eclipse.sapphire.modeling.ModelPropertyChangeEvent;
import org.eclipse.sapphire.modeling.ModelPropertyListener;
import org.eclipse.sapphire.modeling.Path;
import org.eclipse.sapphire.modeling.Value;
import org.eclipse.sapphire.modeling.ValueProperty;
import org.eclipse.sapphire.ui.SapphireAction;
import org.eclipse.sapphire.ui.SapphireRenderingContext;
import org.eclipse.sapphire.ui.def.ISapphireActionHandlerDef;

import com.liferay.ide.eclipse.portlet.core.model.IPortletApp;
import com.liferay.ide.eclipse.portlet.core.model.internal.ResourceBundleRelativePathService;
import com.liferay.ide.eclipse.portlet.core.util.PortletUtil;
import com.liferay.ide.eclipse.portlet.ui.PortletUIPlugin;

/**
* @author <a href="mailto:kamesh.sampath@accenture.com">Kamesh Sampath</a>
*/
public class CreatePortletAppResourceBundleActionHandler extends AbstractResourceBundleActionHandler {

/*
* (non-Javadoc)
* @see org.eclipse.sapphire.ui.SapphirePropertyEditorActionHandler#init(org.eclipse.sapphire.ui.SapphireAction,
* org.eclipse.sapphire.ui.def.ISapphireActionHandlerDef)
*/
@Override
public void init( SapphireAction action, ISapphireActionHandlerDef def ) {
super.init( action, def );
final IModelElement element = getModelElement();
final ModelProperty property = getProperty();
this.listener = new ModelPropertyListener() {

@Override
public void handlePropertyChangedEvent( final ModelPropertyChangeEvent event ) {
refreshEnablementState();
}
};

element.addListener( this.listener, property.getName() );
}

/*
* (non-Javadoc)
* @see org.eclipse.sapphire.ui.SapphireActionHandler#run(org.eclipse.sapphire.ui.SapphireRenderingContext)
*/
@Override
protected Object run( SapphireRenderingContext context ) {
final IModelElement element = getModelElement();
final IProject project = element.adapt( IProject.class );
final ModelProperty property = getProperty();
final Value<Path> resourceBundle = element.read( (ValueProperty) property );
final String defaultRBFileName =
PortletUtil.convertJavaToIoFileName(
resourceBundle.getText(), ResourceBundleRelativePathService.RB_FILE_EXTENSION );

final IPath entryPath = getResourceBundleFolderLocation( project, defaultRBFileName );
if ( getModelElement() instanceof IPortletApp ) {
final IRunnableWithProgress rbCreationProc = new IRunnableWithProgress() {

public void run( final IProgressMonitor monitor ) throws InvocationTargetException,
InterruptedException {
monitor.beginTask( "Creating resource bundle " + defaultRBFileName, 2 );

final StringBuilder rbFileBuffer = new StringBuilder( "#Portlet Application Resource Bundle \n" );
monitor.worked( 1 );

try {
final IFile rbFile = wroot.getFileForLocation( entryPath.append( defaultRBFileName ) );
rbFile.create( new ByteArrayInputStream( rbFileBuffer.toString().getBytes() ), true, monitor );
getModelElement().refresh( getProperty(), true );
monitor.worked( 1 );
}
catch ( CoreException e ) {
PortletUIPlugin.logError( e );
}

}
};

try {
( new ProgressMonitorDialog( context.getShell() ) ).run( false, false, rbCreationProc );
}
catch ( InvocationTargetException e ) {
PortletUIPlugin.logError( e );
}
catch ( InterruptedException e ) {
PortletUIPlugin.logError( e );
}
}
return null;
}

/*
* @Override(non-Javadoc)
* @see org.eclipse.sapphire.ui.SapphirePropertyEditorActionHandler#dispose()
*/
@Override
public void dispose() {
getModelElement().removeListener( this.listener, getProperty().getName() );
super.dispose();
}

}

0 comments on commit 3c49d1d

Please sign in to comment.