13a14,17 > import CH.ifa.draw.framework.*; > import CH.ifa.draw.standard.*; > import CH.ifa.draw.figures.*; > import CH.ifa.draw.util.*; 14a19 > import javax.swing.event.EventListenerList; 19,22d23 < import CH.ifa.draw.framework.*; < import CH.ifa.draw.standard.*; < import CH.ifa.draw.figures.*; < import CH.ifa.draw.util.*; 25,26c26,28 < * DrawApplication defines a standard presentation for standalone drawing < * editors. The presentation is customized in subclasses. --- > * DrawApplication defines a standard presentation for > * standalone drawing editors. The presentation is > * customized in subclasses. 39c41 < implements DrawingEditor, PaletteListener { --- > implements DrawingEditor, PaletteListener, VersionRequester { 41d42 < private Drawing fDrawing; 51d51 < private String fDrawingFilename; 53c53,55 < static String fgUntitled = "untitled"; --- > private UndoManager myUndoManager; > protected static String fgUntitled = "untitled"; > private final EventListenerList listenerList = new EventListenerList(); 57a60 > protected static int winCount = 0; 76d78 < 89a92 > winCount++; 106a110,112 > if (view() == null) { > return; > } 108,110c114,120 < window.open(); < window.setDrawing(drawing()); < window.setDrawingTitle(getDrawingTitle() + " (View)"); --- > window.open(view()); > if (view().drawing().getTitle() != null ) { > window.setDrawingTitle(view().drawing().getTitle() + " (View)"); > } > else { > window.setDrawingTitle(getDefaultDrawingTitle() + " (View)"); > } 114,115c124,125 < * Open a new window for this application containing < * an new (empty) drawing. --- > * Open a new window for this application containing the passed in drawing, > * or a new drawing if the passed in drawing is null. 117c127 < public void newWindow() { --- > public void newWindow(Drawing initialDrawing) { 118a129 > if (initialDrawing == null) { 120a132,135 > else { > window.open(createDrawingView(initialDrawing)); > } > } 123,124c138 < * Opens the window and initializes its contents. < * Clients usually only call but don't override it. --- > * Opens a new window 126a141,150 > open(NullDrawingView.getManagedDrawingView(this)); > // open(createDrawingView()); > } > > /** > * Opens a new window with a drawing view. > */ > protected void open(DrawingView newDrawingView) { > getVersionControlStrategy().assertCompatibleVersion(); > setUndoManager(new UndoManager()); 129,130c153,155 < fView = createDrawingView(); < JComponent contents = createContents((StandardDrawingView)view()); --- > > setView(newDrawingView); > JComponent contents = createContents(view()); 136d160 < 152,153d175 < initDrawing(); < 163a186,187 > > toolDone(); 179,185d202 < protected void initDrawing() { < setDrawing(createDrawing()); < setDrawingTitle(fgUntitled); < view().setDrawing(drawing()); < toolDone(); < } < 209,213c226,228 < JMenu menu = new JMenu("File"); < JMenuItem mi = new JMenuItem("New", new MenuShortcut('n').getKey()); < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { --- > CommandMenu menu = new CommandMenu("File"); > Command cmd = new AbstractCommand("New", this, false) { > public void execute() { 216,218c231,232 < } < ); < menu.add(mi); --- > }; > menu.add(cmd, new MenuShortcut('n')); 220,223c234,235 < mi = new JMenuItem("Open...", new MenuShortcut('o').getKey()); < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { --- > cmd = new AbstractCommand("Open...", this, false) { > public void execute() { 226,228c238,239 < } < ); < menu.add(mi); --- > }; > menu.add(cmd, new MenuShortcut('o')); 230,233c241,242 < mi = new JMenuItem("Save As...", new MenuShortcut('s').getKey()); < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { --- > cmd = new AbstractCommand("Save As...", this, true) { > public void execute() { 236,239c245,246 < } < ); < menu.add(mi); < --- > }; > menu.add(cmd, new MenuShortcut('s')); 241,244c248,250 < mi = new JMenuItem("Print...", new MenuShortcut('p').getKey()); < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { --- > > cmd = new AbstractCommand("Print...", this, true) { > public void execute() { 247,249c253,254 < } < ); < menu.add(mi); --- > }; > menu.add(cmd, new MenuShortcut('p')); 251,254c256,258 < mi = new JMenuItem("Exit"); < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { --- > > cmd = new AbstractCommand("Exit", this, true) { > public void execute() { 257,259c261,262 < } < ); < menu.add(mi); --- > }; > menu.add(cmd); 270c273 < new SelectAllCommand("Select All", view())), new MenuShortcut('a')); --- > new SelectAllCommand("Select All", this)), new MenuShortcut('a')); 273,274c276,277 < new CutCommand("Cut", view())), new MenuShortcut('x')); < menu.add(new CopyCommand("Copy", view()), new MenuShortcut('c')); --- > new CutCommand("Cut", this)), new MenuShortcut('x')); > menu.add(new CopyCommand("Copy", this), new MenuShortcut('c')); 276c279 < new PasteCommand("Paste", view())), new MenuShortcut('v')); --- > new PasteCommand("Paste", this)), new MenuShortcut('v')); 279,280c282,283 < new DuplicateCommand("Duplicate", view())), new MenuShortcut('d')); < menu.add(new UndoableCommand(new DeleteCommand("Delete", view()))); --- > new DuplicateCommand("Duplicate", this)), new MenuShortcut('d')); > menu.add(new UndoableCommand(new DeleteCommand("Delete", this))); 282,283c285,286 < menu.add(new UndoableCommand(new GroupCommand("Group", view()))); < menu.add(new UndoableCommand(new UngroupCommand("Ungroup", view()))); --- > menu.add(new UndoableCommand(new GroupCommand("Group", this))); > menu.add(new UndoableCommand(new UngroupCommand("Ungroup", this))); 285,286c288,289 < menu.add(new UndoableCommand(new SendToBackCommand("Send to Back", view()))); < menu.add(new UndoableCommand(new BringToFrontCommand("Bring to Front", view()))); --- > menu.add(new UndoableCommand(new SendToBackCommand("Send to Back", this))); > menu.add(new UndoableCommand(new BringToFrontCommand("Bring to Front", this))); 288,289c291,292 < menu.add(new UndoCommand("Undo Command", view())); < menu.add(new RedoCommand("Redo Command", view())); --- > menu.add(new UndoCommand("Undo Command", this)); > menu.add(new RedoCommand("Redo Command", this)); 299c302 < menu.addCheckItem(new ToggleGridCommand("Toggle Snap to Grid", view(), new Point(4,4))); --- > menu.addCheckItem(new ToggleGridCommand("Toggle Snap to Grid", this, new Point(4,4))); 302c305 < new AlignCommand(AlignCommand.Alignment.LEFTS, view()))); --- > new AlignCommand(AlignCommand.Alignment.LEFTS, this))); 304c307 < new AlignCommand(AlignCommand.Alignment.CENTERS, view()))); --- > new AlignCommand(AlignCommand.Alignment.CENTERS, this))); 306c309 < new AlignCommand(AlignCommand.Alignment.RIGHTS, view()))); --- > new AlignCommand(AlignCommand.Alignment.RIGHTS, this))); 309c312 < new AlignCommand(AlignCommand.Alignment.TOPS, view()))); --- > new AlignCommand(AlignCommand.Alignment.TOPS, this))); 311c314 < new AlignCommand(AlignCommand.Alignment.MIDDLES, view()))); --- > new AlignCommand(AlignCommand.Alignment.MIDDLES, this))); 313c316 < new AlignCommand(AlignCommand.Alignment.BOTTOMS, view()))); --- > new AlignCommand(AlignCommand.Alignment.BOTTOMS, this))); 322c325 < JMenu menu = new JMenu("Debug"); --- > CommandMenu menu = new CommandMenu("Debug"); 324,329c327,329 < JMenuItem mi = new JMenuItem("Simple Update"); < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { < view().setDisplayUpdate(new SimpleUpdateStrategy()); < } --- > Command cmd = new AbstractCommand("Simple Update", this) { > public void executable() { > this.view().setDisplayUpdate(new SimpleUpdateStrategy()); 331,332c331,332 < ); < menu.add(mi); --- > }; > menu.add(cmd); 334,339c334,336 < mi = new JMenuItem("Buffered Update"); < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { < view().setDisplayUpdate(new BufferedUpdateStrategy()); < } --- > cmd = new AbstractCommand("Buffered Update", this) { > public void executable() { > this.view().setDisplayUpdate(new BufferedUpdateStrategy()); 341,342c338,339 < ); < menu.add(mi); --- > }; > menu.add(cmd); 375c372 < view() --- > this 388c385 < new ChangeAttributeCommand("none", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_NONE), view()))); --- > new ChangeAttributeCommand("none", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_NONE), this))); 390c387 < new ChangeAttributeCommand("at Start", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_START), view()))); --- > new ChangeAttributeCommand("at Start", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_START), this))); 392c389 < new ChangeAttributeCommand("at End", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_END), view()))); --- > new ChangeAttributeCommand("at End", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_END), this))); 394c391 < new ChangeAttributeCommand("at Both", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_BOTH), view()))); --- > new ChangeAttributeCommand("at Both", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_BOTH), this))); 405c402 < for (int i = 0; i < fonts.length; i++) --- > for (int i = 0; i < fonts.length; i++) { 407c404,405 < new ChangeAttributeCommand(fonts[i], "FontName", fonts[i], view()))); --- > new ChangeAttributeCommand(fonts[i], "FontName", fonts[i], this))); > } 417c415 < new ChangeAttributeCommand("Plain", "FontStyle", new Integer(Font.PLAIN), view()))); --- > new ChangeAttributeCommand("Plain", "FontStyle", new Integer(Font.PLAIN), this))); 419c417 < new ChangeAttributeCommand("Italic","FontStyle", new Integer(Font.ITALIC),view()))); --- > new ChangeAttributeCommand("Italic","FontStyle", new Integer(Font.ITALIC), this))); 421c419 < new ChangeAttributeCommand("Bold", "FontStyle", new Integer(Font.BOLD), view()))); --- > new ChangeAttributeCommand("Bold", "FontStyle", new Integer(Font.BOLD), this))); 437c435,437 < new Integer(sizes[i]), view()) --- > new Integer(sizes[i]), > this > ) 448c448 < JMenu menu = new JMenu("Look'n'Feel"); --- > CommandMenu menu = new CommandMenu("Look'n'Feel"); 451d450 < JMenuItem mi = null; 454d452 < mi = new JMenuItem(lafs[i].getName()); 456,458c454,455 < mi.addActionListener( < new ActionListener() { < public void actionPerformed(ActionEvent event) { --- > Command cmd = new AbstractCommand(lafs[i].getName(), this) { > public void execute() { 461,463c458,459 < } < ); < menu.add(mi); --- > }; > menu.add(cmd); 496c492 < return new SelectionTool(view()); --- > return new SelectionTool(this); 512a509,514 > DrawingView createdDrawingView = createDrawingView(createDrawing()); > createdDrawingView.drawing().setTitle(getDefaultDrawingTitle()); > return createdDrawingView; > } > > protected DrawingView createDrawingView(Drawing newDrawing) { 514c516,519 < return new StandardDrawingView(this, d.width, d.height); --- > DrawingView newDrawingView = new StandardDrawingView(this, d.width, d.height); > newDrawingView.setDrawing(newDrawing); > fireViewCreatedEvent(newDrawingView); > return newDrawingView; 540c545,546 < JScrollPane sp = new JScrollPane((StandardDrawingView)view); --- > if (view instanceof Component) { > JScrollPane sp = new JScrollPane((Component)view); 545a552,555 > else { > return new JPanel(); > } > } 562a573 > * Should we through IllegalArguementException if it is? 564c575 < private void setStorageFormatManager(StorageFormatManager storageFormatManager) { --- > protected final void setStorageFormatManager(StorageFormatManager storageFormatManager) { 577,584d587 < * Sets the drawing to be edited. < */ < public void setDrawing(Drawing drawing) { < view().setDrawing(drawing); < fDrawing = drawing; < } < < /** 626,633d628 < * Gets the current drawing. < * @see DrawingEditor < */ < public Drawing drawing() { < return fDrawing; < } < < /** 641a637 > * Retrieve the active view from the window 648a645,654 > protected void setView(DrawingView newView) { > DrawingView oldView = fView; > fView = newView; > fireViewSelectionChangedEvent(oldView, view()); > } > > public DrawingView[] views() { > return new DrawingView[] { view() }; > } > 661,662c667,670 < * Handles a change of the current selection. Updates all < * menu items that are selection sensitive. --- > * Fired by a view when the figure seleciton changes. Since Commands and > * Tools are Actions and they are registered to hear these events, they will > * handle themselves. So selection sensitive menuitems will update their > * own states. 690a699,750 > * Register to hear when the active view is changed. For Single document > * interface, this will happen when a new drawing is created. > */ > public void addViewChangeListener(ViewChangeListener vsl) { > listenerList.add(ViewChangeListener.class, vsl); > } > > /** > * Remove listener > */ > public void removeViewChangeListener(ViewChangeListener vsl) { > listenerList.remove(ViewChangeListener.class, vsl); > } > > /** > * Convience method for switching views. This is uneeded in SDI > * environments. > */ > protected void fireViewSelectionChangedEvent(DrawingView oldView, DrawingView newView) { > final Object[] listeners = listenerList.getListenerList(); > ViewChangeListener vsl = null; > for (int i = listeners.length-2; i>=0 ; i-=2) { > if (listeners[i] == ViewChangeListener.class) { > vsl = (ViewChangeListener)listeners[i+1]; > vsl.viewSelectionChanged(oldView, newView); > } > } > } > > protected void fireViewCreatedEvent(DrawingView view) { > final Object[] listeners = listenerList.getListenerList(); > ViewChangeListener vsl = null; > for (int i = listeners.length-2; i>=0 ; i-=2) { > if (listeners[i] == ViewChangeListener.class) { > vsl = (ViewChangeListener)listeners[i+1]; > vsl.viewCreated(view); > } > } > } > > protected void fireViewDestroyingEvent(DrawingView view) { > final Object[] listeners = listenerList.getListenerList(); > ViewChangeListener vsl = null; > for (int i = listeners.length-2; i>=0 ; i-=2) { > if (listeners[i] == ViewChangeListener.class) { > vsl = (ViewChangeListener)listeners[i+1]; > vsl.viewDestroying( view ); > } > } > } > > /** 699c759,760 < if (tool() != null) --- > // We should not really deactivate a tool which was never activated. > if (tool() != null) { 700a762 > } 724a787,788 > winCount--; > if (winCount == 0) { 726a791 > } 740c805,806 < initDrawing(); --- > view().setDrawing(createDrawing()); > view().drawing().setTitle(getDefaultDrawingTitle()); 822a889,892 > // Need a better alert than this. > if(view() == null) { > return; > } 824c894,896 < setDrawingTitle(storeFormat.store(file, drawing())); --- > String name = storeFormat.store(file, view().drawing()); > view().drawing().setTitle(name); > setDrawingTitle(name); 838,840c910,911 < newWindow(); < setDrawing(restoredDrawing); < setDrawingTitle(file); --- > restoredDrawing.setTitle(file); > newWindow(restoredDrawing); 867,868c938 < fDrawingFilename = drawingTitle; < if (fgUntitled.equals(drawingTitle)) { --- > if (getDefaultDrawingTitle().equals(drawingTitle)) { 880c950 < return fDrawingFilename; --- > return view().drawing().getTitle(); 895a966,996 > > protected void setUndoManager(UndoManager newUndoManager) { > myUndoManager = newUndoManager; > } > > public UndoManager getUndoManager() { > return myUndoManager; > } > > protected VersionControlStrategy getVersionControlStrategy() { > return new StandardVersionControlStrategy(this); > } > > /** > * Subclasses should override this method to specify to which versions of > * JHotDraw they are compatible. A string array is returned so it is possible > * to specify several version numbers of JHotDraw to which the application > * is compatible with. > * > * @return all versions number of JHotDraw the application is compatible with. > */ > public String[] getRequiredVersions() { > String[] requiredVersions = new String[1]; > // return the version of the package we are in > requiredVersions[0] = VersionManagement.getPackageVersion(DrawApplication.class.getPackage()); > return requiredVersions; > } > > protected String getDefaultDrawingTitle() { > return fgUntitled; > }