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? :confused:
Ich bin für jeden Tipp dankbar und bedanke mich vorab schonmal für Eure Ratschläge!

Viele Grüße
Tobias
 

Tobias3339

Mitglied
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:
Ähnliche Java Themen
  Titel Forum Antworten Datum
M import org.bukkit.plugin.java.JavaPlugin; funktioniert nicht IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 17
J Java-File Import IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 12
J Eclipse Trotz .jar-Datei im Classpath Fehler bei import IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 4
Z API hinzugefügt jedoch kein Import möglich. IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 8
OSchriever Eclipse Eclipse - Automatischer import von Klassen IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 1
B IDEA IntelliJ Import Gradle: Plötzlich Android Projekt IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 14
M gradle import in eclipse schlägt fehl IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 1
M Eclipse import eines maven projects "missing artifact" IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 8
A eclipse: AndEnginePhysicsBox2DExtension: Fehler bei import IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 1
S Geany mit Auto-Import ? IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 0
Y (NetBeans JSP) Findet @page import="org.apache.commons.fileupload.* nicht IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 6
N Netbeans import/export IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
K Eclipse Android SKD nach lib-import-Versucht zerschossen IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 0
L Eclipse Import von Library IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 1
J Eclipse Automatischer Import (wie java.lang) IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 10
L javafx import in NetBeans IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
N NetBeans Import eines Projektes IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
J Eclipse Checkout/Import Maven Projekt IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 1
F Eclipse import J2EE IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 9
F Eclipse: Import Vorschlag unsichtbar? IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 4
B Eclipse import java. dann kommt kein Fenster IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
H Eclipse import pakckages werden nicht gefunden IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
A import Anweisungen unter Netbeans IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
Q NetBeans GUI import IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
Sonecc Eclipse: File System import IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 5
M eclipse sound datei import IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 10
T Subversive erkennt Projekt nicht mehr nach Import via Ant IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 14
G Eclipse - org.apache.commons import klappt nicht IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
P Import aus anderen Projects IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 4
L Eclipse: import java.io.* anstatt import java.io.File, ... IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
G netbeans - import IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
T Eclipse 3.2 mag mich net (import problem) IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 8
T Problem beim Import von Projekt IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
C [Netbeans 4.1] import assistenz? IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
T Eclipse Wizard in Eclipse erzeugen IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
F NetBeans Wizard - Panel hinzufügen IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
M NetBeans Mit NetBeans GUI Designer einen Wizard erstellen? IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 10
F NetBeans JLable Text ändern? IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
Helgon Eclipse Shortcut für Text IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
S [Eclipse] Text Farbig gestallten IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 3
S Klassenuebergreifend nach text Suchen? IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 2
B JBuilder-Designer schmiert bei SWT.text.... ab ! IDEs - Eclipse, IntelliJ IDEA, BlueJ & mehr 6

Ähnliche Java Themen

Neue Themen


Oben