| 1 | package org.axdt.mxml.designer; |
|---|
| 2 | |
|---|
| 3 | import org.axdt.mxml.MxmlPlugin; |
|---|
| 4 | import org.axdt.util.BrowserUtils; |
|---|
| 5 | import org.eclipse.core.resources.IResourceChangeEvent; |
|---|
| 6 | import org.eclipse.core.resources.IResourceChangeListener; |
|---|
| 7 | import org.eclipse.core.resources.IResourceDelta; |
|---|
| 8 | import org.eclipse.core.resources.ResourcesPlugin; |
|---|
| 9 | import org.eclipse.core.runtime.IPath; |
|---|
| 10 | import org.eclipse.core.runtime.IProgressMonitor; |
|---|
| 11 | import org.eclipse.core.runtime.Path; |
|---|
| 12 | import org.eclipse.jface.text.IDocument; |
|---|
| 13 | import org.eclipse.swt.SWT; |
|---|
| 14 | import org.eclipse.swt.browser.Browser; |
|---|
| 15 | import org.eclipse.swt.browser.StatusTextEvent; |
|---|
| 16 | import org.eclipse.swt.browser.StatusTextListener; |
|---|
| 17 | import org.eclipse.swt.events.SelectionAdapter; |
|---|
| 18 | import org.eclipse.swt.events.SelectionEvent; |
|---|
| 19 | import org.eclipse.swt.layout.GridData; |
|---|
| 20 | import org.eclipse.swt.layout.GridLayout; |
|---|
| 21 | import org.eclipse.swt.layout.RowLayout; |
|---|
| 22 | import org.eclipse.swt.widgets.Button; |
|---|
| 23 | import org.eclipse.swt.widgets.Composite; |
|---|
| 24 | import org.eclipse.ui.IEditorInput; |
|---|
| 25 | import org.eclipse.ui.IEditorPart; |
|---|
| 26 | import org.eclipse.ui.IEditorSite; |
|---|
| 27 | import org.eclipse.ui.IFileEditorInput; |
|---|
| 28 | import org.eclipse.ui.PartInitException; |
|---|
| 29 | import org.eclipse.ui.part.MultiPageEditorPart; |
|---|
| 30 | import org.eclipse.ui.part.MultiPageEditorSite; |
|---|
| 31 | import org.eclipse.wst.sse.ui.StructuredTextEditor; |
|---|
| 32 | |
|---|
| 33 | public class MxmlDesignEditor extends MultiPageEditorPart implements IResourceChangeListener { |
|---|
| 34 | |
|---|
| 35 | private Browser browser; |
|---|
| 36 | private StructuredTextEditor editor; |
|---|
| 37 | private boolean exportQuery; |
|---|
| 38 | private boolean firstGlance; |
|---|
| 39 | |
|---|
| 40 | public MxmlDesignEditor() { |
|---|
| 41 | super(); |
|---|
| 42 | ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); |
|---|
| 43 | exportQuery = false; |
|---|
| 44 | firstGlance = true; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | @Override |
|---|
| 48 | public void dispose() { |
|---|
| 49 | ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); |
|---|
| 50 | super.dispose(); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | @Override |
|---|
| 54 | protected IEditorSite createSite(IEditorPart page) { |
|---|
| 55 | IEditorSite site = null; |
|---|
| 56 | if (page == editor) { |
|---|
| 57 | site = new MultiPageEditorSite(this, page) { |
|---|
| 58 | public String getId() { |
|---|
| 59 | // Sets this ID so nested editor is configured for XML source |
|---|
| 60 | return "mxml-content"; //$NON-NLS-1$; |
|---|
| 61 | } |
|---|
| 62 | }; |
|---|
| 63 | } |
|---|
| 64 | else { |
|---|
| 65 | site = super.createSite(page); |
|---|
| 66 | } |
|---|
| 67 | return site; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | @Override |
|---|
| 71 | protected void createPages() { |
|---|
| 72 | createEditorPage(); |
|---|
| 73 | createDesignPage(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | private void createEditorPage() { |
|---|
| 77 | try { |
|---|
| 78 | editor = new StructuredTextEditor(); |
|---|
| 79 | int index = addPage(editor, getEditorInput()); |
|---|
| 80 | setPageText(index, editor.getTitle()); |
|---|
| 81 | } catch (PartInitException e) { |
|---|
| 82 | e.printStackTrace(); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | private void createDesignPage() { |
|---|
| 87 | // TODO create swt browser and load the flex designer swf |
|---|
| 88 | Composite composite = new Composite(getContainer(), SWT.NONE); |
|---|
| 89 | GridLayout gridLayout = new GridLayout(1,true); |
|---|
| 90 | gridLayout.marginHeight = 0; |
|---|
| 91 | gridLayout.marginWidth = 0; |
|---|
| 92 | gridLayout.verticalSpacing = 0; |
|---|
| 93 | composite.setLayout(gridLayout); |
|---|
| 94 | Composite buttons = new Composite(composite, SWT.NONE); |
|---|
| 95 | buttons.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|---|
| 96 | buttons.setLayout(new RowLayout(SWT.HORIZONTAL)); |
|---|
| 97 | Button exprt = new Button(buttons, SWT.PUSH); |
|---|
| 98 | exprt.setText("&Export to editor"); |
|---|
| 99 | exprt.addSelectionListener(new SelectionAdapter(){ |
|---|
| 100 | @Override |
|---|
| 101 | public void widgetSelected(SelectionEvent e) { |
|---|
| 102 | exportDesignInput(); |
|---|
| 103 | } |
|---|
| 104 | }); |
|---|
| 105 | Button imprt = new Button(buttons, SWT.PUSH); |
|---|
| 106 | imprt.setText("&Import from editor"); |
|---|
| 107 | imprt.addSelectionListener(new SelectionAdapter(){ |
|---|
| 108 | @Override |
|---|
| 109 | public void widgetSelected(SelectionEvent e) { |
|---|
| 110 | importDesignInput(); |
|---|
| 111 | } |
|---|
| 112 | }); |
|---|
| 113 | browser = new Browser(composite, SWT.NONE); |
|---|
| 114 | browser.setLayoutData(new GridData(GridData.FILL_BOTH)); |
|---|
| 115 | browser.addStatusTextListener(new StatusTextListener(){ |
|---|
| 116 | public void changed(StatusTextEvent event) { |
|---|
| 117 | if (exportQuery && event.text != null && event.text.length() > 0) { |
|---|
| 118 | setEditorContent(event.text); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | }); |
|---|
| 122 | browser.setUrl(getDesignerUrl()); |
|---|
| 123 | MxmlPlugin.getDefault().log(getDesignerUrl()); |
|---|
| 124 | System.out.println(getDesignerUrl()); |
|---|
| 125 | int index = addPage(composite); |
|---|
| 126 | setPageText(index, "Flex Designviewer"); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public String getDesignerUrl() { |
|---|
| 130 | Path path = new Path("designview/index.html"); |
|---|
| 131 | return BrowserUtils.prepareUrl(MxmlPlugin.getBundleURL(path).toString()); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | @Override |
|---|
| 135 | public void doSave(IProgressMonitor monitor) { |
|---|
| 136 | // TODO think about unsaved flex designer content |
|---|
| 137 | editor.doSave(monitor); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | @Override |
|---|
| 141 | public void doSaveAs() { |
|---|
| 142 | editor.doSaveAs(); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | @Override |
|---|
| 146 | public boolean isSaveAsAllowed() { |
|---|
| 147 | return true; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | public void resourceChanged(IResourceChangeEvent event) { |
|---|
| 151 | IEditorInput input = getEditorInput(); |
|---|
| 152 | if (event.getType() == IResourceChangeEvent.POST_CHANGE) { |
|---|
| 153 | IResourceDelta delta = event.getDelta(); |
|---|
| 154 | if (input instanceof IFileEditorInput) { |
|---|
| 155 | IFileEditorInput fileinput = (IFileEditorInput) input; |
|---|
| 156 | IPath path = fileinput.getFile().getFullPath(); |
|---|
| 157 | IResourceDelta member = delta.findMember(path); |
|---|
| 158 | if (member != null && member.getKind() != IResourceDelta.REMOVED) |
|---|
| 159 | importDesignInput(); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | private IDocument getDocument() { |
|---|
| 165 | Object adapter = editor.getAdapter(IDocument.class); |
|---|
| 166 | if (adapter == null) return null; |
|---|
| 167 | return (IDocument) adapter; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | public void importDesignInput() { |
|---|
| 171 | IDocument doc = getDocument(); |
|---|
| 172 | if (doc != null) { |
|---|
| 173 | String input = doc.get(); |
|---|
| 174 | String command = "importInput('"+input+"');"; |
|---|
| 175 | browser.execute(command); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | public void setEditorContent(String text) { |
|---|
| 180 | IDocument doc = getDocument(); |
|---|
| 181 | if (doc != null) { |
|---|
| 182 | doc.set(text); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | public void exportDesignInput() { |
|---|
| 187 | exportQuery = true; |
|---|
| 188 | browser.execute("exportInput();"); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | @Override |
|---|
| 192 | protected void pageChange(int newPageIndex) { |
|---|
| 193 | super.pageChange(newPageIndex); |
|---|
| 194 | if (firstGlance && newPageIndex == 1) { |
|---|
| 195 | firstGlance = false; |
|---|
| 196 | importDesignInput(); |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | } |
|---|