Back to index

moin  1.9.0~rc2
DrawApplet.java
Go to the documentation of this file.
00001 /*
00002  * @(#)DrawApplet.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.applet;
00007 
00008 import java.applet.Applet;
00009 import java.awt.*;
00010 import java.awt.event.*;
00011 import java.util.*;
00012 import java.io.*;
00013 import java.net.*;
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 
00019 
00029 public class DrawApplet
00030         extends Applet
00031            implements DrawingEditor, PaletteListener {
00032 
00033     transient private Drawing         fDrawing;
00034     transient private Tool            fTool;
00035 
00036     transient private StandardDrawingView fView;
00037     transient private ToolButton      fDefaultToolButton;
00038     transient private ToolButton      fSelectedToolButton;
00039 
00040     transient private boolean         fSimpleUpdate;
00041     transient private Button          fUpdateButton;
00042 
00043     transient private Choice          fFrameColor;
00044     transient private Choice          fFillColor;
00045     //transient private Choice          fTextColor;
00046     transient private Choice          fArrowChoice;
00047     transient private Choice          fFontChoice;
00048 
00049     transient private Thread          fSleeper;
00050     private Iconkit                   fIconkit;
00051 
00052     static String                     fgUntitled = "untitled";
00053 
00054     private static final String       fgDrawPath = "/CH/ifa/draw/";
00055     public static final String        IMAGES = fgDrawPath+"images/";
00056 
00060     public void init() {
00061         fIconkit = new Iconkit(this);
00062 
00063               setLayout(new BorderLayout());
00064 
00065         fView = createDrawingView();
00066 
00067         Panel attributes = createAttributesPanel();
00068         createAttributeChoices(attributes);
00069         add("North", attributes);
00070 
00071         Panel toolPanel = createToolPalette();
00072         createTools(toolPanel);
00073         add("West", toolPanel);
00074 
00075         add("Center", fView);
00076         Panel buttonPalette = createButtonPanel();
00077         createButtons(buttonPalette);
00078         add("South", buttonPalette);
00079 
00080         initDrawing();
00081         setBufferedDisplayUpdate();
00082         setupAttributes();
00083     }
00084 
00085     /*
00086      * Gets the iconkit to be used in the applet.
00087      */
00088 
00089      /**** not sure whether this will still be needed on 1.1 enabled browsers
00090      protected Iconkit iconkit() {
00091         if (fIconkit == null) {
00092 
00093             startSleeper();
00094             loadAllImages(this); // blocks until images loaded
00095             stopSleeper();
00096         }
00097         return fIconkit;
00098     } */
00099 
00103     protected Panel createAttributesPanel() {
00104         Panel panel = new Panel();
00105         panel.setLayout(new PaletteLayout(2, new Point(2,2), false));
00106         return panel;
00107     }
00108 
00113     protected void createAttributeChoices(Panel panel) {
00114         panel.add(new Label("Fill"));
00115         fFillColor = createColorChoice("FillColor");
00116         panel.add(fFillColor);
00117 
00118         //panel.add(new Label("Text"));
00119         //fTextColor = createColorChoice("TextColor");
00120         //panel.add(fTextColor);
00121 
00122         panel.add(new Label("Pen"));
00123         fFrameColor = createColorChoice("FrameColor");
00124         panel.add(fFrameColor);
00125 
00126         panel.add(new Label("Arrow"));
00127         CommandChoice choice = new CommandChoice();
00128         fArrowChoice = choice;
00129         choice.addItem(new ChangeAttributeCommand("none",     "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_NONE),  fView));
00130         choice.addItem(new ChangeAttributeCommand("at Start", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_START), fView));
00131         choice.addItem(new ChangeAttributeCommand("at End",   "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_END),   fView));
00132         choice.addItem(new ChangeAttributeCommand("at Both",  "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_BOTH),  fView));
00133         panel.add(fArrowChoice);
00134 
00135         panel.add(new Label("Font"));
00136         fFontChoice = createFontChoice();
00137         panel.add(fFontChoice);
00138     }
00139 
00143     protected Choice createColorChoice(String attribute) {
00144         CommandChoice choice = new CommandChoice();
00145        ColorMap map = ColorMap.getColorMap();
00146         for (int i=0; i < map.size(); i++)
00147             choice.addItem(
00148                 new ChangeAttributeCommand(
00149                     map.name(i), attribute,
00150                     map.color(i),
00151                     fView
00152                 )
00153             );
00154         return choice;
00155     }
00156 
00161     protected Choice createFontChoice() {
00162         CommandChoice choice = new CommandChoice();
00163         String fonts[] = Toolkit.getDefaultToolkit().getFontList();
00164         for (int i = 0; i < fonts.length; i++)
00165             choice.addItem(new ChangeAttributeCommand(fonts[i], "FontName", fonts[i],  fView));
00166         return choice;
00167     }
00168 
00172     protected Panel createButtonPanel() {
00173         Panel panel = new Panel();
00174         panel.setLayout(new PaletteLayout(2, new Point(2,2), false));
00175         return panel;
00176     }
00177 
00183     protected void createButtons(Panel panel) {
00184         panel.add(new Filler(24,20));
00185 
00186         Choice drawingChoice = new Choice();
00187         drawingChoice.addItem(fgUntitled);
00188 
00189            String param = getParameter("DRAWINGS");
00190            if (param == null)
00191                param = "";
00192               StringTokenizer st = new StringTokenizer(param);
00193         while (st.hasMoreTokens())
00194             drawingChoice.addItem(st.nextToken());
00195         // offer choice only if more than one
00196         if (drawingChoice.getItemCount() > 1)
00197             panel.add(drawingChoice);
00198         else
00199             panel.add(new Label(fgUntitled));
00200 
00201               drawingChoice.addItemListener(
00202                   new ItemListener() {
00203                       public void itemStateChanged(ItemEvent e) {
00204                           if (e.getStateChange() == ItemEvent.SELECTED) {
00205                               loadDrawing((String)e.getItem());
00206                           }
00207                       }
00208                   }
00209               );
00210 
00211         panel.add(new Filler(6,20));
00212 
00213         Button button;
00214         button = new CommandButton(new DeleteCommand("Delete", fView));
00215         panel.add(button);
00216 
00217         button = new CommandButton(new DuplicateCommand("Duplicate", fView));
00218         panel.add(button);
00219 
00220         button = new CommandButton(new GroupCommand("Group", fView));
00221         panel.add(button);
00222 
00223         button = new CommandButton(new UngroupCommand("Ungroup", fView));
00224         panel.add(button);
00225 
00226         button = new Button("Help");
00227               button.addActionListener(
00228                   new ActionListener() {
00229                       public void actionPerformed(ActionEvent event) {
00230                           showHelp();
00231                       }
00232                   }
00233               );
00234         panel.add(button);
00235 
00236         fUpdateButton = new Button("Simple Update");
00237               fUpdateButton.addActionListener(
00238                   new ActionListener() {
00239                       public void actionPerformed(ActionEvent event) {
00240                     if (fSimpleUpdate)
00241                         setBufferedDisplayUpdate();
00242                     else
00243                         setSimpleDisplayUpdate();
00244                       }
00245                   }
00246               );
00247 
00248         // panel.add(fUpdateButton); // not shown currently
00249     }
00250 
00254     protected Panel createToolPalette() {
00255         Panel palette = new Panel();
00256         palette.setLayout(new PaletteLayout(2,new Point(2,2)));
00257         return palette;
00258     }
00259 
00266     protected void createTools(Panel palette) {
00267         Tool tool = createSelectionTool();
00268 
00269         fDefaultToolButton = createToolButton(IMAGES+"SEL", "Selection Tool", tool);
00270         palette.add(fDefaultToolButton);
00271     }
00272 
00277     protected Tool createSelectionTool() {
00278         return new SelectionTool(view());
00279     }
00280 
00284     protected ToolButton createToolButton(String iconName, String toolName, Tool tool) {
00285         return new ToolButton(this, iconName, toolName, tool);
00286     }
00287 
00294     protected Drawing createDrawing() {
00295         return new StandardDrawing();
00296     }
00297 
00304     protected StandardDrawingView createDrawingView() {
00305         return new StandardDrawingView(this, 410, 370);
00306     }
00307 
00312     public void paletteUserSelected(PaletteButton button) {
00313         ToolButton toolButton = (ToolButton) button;
00314         setTool(toolButton.tool(), toolButton.name());
00315         setSelected(toolButton);
00316     }
00317 
00322     public void paletteUserOver(PaletteButton button, boolean inside) {
00323         if (inside)
00324             showStatus(((ToolButton) button).name());
00325         else
00326             showStatus(fSelectedToolButton.name());
00327     }
00328 
00333     public Drawing drawing() {
00334         return fDrawing;
00335     }
00336 
00341     public Tool tool() {
00342         return fTool;
00343     }
00344 
00349     public DrawingView view() {
00350         return fView;
00351     }
00352 
00357     public void toolDone() {
00358         setTool(fDefaultToolButton.tool(), fDefaultToolButton.name());
00359         setSelected(fDefaultToolButton);
00360     }
00361 
00367     public void selectionChanged(DrawingView view) {
00368         setupAttributes();
00369     }
00370 
00371     private void initDrawing() {
00372         fDrawing = createDrawing();
00373         fView.setDrawing(fDrawing);
00374         toolDone();
00375     }
00376 
00377     private void setTool(Tool t, String name) {
00378         if (fTool != null)
00379             fTool.deactivate();
00380         fTool = t;
00381         if (fTool != null) {
00382             showStatus(name);
00383             fTool.activate();
00384         }
00385     }
00386 
00387     private void setSelected(ToolButton button) {
00388         if (fSelectedToolButton != null)
00389             fSelectedToolButton.reset();
00390         fSelectedToolButton = button;
00391         if (fSelectedToolButton != null)
00392             fSelectedToolButton.select();
00393     }
00394 
00395     protected void loadDrawing(String param) {
00396         if (param == fgUntitled) {
00397             fDrawing.release();
00398             initDrawing();
00399             return;
00400         }
00401 
00402         String filename = getParameter(param);
00403         if (filename != null)
00404             readDrawing(filename);
00405     }
00406 
00407     private void readDrawing(String filename) {
00408         toolDone();
00409         String type = guessType(filename);
00410         if (type.equals("storable"))
00411             readFromStorableInput(filename);
00412         else if (type.equals("serialized"))
00413             readFromObjectInput(filename);
00414         else
00415             showStatus("Unknown file type");
00416     }
00417 
00418     private void readFromStorableInput(String filename) {
00419         try {
00420             URL url = new URL(getCodeBase(), filename);
00421             InputStream stream = url.openStream();
00422             StorableInput input = new StorableInput(stream);
00423             fDrawing.release();
00424 
00425             fDrawing = (Drawing)input.readStorable();
00426             fView.setDrawing(fDrawing);
00427         } catch (IOException e) {
00428             initDrawing();
00429             showStatus("Error:"+e);
00430         }
00431     }
00432 
00433     private void readFromObjectInput(String filename) {
00434         try {
00435             URL url = new URL(getCodeBase(), filename);
00436             InputStream stream = url.openStream();
00437             ObjectInput input = new ObjectInputStream(stream);
00438             fDrawing.release();
00439             fDrawing = (Drawing)input.readObject();
00440             fView.setDrawing(fDrawing);
00441         } catch (IOException e) {
00442             initDrawing();
00443             showStatus("Error: " + e);
00444         } catch (ClassNotFoundException e) {
00445             initDrawing();
00446             showStatus("Class not found: " + e);
00447         }
00448     }
00449 
00450     private String guessType(String file) {
00451         if (file.endsWith(".draw"))
00452             return "storable";
00453         if (file.endsWith(".ser"))
00454             return "serialized";
00455         return "unknown";
00456     }
00457 
00458     private void setupAttributes() {
00459         Color frameColor =
00460            (Color)AttributeFigure.getDefaultAttribute("FrameColor");
00461         Color fillColor =
00462            (Color)AttributeFigure.getDefaultAttribute("FillColor");
00463         Color textColor =
00464            (Color)AttributeFigure.getDefaultAttribute("TextColor");
00465         Integer arrowMode =
00466            (Integer)AttributeFigure.getDefaultAttribute("ArrowMode");
00467         String fontName =
00468            (String)AttributeFigure.getDefaultAttribute("FontName");
00469 
00470         FigureEnumeration k = fView.selectionElements();
00471         while (k.hasMoreElements()) {
00472             Figure f = k.nextFigure();
00473             frameColor = (Color) f.getAttribute("FrameColor");
00474             fillColor  = (Color) f.getAttribute("FillColor");
00475             textColor  = (Color) f.getAttribute("TextColor");
00476             arrowMode  = (Integer) f.getAttribute("ArrowMode");
00477             fontName   = (String) f.getAttribute("FontName");
00478         }
00479 
00480         fFrameColor.select(ColorMap.getColorMap().colorIndex(frameColor));
00481         fFillColor.select(ColorMap.getColorMap().colorIndex(fillColor));
00482         //fTextColor.select(ColorMap.colorIndex(textColor));
00483         if (arrowMode != null)
00484             fArrowChoice.select(arrowMode.intValue());
00485         if (fontName != null)
00486             fFontChoice.select(fontName);
00487     }
00488 
00489     protected void setSimpleDisplayUpdate() {
00490         fView.setDisplayUpdate(new SimpleUpdateStrategy());
00491         fUpdateButton.setLabel("Simple Update");
00492         fSimpleUpdate = true;
00493     }
00494 
00495     protected void setBufferedDisplayUpdate() {
00496         fView.setDisplayUpdate(new BufferedUpdateStrategy());
00497         fUpdateButton.setLabel("Buffered Update");
00498         fSimpleUpdate = false;
00499     }
00500 
00505     protected void showHelp() {
00506         try {
00507             String appletPath = getClass().getName().replace('.', '/');
00508             URL url = new URL(getCodeBase(), appletPath+"Help.html");
00509             getAppletContext().showDocument(url, "Help");
00510         } catch (IOException e) {
00511             showStatus("Help file not found");
00512         }
00513 
00514     }
00515 
00519     private void startSleeper() {
00520         if (fSleeper == null)
00521             fSleeper = new SleeperThread(this);
00522         fSleeper.start();
00523     }
00524 
00525     private void stopSleeper() {
00526         if (fSleeper != null)
00527             fSleeper.stop();
00528     }
00529 }
00530 
00531 
00532 class SleeperThread extends Thread {
00533 
00534     Applet  fApplet;
00535 
00536     SleeperThread(Applet applet) {
00537         fApplet = applet;
00538     }
00539 
00540     public void run() {
00541         try {
00542             for (;;) {
00543                 fApplet.showStatus("loading icons...");
00544                 sleep(50);
00545             }
00546         } catch (InterruptedException e) {
00547             return;
00548         }
00549     }
00550 
00551 }
00552