Back to index

moin  1.9.0~rc2
DrawFrame.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001 Motorola - All rights reserved
00003  *
00004  * Portions Copyright 2000 by Peter Thoeny, Peter@Thoeny.com.
00005  * It is hereby granted that this software can be used, copied, 
00006  * modified, and distributed without fee provided that this 
00007  * copyright notice appears in all copies.
00008  * Portions Copyright (C) 2001 Motorola - All rights reserved
00009  */
00010 
00011 package CH.ifa.draw.appframe;
00012 
00013 import java.awt.*;
00014 import java.awt.event.*;
00015 import java.util.*;
00016 import java.io.*;
00017 import java.net.*;
00018 import CH.ifa.draw.framework.*;
00019 import CH.ifa.draw.standard.*;
00020 import CH.ifa.draw.figures.*;
00021 import CH.ifa.draw.util.*;
00022 
00030 public class DrawFrame extends Frame
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 Panel       fPanel;
00044 
00045     private Iconkit                   fIconkit;
00046 
00047     static String                     fgUntitled = "untitled";
00048 
00049     private static final String       fgDrawPath = "/CH/ifa/draw/";
00050     public static final String        IMAGES = fgDrawPath+"images/";
00051 
00052     private Application       fApplication;
00053 
00054     public DrawFrame(String title, Application application) {
00055        super(title);
00056 
00057        fApplication = application;
00058 
00059         fIconkit = new Iconkit(this);
00060 
00061        setLayout(new BorderLayout());
00062 
00063         fView = createDrawingView();
00064 
00065        MenuBar mb = new MenuBar();
00066         populateMenuBar(mb);
00067        setMenuBar(mb);
00068 
00069        Panel panel = createSouthPanel();
00070        if (panel != null) {
00071            populateSouthPanel(panel);
00072            add("South", panel);
00073        }
00074 
00075        panel = createWestPanel();
00076        if (panel != null) {
00077            populateWestPanel(panel);
00078            add("West", panel);
00079        }
00080 
00081        panel = createEastPanel();
00082        if (panel != null) {
00083            populateEastPanel(panel);
00084            add("East", panel);
00085        }
00086 
00087        panel = createNorthPanel();
00088        if (panel != null) {
00089            populateNorthPanel(panel);
00090            add("North", panel);
00091        }
00092 
00093        ScrollPane sp = new ScrollPane();
00094        sp.add(fView);
00095        add("Center", sp);
00096 
00097         initDrawing();
00098         setBufferedDisplayUpdate();
00099         //setupAttributes();
00100     }
00101 
00102     public Application getApplication() {
00103        return fApplication;
00104     }
00105 
00106     public void showStatus(String s) {
00107        fApplication.showStatus(s);
00108     }
00109 
00110     protected void populateMenuBar(MenuBar mb) {
00111     }
00112 
00116     protected Panel createSouthPanel() {
00117         Panel panel = new Panel();
00118         panel.setLayout(new PaletteLayout(2, new Point(2,2), false));
00119         return panel;
00120     }
00121 
00127     protected void populateSouthPanel(Panel panel) {
00128         panel.add(new Filler(24,20));
00129 
00130         Choice drawingChoice = new Choice();
00131         drawingChoice.addItem(fgUntitled);
00132 
00133        String param = fApplication.getParameter("DRAWINGS");
00134        if (param == null)
00135            param = "";
00136               StringTokenizer st = new StringTokenizer(param);
00137         while (st.hasMoreTokens())
00138             drawingChoice.addItem(st.nextToken());
00139         // offer choice only if more than one
00140         if (drawingChoice.getItemCount() > 1)
00141             panel.add(drawingChoice);
00142         else
00143             panel.add(new Label(fgUntitled));
00144 
00145        drawingChoice.addItemListener(
00146            new ItemListener() {
00147                   public void itemStateChanged(ItemEvent e) {
00148                      if (e.getStateChange() == ItemEvent.SELECTED) {
00149                          loadDrawing((String)e.getItem());
00150                      }
00151                   }
00152               }
00153            );
00154 
00155         panel.add(new Filler(6,20));
00156 
00157         Button button;
00158         button = new CommandButton(new DeleteCommand("Delete", fView));
00159         panel.add(button);
00160 
00161         button = new CommandButton(new DuplicateCommand("Duplicate", fView));
00162         panel.add(button);
00163 
00164         button = new CommandButton(new GroupCommand("Group", fView));
00165         panel.add(button);
00166 
00167         button = new CommandButton(new UngroupCommand("Ungroup", fView));
00168         panel.add(button);
00169 
00170         button = new Button("Help");
00171        button.addActionListener(
00172            new ActionListener() {
00173                   public void actionPerformed(ActionEvent event) {
00174                      showHelp();
00175                   }
00176               }
00177            );
00178         panel.add(button);
00179 
00180        fUpdateButton = new Button("Simple Update");
00181        fUpdateButton.addActionListener(
00182            new ActionListener() {
00183                   public void actionPerformed(ActionEvent event) {
00184                      if (fSimpleUpdate)
00185                          setBufferedDisplayUpdate();
00186                      else
00187                          setSimpleDisplayUpdate();
00188                   }
00189               }
00190            );
00191        
00192        panel.add(fUpdateButton);
00193     }
00194 
00198     protected CommandMenu createColorMenu(String attribute) {
00199         CommandMenu menu = new CommandMenu("Colour");
00200        ColorMap map = ColorMap.getColorMap();
00201         for (int i = 0; i < map.size(); i++)
00202             menu.add(
00203                 new ChangeAttributeCommand(
00204                     map.name(i), attribute,
00205                     map.color(i), fView));
00206         return menu;
00207     }
00208 
00213     protected CommandMenu createFontMenu() {
00214         CommandMenu menu = new CommandMenu("Font");
00215 
00223         String fonts[] = Toolkit.getDefaultToolkit().getFontList();
00224 
00225         for (int i = 0; i < fonts.length; i++)
00226             menu.add(
00227               new ChangeAttributeCommand(
00228                   fonts[i], "FontName", fonts[i],  fView));
00229         return menu;
00230     }
00231 
00232     protected Panel createEastPanel() {
00233        return null;
00234     }
00235 
00236     protected void populateEastPanel(Panel panel) {
00237     }
00238 
00239     protected Panel createNorthPanel() {
00240        return null;
00241     }
00242 
00243     protected void populateNorthPanel(Panel panel) {
00244     }
00245 
00249     protected Panel createWestPanel() {
00250         Panel palette = new Panel();
00251         palette.setLayout(new PaletteLayout(2,new Point(2,2)));
00252         return palette;
00253     }
00254 
00261     protected void populateWestPanel(Panel palette) {
00262         Tool tool = createSelectionTool();
00263 
00264         fDefaultToolButton = createToolButton(
00265            IMAGES+"SEL", "Selection Tool", tool);
00266         palette.add(fDefaultToolButton);
00267     }
00268 
00273     protected Tool createSelectionTool() {
00274         return new SelectionTool(fView);
00275     }
00276 
00280     protected ToolButton createToolButton(String iconName,
00281                                      String toolName, Tool tool) {
00282         return new ToolButton(this, iconName, toolName, tool);
00283     }
00284 
00291     protected Drawing createDrawing() {
00292         return new StandardDrawing();
00293     }
00294 
00301     protected StandardDrawingView createDrawingView() {
00302         return new StandardDrawingView(this, 410, 370);
00303     }
00304 
00309     public void paletteUserSelected(PaletteButton button) {
00310         ToolButton toolButton = (ToolButton) button;
00311         setTool(toolButton.tool(), toolButton.name());
00312         setSelected(toolButton);
00313     }
00314 
00319     public void paletteUserOver(PaletteButton button, boolean inside) {
00320         if (inside)
00321             showStatus(((ToolButton) button).name());
00322         else
00323             showStatus(fSelectedToolButton.name());
00324     }
00325 
00330     public Drawing drawing() {
00331         return fDrawing;
00332     }
00333 
00338     public Tool tool() {
00339         return fTool;
00340     }
00341 
00346     public DrawingView view() {
00347         return fView;
00348     }
00349 
00354     public void toolDone() {
00355         setTool(fDefaultToolButton.tool(), fDefaultToolButton.name());
00356         setSelected(fDefaultToolButton);
00357     }
00358 
00364     public void selectionChanged(DrawingView view) {
00365         //setupAttributes();
00366     }
00367 
00368     private void initDrawing() {
00369         fDrawing = createDrawing();
00370         fView.setDrawing(fDrawing);
00371         toolDone();
00372     }
00373 
00374     private void setTool(Tool t, String name) {
00375         if (fTool != null)
00376             fTool.deactivate();
00377         fTool = t;
00378         if (fTool != null) {
00379             showStatus(name);
00380             fTool.activate();
00381         }
00382     }
00383 
00384     private void setSelected(ToolButton button) {
00385         if (fSelectedToolButton != null)
00386             fSelectedToolButton.reset();
00387         fSelectedToolButton = button;
00388         if (fSelectedToolButton != null)
00389             fSelectedToolButton.select();
00390     }
00391 
00392     protected void loadDrawing(String param) {
00393         if (param == fgUntitled) {
00394             fDrawing.release();
00395             initDrawing();
00396             return;
00397         }
00398 
00399         String filename = fApplication.getParameter(param);
00400         if (filename != null) {
00401            readDrawing(filename);
00402        }
00403     }
00404 
00405     private void readDrawing(String filename) {
00406         toolDone();
00407         String type = guessType(filename);
00408         if (type.equals("storable"))
00409             readFromStorableInput(filename);
00410         else if (type.equals("serialized"))
00411             readFromObjectInput(filename);
00412         else
00413             showStatus("Unknown file type");
00414     }
00415 
00416     private void readFromStorableInput(String filename) {
00417         try {
00418             URL url = fApplication.getURL(filename);
00419             InputStream stream = url.openStream();
00420             StorableInput input = new StorableInput(stream);
00421             fDrawing.release();
00422 
00423             fDrawing = (Drawing)input.readStorable();
00424             fView.setDrawing(fDrawing);
00425         } catch (IOException e) {
00426             initDrawing();
00427             showStatus("Error reading " + filename + ": "+e);
00428            e.printStackTrace();
00429         }
00430     }
00431 
00432     private void readFromObjectInput(String filename) {
00433         try {
00434             URL url = fApplication.getURL(filename);
00435             InputStream stream = url.openStream();
00436             ObjectInput input = new ObjectInputStream(stream);
00437             fDrawing.release();
00438             fDrawing = (Drawing)input.readObject();
00439             fView.setDrawing(fDrawing);
00440         } catch (IOException e) {
00441             initDrawing();
00442             showStatus("Error reading (OI) " + filename + ": " + e);
00443            e.printStackTrace();
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     protected void setSimpleDisplayUpdate() {
00459         fView.setDisplayUpdate(new SimpleUpdateStrategy());
00460         fUpdateButton.setLabel("Simple Update");
00461         fSimpleUpdate = true;
00462     }
00463 
00464     protected void setBufferedDisplayUpdate() {
00465         fView.setDisplayUpdate(new BufferedUpdateStrategy());
00466         fUpdateButton.setLabel("Buffered Update");
00467         fSimpleUpdate = false;
00468     }
00469 
00474     protected void showHelp() {
00475         try {
00476             String helpPath = getClass().getName().replace('.', '/');
00477             URL url = fApplication.getURL(helpPath + "Help.html");
00478             fApplication.popupFrame(url, "Help");
00479         } catch (IOException e) {
00480             showStatus("Help file not found");
00481         }
00482     }
00483 }
00484