Java tutorial
/******************************************************************************* * Copyright (c) 2012 M. Cenkar, P. Nurkowski, A. Pawelec, M. Piatek * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. ******************************************************************************/ package pl.wroc.pwr.jbehaveplugin.configuration.wizard; import java.io.InputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.ui.INewWizard; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE; import pl.wroc.pwr.jbehaveplugin.configuration.generator.ConfigurationGenerator; import pl.wroc.pwr.jbehaveplugin.utils.SourceCodeFormatter; /** * Kreator tworzenia nowego pliku konfiguracyjnego JBehave. */ public class NewConfigurationFileWizard extends Wizard implements INewWizard { /** * Workbench dla ktrego uruchomiono kreator. */ private IWorkbench workbench; /** * Reprezentuje zaznaczenie, z ktrym uruchomiono kreatora (np. pakiet, w * ktrym plik ma by umieszczony). */ private IStructuredSelection selection; /** * Strona kreatora suca do wyboru pooenia tworzonego pliku * konfiguracyjnego. */ private SelectLocationPage locationPage; /** * Strona kreatora przeznaczona do ustawienia wstpnej konfiguracji. */ private ConfigurationPage configurationPage; @Override public void init(IWorkbench workbench, IStructuredSelection selection) { this.workbench = workbench; this.selection = selection; setWindowTitle("New JBehave Configration Class"); } @Override public void addPages() { super.addPages(); // dodanie strony sucej do wyboru pooenia pliku locationPage = new SelectLocationPage("Select location", selection); addPage(locationPage); // dodanie strony konfiguracyjnej configurationPage = new ConfigurationPage(); addPage(configurationPage); } @Override public boolean performFinish() { // utworzenie pliku IFile file = locationPage.createNewFile(); // nazwa klasy i pakietu String className = readFileBaseName(file.getName(), file.getFileExtension()); String packageName = readPackageName(JavaCore.create(file)); // utworzenie generatora ConfigurationGenerator generator = createConfigurationGenerator(className, packageName); // wygenerowanie konfiguracji i zapis do pliku saveToFile(generator.generateStream(), file); // sformatowanie kodu zgodnie z ustawieniami uytkownika formatSourceCode(file); // otworzenie pliku w edytorze openFileInEditor(file); // zwolnienie zasobw dispose(); return true; } /** * Formatuje kod rdowy. * * @param file plik z kodem rdowym */ private void formatSourceCode(IFile file) { try { SourceCodeFormatter.formatSourceCode(file); } catch (JavaModelException e) { // wywietlenie komunikatu o bdzie MessageBox mb = new MessageBox(workbench.getActiveWorkbenchWindow().getShell(), SWT.ERROR); mb.setText("Error"); mb.setText("Error during configuration file creation: " + e); mb.open(); } } /** * Tworzy generator pliku konfiguracyjnego JBehave, ustawiajc odpowiednie * parametry zgodnie z zaznaczeniami uytkownika na stronach kreatora. * * @param className nazwa klasy generowanej konfiguracji * @param packageName pakiet, w ktrym umieszczona ma by wygenerowana klasa * @return przygotowany generator, gotowy do uycia */ private ConfigurationGenerator createConfigurationGenerator(String className, String packageName) { // wygenerowanie zawartoci klasy konfigurujcej ConfigurationGenerator generator = new ConfigurationGenerator(className, packageName, configurationPage.getBaseClass()); // odczytanie ustawie z okna kreatora generator.setDryRun(configurationPage.getDryRun()); generator.setFailureStrategy(configurationPage.getFailureStrategy()); generator.setPendingStepStrategy(configurationPage.getPendingStepStrategy()); generator.setPrioritisingStrategy(configurationPage.getPrioritisingStrategy()); generator.setReportFormats(configurationPage.getReportFormats()); generator.setPathResolveStrategy(configurationPage.getPathResolveStrategy()); return generator; } /** * Zapisuje zawarto strumienia do pliku. * * @param stream strumie * @param file plik */ private void saveToFile(InputStream stream, IFile file) { // zapis do pliku try { file.appendContents(stream, IFile.FORCE, null); } catch (CoreException e) { // wywietlenie komunikatu o bdzie MessageBox mb = new MessageBox(workbench.getActiveWorkbenchWindow().getShell(), SWT.ERROR); mb.setText("Error"); mb.setText("Error during configuration file creation: " + e); mb.open(); } } /** * Zwraca bazow nazw pliku bez rozszerzenia (np. dla Test.java zwrci * Test). * * @param fileName nazwa pliku z rozszerzeniem * @return bazowa nazwa pliku */ static String readFileBaseName(String fileName, String fileExtension) { return fileName.replace("." + fileExtension, ""); } /** * Otwiera podany plik w edytorze. * * @param file plik do otworzenia */ private void openFileInEditor(IFile file) { try { // pobranie strony workbencha IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); // otwarcie pliku IDE.openEditor(page, file); } catch (PartInitException e) { // wywietlenie komunikatu o bdzie MessageBox mb = new MessageBox(workbench.getActiveWorkbenchWindow().getShell(), SWT.ERROR); mb.setText("Error"); mb.setText("Error during opening configuration file"); mb.open(); } } /** * Odczytuje nazw pakietu na podstawie zaznaczenia lub zwraca warto * <code>null</code> jeeli nazwy nie da si odczyta. * * @param element pocztkowy element Java * @return nazwa pakietu */ private static String readPackageName(IJavaElement element) { // zaznaczony element return findPackage(element); } /** * Odnajduje (rekurencyjnie) nazw pakietu do podanego elementu Java. * * @param element element Java * @return nazwa pakietu, jeeli domylny - <code>null</code> */ private static String findPackage(IJavaElement element) { if (element == null) { return null; } else if (element instanceof IPackageFragment) { // jeeli obiekt jest instancj IPackageFragment to znaczy, e jest // to szukany pakiet return ((IPackageFragment) element).getElementName(); } else { // w innym wypadku rekurencyjne przejcie w gr hierarchii // elementw return findPackage(element.getParent()); } } }