Changeset 3820


Ignore:
Timestamp:
Jun 21, 2008, 11:00:10 PM (12 years ago)
Author:
yura
Message:
  • Creating file with $module and Main function.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel-tools/trunk/eclipse/org.refal.rfpdt.ui/src/org/refal/rfpdt/ui/wizards/NewRfpModulePage.java

    r3818 r3820  
    11package org.refal.rfpdt.ui.wizards;
    22
     3import java.io.ByteArrayInputStream;
     4import java.io.InputStream;
    35import java.lang.reflect.InvocationTargetException;
    46
    57import org.eclipse.core.commands.ExecutionException;
    68import org.eclipse.core.resources.IFile;
     9import org.eclipse.core.resources.IProject;
     10import org.eclipse.core.resources.ProjectScope;
    711import org.eclipse.core.runtime.CoreException;
    812import org.eclipse.core.runtime.IProgressMonitor;
    913import org.eclipse.core.runtime.IStatus;
     14import org.eclipse.core.runtime.Platform;
     15import org.eclipse.core.runtime.preferences.IScopeContext;
     16import org.eclipse.core.runtime.preferences.InstanceScope;
    1017import org.eclipse.jdt.core.IJavaElement;
     18import org.eclipse.jdt.core.IJavaProject;
    1119import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
    1220import org.eclipse.jface.operation.IRunnableWithProgress;
    1321import org.eclipse.jface.viewers.IStructuredSelection;
    1422import org.eclipse.swt.SWT;
     23import org.eclipse.swt.events.SelectionEvent;
     24import org.eclipse.swt.events.SelectionListener;
     25import org.eclipse.swt.layout.GridData;
    1526import org.eclipse.swt.layout.GridLayout;
     27import org.eclipse.swt.widgets.Button;
    1628import org.eclipse.swt.widgets.Composite;
    1729import org.eclipse.ui.PlatformUI;
     
    1931import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
    2032import org.refal.rfpdt.core.RfpCore;
     33import org.refal.rfpdt.parser.RfpCharacter;
    2134import org.refal.rfpdt.ui.RfpUI;
    2235
    2336public class NewRfpModulePage extends NewTypeWizardPage {
     37    private Button button;
     38    private boolean buttonValue = false;
     39
    2440    public NewRfpModulePage () {
    2541        super(false, "NewRfpModulePage");
     
    7692        createTypeNameControls(composite, nColumns);
    7793
    78         // Button button = new Button(composite, SWT.CHECK | SWT.LEFT);
    79         // button.setLayoutData(new GridData(SWT.LEFT, SWT.LEFT, false, false, 4, 1));
    80         // button.setText("Create function $func Main = e");
     94        button = new Button(composite, SWT.CHECK | SWT.LEFT);
     95        button.setLayoutData(new GridData(SWT.LEFT, SWT.LEFT, false, false, 4, 1));
     96        button.setText("Create function $func Main = e");
     97        button.addSelectionListener(new SelectionListener() {
     98
     99            public void widgetDefaultSelected (SelectionEvent e) {
     100                buttonValue = button.getSelection();
     101            }
     102
     103            public void widgetSelected (SelectionEvent e) {
     104                buttonValue = button.getSelection();
     105            }
     106           
     107        });
    81108
    82109        setControl(composite);
     110       
    83111    }
    84112
     
    89117    }
    90118
     119    private InputStream getInputStream () {
     120        String lineDelimiter = getProjectLineDelimiter(getJavaProject());
     121        StringBuffer buf = new StringBuffer();
     122        buf.append("$module ");
     123        String moduleName = getPackageText();
     124        if (! moduleName.equals(""))
     125            moduleName += '.';
     126        moduleName += getTypeName();
     127        if (!RfpCharacter.isIdStartChar(moduleName.charAt(0)))
     128            moduleName = '\"' + moduleName + '\"';
     129        buf.append(moduleName);
     130        buf.append(';');
     131        buf.append(lineDelimiter);
     132        if (buttonValue) {
     133            buf.append(lineDelimiter);
     134            buf.append("$func Main = e;");
     135            buf.append(lineDelimiter);
     136            buf.append(lineDelimiter);
     137            buf.append("Main = /* TODO Auto-generated method stub */;");
     138            buf.append(lineDelimiter);
     139        }
     140        return new ByteArrayInputStream(buf.toString().getBytes());
     141    }
     142
     143    private static String getProjectLineDelimiter (IJavaProject javaProject) {
     144        IProject project = null;
     145        if (javaProject != null)
     146            project = javaProject.getProject();
     147
     148        String lineDelimiter = getLineDelimiterPreference(project);
     149        if (lineDelimiter != null)
     150            return lineDelimiter;
     151
     152        return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
     153    }
     154
     155    private static String getLineDelimiterPreference (IProject project) {
     156        IScopeContext[] scopeContext;
     157        if (project != null) {
     158            // project preference
     159            scopeContext = new IScopeContext[] { new ProjectScope(project) };
     160            String lineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME,
     161                    Platform.PREF_LINE_SEPARATOR, null, scopeContext);
     162            if (lineDelimiter != null)
     163                return lineDelimiter;
     164        }
     165        // workspace preference
     166        scopeContext = new IScopeContext[] { new InstanceScope() };
     167        String platformDefault = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
     168        return Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR,
     169                platformDefault, scopeContext);
     170    }
     171
    91172    public IFile createModule () throws CoreException, InterruptedException {
    92173        final IFile file = RfpCore.getRfpProject(getJavaProject().getProject()).getSource(
     
    94175        IRunnableWithProgress op = new IRunnableWithProgress() {
    95176            public void run (IProgressMonitor monitor) {
    96                 CreateFileOperation op = new CreateFileOperation(file, null, null, "NewFile");
     177                CreateFileOperation op = new CreateFileOperation(file, null, getInputStream(), "NewFile");
    97178                try {
    98179                    PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().execute(op, monitor,
Note: See TracChangeset for help on using the changeset viewer.