📄 cmsdefaultpageeditor.java
字号:
values.add(element.getName());
counter++;
}
}
return buildSelect(attributes, options, values, currentIndex, false);
}
/**
* Builds the html for the font face select box of a WYSIWYG editor.<p>
*
* @param attributes optional attributes for the <select> tag
* @return the html for the font face select box
*/
public String buildSelectFonts(String attributes) {
List names = new ArrayList();
for (int i = 0; i < CmsDefaultPageEditor.SELECTBOX_FONTS.length; i++) {
String value = CmsDefaultPageEditor.SELECTBOX_FONTS[i];
names.add(value);
}
return buildSelect(attributes, names, names, -1, false);
}
/**
* Escapes the content and title parameters to display them in the editor form.<p>
*
* This method has to be called on the JSP right before the form display html is created.<p> *
*/
public void escapeParams() {
// escape the content
setParamContent(CmsEncoder.escapeWBlanks(getParamContent(), CmsEncoder.ENCODING_UTF_8));
}
/**
* Returns the current element locale.<p>
*
* @return the current element locale
*/
public Locale getElementLocale() {
if (m_elementLocale == null) {
m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage());
}
return m_elementLocale;
}
/**
* Returns the current element name.<p>
*
* @return the current element name
*/
public String getParamElementname() {
return m_paramElementname;
}
/**
* Returns the old element name.<p>
*
* @return the old element name
*/
public String getParamOldelementname() {
return m_paramOldelementname;
}
/**
* Returns the OpenCms VFS uri of the style sheet of the current page.<p>
*
* @return the OpenCms VFS uri of the style sheet of the current page
*/
public String getUriStyleSheet() {
String result = "";
try {
String currentTemplate = getUriTemplate();
if (!"".equals(currentTemplate)) {
// read the stylesheet from the template file
result = getCms().readPropertyObject(currentTemplate, CmsPropertyDefinition.PROPERTY_TEMPLATE, false).getValue(
"");
}
} catch (CmsException e) {
LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_STYLESHEET_FAILED_0), e);
}
return result;
}
/**
* Returns the OpenCms VFS uri of the template of the current page.<p>
*
* @return the OpenCms VFS uri of the template of the current page
*/
public String getUriTemplate() {
String result = "";
try {
result = getCms().readPropertyObject(getParamTempfile(), CmsPropertyDefinition.PROPERTY_TEMPLATE, true).getValue(
"");
} catch (CmsException e) {
LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_FAILED_0), e);
}
return result;
}
/**
* Sets the current element name.<p>
*
* @param elementName the current element name
*/
public void setParamElementname(String elementName) {
m_paramElementname = elementName;
}
/**
* Sets the old element name.<p>
*
* @param oldElementName the old element name
*/
public void setParamOldelementname(String oldElementName) {
m_paramOldelementname = oldElementName;
}
/**
* Returns the list of active elements of the page.<p>
*
* @return the list of active elements of the page
*/
protected List getElementList() {
if (m_elementList == null) {
m_elementList = CmsDialogElements.computeElements(getCms(), m_page, getParamTempfile(), getElementLocale());
}
return m_elementList;
}
/**
* Initializes the body element language for the first call of the editor.<p>
*/
protected void initBodyElementLanguage() {
List locales = m_page.getLocales();
Locale defaultLocale = (Locale)OpenCms.getLocaleManager().getDefaultLocales(
getCms(),
getCms().getSitePath(m_file)).get(0);
if (locales.size() == 0) {
// no body present, create default body
if (!m_page.hasValue(CmsDefaultPageEditor.XML_BODY_ELEMENT, defaultLocale)) {
m_page.addValue(CmsDefaultPageEditor.XML_BODY_ELEMENT, defaultLocale);
}
try {
m_file.setContents(m_page.marshal());
getCms().writeFile(m_file);
} catch (CmsException e) {
// show error page
try {
showErrorPage(this, e);
} catch (JspException exc) {
// should usually never happen
if (LOG.isInfoEnabled()) {
LOG.info(exc);
}
}
}
setParamElementlanguage(defaultLocale.toString());
} else {
// body present, get the language
if (locales.contains(defaultLocale)) {
// get the body for the default language
setParamElementlanguage(defaultLocale.toString());
} else {
// get the first body that can be found
setParamElementlanguage(locales.get(0).toString());
}
}
}
/**
* Initializes the body element name of the editor.<p>
*
* This has to be called after the element language has been set with setParamBodylanguage().<p>
*
* @param elementName the name of the element to initialize or null, if default element should be used
*/
protected void initBodyElementName(String elementName) {
if (elementName == null
|| (m_page.hasValue(elementName, getElementLocale()) && !m_page.isEnabled(elementName, getElementLocale()))) {
// elementName not specified or given element is disabled, determine default element
List allElements = m_page.getNames(getElementLocale());
int elementCount = allElements.size();
List elements = new ArrayList(elementCount);
for (int i = 0; i < elementCount; i++) {
// filter disabled elements
if (m_page.isEnabled((String)allElements.get(i), getElementLocale())) {
elements.add(allElements.get(i));
}
}
// get the active page elements
List elementList = getElementList();
for (int i = 0; i < elementList.size(); i++) {
CmsDialogElement checkElement = (CmsDialogElement)elementList.get(i);
if (elements.contains(checkElement.getName())) {
// get the first active element from the element list
setParamElementname(checkElement.getName());
return;
}
}
// no matching active element found
if (elements.contains(CmsDefaultPageEditor.XML_BODY_ELEMENT)) {
// default legacy element present, use it
setParamElementname(CmsDefaultPageEditor.XML_BODY_ELEMENT);
} else {
// use the first element from the element list
setParamElementname((String)elements.get(0));
}
} else {
// elementName specified and element is enabled or not present, set to elementName
setParamElementname(elementName);
}
}
/**
* This method has to be called after initializing the body element name and language.<p>
*
* @see org.opencms.workplace.editors.CmsEditor#initContent()
*/
protected void initContent() {
if (CmsStringUtil.isNotEmpty(getParamContent())) {
if (CmsStringUtil.isNotEmpty(getParamElementname())
&& getParamElementname().equals(getParamOldelementname())) {
if (CmsStringUtil.isNotEmpty(getParamElementlanguage())
&& getParamElementlanguage().equals(getParamOldelementlanguage())) {
return;
}
}
}
getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, new Boolean(true));
String elementData;
if (m_page.hasValue(getParamElementname(), getElementLocale())) {
// element value is available in the page
elementData = m_page.getStringValue(getCms(), getParamElementname(), getElementLocale());
} else {
// value is not available in the page
if (Boolean.valueOf(getParamDirectedit()).booleanValue()) {
// direct edit on a non-existing element: create new element with this name
m_page.addValue(getParamElementname(), getElementLocale());
}
elementData = "";
}
setParamContent(elementData);
}
/**
* Saves the editor content to the temporary file.<p>
*
* @param body the body name to write
* @param locale the body locale to write
* @throws CmsException if writing the file fails
*/
protected void performSaveContent(String body, Locale locale) throws CmsException {
// prepare the content for saving
String content = prepareContent(true);
String contentConversion = m_page.getConversion();
// check if cleanup was selected in the editor, we have to add the cleanup parameter
if (EDITOR_CLEANUP.equals(getParamAction())) {
if ((contentConversion == null) || (contentConversion.equals(CmsHtmlConverter.PARAM_DISABLED))) {
// if the current conversion mode is "false" only, we have to remove the "false" value and set it to "cleanup", as "false" will be stronger than all other values
contentConversion = CmsHtmlConverter.PARAM_WORD;
} else {
// add "cleanup" to the already existing values
contentConversion += ";" + CmsHtmlConverter.PARAM_WORD;
}
}
m_page.setConversion(contentConversion);
// create the element if necessary and if content is present
if (!m_page.hasValue(body, locale) && !"".equals(content)) {
m_page.addValue(body, locale);
}
// get the enabled state of the element
boolean enabled = m_page.isEnabled(body, locale);
// set the element data
if (m_page.hasValue(body, locale)) {
m_page.setStringValue(getCms(), body, locale, content);
}
// write the file
m_file.setContents(m_page.marshal());
m_file = getCms().writeFile(m_file);
// content might have been modified during write operation
m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
if (m_page.hasValue(body, locale)) {
getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, new Boolean(true));
content = m_page.getStringValue(getCms(), body, locale);
if (content == null) {
content = "";
}
setParamContent(content);
prepareContent(false);
m_page.setEnabled(body, locale, enabled);
}
}
/**
* Manipulates the content String for different editor views and the save operation.<p>
*
* @param save if set to true, the result String is not escaped and the content parameter is not updated
* @return the prepared content String
*/
protected abstract String prepareContent(boolean save);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -