From 2bfc1177e20841d7bbf30108bfda6a7cfbbab5b9 Mon Sep 17 00:00:00 2001 From: "youngduk.hwang" Date: Tue, 13 May 2014 23:41:23 +0900 Subject: [PATCH] PAGETEMP: Add CreatePageCommand and apply on Page template CreatePageCommand is applied when we add a new page on Pages view. Then Undo and Redo is possible. Change-Id: I25d7b8a5372e76937929a92a25b40b16caa285fd Signed-off-by: youngduk.hwang --- .../gef/commands/CreatePageCommand.java | 106 +++++++++++++++++++++ .../src/org/tizen/webuibuilder/model/Part.java | 9 +- .../webuibuilder/model/app/AppManagerForHTML.java | 26 ++++- .../pagetemplate/action/NewPageAction.java | 15 ++- .../ui/wizard/policy/NewPagePolicy.java | 16 ++-- .../pagetemplate/ui/wizards/NewPageWizard.java | 17 +++- .../org/tizen/webuibuilder/utility/DomUtil.java | 90 +++++++++++++++++ 7 files changed, 266 insertions(+), 13 deletions(-) create mode 100644 org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/CreatePageCommand.java create mode 100644 org.tizen.webuibuilder/src/org/tizen/webuibuilder/utility/DomUtil.java diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/CreatePageCommand.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/CreatePageCommand.java new file mode 100644 index 0000000..99db7fa --- /dev/null +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/CreatePageCommand.java @@ -0,0 +1,106 @@ +/* + * UI Builder + * + * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + + +package org.tizen.webuibuilder.gef.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.gef.commands.Command; +import org.tizen.webuibuilder.BuilderConstants; +import org.tizen.webuibuilder.model.app.AppManagerForHTML; +import org.tizen.webuibuilder.model.filedata.PageFileData; +import org.tizen.webuibuilder.model.page.PageData; +import org.tizen.webuibuilder.model.page.PageDataForHTML; +import org.tizen.webuibuilder.utility.DomUtil; +import org.w3c.dom.Node; + +public class CreatePageCommand extends Command { + + private AppManagerForHTML appManager; + private List pageDatas; + private Node pageNode; + + private final String BODY_ELEMENT = "//BODY"; + private final String BODY_ELEMENT_LOWER = "//body"; + + public CreatePageCommand(AppManagerForHTML appManager, String html, String name) { + this.appManager = appManager; + + init(html, name); + } + + public void init(String html, String name) { + PageFileData pageFileData = appManager.loadPageFileData(html, name); + + if(pageFileData == null || pageFileData.getRootPartList() == null) { + return; + } + + int size = pageFileData.getRootPartList().size(); + pageDatas = new ArrayList(size); + + for(int i = 0; i < size; i++) { + PageData pageData = new PageDataForHTML(BuilderConstants.EMPTY, + pageFileData, i, pageFileData.getDocument(), + appManager.getDescriptorManager(), null); + + if(pageData != null) { + pageDatas.add(pageData); + } + } + + Node bodyNode = DomUtil.getBodyNode(pageFileData.getDocument(), BODY_ELEMENT); + pageNode = DomUtil.importedNode(appManager.getDocument(), bodyNode.getFirstChild()); + } + + @Override + public boolean canExecute() { + return (pageDatas != null && pageNode != null) ? true : false; + } + + @Override + public void execute() { + if(!canExecute()) { + return; + } + + DomUtil.appendPageNode(appManager.getDocument(), BODY_ELEMENT_LOWER, pageNode); + + for(PageData pagaData : pageDatas) { + appManager.addPage(pagaData, true); + } + + } + + @Override + public void undo() { + DomUtil.removePageNode(this.appManager.getDocument(), BODY_ELEMENT_LOWER, pageNode); + + for(PageData pagaData : pageDatas) { + appManager.removePage(pagaData); + } + + } + +} diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/Part.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/Part.java index 8446aa6..be359af 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/Part.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/Part.java @@ -2227,7 +2227,14 @@ public class Part implements ISerializer { while (child.canSetPropertyValue(PROPERTY_ID, id + String.valueOf(idIndex)) != WidgetValidator.OK) { idIndex++; } - child.setPropertyValue(PROPERTY_ID, id + String.valueOf(idIndex)); + + String newId = id + String.valueOf(idIndex); + + if(child.getElement() != null) { + child.getElement().setAttribute(PROPERTY_ID, newId); + } + + child.setPropertyValue(PROPERTY_ID, newId); } else { child.setPropertyValue(PROPERTY_ID, propertyId); } diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/app/AppManagerForHTML.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/app/AppManagerForHTML.java index b426018..1005402 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/app/AppManagerForHTML.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/app/AppManagerForHTML.java @@ -23,10 +23,10 @@ package org.tizen.webuibuilder.model.app; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.StringBufferInputStream; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; @@ -65,7 +65,6 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; -@SuppressWarnings("deprecation") public class AppManagerForHTML extends AppManager { private Document document; @@ -284,6 +283,27 @@ public class AppManagerForHTML extends AppManager { return load(BuilderConstants.INDEX_HTML_FILE); } + public PageFileData loadPageFileData(String html, String pageName) { + if (html == null || pageName == null) { + return null; + } + + String page = "data-role=\"page\""; //$NON-NLS-1$ + String newHtml = null; + if (html.contains(page)) { + String id = BuilderConstants.PROPERTY_ID + BuilderConstants.EQUAL + + BuilderConstants.QUOTATION_MARK; + int startIndex = html.indexOf(id, html.indexOf(page)) + 4; + String startHtml = html.substring(0, startIndex); + int endIndex = html.indexOf(BuilderConstants.QUOTATION_MARK, + startIndex); + String endHtml = html.substring(endIndex, html.length()); + newHtml = startHtml + pageName + endHtml; + } + return (newHtml != null) ? HtmlReader.loadHtmlStream(this, + new ByteArrayInputStream(newHtml.getBytes())) : null; + } + public void addPage(String html, String pageName) { if (html == null || pageName == null) { return; @@ -309,7 +329,7 @@ public class AppManagerForHTML extends AppManager { return; } - StringBufferInputStream stream = new StringBufferInputStream(html); + ByteArrayInputStream stream = new ByteArrayInputStream(html.getBytes()); PageFileData pageFileData = HtmlReader.loadHtmlStream(this, stream); if (pageFileData == null) { return; diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/action/NewPageAction.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/action/NewPageAction.java index baf9e7c..6d566e1 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/action/NewPageAction.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/action/NewPageAction.java @@ -25,12 +25,15 @@ package org.tizen.webuibuilder.pagetemplate.action; import org.eclipse.gef.ui.actions.SelectionAction; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.window.Window; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PlatformUI; +import org.tizen.webuibuilder.gef.commands.CreatePageCommand; import org.tizen.webuibuilder.model.app.AppManager; +import org.tizen.webuibuilder.model.app.AppManagerForHTML; import org.tizen.webuibuilder.pagetemplate.ui.wizards.NewPageWizard; import org.tizen.webuibuilder.ui.actions.WebUIBuilderActionConstants; import org.tizen.webuibuilder.ui.contextmenu.Messages; @@ -73,7 +76,17 @@ public class NewPageAction extends SelectionAction { IStructuredSelection selection = new StructuredSelection(appManager); newpage.init(workbench, selection); WizardDialog dialog = new WizardDialog(shell, newpage); - dialog.open(); + int r = dialog.open(); + + if(r == Window.OK) { + if(!(appManager instanceof AppManagerForHTML)) { + return; + } + AppManagerForHTML manager = (AppManagerForHTML) appManager; + + getCommandStack().execute(new CreatePageCommand(manager, newpage.getTemplate().getDescriptor().getText(), newpage.getName())); + + } } } diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizard/policy/NewPagePolicy.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizard/policy/NewPagePolicy.java index ad77c3d..1af6e8b 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizard/policy/NewPagePolicy.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizard/policy/NewPagePolicy.java @@ -62,11 +62,11 @@ public class NewPagePolicy implements IWizardPolicy { */ @Override public boolean run() { - if (!(model instanceof AppManagerForHTML)) { - return false; - } - AppManagerForHTML manager = (AppManagerForHTML) model; - manager.addPage(template.getDescriptor().getText(), name); +// if (!(model instanceof AppManagerForHTML)) { +// return false; +// } +// AppManagerForHTML manager = (AppManagerForHTML) model; +// manager.addPage(template.getDescriptor().getText(), name); // PageFileData pageFileData = PageReader.readPage(template.getDescriptor().getText(), manager.getPartFactory()); // PageData pageData = new PageDataForHTML(BuilderConstants.EMPTY, pageFileData, 0, // pageFileData.getDocument(), manager.getDescriptorManager(), @@ -74,6 +74,10 @@ public class NewPagePolicy implements IWizardPolicy { // manager.addPage(pageData, true); // manager.addPage(pageFileData); // manager.addPage(template.getDescriptor().getText(), name); - return true; + + return (template != null && + template.getDescriptor() != null && + template.getDescriptor().getText() != null && + !template.getDescriptor().getText().isEmpty()); } } \ No newline at end of file diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizards/NewPageWizard.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizards/NewPageWizard.java index 050a57a..7755340 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizards/NewPageWizard.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/pagetemplate/ui/wizards/NewPageWizard.java @@ -41,6 +41,9 @@ public class NewPageWizard extends Wizard implements INewWizard { private ISelection fSelection = null; // private NewPageWizardFirstPage fFirstPage = null; private NewPageWizardSecondPage fSecondPage = null; + + private String name = null; + private ITemplate template = null; /* * (non-Javadoc) @@ -91,8 +94,8 @@ public class NewPageWizard extends Wizard implements INewWizard { @Override public boolean performFinish() { // get inputs - String name = fSecondPage.getPageName(); - ITemplate template = fSecondPage.getTemplate(); + this.name = fSecondPage.getPageName(); + this.template = fSecondPage.getTemplate(); // create policies NewPagePolicy newPagePolicy = new NewPagePolicy(appManager, name, template); @@ -102,4 +105,14 @@ public class NewPageWizard extends Wizard implements INewWizard { processor.add(newPagePolicy); return processor.run(); } + + public String getName() { + return name; + } + + public ITemplate getTemplate() { + return template; + } + + } diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/utility/DomUtil.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/utility/DomUtil.java new file mode 100644 index 0000000..8c06391 --- /dev/null +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/utility/DomUtil.java @@ -0,0 +1,90 @@ +/* + * UI Builder + * + * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.webuibuilder.utility; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.tizen.webuibuilder.BuilderConstants; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +public class DomUtil { + + + /** + * @param doc + * @param xpath "//BODY" or "//body" + * @return + */ + public static Node getBodyNode(Document doc, String xpath) { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath expr = xpathFactory.newXPath(); + Object bodyNodeset = null; + try { + if (xpath != null && !xpath.equals(BuilderConstants.EMPTY)) { + bodyNodeset = expr.evaluate(xpath, doc, XPathConstants.NODE); + } + } catch (XPathExpressionException e) { + e.printStackTrace(); + } + + if (bodyNodeset instanceof Node) { + return (Node) bodyNodeset; + } + + return null; + } + + public static Node importedNode(Document targetDocument, Node pageNode) { + return targetDocument.importNode(pageNode, true); + } + + /** + * @param targetDocument + * @param pageNode imported pageNode + */ + public static void appendPageNode(Document targetDocument, String xpath, Node pageNode) { + Node targetBodyNode = getBodyNode(targetDocument, xpath); + + if(targetBodyNode == null) { + return; + } + + targetBodyNode.appendChild(pageNode); + } + + public static void removePageNode(Document targetDocument, String xpath, Node pageNode) { + Node targetBodyNode = getBodyNode(targetDocument, xpath); + + if(targetBodyNode == null) { + return; + } + + targetBodyNode.removeChild(pageNode); + } + + +} -- 2.7.4