Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Generate license file for some licenses in cabal editor

  • Loading branch information...
commit b2c3721e793e78bd6545090eac488572553d76ff 1 parent ee572b8
@JPMoresmau authored
View
1  docs/releasenotes/net.sf.eclipsefp.haskell_2.6.0.txt
@@ -19,6 +19,7 @@ Features:
- Launch configurations for cabal test and cabal bench: allows you to run all test suites or all benchmarks
- Extra license codes in cabal editor
- Category drop-down in cabal editor
+ - Generate license file for some licenses in cabal editor
Internal:
View
3  net.sf.eclipsefp.haskell.ui/build.properties
@@ -6,6 +6,7 @@ bin.includes = plugin.xml,\
icons/,\
plugin.properties,\
templates/,\
- newFiles/
+ newFiles/,\
+ licenses/
View
12 net.sf.eclipsefp.haskell.ui/licenses/bsd3.txt
@@ -0,0 +1,12 @@
+Copyright (c) ${year}, ${owner}
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+3. Neither the name of ${owner} nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
168 ...i/src/net/sf/eclipsefp/haskell/ui/internal/editors/cabal/forms/overview/LegalSection.java
@@ -4,14 +4,34 @@
// version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
package net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.overview;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+import java.util.Calendar;
import net.sf.eclipsefp.haskell.core.cabalmodel.CabalSyntax;
+import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
import net.sf.eclipsefp.haskell.ui.internal.editors.cabal.CabalFormEditor;
import net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.CabalFormSection;
+import net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.FormEntry;
+import net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.IFormEntryListener;
import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
+import net.sf.eclipsefp.haskell.util.StreamRedirect;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.variables.IStringVariableManager;
+import org.eclipse.core.variables.IValueVariable;
+import org.eclipse.core.variables.VariablesPlugin;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.editor.IFormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
@@ -23,6 +43,10 @@
* @author Leif Frenzel
*/
class LegalSection extends CabalFormSection {
+ private FormEntry licenseField;
+ private FormEntry fileField;
+ private Button bGenerate;
+ LicenseFormListener listener;
LegalSection( final IFormPage page, final Composite parent,
final CabalFormEditor editor, final IProject project ) {
@@ -35,16 +59,154 @@ protected void createClient( final FormToolkit toolkit ) {
container.setLayout( new GridLayout( 2, false ) );
GridData data = new GridData( GridData.FILL_BOTH );
getSection().setLayoutData( data );
-
+ listener=new LicenseFormListener();
String text = UITexts.legalSection_entryCopyright;
createFormEntry( CabalSyntax.FIELD_COPYRIGHT, toolkit, container, text );
String text2 = UITexts.legalSection_entryLicense;
- createComboFormEntry( CabalSyntax.FIELD_LICENSE, new LicenseChoice(),
+ licenseField=createComboFormEntry( CabalSyntax.FIELD_LICENSE, new LicenseChoice(),
toolkit, container, text2 );
String text3 = UITexts.legalSection_entryLicenseFile;
- createFormEntry( CabalSyntax.FIELD_LICENSE_FILE, toolkit, container, text3 );
+ fileField=createFormEntry( CabalSyntax.FIELD_LICENSE_FILE, toolkit, container, text3 );
+ licenseField.addFormEntryListener( listener );
+ fileField.addFormEntryListener( listener );
+ bGenerate=new Button(container,SWT.PUSH);
+ bGenerate.setText( UITexts.legalSection_license_generate );
+ bGenerate.addSelectionListener( new GenerateListener() );
+ listener.enableButton();
+ GridData gd=new GridData(GridData.HORIZONTAL_ALIGN_END);
+ gd.horizontalSpan=2;
+ bGenerate.setLayoutData( gd );
toolkit.paintBordersFor( container );
getSection().setClient( container );
}
+
+ /* (non-Javadoc)
+ * @see net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.CabalFormSection#fillInValues(boolean)
+ */
+ @Override
+ protected void fillInValues( final boolean first ) {
+ super.fillInValues( first );
+ listener.enableButton();
+ }
+
+ /**
+ * listen for changes in license type and file name
+ *
+ * @author JP Moresmau
+ *
+ */
+ private class LicenseFormListener implements IFormEntryListener{
+
+ /* (non-Javadoc)
+ * @see net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.IFormEntryListener#focusGained(net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.FormEntry)
+ */
+ @Override
+ public void focusGained( final FormEntry entry ) {
+ enableButton();
+
+ }
+
+ /* (non-Javadoc)
+ * @see net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.IFormEntryListener#textDirty(net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.FormEntry)
+ */
+ @Override
+ public void textDirty( final FormEntry entry ) {
+ enableButton();
+
+ }
+
+ /* (non-Javadoc)
+ * @see net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.IFormEntryListener#textValueChanged(net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.FormEntry)
+ */
+ @Override
+ public void textValueChanged( final FormEntry entry ) {
+ enableButton();
+
+ }
+
+ /* (non-Javadoc)
+ * @see net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.IFormEntryListener#selectionChanged(net.sf.eclipsefp.haskell.ui.internal.editors.cabal.forms.FormEntry)
+ */
+ @Override
+ public void selectionChanged( final FormEntry entry ) {
+ enableButton();
+
+ }
+
+ void enableButton(){
+ String license=licenseField.getValue();
+ String tempFile=null;
+ for( License l: License.values() ) {
+ if( l.getCabalName().equals( license ) ) {
+ tempFile=l.getFileName();
+ }
+ }
+ if (tempFile!=null && tempFile.length()>0){
+ bGenerate.setEnabled( true );
+ } else {
+ bGenerate.setEnabled( false );
+ }
+ }
+
+ }
+
+ /**
+ * generate file
+ *
+ * @author JP Moresmau
+ *
+ */
+ private class GenerateListener extends SelectionAdapter {
+ /* (non-Javadoc)
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+ */
+ @Override
+ public void widgetSelected( final SelectionEvent arg0 ) {
+ String license=licenseField.getValue();
+ String tempFile=null;
+ for( License l: License.values() ) {
+ if( l.getCabalName().equals( license ) ) {
+ tempFile=l.getFileName();
+ }
+ }
+ String file=((Text)fileField.getControl()).getText();
+ if (file==null || file.trim().length()==0){
+ file="LICENSE";
+ fileField.setValue( file, false );
+ setNewValue( file, fileField.getProperty() );
+ }
+ if (tempFile!=null && tempFile.length()>0){
+ InputStream is=getClass().getResourceAsStream( "/licenses/"+tempFile );
+ if (is!=null){
+ StringWriter sw=new StringWriter();
+ StreamRedirect sr=new StreamRedirect( new InputStreamReader( is ), sw );
+ sr.run();
+ String s=sw.toString();
+ IStringVariableManager mgr=VariablesPlugin.getDefault().getStringVariableManager();
+ IValueVariable[] vars = new IValueVariable[] {
+ mgr.newValueVariable( "year", "", true, String.valueOf(Calendar.getInstance().get( Calendar.YEAR )) ),
+ mgr.newValueVariable( "owner", "", true, getStanza().getProperties().get( CabalSyntax.FIELD_AUTHOR.toString() )),
+ };
+ try {
+ mgr.addVariables( vars );
+ try {
+ s=mgr.performStringSubstitution( s );
+
+ mgr.removeVariables( vars );
+ IFile f=getProject().getFile( file );
+ if (f.exists()){
+ f.delete( true, new NullProgressMonitor() );
+ }
+ f.create( new ByteArrayInputStream( s.getBytes() ), true, new NullProgressMonitor() );
+ } finally {
+ mgr.removeVariables( vars );
+ }
+ } catch (CoreException ce){
+ HaskellUIPlugin.log( ce );
+ }
+ }
+ }
+ }
+ }
}
View
19 ...ell.ui/src/net/sf/eclipsefp/haskell/ui/internal/editors/cabal/forms/overview/License.java
@@ -20,7 +20,7 @@
LGPL ("LGPL", "GNU Lesser General Public License"),
LGPL3 ("LGPL-3", "GNU Lesser General Public License, version 3"),
LGPL2 ("LGPL-2.1", "GNU Lesser General Public License, version 2.1"),
- BSD3 ("BSD3", "3-clause BSD license (no advertising clause)"),
+ BSD3 ("BSD3", "3-clause BSD license (no advertising clause)","bsd3.txt"),
BSD4 ("BSD4", "4-clause BSD license (with advertising clause)"),
Apache2 ("Apache-2", "Apache License, version 2.0"),
MIT ("MIT", "MIT license"),
@@ -30,12 +30,19 @@
private String cabalName;
private String shownName;
+ private String fileName;
License(final String cabalName, final String shownName) {
this.cabalName = cabalName;
this.shownName = shownName;
}
+ License(final String cabalName, final String shownName,final String fileName) {
+ this.cabalName = cabalName;
+ this.shownName = shownName;
+ this.fileName = fileName;
+ }
+
public String getCabalName() {
return cabalName;
}
@@ -43,4 +50,14 @@ public String getCabalName() {
public String getShownName() {
return shownName;
}
+
+
+ /**
+ * @return the fileName
+ */
+ public String getFileName() {
+ return fileName;
+ }
+
+
}
View
1  net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/util/UITexts.java
@@ -200,6 +200,7 @@
public static String legalSection_entryLicense;
public static String legalSection_entryLicenseFile;
public static String legalSection_title;
+ public static String legalSection_license_generate;
public static String mkPointFree_refuseDlg_title;
public static String mkPointFree_refuseDlg_message;
View
1  net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/util/uitexts.properties
@@ -224,6 +224,7 @@ legalSection_entryCopyright = Copyright
legalSection_entryLicense = License
legalSection_entryLicenseFile = License file
legalSection_title = Legal
+legalSection_license_generate = Generate
mkPointFree_refuseDlg_title=Pointfree-style refactoring
mkPointFree_refuseDlg_message=This refactoring is not available on the selected code portion.
Please sign in to comment.
Something went wrong with that request. Please try again.