001: /*
002: * $Id: JGraphpadTWikiAction.java,v 1.7 2005/10/09 12:00:04 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.twikiplugin;
011:
012: import java.awt.event.ActionEvent;
013: import java.awt.image.BufferedImage;
014: import java.io.IOException;
015: import java.io.OutputStream;
016: import java.net.URL;
017: import java.util.zip.GZIPOutputStream;
018:
019: import javax.imageio.ImageIO;
020:
021: import org.jgraph.JGraph;
022:
023: import com.jgraph.JGraphEditor;
024: import com.jgraph.codecplugin.JGraphpadCodecAction;
025: import com.jgraph.editor.JGraphEditorAction;
026: import com.jgraph.editor.JGraphEditorFile;
027: import com.jgraph.editor.JGraphEditorResources;
028: import com.jgraph.editor.factory.JGraphEditorDiagramPane;
029:
030: /**
031: * Implements all actions that require the browser launcher.
032: */
033: public class JGraphpadTWikiAction extends JGraphpadCodecAction {
034:
035: /**
036: * Specifies the name for the <code>uploadToTWiki</code> action.
037: */
038: public static final String NAME_UPLOADTOTWIKI = "uploadToTWiki";
039:
040: /**
041: * Specifies the name for the <code>uploadAndExit</code> action.
042: */
043: public static final String NAME_UPLOADANDEXIT = "uploadAndExit";
044:
045: /**
046: * Constructs a new file action for the specified name and editor.
047: *
048: * @param name
049: * The name of the action to be created.
050: * @param editor
051: * The enclosing editor for the action.
052: */
053: public JGraphpadTWikiAction(String name, JGraphEditor editor) {
054: super (name, editor);
055: }
056:
057: /**
058: * Executes the action based on the action name.
059: *
060: * @param e
061: * The object that describes the event.
062: */
063: public void actionPerformed(ActionEvent e) {
064: try {
065: JGraphEditorFile file = getPermanentFocusOwnerFile();
066: JGraph graph = getPermanentFocusOwnerGraph();
067: if (file != null && graph != null) {
068: if (getName().equals(NAME_UPLOADTOTWIKI)) {
069: doUploadToTWiki(file, graph, 0);
070: } else if (getName().equals(NAME_UPLOADANDEXIT)) {
071: doUploadToTWiki(file, graph, 0);
072: doExit();
073: }
074: }
075: } catch (Exception e1) {
076: e1.printStackTrace();
077: dlgs.errorDialog(getPermanentFocusOwner(), e1
078: .getLocalizedMessage());
079: }
080: }
081:
082: protected void doUploadToTWiki(JGraphEditorFile file, JGraph graph,
083: int inset) throws IOException {
084: String uploadURL = (String) editor.getSettings().getObject(
085: "upload");
086: if (uploadURL != null) {
087: // Contains Full URL
088: String filepath = file.getFilename();
089: int lastSlash = filepath.lastIndexOf("/");
090: // Contains basedir URL
091: String baseurl = filepath.substring(0, lastSlash);
092: // Contains filename with extension
093: String filename = filepath.substring(lastSlash + 1);
094:
095: // Contains filename without extension
096: String basename = filename;
097: if (basename.toLowerCase().endsWith(".gz"))
098: basename = basename.substring(0, basename.length() - 4);
099: if (basename.toLowerCase().endsWith(".xml"))
100: basename = basename.substring(0, basename.length() - 4);
101:
102: // Writes diagram file. Do not use
103: // editor.getModel().getOutputStream()
104: // for the file is we need to keep a reference to the innermost
105: // bytearrayoutputstream for passing to the post method!
106: OutputStream out = JGraphEditorResources
107: .getOutputStream(filepath);
108: OutputStream inner = out;
109: if (filename.toLowerCase().endsWith((".gz")))
110: out = new GZIPOutputStream(out);
111: editor.getModel().writeObject(file, out);
112: out.flush();
113: out.close();
114:
115: // Posts inner output stream
116: post(new URL(uploadURL), filename, (filename.toLowerCase()
117: .endsWith(".gz")) ? "application/octet-stream"
118: : MIME_PLAINTEXT, inner);
119:
120: // Writes map file
121: String mapFilename = baseurl + "/" + basename + ".map";
122: out = editor.getModel().getOutputStream(mapFilename);
123: int written = writeMap(graph, basename, inset, out);
124: out.flush();
125: out.close();
126: if (written > 0) {
127: post(new URL(uploadURL), mapFilename, MIME_PLAINTEXT,
128: out);
129: }
130:
131: // Writes PNG image
132: String imgFilename = baseurl + "/" + basename + ".png";
133: out = editor.getModel().getOutputStream(imgFilename);
134: JGraphEditorDiagramPane diagramPane = JGraphEditorDiagramPane
135: .getParentDiagramPane(graph);
136: BufferedImage img = diagramPane.getImage(null, inset);
137:
138: // Note: Use JGraphpadImageEncoder.writeGIF(img, out) for GIF files,
139: // this is OK for all built-in types (eg. JPG, BMP and PNG).
140: ImageIO.write(img, "png", out);
141: out.flush();
142: out.close();
143: post(new URL(uploadURL), imgFilename, "image/png", out);
144:
145: file.setNew(false);
146: file.setModified(false);
147: }
148: }
149:
150: /**
151: * Bundle of all actions in this class.
152: */
153: public static class AllActions implements Bundle {
154:
155: /**
156: * Holds the actions. The actions are constructed in the constructor as
157: * they require an editor instance.
158: */
159: public JGraphEditorAction actionUploadToTWiki,
160: actionUploadAndExit;
161:
162: /**
163: * Constructs the action bundle for the specified editor.
164: *
165: * @param editor
166: * The enclosing editor for this bundle.
167: */
168: public AllActions(JGraphEditor editor) {
169: actionUploadToTWiki = new JGraphpadTWikiAction(
170: NAME_UPLOADTOTWIKI, editor);
171: actionUploadAndExit = new JGraphpadTWikiAction(
172: NAME_UPLOADANDEXIT, editor);
173: }
174:
175: /*
176: * (non-Javadoc)
177: */
178: public JGraphEditorAction[] getActions() {
179: return new JGraphEditorAction[] { actionUploadToTWiki,
180: actionUploadAndExit };
181: }
182:
183: /*
184: * (non-Javadoc)
185: */
186: public void update() {
187: // always enabled
188: }
189:
190: }
191:
192: }
|