### Eclipse Workspace Patch 1.0
#P org.jboss.tools.jst.jsp
Index: META-INF/MANIFEST.MF
===================================================================
--- META-INF/MANIFEST.MF (revision 26130)
+++ META-INF/MANIFEST.MF (working copy)
@@ -59,3 +59,4 @@
org.eclipse.ui
Bundle-Version: 3.2.0.qualifier
Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Import-Package: org.jboss.tools.jsf.model.pv
Index: src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizardPage.java
===================================================================
--- src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizardPage.java (revision 26130)
+++ src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizardPage.java (working copy)
@@ -92,6 +92,8 @@
public class ExternalizeStringsWizardPage extends WizardPage {
+ public static final String PAGE_NAME = "ExternalizeStringsWizardBasicPage"; //$NON-NLS-1$
+
private final char[] REPLACED_CHARACTERS = new char[] {'~', '!', '@', '#',
'$', '%', '^', '&', '*', '(', ')', '-', '+', '=', '{', '}', '[', ']', ':', ';', ',', '.', '?', '\\', '/'};
private final char[] LINE_DELEMITERS = new char[] {'\r', '\n', '\t'};
@@ -266,8 +268,192 @@
*/
setControl(composite);
}
+
+ /**
+ * Generate properties key.
+ * Replaces all non-word characters with
+ * underline character.
+ *
+ * @param text the text
+ * @return the result string
+ */
+ public String generatePropertyKey(String text) {
+ String result = text.trim();
+ /*
+ * Replace all other symbols with '_'
+ */
+ for (char ch : REPLACED_CHARACTERS) {
+ result = result.replace(ch, '_');
+ }
+ /*
+ * Replace line delimiters white space
+ */
+ for (char ch : LINE_DELEMITERS) {
+ result = result.replace(ch, ' ');
+ }
+ /*
+ * Replace all white spaces with '_'
+ */
+ result = result.replaceAll(Constants.WHITE_SPACE,
+ Constants.UNDERSCORE);
+ /*
+ * Correct underline symbols:
+ * show only one of them
+ */
+ result = result.replaceAll("_+", Constants.UNDERSCORE); //$NON-NLS-1$
+ /*
+ * Remove leading and trailing '_'
+ */
+ if (result.startsWith(Constants.UNDERSCORE)) {
+ result = result.substring(1);
+ }
+ if (result.endsWith(Constants.UNDERSCORE)) {
+ result = result.substring(0, result.length() - 1);
+ }
+ /*
+ * Return the result
+ */
+ return result;
+ }
/**
+ * Gets the bundle prefix.
+ *
+ * @return the bundle prefix
+ */
+ public String getBundlePrefix() {
+ String bundlePrefix = Constants.EMPTY;
+ if (!isNewFile()) {
+ for (BundleEntry be : bm.getBundles()) {
+ if (be.uri.equalsIgnoreCase(rbCombo.getText())) {
+ bundlePrefix = be.prefix;
+ }
+ }
+ }
+ return bundlePrefix;
+ }
+
+ /**
+ * Gets resource bundle's file
+ * @return the file
+ */
+ public IFile getBundleFile() {
+ return bm.getBundleFile(rbCombo.getText());
+ }
+
+ /**
+ * Use existed key-value pair from the properties file
+ * without writing any data to the file.
+ *
+ * @return
+ */
+ public boolean isDuplicatedKeyAndValue() {
+ boolean exists = false;
+ if (isValueDuplicated(propsValue.getText())
+ && isKeyDuplicated(propsKey.getText())) {
+ exists = true;
+ }
+ return exists;
+ }
+
+ /**
+ * Gets key=value
pair
+ *
+ * @return a pair key=value
+ */
+ public String getKeyValuePair() {
+ return propsKey.getText() + Constants.EQUAL + propsValue.getText();
+ }
+
+ /**
+ * Gets the key.
+ *
+ * @return the key
+ */
+ public String getKey() {
+ return propsKey.getText();
+ }
+
+ /**
+ * Check if "Create new file.." option is enabled
+ *
+ * @return the status
+ */
+ public boolean isNewFile() {
+ return newFile.getSelection();
+ }
+
+ /**
+ * Replaces the text in the current file
+ */
+ public void replaceText(String replacement) {
+ IDocumentProvider prov = editor.getDocumentProvider();
+ IDocument doc = prov.getDocument(editor.getEditorInput());
+ ISelection sel = editor.getSelectionProvider().getSelection();
+ if (ExternalizeStringsUtils.isSelectionCorrect(sel)) {
+ try {
+ /*
+ * Get source text and new text
+ */
+ TextSelection textSel = (TextSelection) sel;
+ IStructuredSelection structuredSelection = (IStructuredSelection) sel;
+ Object firstElement = structuredSelection.getFirstElement();
+ int offset = 0;
+ int length = 0;
+ /*
+ * When user selection is empty
+ * underlying node will e automatically selected.
+ * Thus we need to correct replacement offsets.
+ */
+ if ((textSel.getLength() != 0)) {
+ offset = textSel.getOffset();
+ length = textSel.getLength();
+ } else if (firstElement instanceof TextImpl) {
+ TextImpl ti = (TextImpl) firstElement;
+ offset = ti.getStartOffset();
+ length = ti.getLength();
+ } else if (firstElement instanceof AttrImpl) {
+ AttrImpl ai = (AttrImpl) firstElement;
+ /*
+ * Get offset and length without quotes ".."
+ */
+ offset = ai.getValueRegionStartOffset() + 1;
+ length = ai.getValueRegionText().length() - 2;
+ }
+ /*
+ * Replace text in the editor with "key.value"
+ */
+ doc.replace(offset, length, replacement);
+ } catch (BadLocationException ex) {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+ @Override
+ public boolean isPageComplete() {
+ boolean isPageComplete = false;
+ /*
+ * The page is ready when there are no error messages
+ * and the bundle is selected
+ * and "key=value" exists.
+ */
+ if ((getErrorMessage() == null)
+ && !Constants.EMPTY.equalsIgnoreCase(propsKey.getText().trim())
+ && !Constants.EMPTY.equalsIgnoreCase(propsValue.getText().trim())
+ && ((rbCombo.getSelectionIndex() != -1) || isNewFile())) {
+ isPageComplete = true;
+ }
+ return isPageComplete;
+ }
+
+ @Override
+ public boolean canFlipToNextPage() {
+ return isPageComplete() && (getNextPage() != null)
+ && isNewFile();
+ }
+
+ /**
* Initialize dialog's controls.
* Fill in appropriate text and make validation.
*/
@@ -596,88 +782,6 @@
}
/**
- * Gets key=value
pair
- *
- * @return a pair \nkey=value\n
- */
- public String getKeyValuePair() {
- return propsKey.getText() + Constants.EQUAL + propsValue.getText();
- }
-
- /**
- * Gets resource bundle's file
- * @return the file
- */
- public IFile getBundleFile() {
- return bm.getBundleFile(rbCombo.getText());
- }
-
- /**
- * Check if "Create new file.." option is enabled
- *
- * @return the status
- */
- public boolean isNewFile() {
- return newFile.getSelection();
- }
-
- /**
- * Replaces the text in the current file
- */
- public void replaceText() {
- IDocumentProvider prov = editor.getDocumentProvider();
- IDocument doc = prov.getDocument(editor.getEditorInput());
- ISelection sel = editor.getSelectionProvider().getSelection();
- if (ExternalizeStringsUtils.isSelectionCorrect(sel)) {
- try {
- /*
- * Get source text and new text
- */
- TextSelection textSel = (TextSelection) sel;
- IStructuredSelection structuredSelection = (IStructuredSelection) sel;
- Object firstElement = structuredSelection.getFirstElement();
- int offset = 0;
- int length = 0;
- /*
- * When user selection is empty
- * underlying node will e automatically selected.
- * Thus we need to correct replacement offsets.
- */
- if ((textSel.getLength() != 0)) {
- offset = textSel.getOffset();
- length = textSel.getLength();
- } else if (firstElement instanceof TextImpl) {
- TextImpl ti = (TextImpl) firstElement;
- offset = ti.getStartOffset();
- length = ti.getLength();
- } else if (firstElement instanceof AttrImpl) {
- AttrImpl ai = (AttrImpl) firstElement;
- /*
- * Get offset and length without quotes ".."
- */
- offset = ai.getValueRegionStartOffset() + 1;
- length = ai.getValueRegionText().length() - 2;
- }
- /*
- * Replace text in the editor with "key.value"
- */
- String bundlePrefix = Constants.EMPTY;
- if (!isNewFile()) {
- for (BundleEntry be : bm.getBundles()) {
- if (be.uri.equalsIgnoreCase(rbCombo.getText())) {
- bundlePrefix = be.prefix;
- }
- }
- }
- String newText = "#{" + bundlePrefix + Constants.DOT + propsKey.getText() + "}"; //$NON-NLS-1$ //$NON-NLS-2$
- doc.replace(offset, length, newText);
- } catch (BadLocationException ex) {
- ex.printStackTrace();
- }
- }
- }
-
- /**
* Update duplicate key status.
*/
private void updateDuplicateKeyStatus() {
@@ -803,29 +907,6 @@
}
}
- @Override
- public boolean isPageComplete() {
- boolean isPageComplete = false;
- /*
- * The page is ready when there are no error messages
- * and the bundle is selected
- * and "key=value" exists.
- */
- if ((getErrorMessage() == null)
- && !Constants.EMPTY.equalsIgnoreCase(propsKey.getText().trim())
- && !Constants.EMPTY.equalsIgnoreCase(propsValue.getText().trim())
- && ((rbCombo.getSelectionIndex() != -1) || isNewFile())) {
- isPageComplete = true;
- }
- return isPageComplete;
- }
-
- @Override
- public boolean canFlipToNextPage() {
- return isPageComplete() && (getNextPage() != null)
- && isNewFile();
- }
-
/**
* Creates new bundle map if no one was specified
* during initialization of the page.
@@ -946,68 +1027,6 @@
}
return bm;
}
-
- /**
- * Use existed key-value pair from the properties file
- * without writing any data to the file.
- *
- * @return
- */
- public boolean isDuplicatedKeyValue() {
- boolean exists = false;
- if (isValueDuplicated(propsValue.getText())
- && isKeyDuplicated(propsKey.getText())) {
- exists = true;
- }
- return exists;
- }
-
- /**
- * Generate properties key.
- * Replaces all non-word characters with
- * underline character.
- *
- * @param text the text
- * @return the result string
- */
- public String generatePropertyKey(String text) {
- String result = text.trim();
- /*
- * Replace all other symbols with '_'
- */
- for (char ch : REPLACED_CHARACTERS) {
- result = result.replace(ch, '_');
- }
- /*
- * Replace line delimiters white space
- */
- for (char ch : LINE_DELEMITERS) {
- result = result.replace(ch, ' ');
- }
- /*
- * Replace all white spaces with '_'
- */
- result = result.replaceAll(Constants.WHITE_SPACE,
- Constants.UNDERSCORE);
- /*
- * Correct underline symbols:
- * show only one of them
- */
- result = result.replaceAll("_+", Constants.UNDERSCORE); //$NON-NLS-1$
- /*
- * Remove leading and trailing '_'
- */
- if (result.startsWith(Constants.UNDERSCORE)) {
- result = result.substring(1);
- }
- if (result.endsWith(Constants.UNDERSCORE)) {
- result = result.substring(0, result.length() - 1);
- }
- /*
- * Return the result
- */
- return result;
- }
/**
* Sets the resource bundle path according to the selection
Index: src/org/jboss/tools/jst/jsp/messages/messages.properties
===================================================================
--- src/org/jboss/tools/jst/jsp/messages/messages.properties (revision 26130)
+++ src/org/jboss/tools/jst/jsp/messages/messages.properties (working copy)
@@ -137,3 +137,8 @@
EXTERNALIZE_STRINGS_DIALOG_ENTER_KEY_NAME=Please specify the property key
EXTERNALIZE_STRINGS_DIALOG_SELECT_RESOURCE_BUNDLE=Please select the resource bundle
CANNOT_LOAD_TAGLIBS_FROM_PAGE_CONTEXT=Cannot load taglibs from PageContext!
+EXTERNALIZE_STRINGS_DIALOG_SAVE_RESOURCE_BUNDLE=Select a place to register the bundle
+EXTERNALIZE_STRINGS_DIALOG_FACES_CONFIG=in the faces-config.xml file
+EXTERNALIZE_STRINGS_DIALOG_LOAD_BUNDLE=via tag on the current page
+EXTERNALIZE_STRINGS_DIALOG_USER_DEFINED=manually by user
+EXTERNALIZE_STRINGS_DIALOG_BUNDLE_NAME=Bundle name:
Index: src/org/jboss/tools/jst/jsp/bundle/BundleMap.java
===================================================================
--- src/org/jboss/tools/jst/jsp/bundle/BundleMap.java (revision 26130)
+++ src/org/jboss/tools/jst/jsp/bundle/BundleMap.java (working copy)
@@ -196,13 +196,15 @@
if(entry == null){
if (hasJsfProjectNatureType()) {
- IProject project = ((IFileEditorInput)editor.getEditorInput()).getFile().getProject();
- XModel model = EclipseResourceUtil.getModelNature(project).getModel();
- String prefix2 = prefix;
- if(propertyName != null && prefix != null) {
- prefix2 = prefix + "." + propertyName; //$NON-NLS-1$
+ if (editor.getEditorInput() instanceof IFileEditorInput) {
+ IProject project = ((IFileEditorInput)editor.getEditorInput()).getFile().getProject();
+ XModel model = EclipseResourceUtil.getModelNature(project).getModel();
+ String prefix2 = prefix;
+ if(propertyName != null && prefix != null) {
+ prefix2 = prefix + "." + propertyName; //$NON-NLS-1$
+ }
+ WebPromptingProvider.getInstance().getList(model, WebPromptingProvider.JSF_BEAN_OPEN, prefix2, null);
}
- WebPromptingProvider.getInstance().getList(model, WebPromptingProvider.JSF_BEAN_OPEN, prefix2, null);
}
return false;
}
@@ -213,12 +215,13 @@
p.put(WebPromptingProvider.KEY, propertyName);
if (locale != null) p.put(WebPromptingProvider.LOCALE, locale);
p.put(WebPromptingProvider.FILE, ((IFileEditorInput)editor.getEditorInput()).getFile().getProject());
-
- IProject project = ((IFileEditorInput)editor.getEditorInput()).getFile().getProject();
- XModel model = EclipseResourceUtil.getModelNature(project).getModel();
-
- WebPromptingProvider.getInstance().getList(model, WebPromptingProvider.JSF_OPEN_KEY, entry.uri, p);
- String error = p.getProperty(WebPromptingProvider.ERROR);
+ String error = null;
+ if (editor.getEditorInput() instanceof IFileEditorInput) {
+ IProject project = ((IFileEditorInput)editor.getEditorInput()).getFile().getProject();
+ XModel model = EclipseResourceUtil.getModelNature(project).getModel();
+ WebPromptingProvider.getInstance().getList(model, WebPromptingProvider.JSF_OPEN_KEY, entry.uri, p);
+ error = p.getProperty(WebPromptingProvider.ERROR);
+ }
return (error == null || error.length() == 0);
}
return false;
Index: src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsUtils.java
===================================================================
--- src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsUtils.java (revision 26130)
+++ src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsUtils.java (working copy)
@@ -13,6 +13,12 @@
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
+import org.jboss.tools.common.model.XModel;
+import org.jboss.tools.common.model.XModelObject;
+import org.jboss.tools.jsf.model.pv.JSFProjectTreeConstants;
+import org.jboss.tools.jsf.model.pv.JSFProjectsRoot;
+import org.jboss.tools.jsf.model.pv.JSFProjectsTree;
+import org.jboss.tools.jst.web.model.pv.WebProjectNode;
import org.w3c.dom.Element;
/**
@@ -50,5 +56,34 @@
}
return isSelectionCorrect;
}
-
+
+ public static XModelObject findFacesConfig(XModel model) {
+ XModelObject facesConfig = null;
+ JSFProjectsRoot root = JSFProjectsTree.getProjectsRoot(model);
+ if (root != null) {
+ WebProjectNode n = (WebProjectNode) root
+ .getChildByPath(JSFProjectTreeConstants.CONFIGURATION);
+ if (n != null) {
+ /*
+ * The array contains the all configuration files in the project
+ * including files from jar archives.
+ * Only editable object is be the necessary faces-config file.
+ */
+ XModelObject[] os = n.getTreeChildren();
+ for (XModelObject o : os) {
+ if (o.isObjectEditable()) {
+ facesConfig = o;
+ break;
+ }
+ }
+ }
+ }
+ /*
+ * When nothing has been found try the last straight-forward way.
+ */
+ if (facesConfig == null) {
+ facesConfig = model.getByPath("/faces-config.xml"); //$NON-NLS-1$
+ }
+ return facesConfig;
+ }
}
Index: src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizardRegisterBundlePage.java
===================================================================
--- src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizardRegisterBundlePage.java (revision 0)
+++ src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizardRegisterBundlePage.java (revision 0)
@@ -0,0 +1,158 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2010 Exadel, Inc. and Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Exadel, Inc. and Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.jst.jsp.i18n;
+
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.jboss.tools.common.model.ui.ModelUIImages;
+import org.jboss.tools.common.resref.core.ResourceReference;
+import org.jboss.tools.jst.jsp.messages.JstUIMessages;
+import org.jboss.tools.jst.jsp.outline.cssdialog.common.Constants;
+
+public class ExternalizeStringsWizardRegisterBundlePage extends WizardPage
+ implements SelectionListener {
+
+ public static final String PAGE_NAME = "ExternalizeStringsWizardRegisterBundlePage"; //$NON-NLS-1$
+ public static final int FACES_CONFIG = 1;
+ public static final int LOAD_BUNDLE = 2;
+ public static final int USER_DEFINED = 3;
+
+ private final int DIALOG_WIDTH = 450;
+ private final int DIALOG_HEIGHT = 650;
+
+ private Button facesConfig;
+ private Button laodBundle;
+ private Button userDefined;
+ private Label bundleLabel;
+ private Text bundleName;
+
+ public ExternalizeStringsWizardRegisterBundlePage(String pageName) {
+ super(pageName, JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_TITLE,
+ ModelUIImages.getImageDescriptor(ModelUIImages.WIZARD_DEFAULT));
+ setDescription(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_DESCRIPTION);
+ setPageComplete(false);
+ }
+
+ @Override
+ public void createControl(Composite parent) {
+ /*
+ * Create basic container
+ */
+ Composite composite = new Composite(parent, SWT.NONE);
+ composite.setLayout(new GridLayout(2, false));
+ GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
+ gd.widthHint = DIALOG_WIDTH;
+ gd.heightHint = DIALOG_HEIGHT;
+ composite.setLayoutData(gd);
+
+ /*
+ * Group with a place for bundle
+ */
+ Group group = new Group(composite, SWT.SHADOW_ETCHED_IN);
+ group.setLayout(new GridLayout(1, true));
+ group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
+ group.setText(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_SAVE_RESOURCE_BUNDLE);
+
+ facesConfig = new Button(group, SWT.RADIO);
+ laodBundle = new Button(group, SWT.RADIO);
+ userDefined = new Button(group, SWT.RADIO);
+ facesConfig.setText(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_FACES_CONFIG);
+ laodBundle.setText(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_LOAD_BUNDLE);
+ userDefined.setText(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_USER_DEFINED);
+ facesConfig.setSelection(true);
+ facesConfig.addSelectionListener(this);
+ laodBundle.addSelectionListener(this);
+ userDefined.addSelectionListener(this);
+
+ /*
+ * Input field with bundle name
+ */
+ bundleLabel = new Label(composite, SWT.NONE);
+ bundleLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.NONE, false, false, 1, 1));
+ bundleLabel.setText(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_BUNDLE_NAME);
+ bundleName = new Text(composite, SWT.BORDER);
+ bundleName.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 1, 1));
+ String fileName = Constants.EMPTY;
+ if (getWizard() instanceof ExternalizeStringsWizard) {
+ fileName = ((ExternalizeStringsWizard) getWizard()).editor
+ .getEditorInput().getName();
+ int pos = fileName.lastIndexOf(Constants.DOT);
+ if (pos != -1) {
+ fileName = fileName.substring(0, pos);
+ }
+ }
+ bundleName.setText(fileName);
+
+ /*
+ * Wizard Page control should be initialized.
+ */
+ setControl(composite);
+ }
+
+ public String getBundleName() {
+ String name = Constants.EMPTY;
+ if (bundleName != null) {
+ name = bundleName.getText();
+ }
+ return name;
+ }
+
+ public boolean isInFacesConfig() {
+ return (null != facesConfig) && facesConfig.getSelection();
+ }
+
+ public boolean isViaLoadBundle() {
+ return (null != laodBundle) && laodBundle.getSelection();
+ }
+
+ public boolean isUserDefined() {
+ return (null != userDefined) && userDefined.getSelection();
+ }
+
+ private int getSelectedPlace() {
+ int place = FACES_CONFIG;
+ if (isInFacesConfig()) {
+ place = FACES_CONFIG;
+ } else if (isViaLoadBundle()) {
+ place = LOAD_BUNDLE;
+ } else if (isUserDefined()) {
+ place = USER_DEFINED;
+ }
+ return place;
+ }
+
+ private void updateBundleNameField() {
+ if (isUserDefined()) {
+ bundleLabel.setEnabled(false);
+ bundleName.setEnabled(false);
+ } else {
+ bundleLabel.setEnabled(true);
+ bundleName.setEnabled(true);
+ }
+ }
+
+ public void widgetSelected(SelectionEvent e) {
+ updateBundleNameField();
+ }
+
+ public void widgetDefaultSelected(SelectionEvent e) {
+ updateBundleNameField();
+ }
+}
Index: src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizard.java
===================================================================
--- src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizard.java (revision 26130)
+++ src/org/jboss/tools/jst/jsp/i18n/ExternalizeStringsWizard.java (working copy)
@@ -13,30 +13,47 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Properties;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
+import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
+import org.jboss.tools.common.EclipseUtil;
+import org.jboss.tools.common.meta.action.XActionInvoker;
+import org.jboss.tools.common.meta.action.impl.handlers.DefaultCreateHandler;
+import org.jboss.tools.common.model.XModel;
+import org.jboss.tools.common.model.XModelException;
+import org.jboss.tools.common.model.XModelObject;
import org.jboss.tools.common.model.ui.ModelUIImages;
+import org.jboss.tools.common.model.util.EclipseResourceUtil;
import org.jboss.tools.jst.jsp.JspEditorPlugin;
import org.jboss.tools.jst.jsp.bundle.BundleMap;
import org.jboss.tools.jst.jsp.messages.JstUIMessages;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.Constants;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
public class ExternalizeStringsWizard extends Wizard {
- public String ExternalizeStringsWizardPageName = "ExternalizeStringsWizardPage"; //$NON-NLS-1$
- public String NewFileCreationPageName = "NewFileCreationPage"; //$NON-NLS-1$
+ public static final String EXTERNALIZE_STRINGS_DIALOG_NEW_FILE_PAGE =
+ "EXTERNALIZE_STRINGS_DIALOG_NEW_FILE_PAGE"; //$NON-NLS-1$
ITextEditor editor = null;
BundleMap bm = null;
ExternalizeStringsWizardPage page1 = null;
WizardNewFileCreationPage page2 = null;
+ ExternalizeStringsWizardRegisterBundlePage page3 = null;
public ExternalizeStringsWizard(ITextEditor editor, BundleMap bm) {
super();
@@ -50,13 +67,21 @@
public void addPages() {
super.addPages();
page1 = new ExternalizeStringsWizardPage(
- ExternalizeStringsWizardPageName, editor, bm);
- page2 = new WizardNewFileCreationPage(NewFileCreationPageName,
+ ExternalizeStringsWizardPage.PAGE_NAME, editor, bm);
+ page2 = new WizardNewFileCreationPage(EXTERNALIZE_STRINGS_DIALOG_NEW_FILE_PAGE,
(IStructuredSelection) editor.getSelectionProvider().getSelection()) {
protected InputStream getInitialContents() {
return new ByteArrayInputStream(page1.getKeyValuePair().getBytes());
}
+
+ @Override
+ public boolean canFlipToNextPage() {
+ return isPageComplete();
+ }
+
};
+ page3 = new ExternalizeStringsWizardRegisterBundlePage(
+ ExternalizeStringsWizardRegisterBundlePage.PAGE_NAME);
page2.setTitle(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_TITLE);
page2.setDescription(JstUIMessages.EXTERNALIZE_STRINGS_DIALOG_DESCRIPTION);
page2.setImageDescriptor(ModelUIImages.getImageDescriptor(ModelUIImages.WIZARD_DEFAULT));
@@ -64,22 +89,37 @@
* https://jira.jboss.org/browse/JBIDE-7247
* Set initial values for the new properties file
*/
- if (editor.getEditorInput() instanceof IStorageEditorInput) {
+ IPath containerFullPath = null;
+ if (editor.getEditorInput() instanceof IFileEditorInput) {
+ IProject project = ((IFileEditorInput)editor.getEditorInput()).getFile().getProject();
+ IResource[] src = EclipseUtil.getJavaSourceRoots(project);
+ if (src.length > 0) {
+ containerFullPath = src[0].getFullPath();
+ }
+ } else if (editor.getEditorInput() instanceof IStorageEditorInput) {
try {
- IPath fullPath = ((IStorageEditorInput) editor.getEditorInput()).getStorage().getFullPath();
- page2.setContainerFullPath(fullPath);
+ containerFullPath = ((IStorageEditorInput) editor.getEditorInput()).getStorage().getFullPath();
} catch (CoreException e) {
JspEditorPlugin.getDefault().logError(e);
}
-
}
+ if (null != containerFullPath) {
+ page2.setContainerFullPath(containerFullPath);
+ }
String fileName = editor.getEditorInput().getName();
int pos = fileName.lastIndexOf(Constants.DOT);
if (pos != -1) {
fileName = fileName.substring(0, pos) + Constants.PROPERTIES_EXTENTION;
}
+ /*
+ * Set the file name
+ */
page2.setFileName(fileName);
/*
+ * Set the supported extension
+ */
+ page2.setFileExtension("properties"); //$NON-NLS-1$
+ /*
* The new file should not exist
*/
page2.setAllowExistingResources(false);
@@ -88,6 +128,7 @@
*/
addPage(page1);
addPage(page2);
+ addPage(page3);
}
@Override
@@ -98,7 +139,9 @@
@Override
public boolean performFinish() {
- if (!page1.isDuplicatedKeyValue()) {
+ String var = page1.getBundlePrefix();
+ String key = page1.getKey();
+ if (!page1.isDuplicatedKeyAndValue()) {
IFile bundleFile = null;
if (page1.isNewFile()) {
bundleFile = page2.createNewFile();
@@ -133,13 +176,112 @@
e.printStackTrace();
}
}
+ /*
+ * Register new resource bundle when new file is created.
+ */
+ if (page1.isNewFile() && !page3.isUserDefined()) {
+ var = page3.getBundleName();
+ if (editor.getEditorInput() instanceof IFileEditorInput) {
+ IProject project = ((IFileEditorInput)editor.getEditorInput()).getFile().getProject();
+ String userDefinedPath = page2.getContainerFullPath().toString();
+ /*
+ * Get the source folders for the project
+ */
+ IResource[] src = EclipseUtil.getJavaSourceRoots(project);
+ /*
+ * When there are multiple source folders --
+ * match user defined folder to them.
+ */
+ String srcPath = userDefinedPath;
+ String bundlePath = Constants.EMPTY;
+ if (src.length > 1) {
+ for (IResource res : src) {
+ srcPath = res.getFullPath().toString();
+ System.out.println(srcPath);
+ if (userDefinedPath.indexOf(srcPath) > -1) {
+ break;
+ }
+ }
+ } else if (src.length == 1) {
+ srcPath = src[0].getFullPath().toString();
+ }
+ /*
+ * After the source folder has been found --
+ * generate the bundle path.
+ * TODO: when user has entered a path that is not source folder -- ERROR
+ */
+ if (!userDefinedPath.equalsIgnoreCase(bundlePath)) {
+ bundlePath = userDefinedPath.replaceFirst(srcPath, "").replaceAll("/", "\\."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ if (bundlePath.startsWith(".")) { //$NON-NLS-1$
+ bundlePath = bundlePath.substring(1);
+ }
+ String fileName = page2.getFileName();
+ int pos = fileName.indexOf(Constants.PROPERTIES_EXTENTION);
+ if (pos > -1) {
+ fileName = fileName.substring(0, pos);
+ }
+ if (bundlePath.length() != 0) {
+ bundlePath = bundlePath + Constants.DOT;
+ }
+ bundlePath = bundlePath + fileName;
+ }
+ /*
+ * And then decide where to register the resource bundle
+ */
+ if (page3.isInFacesConfig()) {
+ /*
+ * Register new bundle in the faces-config.xml
+ * We should write only correct base-name.
+ * If it is not -- then just skip it. But such a situation
+ * should never happen.
+ */
+ XModel model = EclipseResourceUtil.getModelNature(project).getModel();
+ XModelObject facesConfig = ExternalizeStringsUtils.findFacesConfig(model);
+ XModelObject application = facesConfig.getChildByPath("application"); //$NON-NLS-1$
+ XModelObject resourceBundle = facesConfig.getModel().createModelObject("JSFResourceBundle", null); //$NON-NLS-1$
+ resourceBundle.setAttributeValue("base-name", bundlePath); //$NON-NLS-1$
+ resourceBundle.setAttributeValue("var", var); //$NON-NLS-1$
+ try {
+ DefaultCreateHandler.addCreatedObject(application, resourceBundle, 0);
+ } catch (XModelException e) {
+ JspEditorPlugin.getDefault().logError(
+ "Could not add to the faces-config.xml", e); //$NON-NLS-1$
+ }
+ /*
+ * When the faces-config.xml is opened in the editor the following
+ * action should be called to ensure that changes have been applied.
+ */
+ XActionInvoker.invoke("SaveActions.Save", facesConfig, new Properties()); //$NON-NLS-1$
+ } else if (page3.isViaLoadBundle()) {
+ /*
+ * Add tag to the current page.
+ * Insert the tag before currently selected tag.
+ */
+ IDocumentProvider prov = editor.getDocumentProvider();
+ IDocument doc = prov.getDocument(editor.getEditorInput());
+ ISelection sel = editor.getSelectionProvider().getSelection();
+ if (ExternalizeStringsUtils.isSelectionCorrect(sel)) {
+ IStructuredSelection structuredSelection = (IStructuredSelection) sel;
+ Object selectedElement = structuredSelection.getFirstElement();
+ if (selectedElement instanceof Node) {
+ Node node = (Node) selectedElement;
+ // TODO find a suitable prefix in registered taglibs
+ Element loadBundle = node.getOwnerDocument().createElement("f:loadBundle"); //$NON-NLS-1$
+ loadBundle.setAttribute("var", var); //$NON-NLS-1$
+ loadBundle.setAttribute("basename", bundlePath); //$NON-NLS-1$
+ // TODO find a place to put the f:loadBundle
+ node.getParentNode().insertBefore(loadBundle, node);
+ }
+ }
+ }
+ }
+ }
}
/*
* Replace text in the editor
*/
- page1.replaceText();
-
+ String replacement = "#{" + var + Constants.DOT + key + "}"; //$NON-NLS-1$ //$NON-NLS-2$
+ page1.replaceText(replacement);
return true;
}
-
}
Index: src/org/jboss/tools/jst/jsp/messages/JstUIMessages.java
===================================================================
--- src/org/jboss/tools/jst/jsp/messages/JstUIMessages.java (revision 26130)
+++ src/org/jboss/tools/jst/jsp/messages/JstUIMessages.java (working copy)
@@ -158,5 +158,10 @@
public static String EXTERNALIZE_STRINGS_DIALOG_ENTER_KEY_NAME;
public static String EXTERNALIZE_STRINGS_DIALOG_SELECT_RESOURCE_BUNDLE;
public static String CANNOT_LOAD_TAGLIBS_FROM_PAGE_CONTEXT;
+ public static String EXTERNALIZE_STRINGS_DIALOG_SAVE_RESOURCE_BUNDLE;
+ public static String EXTERNALIZE_STRINGS_DIALOG_FACES_CONFIG;
+ public static String EXTERNALIZE_STRINGS_DIALOG_LOAD_BUNDLE;
+ public static String EXTERNALIZE_STRINGS_DIALOG_USER_DEFINED;
+ public static String EXTERNALIZE_STRINGS_DIALOG_BUNDLE_NAME;
}
\ No newline at end of file