Back to index

moin  1.9.0~rc2
TWikiFrame.java
Go to the documentation of this file.
00001 /*
00002  * @(#)TWikiDraw.java 5.1
00003  * Copyright 2000 by Peter Thoeny, Peter@Thoeny.com.
00004  * It is hereby granted that this software can be used, copied, 
00005  * modified, and distributed without fee provided that this 
00006  * copyright notice appears in all copies.
00007  * Portions Copyright (C) 2001 Motorola - All Rights Reserved
00008  */
00009 
00010 package CH.ifa.draw.twiki;
00011 
00012 import Acme.JPM.Encoders.*;
00013 import CH.ifa.draw.appframe.*;
00014 import CH.ifa.draw.framework.*;
00015 import CH.ifa.draw.standard.*;
00016 import CH.ifa.draw.figures.*;
00017 import CH.ifa.draw.util.*;
00018 import CH.ifa.draw.applet.*;
00019 import CH.ifa.draw.contrib.*;
00020 //import SVG.wiki.*;
00021 
00022 import java.awt.*;
00023 import java.awt.event.*;
00024 import java.util.*;
00025 import java.io.*;
00026 import java.net.*;
00027 import java.lang.reflect.*;
00028 
00029 import com.keypoint.PngEncoderIndexed;
00030 
00031 public class TWikiFrame extends DrawFrame {
00032 
00036     static private String UNTITLED_PARAMETER   = "untitled";
00037     static private String DRAWPATH_PARAMETER   = "drawpath";
00038     static private String GIFPATH_PARAMETER    = "gifpath";
00039     static private String PNGPATH_PARAMETER    = "pngpath";
00040     static private String SVGPATH_PARAMETER    = "svgpath";
00041     static private String SAVEPATH_PARAMETER   = "savepath";
00042     static private String BASENAME_PARAMETER   = "basename";
00043     static private String HELPPATH_PARAMETER   = "helppath";
00044     static private String BORDERSIZE_PARAMETER = "bordersize";
00045 
00046     private Label fStatusLabel;
00047 
00048     public TWikiFrame(Application applet) {
00049         super("TWikiDraw", applet);
00050 
00051         this.view().setBackground(Color.white);
00052 
00053         // load      drawing
00054         if (applet.getParameter(DRAWPATH_PARAMETER) !=  null) {
00055             loadDrawing(DRAWPATH_PARAMETER);
00056         }
00057     }
00058 
00059     protected void populateMenuBar(MenuBar menuBar) {
00060         menuBar.add(createDrawingMenu());
00061         menuBar.add(createEditMenu());
00062         menuBar.add(createSelectionMenu());
00063         menuBar.add(createFillMenu());
00064         menuBar.add(createLineMenu());
00065        menuBar.add(createTextMenu());
00066        setMenuBar(menuBar);
00067     }
00068 
00069     protected CommandMenu createDrawingMenu() {
00070         CommandMenu menu = new CommandMenu("Drawing");
00071 
00072        menu.add(new NewDrawingCommand(this));
00073        menu.add(new ReloadDrawingCommand(this));
00074        menu.add(new SaveDrawingCommand(this));
00075        menu.add(new ExitDrawingCommand(this));
00076 
00077        return menu;
00078     }
00079 
00080     protected CommandMenu createEditMenu() {
00081         CommandMenu menu = new CommandMenu("Edit");
00082 
00083        menu.add(new DeleteCommand("Delete", view()));
00084        menu.add(new CutCommand("Cut", view()));
00085        menu.add(new CopyCommand("Copy", view()));
00086        menu.add(new PasteCommand("Paste", view()));
00087        menu.add(new ToggleGuidesCommand("Toggle guides", view()));
00088 
00089        return menu;
00090     }
00091 
00092     protected CommandMenu createSelectionMenu() {
00093         CommandMenu menu = new CommandMenu("Selection");
00094 
00095        menu.add(new DuplicateCommand("Duplicate", view()));
00096        menu.add(new GroupCommand("Group", view()));
00097        menu.add(new UngroupCommand("Ungroup", view()));
00098        menu.add(new SendToBackCommand("Send to Back", view()));
00099        menu.add(new BringToFrontCommand("Bring to Front", view()));
00100 
00101         CommandMenu align = new CommandMenu("Align");
00102        align.add(new AlignCommand("Lefts", view(), AlignCommand.LEFTS));
00103        align.add(new AlignCommand("Centres", view(), AlignCommand.CENTERS));
00104        align.add(new AlignCommand("Rights", view(), AlignCommand.RIGHTS));
00105        align.add(new AlignCommand("Tops", view(), AlignCommand.TOPS));
00106        align.add(new AlignCommand("Middles", view(), AlignCommand.MIDDLES));
00107        align.add(new AlignCommand("Bottoms", view(), AlignCommand.BOTTOMS));
00108        menu.add(align);
00109 
00110        return menu;
00111     }
00112 
00113     protected CommandMenu createFillMenu() {
00114         CommandMenu menu = new CommandMenu("Fill");
00115         menu.add(createColorMenu("FillColor"));
00116        return menu;
00117     }
00118 
00119     protected CommandMenu createLineMenu() {
00120         CommandMenu menu = new CommandMenu("Line");
00121 
00122         menu.add(createColorMenu("FrameColor"));
00123 
00124        CommandMenu arrowStyle = new CommandMenu("Arrows");
00125         arrowStyle.add(
00126            new ChangeAttributeCommand(
00127               "none", "ArrowMode", 
00128               new Integer(PolyLineFigure.ARROW_TIP_NONE),  view()));
00129         arrowStyle.add(
00130            new ChangeAttributeCommand(
00131               "at Start", "ArrowMode",
00132               new Integer(PolyLineFigure.ARROW_TIP_START), view()));
00133         arrowStyle.add(
00134            new ChangeAttributeCommand(
00135               "at End",   "ArrowMode",
00136               new Integer(PolyLineFigure.ARROW_TIP_END),   view()));
00137         arrowStyle.add(
00138            new ChangeAttributeCommand(
00139               "at Both ends",  "ArrowMode",
00140               new Integer(PolyLineFigure.ARROW_TIP_BOTH),  view()));
00141        menu.add(arrowStyle);
00142 
00143        return menu;
00144     }
00145 
00146     protected CommandMenu createTextMenu() {
00147         CommandMenu menu = new CommandMenu("Text");
00148 
00149         menu.add(createFontMenu());
00150         menu.add(createColorMenu("TextColor"));
00151 
00152         CommandMenu style = new CommandMenu("Style");
00153         style.add(new ChangeAttributeCommand(
00154            "Plain", "FontStyle", new Integer(Font.PLAIN),  view()));
00155         style.add(new ChangeAttributeCommand(
00156            "Italic","FontStyle", new Integer(Font.ITALIC), view()));
00157         style.add(new ChangeAttributeCommand(
00158            "Bold",  "FontStyle", new Integer(Font.BOLD),   view()));
00159        menu.add(style);
00160 
00161         CommandMenu align = new CommandMenu("Align");
00162         align.add(new ChangeAttributeCommand(
00163            "Left", "TextAlign", "Left",  view()));
00164         align.add(new ChangeAttributeCommand(
00165            "Centre", "TextAlign", "Centre", view()));
00166         align.add(new ChangeAttributeCommand(
00167            "Right",  "TextAlign", "Right",   view()));
00168        menu.add(align);
00169 
00170        return menu;
00171     }
00172 
00173     //-- DrawFrame overrides -----------------------------------------
00174 
00175     protected void populateWestPanel(Panel palette) {
00176        // get the default selection tool
00177         super.populateWestPanel(palette);
00178 
00179         Tool tool = new TextTool(view(), new TextFigure());
00180         palette.add(createToolButton(IMAGES+"TEXT", "Text Tool", tool));
00181 
00182         tool = new ConnectedTextTool(view(), new TextFigure());
00183         palette.add(createToolButton(IMAGES+"ATEXT", "Connected Text Tool", tool));
00184 
00185         tool = new CreationTool(view(), new RectangleFigure());
00186         palette.add(createToolButton(IMAGES+"RECT", "Rectangle Tool", tool));
00187 
00188         tool = new CreationTool(view(), new RoundRectangleFigure());
00189         palette.add(createToolButton(IMAGES+"RRECT", "Round Rectangle Tool", tool));
00190 
00191         tool = new CreationTool(view(), new EllipseFigure());
00192         palette.add(createToolButton(IMAGES+"ELLIPSE", "Ellipse Tool", tool));
00193 
00194         tool = new CreationTool(view(), new LineFigure());
00195         palette.add(createToolButton(IMAGES+"LINE", "Line Tool", tool));
00196 
00197         tool = new ConnectionTool(view(), new LineConnection());
00198         palette.add(createToolButton(IMAGES+"CONN", "Connection Tool", tool));
00199 
00200         tool = new ConnectionTool(view(), new ElbowConnection());
00201         palette.add(createToolButton(IMAGES+"OCONN", "Elbow Connection Tool", tool));
00202 
00203         tool = new ScribbleTool(view());
00204         palette.add(createToolButton(IMAGES+"SCRIBBL", "Scribble Tool", tool));
00205 
00206         tool = new PolygonTool(view());
00207         palette.add(createToolButton(IMAGES+"POLYGON", "Polygon Tool", tool));
00208 
00209         tool = new BorderTool(view());
00210         palette.add(createToolButton(IMAGES+"BORDDEC", "Border Tool", tool));
00211 
00212         tool = new URLTool(view(), new RectangleFigure());
00213         palette.add(createToolButton(IMAGES+"URL", "URL Tool", tool));
00214     }
00215 
00216     protected Panel createSouthPanel() {
00217        Panel split = new Panel();
00218         split.setLayout(new GridLayout(2, 1));
00219        return split;
00220     }
00221 
00222     protected void populateSouthPanel(Panel panel) {
00223        Panel buttons = new Panel();
00224         Button button;
00225 
00226         button = new CommandButton(new DeleteCommand("Delete", view()));
00227         buttons.add(button);
00228 
00229         button = new CommandButton(new DuplicateCommand("Duplicate", view()));
00230         buttons.add(button);
00231 
00232         button = new CommandButton(new GroupCommand("Group", view()));
00233         buttons.add(button);
00234 
00235         button = new CommandButton(new UngroupCommand("Ungroup", view()));
00236         buttons.add(button);
00237 
00238         button = new CommandButton(new BringToFrontCommand("Bring To Front", view()));
00239         buttons.add(button);
00240 
00241         button = new CommandButton(new SendToBackCommand("Send To Back", view()));
00242         buttons.add(button);
00243 
00244         button = new Button("Help");
00245        button.addActionListener(
00246            new ActionListener() {
00247                   public void actionPerformed(ActionEvent event) {
00248                      showHelp();
00249                   }
00250               }
00251            );
00252         buttons.add(button);
00253        panel.add(buttons);
00254        fStatusLabel = new Label("Status");
00255        panel.add(fStatusLabel);
00256     }
00257 
00258     public void showStatus(String s) {
00259        if (fStatusLabel != null)
00260            fStatusLabel.setText(s);
00261        else
00262            getApplication().showStatus(s);
00263     }
00264 
00268     protected void setSimpleDisplayUpdate() {}
00269     protected void setBufferedDisplayUpdate() {}
00270 
00271 
00272     //-- actions -----------------------------------------
00273 
00274     protected void showHelp() {
00275         try {
00276             // gets help file path
00277             String helpPath =
00278               getApplication().getParameter(HELPPATH_PARAMETER);
00279             if (helpPath != null) {
00280                 URL url = getApplication().getURL(helpPath);
00281                 getApplication().popupFrame(url, "Help");
00282             }
00283         } catch (IOException e) {
00284             showStatus("Help file not found");
00285         }
00286     }
00287 
00288     class NewDrawingCommand extends Command {
00289        TWikiFrame frame;
00290        public NewDrawingCommand(TWikiFrame frm) {
00291            super("Clear");
00292            frame = frm;
00293        }
00294         public void execute() {
00295            frame.doLoadDrawing(UNTITLED_PARAMETER);
00296        }
00297     }
00298 
00299     class ReloadDrawingCommand extends Command {
00300        TWikiFrame frame;
00301        public ReloadDrawingCommand(TWikiFrame frm) {
00302            super("Reload");
00303            frame = frm;
00304        }
00305         public void execute() {
00306            frame.doLoadDrawing(DRAWPATH_PARAMETER);
00307        }
00308     }
00309 
00310     class SaveDrawingCommand extends Command {
00311        TWikiFrame frame;
00312        public SaveDrawingCommand(TWikiFrame frm) {
00313            super("Save and Exit");
00314            frame = frm;
00315        }
00316         public void execute() {
00317            if (frame.doSaveDrawing())
00318               ((TWikiDraw)getApplication()).exitApplet();
00319        }
00320     }
00321 
00322     class ExitDrawingCommand extends Command {
00323        TWikiFrame frame;
00324        public ExitDrawingCommand(TWikiFrame frm) {
00325            super("Exit without saving");
00326            frame = frm;
00327        }
00328         public void execute() {
00329            ((TWikiDraw)getApplication()).exitApplet();
00330        }
00331     }
00332 
00333     public void doLoadDrawing(String name) {
00334               loadDrawing(name);
00335               view().setBackground(Color.white);
00336     }
00337        
00338     public boolean doSaveDrawing() {
00339               TWikiDraw app = (TWikiDraw)getApplication();
00340               boolean savedDraw, savedSvg, savedGif, savedPng, savedMap;
00341               savedDraw = savedSvg = savedGif = savedPng = savedMap = false;
00342               
00343               // set wait cursor
00344               setCursor(new Cursor(Cursor.WAIT_CURSOR));
00345               
00346               try {
00347                      // saves the drawing to the stream of bytes
00348                      ByteArrayOutputStream out = new ByteArrayOutputStream();
00349                      StorableOutput output = new StorableOutput(out);
00350             // drawing() (class DrawFrame) returns the Drawing element
00351             // Drawing is a StandardDrawing (extends Drawing
00352                      // and implements CompositeFigures)
00353                      output.writeStorable(drawing());
00354                      output.close();
00355                      
00356                      // gets pathname of the drawing
00357                      String drawingPath = app.getParameter(DRAWPATH_PARAMETER);
00358                      if (drawingPath == null)
00359                             drawingPath = "untitled.draw";
00360                      
00361                      // gets script name
00362                      String savePath = app.getParameter(SAVEPATH_PARAMETER);
00363                      if (savePath == null)
00364                             savePath = "";
00365                      
00366                      // gets base filename
00367                      String baseName = app.getParameter(BASENAME_PARAMETER);
00368                      if (baseName == null)
00369                             baseName = "SET_BASENAME_PARAM";
00370                      
00371                      // submit POST command to the server three times:
00372                      // *.draw, *.map and *.gif
00373                      // first upload *.draw file
00374                      showStatus("Saving " + baseName + ".draw");
00375                      savedDraw = app.post(savePath, baseName + ".draw", "text/plain", drawingPath,
00376                                                          out.toString(), "TWikiDraw draw file");
00377                      
00378                      // calculate the minimum size of the gif image
00379                      Dimension d = new Dimension(0, 0); // not this.view().getSize();
00380                      FigureEnumeration k = drawing().figuresReverse();
00381                      while (k.hasMoreElements()) {
00382                             Figure figure = k.nextFigure();
00383                             Rectangle r = figure.displayBox();
00384                             if (r.x + r.width > d.width) {
00385                                    d.setSize(r.x + r.width, d.height);
00386                             }
00387                             if (r.y + r.height > d.height) {
00388                                    d.setSize(d.width, r.y + r.height);
00389                             }
00390                      }
00391                      
00392                      // gets add border size
00393                      int iBorder = 10;
00394                      String sBorder = getApplication().getParameter(BORDERSIZE_PARAMETER);
00395                      if (sBorder != null) {
00396                             iBorder = Integer.valueOf(sBorder).intValue();
00397                             if (iBorder < 0) {
00398                                    iBorder = 0;
00399                             }
00400                      }
00401                      
00402                      String map = drawing().getMap();
00403                      if (map.length() > 0) {
00404                             String mapPath = drawingPath.substring(0, drawingPath.length() - 5);
00405                             showStatus("Saving " + baseName + ".map");
00406                             // enclose the map and add editable border. Note that the
00407                             // edit border is add LAST so the earlier AREAs take
00408                             // precedence.
00409                             map = "<map name=\"%MAPNAME%\">\n" + map +
00410                                    "<area shape=\"rect\" coords=\"" +
00411                                    "0,0," + (d.width + iBorder) + "," + (iBorder/2) +
00412                                    "\" href=\"%TWIKIDRAW%\" />\n" +
00413                                    "<area shape=\"rect\" coords=\"" +
00414                                    "0,0," + (iBorder/2) + "," + (d.height + iBorder) +
00415                                    "\" href=\"%TWIKIDRAW%\" />\n" +
00416                                    "<area shape=\"rect\" coords=\"" +
00417                                    (d.width + iBorder/2) + ",0," + (d.width + iBorder) + "," + (d.height + iBorder) +
00418                                    "\" href=\"%TWIKIDRAW%\" />\n" +
00419                                    "<area shape=\"rect\" coords=\"" +
00420                                    "0," + (d.height + iBorder/2) + "," +
00421                                    (d.width + iBorder) + "," + (d.height + iBorder) +
00422                                    "\" href=\"%TWIKIDRAW%\" />\n" +
00423                                    "</map>";
00424                             savedMap = app.post(savePath, baseName + ".map", "text/plain", mapPath + ".map",
00425                                                                map, "TWikiDraw map file");
00426                      } else {
00427                             // erase any previous map file
00428                             String mapPath = drawingPath.substring(0, drawingPath.length() - 5);
00429                             savedMap = app.post( savePath, baseName + ".map", "text/plain", mapPath + ".map", "", "");
00430             }
00431                      
00432             // get pathname of the SVG file
00433                      /*
00434                        String svgPath = getApplication().getParameter(SVGPATH_PARAMETER);
00435                        if (svgPath != null && svgPath.length() > 0) {
00436                        showStatus("Saving " + svgPath);
00437                        // create the svg data
00438                        String outsvg = SvgSaver.convertSVG(out.toString());
00439                        // now upload *.svg file
00440                        savedSvg = app.post(savePath, "", "image/svg", svgPath,
00441                        outsvg, "SVG file");
00442                        }
00443                      */
00444                      
00445                      // gets pathname of the GIF image
00446                      String gifPath = getApplication().getParameter(GIFPATH_PARAMETER);
00447                      if (gifPath == null || gifPath.length() == 0) gifPath = null;
00448                      
00449                      // gets pathname of the PNG image
00450                      String pngPath = getApplication().getParameter(PNGPATH_PARAMETER);
00451                      if (pngPath == null || pngPath.length() == 0) pngPath = null;
00452                      
00453                      if (gifPath != null || pngPath != null) {
00454                             // clear the selection so it doesn't appear
00455                             view().clearSelection();
00456                             
00457                             Image oImgBuffer =
00458                                    this.view().createImage(d.width + iBorder, d.height + iBorder);
00459                             Graphics oGrf = oImgBuffer.getGraphics();
00460                             this.view().enableGuides(false);
00461                             this.view().drawAll(oGrf);
00462                             
00463                             if (pngPath != null) {
00464                                    // then create *.png image and upload file
00465                                    showStatus("Saving " + baseName + ".png");
00466                                    
00467                                    PngEncoderIndexed oEncode = new PngEncoderIndexed(oImgBuffer);
00468                                    byte[] aByte = oEncode.pngEncode();
00469                                    int size = aByte.length;
00470                                    char[] aChar = new char[size];
00471                                    for(int i = 0; i < size; i++) {
00472                                           aChar[i] = (char)aByte[i];
00473                                    }
00474                                    
00475                                    // upload *.png file
00476                                    savedPng = app.post(savePath, baseName + ".png", "image/png",
00477                                                                       pngPath, String.valueOf( aChar, 0, size),
00478                                                                       "TWikiDraw PNG file");
00479                             }
00480                             
00481                             if (gifPath != null) {
00482                                    // then create *.gif image and upload file
00483                                    showStatus("Saving " + baseName + ".gif");
00484                                    
00485                                    ByteArrayOutputStream oOut = new ByteArrayOutputStream();
00486                                    GifEncoder oEncode = new GifEncoder(oImgBuffer, oOut);
00487                                    oEncode.encode();
00488                                    byte[] aByte = oOut.toByteArray();
00489                                    int size = oOut.size();
00490                                    char[] aChar = new char[size];
00491                                    for(int i = 0; i < size; i++) {
00492                                           aChar[i] = (char)aByte[i];
00493                                    }
00494                                    
00495                                    // upload *.gif file
00496                                    savedGif = app.post(savePath, baseName + ".gif", "image/gif",
00497                                                                       gifPath, String.valueOf( aChar, 0, size),
00498                                                                       "TWikiDraw GIF file");
00499                             }
00500                      }
00501               } catch (MalformedURLException e) {
00502             this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
00503             showStatus("Bad Wiki servlet URL: "+e.getMessage());
00504               } catch (IOException e) {
00505                      e.printStackTrace();
00506                      this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
00507                      showStatus(e.toString());
00508               }
00509               this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
00510               showStatus("Saved .draw " + (savedDraw ? "OK" : "Failed") +
00511                                " .map " + (savedMap ? "OK" : "Failed") +
00512                                " .gif " + (savedGif ? "OK" : "Failed") +
00513                                " .png " + (savedPng ? "OK" : "Failed"));
00514               return savedDraw;
00515        }
00516 
00517 }