001: // The UMLet source code is distributed under the terms of the GPL; see license.txt
002: /*******************************************************************************
003: * 2004-03-03 15:00 runs
004: *******************************************************************************/package com.umlet.plugin.editors;
005:
006: import java.awt.*;
007: import java.io.*;
008:
009: import org.eclipse.core.resources.*;
010: import org.eclipse.core.runtime.*;
011: import org.eclipse.swt.widgets.Display;
012: import org.eclipse.ui.*;
013: import org.eclipse.ui.part.*; //org.eclipse.ui.part.FileEditorInput;
014: import org.eclipse.swt.SWT;
015:
016: import com.umlet.control.*;
017: import com.umlet.control.io.*;
018:
019: public class UmletEditor extends EditorPart implements
020: UmletPluginHandler {
021: private boolean isDirty = false;
022: private org.eclipse.swt.widgets.Composite parent;
023:
024: public Panel embedded_umlet_panel;
025:
026: public static String UMLET_SAVE = "UMLET_SAVE";
027: public static String UMLET_CLOSE = "UMLET_CLOSE";
028:
029: private class UmletEventThread extends Thread {
030:
031: private IEditorPart editor;
032: private String action;
033: private boolean inFlag;
034: public boolean returnCode;
035:
036: public UmletEventThread(IEditorPart editor, String action,
037: boolean inFlag) {
038: this .editor = editor;
039: this .action = action;
040: this .inFlag = inFlag;
041: }
042:
043: public void run() {
044: System.out.println("UmletEventThread.run");
045: if (action.equalsIgnoreCase(UMLET_SAVE)) {
046: returnCode = editor.getSite().getPage().saveEditor(
047: editor, inFlag);
048: } else if (action.equalsIgnoreCase(UMLET_CLOSE)) {
049: returnCode = editor.getSite().getPage().closeEditor(
050: editor, inFlag);
051: }
052: }
053: }
054:
055: // *** Implementation of interface UmletPluginHandler ***
056: public boolean notifyIsClosing() {
057: System.out.println("UmletPlugin->notifyIsClosing...");
058: if (!Umlet.isEmbedded())
059: Umlet.getInstance().window.toBack();
060: Display disp = getSite().getShell().getDisplay();
061: UmletEventThread umletEvent = new UmletEventThread(this ,
062: UMLET_CLOSE, true);
063: disp.syncExec(umletEvent);
064: System.out.println("UmletPlugin->after exec..."
065: + umletEvent.returnCode);
066: if (!umletEvent.returnCode && !Umlet.isEmbedded())
067: Umlet.getInstance().window.toFront();
068: return umletEvent.returnCode;
069: }
070:
071: public void notifyIsDirty(boolean isDirty) {
072: this .isDirty = isDirty;
073: }
074:
075: public boolean notifySave(boolean ask) {
076: System.out.println("UmletPlugin->notifySave...");
077: Display disp = getSite().getShell().getDisplay();
078: UmletEventThread umletEvent = new UmletEventThread(this ,
079: UMLET_SAVE, ask);
080: disp.syncExec(umletEvent);
081: return umletEvent.returnCode;
082: //getSite().
083: }
084:
085: public boolean notifySaveAsFormat(String format) {
086: try {
087: setFocus();
088: if (!hasUmletHandle())
089: return false;
090: ByteArrayOutputStream outdata = new ByteArrayOutputStream();
091: if (format.equalsIgnoreCase("PDF")) {
092: GenPdf.getInstance().createPdfToStream(outdata);
093: } else if (format.equalsIgnoreCase("JPG")) {
094: GenPdf.getInstance().createJpgToStream(outdata);
095: } else if (format.equalsIgnoreCase("SVG")) {
096: GenSvg.createSvgToStream(outdata);
097: } else if (format.equalsIgnoreCase("EPS")) {
098: GenEps.getInstance().createEpsToStream(outdata);
099: }
100: IFile selFile = (IFile) ((FileEditorInput) getEditorInput())
101: .getStorage();
102: IContainer targetFolder = selFile.getParent();
103:
104: IPath newFilePath = targetFolder.getFullPath().append(
105: "/"
106: + selFile.getName().substring(0,
107: selFile.getName().length() - 4)
108: + "." + format.toLowerCase());
109: //[UB]: changed WorkspacePlugin to ResourcesPlugin for eclipse3.0 compatibility
110: IFile newFile = ResourcesPlugin.getWorkspace().getRoot()
111: .getFile(newFilePath);
112: if (!newFile.exists()) {
113: newFile.create(new ByteArrayInputStream(outdata
114: .toByteArray()), false, null);
115: } else {
116: newFile.setContents(new ByteArrayInputStream(outdata
117: .toByteArray()), false, true, null);
118: }
119: } catch (CoreException e) {
120: throw new RuntimeException(e.getMessage());
121: }
122: return true;
123: }
124:
125: // ********************************************************
126:
127: public void dispose() {
128: System.out.println("UmletPlugin->dispose...");
129: if (hasUmletHandle()) {
130: if (!Umlet.isEmbedded())
131: Umlet.getInstance().window.setVisible(false);
132: Umlet.pluginHandler = null;
133: }
134: }
135:
136: /* (non-Javadoc)
137: * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
138: */
139: public void doSave(IProgressMonitor monitor) {
140: System.out.println("UmletPlugin->doSave...");
141: if (!hasUmletHandle())
142: return;
143: try {
144: IFile filestorage = (IFile) ((FileEditorInput) getEditorInput())
145: .getStorage();
146: InputStream newContent = new ByteArrayInputStream(Umlet
147: .getInstance().createStringToBeSaved().getBytes(
148: "UTF-8"));
149: filestorage.setContents(newContent, false, true, monitor);
150: Umlet.getInstance().setChanged(false);
151: } catch (Exception e) {
152: throw new RuntimeException(e.getMessage());
153: }
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.ui.ISaveablePart#doSaveAs()
158: */
159: public void doSaveAs() {
160: System.out.println("UmletPlugin->doSaveAs...");
161: }
162:
163: /* (non-Javadoc)
164: * @see org.eclipse.ui.IEditorPart#gotoMarker(org.eclipse.core.resources.IMarker)
165: */
166: public void gotoMarker(IMarker marker) {
167: System.out.println("UmletPlugin->gotoMarker...");
168:
169: }
170:
171: /* (non-Javadoc)
172: * @see org.eclipse.ui.IEditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
173: */
174: //public String title;
175: public void init(IEditorSite site, IEditorInput input)
176: throws PartInitException {
177: System.out.println("UmletPlugin->init..." + this .toString());
178:
179: //[UB]: changed setTitle(input.getName()); to setPartName
180: //for eclipse-3.0 compatibility
181: setPartName(input.getName());
182: setInput(input);
183: setSite(site);
184:
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.ui.ISaveablePart#isDirty()
189: */
190: public boolean isDirty() {
191: if (hasUmletHandle())
192: isDirty = Umlet.getInstance().isChanged();
193: System.out.println("UmletPlugin->isDirty..." + isDirty);
194: return isDirty;
195: }
196:
197: /* (non-Javadoc)
198: * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
199: */
200: public boolean isSaveAsAllowed() {
201: System.out.println("UmletPlugin->isSaveAsAllowed...");
202: return isDirty();
203: }
204:
205: /* (non-Javadoc)
206: * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
207: */
208: public void createPartControl(
209: org.eclipse.swt.widgets.Composite parent) {
210: System.out.println("UmletPlugin->createPartControl...");
211:
212: // trying to embed the Umlet Swing-Panel
213: try {
214:
215: /* [UB]: embedding works a bit differently in eclipe-3.0
216: * It should work on linux too.
217: */
218: org.eclipse.swt.widgets.Composite goodSWTComposite = new org.eclipse.swt.widgets.Composite(
219: parent, SWT.EMBEDDED); //we need the embedded attribute set
220: java.awt.Frame frame = org.eclipse.swt.awt.SWT_AWT
221: .new_Frame(goodSWTComposite);
222: embedded_umlet_panel = new java.awt.Panel(
223: new java.awt.BorderLayout());
224: frame.add(embedded_umlet_panel);
225: //embedded_umlet_panel.validate();
226:
227: //parent.layout();
228: //parent.redraw();
229: this .parent = goodSWTComposite;
230: } catch (IllegalArgumentException e) {
231: System.out.println(e.getMessage());
232: } catch (Throwable e) {
233: System.err.println("UmletPlugin->Could not embed windows. "
234: + e.getMessage());
235: // something went wrong - show Umlet as standalone window
236: }
237: }
238:
239: /* (non-Javadoc)
240: * @see org.eclipse.ui.IWorkbenchPart#setFocus()
241: */
242: public void setFocus() {
243: System.out.println("UmletPlugin->setFocus...");
244: // open File Data with Umlet if not empty:
245: if (!hasUmletHandle()) {
246: // trying to grap the Umlet Singleton Instance:
247: if (Umlet.pluginHandler != null) {
248: if (!Umlet.pluginHandler.notifySave(true)) {
249: //throw new RuntimeException("Cant not open Umlet editor for "+getEditorInput().getName()+".");
250: getSite().getPage().activate(
251: (UmletEditor) Umlet.pluginHandler);
252: return;
253: }
254: if (isUmletEmbedded()) {
255: ((UmletEditor) Umlet.pluginHandler).embedded_umlet_panel
256: .removeAll();
257: }
258: }
259: try {
260: FileEditorInput fileIn = (FileEditorInput) getEditorInput();
261: InputStream data = fileIn.getFile().getContents();
262: if (isUmletEmbedded()) {
263: embedded_umlet_panel.add(Umlet.getInstance()); //LME: Umlet.getInstance() first call
264: //if (isUmletEmbedded()) Umlet.getInstance().getJspMain().setDividerLocation(Umlet.getInstance().getJspMain().getDividerLocation());
265: //embedded_umlet_panel.repaint();
266: }
267: Umlet.init(this , isUmletEmbedded());
268: Umlet.openStreamToPanel(data, Umlet.getInstance()
269: .getPanel());
270: Umlet.getInstance().setFileName(fileIn.getName());
271: Umlet.appendToTitle(fileIn.getName());
272: if (isUmletEmbedded()) {
273: //if (isUmletEmbedded()) Umlet.getInstance().getJspMain().setDividerLocation(Umlet.getInstance().getJspMain().getDividerLocation());
274: embedded_umlet_panel.validate();
275: //embedded_umlet_panel.repaint();
276: }
277: } catch (Exception e) {
278: throw new RuntimeException(e.getMessage());
279: }
280: }
281:
282: if (!Umlet.isEmbedded()) {
283: Umlet.getInstance().window.setVisible(true);
284: Umlet.getInstance().window.toFront();
285: }
286: }
287:
288: private boolean hasUmletHandle() {
289: return Umlet.pluginHandler == this ;
290: }
291:
292: private boolean isUmletEmbedded() {
293: return embedded_umlet_panel != null;
294: }
295: }
|