Mercurial > hg > thermostat-tools-eclipse
changeset 1:4e0c945cb597
Editor can now update xml files
author | Andriy Petrus <andriy.petrus@gmail.com> |
---|---|
date | Fri, 15 Nov 2013 17:06:46 -0500 |
parents | 430e3d29c3ac |
children | 12f694a80bd4 |
files | bin/thermostatplugin/editors/MultiPageEditor$1.class bin/thermostatplugin/editors/MultiPageEditor$2.class bin/thermostatplugin/editors/MultiPageEditor.class src/thermostatplugin/editors/MultiPageEditor.java |
diffstat | 4 files changed, 298 insertions(+), 253 deletions(-) [+] |
line wrap: on
line diff
--- a/src/thermostatplugin/editors/MultiPageEditor.java Thu Nov 07 12:56:08 2013 -0500 +++ b/src/thermostatplugin/editors/MultiPageEditor.java Fri Nov 15 17:06:46 2013 -0500 @@ -12,10 +12,17 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IResourceChangeEvent; import org.eclipse.core.resources.IResourceChangeListener; +import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.dialogs.ErrorDialog; @@ -23,15 +30,12 @@ import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.FillLayout; 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.Display; -import org.eclipse.swt.widgets.FontDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IEditorInput; @@ -44,6 +48,7 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.part.MultiPageEditorPart; +import org.w3c.dom.CharacterData; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -51,8 +56,6 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import org.w3c.dom.CharacterData; - /** * An example showing how to create a multi-page editor. This example has 3 * pages: @@ -63,282 +66,324 @@ * </ul> */ public class MultiPageEditor extends MultiPageEditorPart implements - IResourceChangeListener { + IResourceChangeListener { - /** The text editor used in page 0. */ - private TextEditor editor; + /** The text editor used in page 0. */ + private TextEditor editor; - /** The font chosen in page 1. */ - private Font font; + // /** The font chosen in page 1. */ + // private Font font; - /** The text widget used in page 2. */ - private StyledText text; + /** The text widget used in page 2. */ + private StyledText text; + + private String extensionName; + private String extensionDescription; - private String extensionName; - private String extensionDescription; + /** + * Creates a multi-page editor example. + */ + public MultiPageEditor() { + super(); + ResourcesPlugin.getWorkspace().addResourceChangeListener(this); + extensionName = new String(""); + } - /** - * Creates a multi-page editor example. - */ - public MultiPageEditor() { - super(); - ResourcesPlugin.getWorkspace().addResourceChangeListener(this); - extensionName = new String(""); - } + /** + * Creates page 0 of the multi-page editor, which contains a text editor. + */ + void createPage0() { + try { + editor = new TextEditor(); + int index = addPage(editor, getEditorInput()); + setPageText(index, editor.getTitle()); + } catch (PartInitException e) { + ErrorDialog.openError(getSite().getShell(), + "Error creating nested text editor", null, e.getStatus()); + } + } - /** - * Creates page 0 of the multi-page editor, which contains a text editor. - */ - void createPage0() { - try { - editor = new TextEditor(); - int index = addPage(editor, getEditorInput()); - setPageText(index, editor.getTitle()); - } catch (PartInitException e) { - ErrorDialog.openError(getSite().getShell(), - "Error creating nested text editor", null, e.getStatus()); - } - } + /** + * Creates page 2 of the multi-page editor, which shows the sorted text. + */ + void createPage1() { + Composite composite = new Composite(getContainer(), SWT.NONE); + FillLayout layout = new FillLayout(); + composite.setLayout(layout); + text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL); + text.setEditable(false); + + int index = addPage(composite); + setPageText(index, "Preview"); + } - /** - * Creates page 2 of the multi-page editor, which shows the sorted text. - */ - void createPage1() { - Composite composite = new Composite(getContainer(), SWT.NONE); - FillLayout layout = new FillLayout(); - composite.setLayout(layout); - text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL); - text.setEditable(false); - - int index = addPage(composite); - setPageText(index, "Preview"); - } + /** + * Creates the pages of the multi-page editor. + */ + protected void createPages() { + createPage0(); + createExtensionsPage(); + createPage1(); + } - /** - * Creates the pages of the multi-page editor. - */ - protected void createPages() { - createPage0(); - createExtensionsPage(); - createPage1(); - } + private void createExtensionsPage() { + getEditorInput(); + Composite composite = new Composite(getContainer(), SWT.NONE); + GridLayout layout = new GridLayout(); + composite.setLayout(layout); + layout.numColumns = 2; - private void createExtensionsPage() { - getEditorInput(); - Composite composite = new Composite(getContainer(), SWT.NONE); - GridLayout layout = new GridLayout(); - composite.setLayout(layout); - layout.numColumns = 2; + final Label extensionNameLabel = new Label(composite, SWT.NONE); + extensionNameLabel.setText("Initial Extension Name: "); + extensionNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, + false, false, 1, 1)); + getAttributes(); + final Text extensionNameText = new Text(composite, SWT.BORDER); + extensionNameText.setText(extensionName); + extensionNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, + false, false, 1, 1)); - Label extensionNameLabel = new Label(composite, SWT.NONE); - extensionNameLabel.setText("Initial Extension Name: "); - extensionNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, - false, false, 1, 1)); - getAttributes(); - final Text extensionNameText = new Text(composite, SWT.BORDER); - extensionNameText.setText(extensionName); - // extensionNameText.setText("stringgg"); - extensionNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, - false, false, 1, 1)); + Label extensionDescriptionLabel = new Label(composite, SWT.NONE); + extensionDescriptionLabel.setText("Extension Description: "); + extensionDescriptionLabel.setLayoutData(new GridData(SWT.RIGHT, + SWT.CENTER, false, false, 1, 1)); - Label extensionDescriptionLabel = new Label(composite, SWT.NONE); - extensionDescriptionLabel.setText("Extension Description: "); - extensionDescriptionLabel.setLayoutData(new GridData(SWT.RIGHT, - SWT.CENTER, false, false, 1, 1)); + final Text extensionEnvironmentText = new Text(composite, SWT.BORDER); + extensionEnvironmentText.setText(extensionDescription); + extensionEnvironmentText.setLayoutData(new GridData(SWT.FILL, + SWT.CENTER, false, false, 1, 1)); + Button fontButton = new Button(composite, SWT.NONE); + GridData gd = new GridData(GridData.BEGINNING); + gd.horizontalSpan = 2; + fontButton.setLayoutData(gd); + fontButton.setText("Save..."); - Text extensionEnvironmentText = new Text(composite, SWT.BORDER); - extensionEnvironmentText.setText(extensionDescription); - extensionEnvironmentText.setLayoutData(new GridData(SWT.FILL, - SWT.CENTER, false, false, 1, 1)); - Button fontButton = new Button(composite, SWT.NONE); - GridData gd = new GridData(GridData.BEGINNING); - gd.horizontalSpan = 2; - fontButton.setLayoutData(gd); - fontButton.setText("Save..."); + fontButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + // setFont(); + // extensionNameText.setText("Saved..."); + extensionName = new String(extensionNameText.getText()); + // extensionEnvironmentText.setText(extensionNameText.getText()); + updateElementValues(); + } + }); - fontButton.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent event) { -// setFont(); - extensionNameText.setText("Saved..."); - } - }); + int index = addPage(composite); + setPageText(index, "Extensions"); + + } - int index = addPage(composite); - setPageText(index, "Extensions"); - - } + /** + * The <code>MultiPageEditorPart</code> implementation of this + * <code>IWorkbenchPart</code> method disposes all nested editors. + * Subclasses may extend. + */ + public void dispose() { + ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); + super.dispose(); + } - /** - * The <code>MultiPageEditorPart</code> implementation of this - * <code>IWorkbenchPart</code> method disposes all nested editors. - * Subclasses may extend. - */ - public void dispose() { - ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); - super.dispose(); - } + /** + * Saves the multi-page editor's document. + */ + public void doSave(IProgressMonitor monitor) { + getEditor(0).doSave(monitor); + } - /** - * Saves the multi-page editor's document. - */ - public void doSave(IProgressMonitor monitor) { - getEditor(0).doSave(monitor); - } + /** + * Saves the multi-page editor's document as another file. Also updates the + * text for page 0's tab, and updates this multi-page editor's input to + * correspond to the nested editor's. + */ + public void doSaveAs() { + IEditorPart editor = getEditor(0); + editor.doSaveAs(); + setPageText(0, editor.getTitle()); + setInput(editor.getEditorInput()); + } - /** - * Saves the multi-page editor's document as another file. Also updates the - * text for page 0's tab, and updates this multi-page editor's input to - * correspond to the nested editor's. - */ - public void doSaveAs() { - IEditorPart editor = getEditor(0); - editor.doSaveAs(); - setPageText(0, editor.getTitle()); - setInput(editor.getEditorInput()); - } + /* + * (non-Javadoc) Method declared on IEditorPart + */ + public void gotoMarker(IMarker marker) { + setActivePage(0); + IDE.gotoMarker(getEditor(0), marker); + } + + /** + * The <code>MultiPageEditorExample</code> implementation of this method + * checks that the input is an instance of <code>IFileEditorInput</code>. + */ + public void init(IEditorSite site, IEditorInput editorInput) + throws PartInitException { + if (!(editorInput instanceof IFileEditorInput)) + throw new PartInitException( + "Invalid Input: Must be IFileEditorInput"); + super.init(site, editorInput); + } - /* - * (non-Javadoc) Method declared on IEditorPart - */ - public void gotoMarker(IMarker marker) { - setActivePage(0); - IDE.gotoMarker(getEditor(0), marker); - } + /* + * (non-Javadoc) Method declared on IEditorPart. + */ + public boolean isSaveAsAllowed() { + return true; + } - /** - * The <code>MultiPageEditorExample</code> implementation of this method - * checks that the input is an instance of <code>IFileEditorInput</code>. - */ - public void init(IEditorSite site, IEditorInput editorInput) - throws PartInitException { - if (!(editorInput instanceof IFileEditorInput)) - throw new PartInitException( - "Invalid Input: Must be IFileEditorInput"); - super.init(site, editorInput); - } - - /* - * (non-Javadoc) Method declared on IEditorPart. - */ - public boolean isSaveAsAllowed() { - return true; - } + /** + * Calculates the contents of page 2 when the it is activated. + */ + protected void pageChange(int newPageIndex) { + super.pageChange(newPageIndex); + if (newPageIndex == 2) { + sortWords(); + } + } - /** - * Calculates the contents of page 2 when the it is activated. - */ - protected void pageChange(int newPageIndex) { - super.pageChange(newPageIndex); - if (newPageIndex == 2) { - sortWords(); - } - } + /** + * Closes all project files on project close. + */ + public void resourceChanged(final IResourceChangeEvent event) { + if (event.getType() == IResourceChangeEvent.PRE_CLOSE) { + Display.getDefault().asyncExec(new Runnable() { + public void run() { + IWorkbenchPage[] pages = getSite().getWorkbenchWindow() + .getPages(); + for (int i = 0; i < pages.length; i++) { + if (((FileEditorInput) editor.getEditorInput()) + .getFile().getProject() + .equals(event.getResource())) { + IEditorPart editorPart = pages[i].findEditor(editor + .getEditorInput()); + pages[i].closeEditor(editorPart, true); + } + } + } + }); + } + } + + private void getAttributes() { + String editorText = editor.getDocumentProvider() + .getDocument(editor.getEditorInput()).get(); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder; - /** - * Closes all project files on project close. - */ - public void resourceChanged(final IResourceChangeEvent event) { - if (event.getType() == IResourceChangeEvent.PRE_CLOSE) { - Display.getDefault().asyncExec(new Runnable() { - public void run() { - IWorkbenchPage[] pages = getSite().getWorkbenchWindow() - .getPages(); - for (int i = 0; i < pages.length; i++) { - if (((FileEditorInput) editor.getEditorInput()) - .getFile().getProject() - .equals(event.getResource())) { - IEditorPart editorPart = pages[i].findEditor(editor - .getEditorInput()); - pages[i].closeEditor(editorPart, true); - } - } - } - }); - } - } + InputSource inputStream = new InputSource(); + inputStream.setCharacterStream(new StringReader(editorText)); + try { + dBuilder = factory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputStream); + + NodeList nList = doc.getElementsByTagName("command"); + for (int i = 0; i < nList.getLength(); i++) { + Element element = (Element) nList.item(i); + extensionName = getValueFromElement(element, "name"); + extensionDescription = getValueFromElement(element, + "description"); + } + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static String getValueFromElement(Element element, + String attribute) { + NodeList name = element.getElementsByTagName(attribute); + Element line = (Element) name.item(0); - /** - * Sets the font related data to be applied to the text in page 2. - */ - void setFont() { - FontDialog fontDialog = new FontDialog(getSite().getShell()); - fontDialog.setFontList(text.getFont().getFontData()); - FontData fontData = fontDialog.open(); - if (fontData != null) { - if (font != null) - font.dispose(); - font = new Font(text.getDisplay(), fontData); - text.setFont(font); - } - } + Node child = line.getFirstChild(); + if (child instanceof CharacterData) { + CharacterData cd = (CharacterData) child; + return cd.getData(); + } + return ""; + } + + private void updateElementValues() { + String editorText = editor.getDocumentProvider() + .getDocument(editor.getEditorInput()).get(); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder; + + InputSource inputStream = new InputSource(); + inputStream.setCharacterStream(new StringReader(editorText)); + try { + dBuilder = factory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputStream); - void getAttributes() { - String editorText = editor.getDocumentProvider() - .getDocument(editor.getEditorInput()).get(); + NodeList nList = doc.getElementsByTagName("command"); + Element element = null; + for (int i = 0; i < nList.getLength(); i++) { + element = (Element) nList.item(i); + Node name = element.getElementsByTagName("name").item(0) + .getFirstChild(); + name.setNodeValue(extensionName); + } + TransformerFactory transformerFactory = TransformerFactory + .newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(doc); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder; + IEditorPart editor = getEditor(0); + editor.getEditorInput().getName(); - InputSource inputStream = new InputSource(); - inputStream.setCharacterStream(new StringReader(editorText)); - try { - dBuilder = factory.newDocumentBuilder(); - Document doc = dBuilder.parse(inputStream); + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + String path = new String(root.getLocation().toOSString()); + + // String projectPath = root.getProject().getName(); TODO find a way to correctly find project/container name - NodeList nList = doc.getElementsByTagName("command"); - for (int i = 0; i < nList.getLength(); i++) { - Element element = (Element) nList.item(i); - extensionName = getCharacterDataFromElement(element, "name"); - extensionDescription = getCharacterDataFromElement(element, "description"); - } - } catch (SAXException e) { - // TODO Auto-generated catch block - extensionName = new String(e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - extensionName = new String(e.getMessage()); - e.printStackTrace(); - } catch (ParserConfigurationException e) { - // TODO Auto-generated catch block - extensionName = new String(e.getMessage()); - e.printStackTrace(); - } - } + String fileName = editor.getEditorInput().getName(); + String filePath = new String(path + "/test/" + fileName); //TODO replace /test/ with an actual container name and system path separators + + System.out.println(filePath); //TEST OUTPUT, TODO remove this. + StreamResult result = new StreamResult(new File(filePath)); + + transformer.transform(source, result); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (TransformerException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } - public static String getCharacterDataFromElement(Element element, String attribute) { - NodeList name = element.getElementsByTagName(attribute); - Element line = (Element) name.item(0); - - Node child = line.getFirstChild(); - if (child instanceof CharacterData) { - CharacterData cd = (CharacterData) child; - return cd.getData(); - } - return ""; - } + /** + * Sorts the words in page 0, and shows them in page 2. + */ + void sortWords() { - /** - * Sorts the words in page 0, and shows them in page 2. - */ - void sortWords() { + String editorText = editor.getDocumentProvider() + .getDocument(editor.getEditorInput()).get(); - String editorText = editor.getDocumentProvider() - .getDocument(editor.getEditorInput()).get(); - - StringTokenizer tokenizer = new StringTokenizer(editorText, - " \t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\"); - ArrayList editorWords = new ArrayList(); - while (tokenizer.hasMoreTokens()) { - editorWords.add(tokenizer.nextToken()); - } + StringTokenizer tokenizer = new StringTokenizer(editorText, + " \t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\"); + ArrayList editorWords = new ArrayList(); + while (tokenizer.hasMoreTokens()) { + editorWords.add(tokenizer.nextToken()); + } - Collections.sort(editorWords, Collator.getInstance()); - StringWriter displayText = new StringWriter(); - for (int i = 0; i < editorWords.size(); i++) { - displayText.write(((String) editorWords.get(i))); - displayText.write(System.getProperty("line.separator")); - } - text.setText(displayText.toString()); - } + Collections.sort(editorWords, Collator.getInstance()); + StringWriter displayText = new StringWriter(); + for (int i = 0; i < editorWords.size(); i++) { + displayText.write(((String) editorWords.get(i))); + displayText.write(System.getProperty("line.separator")); + } + text.setText(displayText.toString()); + } }