Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Improved CreateResourceBundleAction for PortletApp and Portlet
- Loading branch information
1 parent
a585f91
commit 3c49d1d
Showing
6 changed files
with
494 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...m/liferay/ide/eclipse/portlet/ui/editor/internal/AbstractResourceBundleActionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
...y/ide/eclipse/portlet/ui/editor/internal/CreatePortletAppResourceBundleActionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
Oops, something went wrong.