Eclipse Import Wizard: Text Datei verarbeiten

Tobias3339

Mitglied
Guten Abend/Morgen zusammen,

ich tue mich gerade etwas schwer beim Erstellen eines Import-Wizards für Eclipse. Dieser soll einfach eine .txt-Datei an eine andere Funktion übergeben, ist quasi nur Schnittstelle.
Ich habe den default Wizard org.eclipse.ui.importWizards genutzt und würde diesen gerne modifizieren. Dafür habe ich zwei Funktionen dem Konstruktor ImportWizardPage hinzugefügt:

1) myMethod();
2) workwithFile();

myMethod enabled den Finish Button.
workwithFile soll die übergebe Text-File in einen String umwandeln und an die "Verarbeitungsfunktion geben".

Java:
public void workwithFile() throws IOException{
		if (getWizard().performFinish()){
			InputStream in;
			try {
				in = getInitialContents();
				BufferedReader reader = new BufferedReader(new InputStreamReader(in));
				 StringBuilder out = new StringBuilder();
			     String line,input;
			        
				while ((line = reader.readLine()) != null) {
				    out.append(line);
				}
				input = out.toString();  //Prints the string content read from input stream
		        System.out.println("all right!");
				
		        reader.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	}

Leider bekomme ich bisher immer wieder Null-Pointer-Exceptions. Ist mein Ansatz richtig, oder liege ich völlig daneben? Warum klappt es nicht? 😕
Ich bin für jeden Tipp dankbar und bedanke mich vorab schonmal für Eure Ratschläge!

Viele Grüße
Tobias
 
Ich bekomme aktuell eine unhandled event loop exception. Wie bekomme ich nur das Event auf den Finish-Button? -.-

Ich bin in einem Blog auf die Methode finish() gestoßen. Brauche ich die oder würde workwithFile() reichen?
Ich poste einfach mal den kompletten Code, dann braucht ihr Euch den default Import-Wizard nicht zu Erstellen:

Import Wizard.java
Java:
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation and others.
 * 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
 * [url=http://www.eclipse.org/legal/epl-v10.html]Eclipse Public License - Version 1.0[/url]
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package import_text_document.importWizards;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;

public class ImportWizard extends Wizard implements IImportWizard {
	
	ImportWizardPage mainPage;

	public ImportWizard() {
		super();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	public boolean performFinish() {
		IFile file = mainPage.createNewFile();
        if (file == null)
            return false;      
        return true;
        
	}
	
	 
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		setWindowTitle("File Import Wizard"); //NON-NLS-1
		setNeedsProgressMonitor(true);
		try {
			mainPage = new ImportWizardPage("Import Text Document",selection);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //NON-NLS-1
	}
	
	/* (non-Javadoc)
     * @see org.eclipse.jface.wizard.IWizard#addPages()
     */
    public void addPages() {
        super.addPages(); 
        addPage(mainPage);
    }

}


ImportWizardPage
Java:
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation and others.
 * 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
 * [url=http://www.eclipse.org/legal/epl-v10.html]Eclipse Public License - Version 1.0[/url]
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package import_text_document.importWizards;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;


public class ImportWizardPage extends WizardNewFileCreationPage {
	
	protected FileFieldEditor editor;
	private boolean m_isPageComplete = false;

	public ImportWizardPage(String pageName, IStructuredSelection selection) throws IOException {
		super(pageName, selection);
		setTitle(pageName); //NON-NLS-1
		setDescription("Import a file from the local file system into the workspace"); //NON-NLS-1
		myMethod();
		workwithFile();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createAdvancedControls(org.eclipse.swt.widgets.Composite)
	 */	
	protected void createAdvancedControls(Composite parent) {
		Composite fileSelectionArea = new Composite(parent, SWT.NONE);
		GridData fileSelectionData = new GridData(GridData.GRAB_HORIZONTAL
				| GridData.FILL_HORIZONTAL);
		fileSelectionArea.setLayoutData(fileSelectionData);

		GridLayout fileSelectionLayout = new GridLayout();
		fileSelectionLayout.numColumns = 3;
		fileSelectionLayout.makeColumnsEqualWidth = false;
		fileSelectionLayout.marginWidth = 0;
		fileSelectionLayout.marginHeight = 0;
		fileSelectionArea.setLayout(fileSelectionLayout);
		
		editor = new FileFieldEditor("fileSelect","Select File: ",fileSelectionArea); //NON-NLS-1 //NON-NLS-2
		editor.getTextControl(fileSelectionArea).addModifyListener(new ModifyListener(){
			public void modifyText(ModifyEvent e) {
				IPath path = new Path(ImportWizardPage.this.editor.getStringValue());
				setFileName(path.lastSegment());
			}
		});
		String[] extensions = new String[] { "*.txt" }; //NON-NLS-1
		editor.setFileExtensions(extensions);
		fileSelectionArea.moveAbove(null);

	}
	
	 /* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createLinkTarget()
	 */
	protected void createLinkTarget() {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#getInitialContents()
	 */
	protected InputStream getInitialContents() {
		try {
			return new FileInputStream(new File(editor.getStringValue()));
		} catch (FileNotFoundException e) {
			return null;
		}
	}
	
	

	/* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#getNewFileLabel()
	 */
	protected String getNewFileLabel() {
		return "New File Name:"; //NON-NLS-1
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validateLinkedResource()
	 */
	protected IStatus validateLinkedResource() {
		return new Status(IStatus.OK, "Import_Text_Document", IStatus.OK, "", null); //NON-NLS-1 //NON-NLS-2
	}
	
	
	
	
	void myMethod(){
		   m_isPageComplete = true;  // Das Flag, welches enabled/disabled
		  // getWizard().getContainer().updateButtons();
		}
	
	@Override
	public boolean isPageComplete() {
	   return m_isPageComplete;
	}
	
	 public boolean isHandled(){
	      return true;  // Muss true sein, sonst gibt's eine Exception
	   }
	 
	 
	 public void addHandlerListener(IHandlerListener handlerListener){
	   }
	 
	   @Override
	   public void dispose(){
	   }
	   
	   public void removeHandlerListener(IHandlerListener handlerListener){
	   }
	 
	 
	
	void finish() throws IOException {
		   InputStream in;
			try {
				in = getInitialContents();
				BufferedReader reader = new BufferedReader(new InputStreamReader(in));
				 StringBuilder out = new StringBuilder();
			     String line,input;
			     System.out.println("hhhhhhhhhhhhhhhhhhh");
			        
				while ((line = reader.readLine()) != null) {
				    out.append(line);
				}
				input = out.toString();  //Prints the string content read from input stream
		        System.out.println("all right!");
				
		        reader.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	}
	
	
	public void workwithFile() throws IOException{
		if (getWizard().performFinish()){
			InputStream in;
			try {
				in = getInitialContents();
				BufferedReader reader = new BufferedReader(new InputStreamReader(in));
				 StringBuilder out = new StringBuilder();
			     String line,input;
			        
				while ((line = reader.readLine()) != null) {
				    out.append(line);
				}
				input = out.toString();  //Prints the string content read from input stream
		        System.out.println("all right!");
				
		        reader.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	        

	
		}
	
	}
	
}
 
Zuletzt bearbeitet:

Zurück
Oben