List of usage examples for org.eclipse.jdt.core IJavaProject findPackageFragment
IPackageFragment findPackageFragment(IPath path) throws JavaModelException;
null
if none exist. From source file:org.eclipse.che.jdt.rest.RefactoringService.java
License:Open Source License
/** * Create rename refactoring session.// www. j av a 2s. c o m * * @param settings * rename settings * @return the rename refactoring session * @throws CoreException * when RenameSupport can't be created * @throws RefactoringException * when Java element was not found */ @POST @Path("rename/create") @Produces("application/json") @Consumes("application/json") public RenameRefactoringSession createRenameRefactoring(CreateRenameRefactoring settings) throws CoreException, RefactoringException { IJavaProject javaProject = model.getJavaProject(settings.getProjectPath()); IJavaElement elementToRename; ICompilationUnit cu = null; switch (settings.getType()) { case COMPILATION_UNIT: elementToRename = javaProject.findType(settings.getPath()).getCompilationUnit(); break; case PACKAGE: elementToRename = javaProject .findPackageFragment(new org.eclipse.core.runtime.Path(settings.getPath())); break; case JAVA_ELEMENT: cu = javaProject.findType(settings.getPath()).getCompilationUnit(); elementToRename = getSelectionElement(cu, settings.getOffset()); break; default: elementToRename = null; } if (elementToRename == null) { throw new RefactoringException("Can't find java element to rename."); } return manager.createRenameRefactoring(elementToRename, cu, settings.getOffset(), settings.isRefactorLightweight()); }
From source file:org.eclipse.che.plugin.java.testing.JavaTestFinder.java
License:Open Source License
/** * Finds test classes in package./*from www . j av a 2 s . c o m*/ * * @param javaProject java project * @param packagePath package path * @param testMethodAnnotation java annotation which describes test method in the test framework * @param testClassAnnotation java annotation which describes test class in the test framework * @return list of test classes which should be ran. */ public List<String> findClassesInPackage(IJavaProject javaProject, String packagePath, String testMethodAnnotation, String testClassAnnotation) { IPackageFragment packageFragment = null; try { packageFragment = javaProject.findPackageFragment(new Path(packagePath)); } catch (JavaModelException e) { LOG.info("Can't find package.", e); } return packageFragment == null ? emptyList() : findClassesInContainer(packageFragment, testMethodAnnotation, testClassAnnotation); }
From source file:org.eclipse.jst.ws.internal.common.ResourceUtils.java
License:Open Source License
/** * Given the <code>absolutePath</code> of a Java resource, returns the * package name of the resource or null if the resource is not properly * located in a project or folder on the build classpath or that is the * build output path.//from ww w . ja va 2 s . c om * * @param absolutePath * The absolute path of the Java resource. * @return the package name of the Java resource. */ public static String getJavaResourcePackageName(IPath absolutePath) { try { IPath javaFolderPath = absolutePath.removeLastSegments(1); IProject project = getProjectOf(absolutePath); IJavaProject javaProject = JavaCore.create(project); if (javaProject != null) { IPackageFragment fragment = javaProject.findPackageFragment(javaFolderPath); if (fragment != null) { return fragment.getElementName(); } IPath outputPath = getJavaOutputLocation(project); if (outputPath.isPrefixOf(javaFolderPath)) { IPath javaPackagePath = javaFolderPath.removeFirstSegments(outputPath.segmentCount()); return javaPackagePath.isEmpty() ? null : javaPackagePath.toString().replace(IPath.SEPARATOR, '.'); } } } catch (JavaModelException e) { } return null; }
From source file:org.eclipse.pde.nls.internal.ui.model.ResourceBundleModel.java
License:Open Source License
/** * @param monitor// w ww. ja v a 2 s. co m * @throws CoreException */ private void populateFromWorkspace(IProgressMonitor monitor) throws CoreException { IWorkspace workspace = ResourcesPlugin.getWorkspace(); IWorkspaceRoot root = workspace.getRoot(); IProject[] projects = root.getProjects(); for (IProject project : projects) { try { if (!project.isOpen()) continue; IJavaProject javaProject = (IJavaProject) project.getNature(JAVA_NATURE); String pluginId = null; try { Class IFragmentModel = Class.forName("org.eclipse.pde.core.plugin.IFragmentModel"); Class IPluginModelBase = Class.forName("org.eclipse.pde.core.plugin.IPluginModelBase"); Class PluginRegistry = Class.forName("org.eclipse.pde.core.plugin.PluginRegistry"); Class IPluginBase = Class.forName("org.eclipse.pde.core.plugin.IPluginBase"); Class PluginFragmentModel = Class.forName("org.eclipse.core.runtime.model.PluginFragmentModel"); // Plugin and fragment projects Class pluginModel = (Class) PluginRegistry.getMethod("findModel", IProject.class).invoke(null, project); if (pluginModel != null) { // Get plugin id BundleDescription bd = (BundleDescription) IPluginModelBase .getMethod("getBundleDescription").invoke(pluginModel); pluginId = bd.getName(); // OSGi bundle name if (pluginId == null) { Object pluginBase = IPluginModelBase.getMethod("getPluginBase").invoke(pluginModel); pluginId = (String) IPluginBase.getMethod("getId").invoke(pluginBase); // non-OSGi // plug-in id } boolean isFragment = IFragmentModel.isInstance(pluginModel); if (isFragment) { Object pfm = IFragmentModel.getMethod("getFragment"); pluginId = (String) PluginFragmentModel.getMethod("getPluginId").invoke(pfm); } // Look for additional 'nl' resources IFolder nl = project.getFolder("nl"); //$NON-NLS-1$ if (isFragment && nl.exists()) { IResource[] members = nl.members(); for (IResource member : members) { if (member instanceof IFolder) { IFolder langFolder = (IFolder) member; String language = langFolder.getName(); // Collect property files IFile[] propertyFiles = collectPropertyFiles(langFolder); for (IFile file : propertyFiles) { // Compute path name IPath path = file.getProjectRelativePath(); String country = ""; //$NON-NLS-1$ String packageName = null; int segmentCount = path.segmentCount(); if (segmentCount > 1) { StringBuilder builder = new StringBuilder(); // Segment 0: 'nl' // Segment 1: language code // Segment 2: (country code) int begin = 2; if (segmentCount > 2 && isCountry(path.segment(2))) { begin = 3; country = path.segment(2); } for (int i = begin; i < segmentCount - 1; i++) { if (i > begin) builder.append('.'); builder.append(path.segment(i)); } packageName = builder.toString(); } String baseName = getBaseName(file.getName()); ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName, baseName); addBundle(family, getLocale(language, country), file); } } } } // Collect property files if (isFragment || javaProject == null) { IFile[] propertyFiles = collectPropertyFiles(project); for (IFile file : propertyFiles) { IPath path = file.getProjectRelativePath(); int segmentCount = path.segmentCount(); if (segmentCount > 0 && path.segment(0).equals("nl")) //$NON-NLS-1$ continue; // 'nl' resource have been // processed // above // Guess package name String packageName = null; if (segmentCount > 1) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < segmentCount - 1; i++) { if (i > 0) builder.append('.'); builder.append(path.segment(i)); } packageName = builder.toString(); } String baseName = getBaseName(file.getName()); String language = getLanguage(file.getName()); String country = getCountry(file.getName()); ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName, baseName); addBundle(family, getLocale(language, country), file); } } } } catch (Throwable e) { // MessagesEditorPlugin.log(e); } // Look for resource bundles in Java packages (output folders, // e.g. 'bin', will be ignored) if (javaProject != null) { IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true); for (IClasspathEntry entry : classpathEntries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath path = entry.getPath(); IFolder folder = workspace.getRoot().getFolder(path); IFile[] propertyFiles = collectPropertyFiles(folder); for (IFile file : propertyFiles) { String name = file.getName(); String baseName = getBaseName(name); String language = getLanguage(name); String country = getCountry(name); IPackageFragment pf = javaProject .findPackageFragment(file.getParent().getFullPath()); String packageName = pf.getElementName(); ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName, baseName); addBundle(family, getLocale(language, country), file); } } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { IPackageFragmentRoot[] findPackageFragmentRoots = javaProject .findPackageFragmentRoots(entry); for (IPackageFragmentRoot packageFragmentRoot : findPackageFragmentRoots) { IJavaElement[] children = packageFragmentRoot.getChildren(); for (IJavaElement child : children) { IPackageFragment pf = (IPackageFragment) child; Object[] nonJavaResources = pf.getNonJavaResources(); for (Object resource : nonJavaResources) { if (resource instanceof IJarEntryResource) { IJarEntryResource jarEntryResource = (IJarEntryResource) resource; String name = jarEntryResource.getName(); if (name.endsWith(PROPERTIES_SUFFIX)) { String baseName = getBaseName(name); String language = getLanguage(name); String country = getCountry(name); String packageName = pf.getElementName(); ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName, baseName); addBundle(family, getLocale(language, country), jarEntryResource); } } } } } } } // Collect non-Java resources Object[] nonJavaResources = javaProject.getNonJavaResources(); ArrayList<IFile> files = new ArrayList<IFile>(); for (Object resource : nonJavaResources) { if (resource instanceof IContainer) { IContainer container = (IContainer) resource; collectPropertyFiles(container, files); } else if (resource instanceof IFile) { IFile file = (IFile) resource; String name = file.getName(); if (isIgnoredFilename(name)) continue; if (name.endsWith(PROPERTIES_SUFFIX)) { files.add(file); } } } for (IFile file : files) { // Convert path to package name format IPath path = file.getProjectRelativePath(); String packageName = null; int segmentCount = path.segmentCount(); if (segmentCount > 1) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < segmentCount - 1; i++) { if (i > 0) builder.append('.'); builder.append(path.segment(i)); } packageName = builder.toString(); } String baseName = getBaseName(file.getName()); String language = getLanguage(file.getName()); String country = getCountry(file.getName()); ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName, baseName); addBundle(family, getLocale(language, country), file); } } } catch (Exception e) { MessagesEditorPlugin.log(e); } } }
From source file:org.eclipse.vjet.eclipse.javalaunch.utils.EclipseResourceUtils.java
License:Open Source License
public static File getSourceRoot(IJavaProject project, IJavaElement e) { try {//from w w w. j ava 2 s .c o m IPackageFragment frag = project.findPackageFragment(e.getPath()); while (frag.isDefaultPackage() == false) { e = e.getParent(); frag = project.findPackageFragment(e.getPath()); } File f = e.getResource().getLocation().toFile(); return f; } catch (JavaModelException e1) { throw new RuntimeException(e1); } }
From source file:org.eclipse.wb.tests.designer.core.nls.EditableSupportTest.java
License:Open Source License
public void test_addSource() throws Exception { waitForAutoBuild();/* w w w .j a va2 s .c om*/ ContainerInfo frame = parseContainer("public class Test extends JFrame {", " public Test() {", " setTitle('My JFrame');", " //", " JButton button = new JButton('abc');", " getContentPane().add(button);", " }", "}"); ContainerInfo contentPane = (ContainerInfo) frame.getChildrenComponents().get(0); ComponentInfo button = contentPane.getChildrenComponents().get(0); NlsSupport support = NlsSupport.get(frame); // // STAGE #1 // { EditableSupport editableSupport = (EditableSupport) support.getEditable(); // add listener final StringBuffer buffer = new StringBuffer(); IEditableSupportListener editableSupportListener = createEditableSupportListener(buffer); editableSupport.addListener(editableSupportListener); // addSource() IEditableSource editableSource; { editableSource = NlsTestUtils.createEmptyEditable("messages"); SourceDescription sourceDescription = NlsSupport.getSourceDescriptions(frame)[0]; // prepare parameters SourceParameters parameters = new SourceParameters(); IJavaProject javaProject = m_lastEditor.getJavaProject(); { parameters.m_accessorSourceFolder = javaProject .findPackageFragmentRoot(new Path("/TestProject/src")); parameters.m_accessorPackage = javaProject .findPackageFragment(new Path("/TestProject/src/test")); parameters.m_accessorPackageName = parameters.m_accessorPackage.getElementName(); parameters.m_accessorClassName = "Messages"; parameters.m_accessorFullClassName = "test.Messages"; parameters.m_accessorExists = false; } { parameters.m_propertySourceFolder = parameters.m_accessorSourceFolder; parameters.m_propertyPackage = parameters.m_accessorPackage; parameters.m_propertyFileName = "messages.properties"; parameters.m_propertyBundleName = "test.messages"; parameters.m_propertyFileExists = false; } parameters.m_withDefaultValue = true; // do add editableSupport.addSource(editableSource, sourceDescription, parameters); assertContains(buffer, "sourceAdded"); } // no key before externalizing { assertTrue(editableSource.getKeys().isEmpty()); assertTrue(editableSource.getFormKeys().isEmpty()); } // externalize { List<StringPropertyInfo> properties = Lists.newArrayList(); properties.addAll(editableSupport.getProperties(frame)); properties.addAll(editableSupport.getProperties(button)); for (StringPropertyInfo propertyInfo : properties) { editableSupport.externalizeProperty(propertyInfo, editableSource, true); assertContains(buffer, "externalizedPropertiesChanged"); } } // check keys { assertStringSet(editableSource.getKeys(), new String[] { "Test.this.title", "Test.button.text" }); assertStringSet(editableSource.getFormKeys(), new String[] { "Test.this.title", "Test.button.text" }); checkComponentsMap(editableSource, new String[] { "Test.this.title", "Test.button.text" }, new JavaInfo[][] { new JavaInfo[] { frame }, new JavaInfo[] { button } }); } // check values { assertEquals("My JFrame", editableSource.getValue(LocaleInfo.DEFAULT, "Test.this.title")); assertEquals("abc", editableSource.getValue(LocaleInfo.DEFAULT, "Test.button.text")); } // check for value in not existing locale { String value = editableSource.getValue(new LocaleInfo(Locale.GERMAN), "Test.this.title"); assertNull(value); } // rename key { IEditableSourceListener listener = createEditableSourceListener(buffer); editableSource.addListener(listener); // don't change key, ignored, no log expected editableSource.renameKey("Test.button.text", "Test.button.text"); assertTrue(buffer.length() == 0); // change wrong key, ignored, no log expected editableSource.renameKey("no-such-key", "some-other-key"); assertTrue(buffer.length() == 0); // rename key with listener editableSource.renameKey("Test.button.text", "Test.button.text3"); assertContains(buffer, "keyRenamed: Test.button.text -> Test.button.text3"); // remove listener, no log in buffer expected editableSource.removeListener(listener); editableSource.renameKey("Test.button.text3", "Test.button.text2"); assertTrue(buffer.length() == 0); // check keys { assertStringSet(editableSource.getKeys(), new String[] { "Test.this.title", "Test.button.text2" }); assertStringSet(editableSource.getFormKeys(), new String[] { "Test.this.title", "Test.button.text2" }); checkComponentsMap(editableSource, new String[] { "Test.this.title", "Test.button.text2" }, new JavaInfo[][] { new JavaInfo[] { frame }, new JavaInfo[] { button } }); } // check values { assertEquals("My JFrame", editableSource.getValue(LocaleInfo.DEFAULT, "Test.this.title")); assertEquals("abc", editableSource.getValue(LocaleInfo.DEFAULT, "Test.button.text2")); } } // apply commands support.applyEditable(editableSupport); editableSupport.removeListener(editableSupportListener); // checks { { String newProperties = getFileContentSrc("test/messages.properties"); assertTrue(newProperties.contains("#Eclipse messages class")); assertTrue(newProperties.contains("Test.this.title=My JFrame")); assertTrue(newProperties.contains("Test.button.text2=abc")); } assertEditor("public class Test extends JFrame {", " public Test() {", " setTitle(Messages.getString(\"Test.this.title\", \"My JFrame\")); //$NON-NLS-1$ //$NON-NLS-2$", " //", " JButton button = new JButton(Messages.getString(\"Test.button.text2\", \"abc\")); //$NON-NLS-1$ //$NON-NLS-2$", " getContentPane().add(button);", " }", "}"); } } // // STAGE #2 // { final StringBuffer buffer = new StringBuffer(); // prepare editable support EditableSupport editableSupport = (EditableSupport) support.getEditable(); editableSupport.addListener(createEditableSupportListener(buffer)); // prepare editable source IEditableSource editableSource = getSingleExistingEditableSource(editableSupport, buffer); // internalize { assertStringSet(editableSource.getKeys(), new String[] { "Test.this.title", "Test.button.text2" }); // editableSource.internalizeKey("Test.button.text2"); assertContains(buffer, "externalizedPropertiesChanged", false); assertContains(buffer, "keyRemoved: Test.button.text2"); // assertStringSet(editableSource.getKeys(), new String[] { "Test.this.title" }); checkComponentsMap(editableSource, new String[] { "Test.this.title" }, new JavaInfo[][] { new JavaInfo[] { frame } }); } // apply commands support.applyEditable(editableSupport); // checks { { String newProperties = getFileContentSrc("test/messages.properties"); assertTrue(newProperties.contains("Test.this.title=My JFrame")); assertFalse(newProperties.contains("Test.button.text2=abc")); } assertEditor("public class Test extends JFrame {", " public Test() {", " setTitle(Messages.getString(\"Test.this.title\", \"My JFrame\")); //$NON-NLS-1$ //$NON-NLS-2$", " //", " JButton button = new JButton(\"abc\");", " getContentPane().add(button);", " }", "}"); } } frame.refresh(); // // STAGE #3 // { final StringBuffer buffer = new StringBuffer(); // prepare editable support EditableSupport editableSupport = (EditableSupport) support.getEditable(); editableSupport.addListener(createEditableSupportListener(buffer)); // prepare editable source IEditableSource editableSource = getSingleExistingEditableSource(editableSupport, buffer); // externalize { assertStringSet(editableSource.getKeys(), new String[] { "Test.this.title" }); // StringPropertyInfo property = editableSupport.getProperties(button).get(0); editableSource.externalize(property, true); assertContains(buffer, "externalizedPropertiesChanged", false); assertContains(buffer, "keyAdded: Test.button.text"); // assertStringSet(editableSource.getKeys(), new String[] { "Test.this.title", "Test.button.text" }); checkComponentsMap(editableSource, new String[] { "Test.this.title", "Test.button.text" }, new JavaInfo[][] { new JavaInfo[] { frame }, new JavaInfo[] { button } }); } // set "empty" value for unknown key, ignored { String key = "no-such-key"; editableSource.setValue(LocaleInfo.DEFAULT, key, ""); assertNull(editableSource.getValue(LocaleInfo.DEFAULT, key)); } // set same value, ignored { String key = "Test.this.title"; String oldValue = editableSource.getValue(LocaleInfo.DEFAULT, key); editableSource.setValue(LocaleInfo.DEFAULT, key, new String("My JFrame")); assertSame(oldValue, editableSource.getValue(LocaleInfo.DEFAULT, key)); } // locales { { LocaleInfo[] locales = editableSource.getLocales(); assertEquals(1, locales.length); assertEquals("(default)", locales[0].getTitle()); } // add locales editableSource.addLocale(new LocaleInfo(new Locale("it")), LocaleInfo.DEFAULT); editableSource.addLocale(new LocaleInfo(new Locale("fr")), null); { LocaleInfo[] locales = editableSource.getLocales(); assertEquals(3, locales.length); assertEquals("(default)", locales[0].getTitle()); assertEquals("fr", locales[1].getTitle()); assertEquals("it", locales[2].getTitle()); } } // apply commands support.applyEditable(editableSupport); // checks { // *.properties: default { String newProperties = getFileContentSrc("test/messages.properties"); assertTrue(newProperties.contains("Test.this.title=My JFrame")); assertTrue(newProperties.contains("Test.button.text=abc")); } // *.properties: it { String newProperties = getFileContentSrc("test/messages_it.properties"); assertTrue(newProperties.contains("Test.this.title=My JFrame")); assertTrue(newProperties.contains("Test.button.text=abc")); } // *.properties: fr { String newProperties = getFileContentSrc("test/messages_fr.properties"); assertFalse(newProperties.contains("Test.this.title=My JFrame")); assertFalse(newProperties.contains("Test.button.text=abc")); } assertEditor("public class Test extends JFrame {", " public Test() {", " setTitle(Messages.getString(\"Test.this.title\", \"My JFrame\")); //$NON-NLS-1$ //$NON-NLS-2$", " //", " JButton button = new JButton(Messages.getString(\"Test.button.text\", \"abc\")); //$NON-NLS-1$ //$NON-NLS-2$", " getContentPane().add(button);", " }", "}"); } } frame.refresh(); // // STAGE #4 // { final StringBuffer buffer = new StringBuffer(); // prepare editable support EditableSupport editableSupport = (EditableSupport) support.getEditable(); editableSupport.addListener(createEditableSupportListener(buffer)); // prepare editable source IEditableSource editableSource = getSingleExistingEditableSource(editableSupport, buffer); // rename key (in 'fr' we don't have value) { editableSource.renameKey("Test.button.text", "Test.button.text3"); assertContains(buffer, "keyRenamed: Test.button.text -> Test.button.text3"); } // remove locale { { LocaleInfo[] locales = editableSource.getLocales(); assertEquals(3, locales.length); assertEquals("fr", locales[1].toString()); assertEquals("it", locales[2].toString()); } editableSource.removeLocale(editableSource.getLocales()[1]); { LocaleInfo[] locales = editableSource.getLocales(); assertEquals(2, locales.length); assertEquals("(default)", locales[0].getTitle()); assertEquals("it", locales[1].getTitle()); } } // apply commands support.applyEditable(editableSupport); // checks { assertTrue(getFileSrc("test", "messages.properties").exists()); assertTrue(getFileSrc("test", "messages_it.properties").exists()); // *.properties: fr - should be removed assertFalse(getFileSrc("test", "messages_fr.properties").exists()); } } frame.refresh(); // clean up frame.refresh_dispose(); }
From source file:org.eclipse.wb.tests.designer.core.nls.SourceDirectTest.java
License:Open Source License
public void test_create() throws Exception { ContainerInfo frame = parseContainer("public class Test extends JFrame {", " public Test() {", " setTitle('My JFrame');", " }", "}"); NlsSupport support = NlsSupport.get(frame); IEditableSupport editableSupport = support.getEditable(); // prepare editable source IEditableSource editableSource = NlsTestUtils.createEmptyEditable("test.messages"); // prepare parameters SourceParameters parameters = new SourceParameters(); IJavaProject javaProject = m_lastEditor.getJavaProject(); {/* w w w . j a v a 2 s . com*/ parameters.m_propertySourceFolder = javaProject.findPackageFragmentRoot(new Path("/TestProject/src")); parameters.m_propertyPackage = javaProject.findPackageFragment(new Path("/TestProject/src/test")); parameters.m_propertyFileName = "messages.properties"; parameters.m_propertyBundleName = "test.messages"; parameters.m_propertyFileExists = false; } // add source editableSupport.addSource(editableSource, new SourceDescription(DirectSource.class, DirectSourceNewComposite.class), parameters); // do externalize StringPropertyInfo propertyInfo = editableSupport.getProperties(frame).get(0); editableSupport.externalizeProperty(propertyInfo, editableSource, true); // apply commands support.applyEditable(editableSupport); // checks assertEditor("import java.util.ResourceBundle;", "public class Test extends JFrame {", " public Test() {", " setTitle(ResourceBundle.getBundle('test.messages').getString('Test.this.title')); //$NON-NLS-1$ //$NON-NLS-2$", " }", "}"); { String newProperties = getFileContentSrc("test/messages.properties"); assertTrue(newProperties.contains("#Direct ResourceBundle")); assertTrue(newProperties.contains("Test.this.title=My JFrame")); } }
From source file:org.eclipse.wb.tests.designer.core.nls.SourceEclipseModernTest.java
License:Open Source License
public void test_create() throws Exception { ContainerInfo frame = parseContainer("public class Test extends JFrame {", " public Test() {", " setTitle('My JFrame');", " }", "}"); NlsSupport support = NlsSupport.get(frame); IEditableSupport editableSupport = support.getEditable(); // prepare editable source IEditableSource editableSource = NlsTestUtils.createEmptyEditable("test.messages"); editableSource.setKeyGeneratorStrategy(ModernEclipseSource.MODERN_KEY_GENERATOR); // prepare parameters SourceParameters parameters = new SourceParameters(); IJavaProject javaProject = m_lastEditor.getJavaProject(); {/*from w ww. jav a 2s. c o m*/ parameters.m_accessorSourceFolder = javaProject.findPackageFragmentRoot(new Path("/TestProject/src")); parameters.m_accessorPackage = javaProject.findPackageFragment(new Path("/TestProject/src/test")); parameters.m_accessorPackageName = parameters.m_accessorPackage.getElementName(); parameters.m_accessorClassName = "Messages"; parameters.m_accessorFullClassName = "test.Messages"; parameters.m_accessorExists = false; } { parameters.m_propertySourceFolder = parameters.m_accessorSourceFolder; parameters.m_propertyPackage = parameters.m_accessorPackage; parameters.m_propertyFileName = "messages.properties"; parameters.m_propertyBundleName = "test.messages"; parameters.m_propertyFileExists = false; } // add source editableSupport.addSource(editableSource, new SourceDescription(ModernEclipseSource.class, ModernEclipseSourceNewComposite.class), parameters); // do externalize StringPropertyInfo propertyInfo = editableSupport.getProperties(frame).get(0); editableSupport.externalizeProperty(propertyInfo, editableSource, true); // apply commands support.applyEditable(editableSupport); // checks assertEditor("public class Test extends JFrame {", " public Test() {", " setTitle(Messages.Test_this_title);", " }", "}"); { String messages = StringUtils.replace(getFileContentSrc("test/Messages.java"), "\r\n", "\n"); assertEquals(getSourceDQ("package test;", "", "import org.eclipse.osgi.util.NLS;", "", "public class Messages extends NLS {", " private static final String BUNDLE_NAME = 'test.messages'; //$NON-NLS-1$", " public static String Test_this_title;", " ////////////////////////////////////////////////////////////////////////////", " //", " // Constructor", " //", " ////////////////////////////////////////////////////////////////////////////", " private Messages() {", " // do not instantiate", " }", " ////////////////////////////////////////////////////////////////////////////", " //", " // Class initialization", " //", " ////////////////////////////////////////////////////////////////////////////", " static {", " // load message values from bundle file", " NLS.initializeMessages(BUNDLE_NAME, Messages.class);", " }", "}"), messages); } { String newProperties = getFileContentSrc("test/messages.properties"); assertTrue(newProperties.contains("#Eclipse modern messages class")); assertTrue(newProperties.contains("Test_this_title=My JFrame")); } }
From source file:org.eclipse.wb.tests.designer.core.nls.SourceEclipseOldTest.java
License:Open Source License
/** * Use constructor without accessor, only to create {@link IEditableSource} using existing * *.properties.//from ww w.ja va 2 s. co m */ public void test_constructorWithoutAccessor() throws Exception { setFileContentSrc("test/messages.properties", getSourceDQ("frame.title=My JFrame")); waitForAutoBuild(); // ContainerInfo frame = parseContainer("// filler filler filler", "public class Test extends JFrame {", " public Test() {", " }", "}"); NlsSupport support = NlsSupport.get(frame); // prepare editable source IEditableSource editableSource; { EclipseSource source = new EclipseSource(frame, null, "test.messages"); editableSource = source.getEditable(); assertEquals("My JFrame", editableSource.getValue(LocaleInfo.DEFAULT, "frame.title")); } // prepare parameters SourceParameters parameters = new SourceParameters(); IJavaProject javaProject = m_lastEditor.getJavaProject(); { parameters.m_accessorSourceFolder = javaProject.findPackageFragmentRoot(new Path("/TestProject/src")); parameters.m_accessorPackage = javaProject.findPackageFragment(new Path("/TestProject/src/test")); parameters.m_accessorPackageName = parameters.m_accessorPackage.getElementName(); parameters.m_accessorClassName = "Messages"; parameters.m_accessorFullClassName = "test.Messages"; parameters.m_accessorExists = false; } { parameters.m_propertySourceFolder = parameters.m_accessorSourceFolder; parameters.m_propertyPackage = parameters.m_accessorPackage; parameters.m_propertyFileName = "messages.properties"; parameters.m_propertyBundleName = "test.messages"; parameters.m_propertyFileExists = false; } // add source IEditableSupport editableSupport = support.getEditable(); editableSupport.addSource(editableSource, NlsSupport.getSourceDescriptions(frame)[0], parameters); // apply commands support.applyEditable(editableSupport); // checks assertTrue(getFileSrc("/test/Messages.java").exists()); { String newProperties = getFileContentSrc("test/messages.properties"); // no comment expected because existing file should be used assertFalse(newProperties.contains("#Eclipse messages class")); // also no change assertTrue(newProperties.contains("frame.title=My JFrame")); } }
From source file:org.eclipse.wb.tests.designer.core.nls.SourceEclipseOldTest.java
License:Open Source License
public void test_addSource() throws Exception { ContainerInfo frame = parseContainer("public class Test extends JFrame {", " public Test() {", " setTitle('My JFrame');", " }", "}"); NlsSupport support = NlsSupport.get(frame); ///* w w w .j a v a 2s .c o m*/ EditableSupport editableSupport = (EditableSupport) support.getEditable(); // addSource() IEditableSource editableSource; { editableSource = NlsTestUtils.createEmptyEditable("messages"); SourceDescription sourceDescription = NlsSupport.getSourceDescriptions(frame)[0]; assertSame(EclipseSource.class, sourceDescription.getSourceClass()); // prepare parameters SourceParameters parameters = new SourceParameters(); IJavaProject javaProject = m_lastEditor.getJavaProject(); { parameters.m_accessorSourceFolder = javaProject .findPackageFragmentRoot(new Path("/TestProject/src")); parameters.m_accessorPackage = javaProject.findPackageFragment(new Path("/TestProject/src/test")); parameters.m_accessorPackageName = parameters.m_accessorPackage.getElementName(); parameters.m_accessorClassName = "Messages"; parameters.m_accessorFullClassName = "test.Messages"; parameters.m_accessorExists = false; } { parameters.m_propertySourceFolder = parameters.m_accessorSourceFolder; parameters.m_propertyPackage = parameters.m_accessorPackage; parameters.m_propertyFileName = "messages.properties"; parameters.m_propertyBundleName = "test.messages"; parameters.m_propertyFileExists = false; } parameters.m_withDefaultValue = false; // do add editableSupport.addSource(editableSource, sourceDescription, parameters); } // apply commands support.applyEditable(editableSupport); assertEquals( getSourceDQ("package test;", "", "import java.beans.Beans;", "import java.util.MissingResourceException;", "import java.util.ResourceBundle;", "", "public class Messages {", " ////////////////////////////////////////////////////////////////////////////", " //", " // Constructor", " //", " ////////////////////////////////////////////////////////////////////////////", " private Messages() {", " // do not instantiate", " }", " ////////////////////////////////////////////////////////////////////////////", " //", " // Bundle access", " //", " ////////////////////////////////////////////////////////////////////////////", " private static final String BUNDLE_NAME = 'test.messages'; //$NON-NLS-1$", " private static final ResourceBundle RESOURCE_BUNDLE = loadBundle();", " private static ResourceBundle loadBundle() {", " return ResourceBundle.getBundle(BUNDLE_NAME);", " }", " ////////////////////////////////////////////////////////////////////////////", " //", " // Strings access", " //", " ////////////////////////////////////////////////////////////////////////////", " public static String getString(String key) {", " try {", " ResourceBundle bundle = Beans.isDesignTime() ? loadBundle() : RESOURCE_BUNDLE;", " return bundle.getString(key);", " } catch (MissingResourceException e) {", " return '!' + key + '!';", " }", " }", "}"), StringUtils.replace(getFileContentSrc("test/Messages.java"), "\r\n", "\n")); assertTrue(getFileSrc("test/messages.properties").exists()); }