Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/modama/gui/mgraph/MApp.java


1   /**
2    Modama project, Institute for Polymer Research Dresden
3    Copyright (C) 2003 P. Fritsche, A. Uhlig
4    http://www.modama.org
5    info@modama.org
6   
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  
21   file created: May 10, 2003
22  
23   */
24  package org.modama.gui.mgraph;
25  
26  import com.jgraph.graph.DefaultGraphModel;
27  import com.jgraph.layout.*;
28  import com.sun.media.jai.codec.ImageCodec;
29  
30  import javax.swing.*;
31  
32  import org.modama.framework.entities.*;
33  import org.modama.framework.tools.Resources;
34  import org.modama.framework.operations.*;
35  import org.modama.gui.tools.*;
36  import org.modama.gui.mgraph.actions.*;
37  import org.modama.jaiutil.codecs.GQE.GQECodec;
38  import org.modama.jaiutil.codecs.XDR.XDRCodec;
39  import org.modama.common.version.ModamaVersionInfo;
40  import org.apache.log4j.BasicConfigurator;
41  
42  import java.awt.*;
43  import java.awt.event.KeyAdapter;
44  import java.awt.event.KeyEvent;
45  import java.util.Properties;
46  
47  /**
48   * The main application
49   * its contains some methods to gain access to the currently opened document and JGraph
50   * so its possible to implement additional functionality to enable MDI support
51   * the app registers to each JGraph its own MarqueeHandler, where mouse events are caught
52   * and processed in some different way.
53   *
54   *
55   */
56  public class MApp
57  {
58      /**
59       * Define all actions here
60       */
61      AbstractDefaultAction[] actions =
62              {
63                  new ShowEdit( this ),
64                  new ShowProperties( this ),
65                  new DeleteEntity( this ),
66                  new ClearConnections( this ),
67  
68  
69                  new GenerateOutput( this ),
70                  new ShowEditsForSelection( this ),
71                  new InsertThing( this, NumberEntity.class),
72                  new InsertThing( this, ImageEntity.class),
73                  new InsertThing( this, NumberList.class),
74                  new InsertThing( this, ImageList.class),
75                  new InsertThing( this, OpAdd.class),
76                  new InsertThing( this, OpNeg.class),
77                  new InsertThing( this, OpMul.class),
78                  new InsertThing( this, OpInv.class),
79                  new InsertThing( this, OpSlice.class),
80  
81                  new Load( this ),
82                  new Save( this ),
83                  new SaveWhatYouSee( this ),
84  
85                  new ShowLUTEditor( this ),
86                  new ShowImageEntityViewOptions( this ),
87                  new ShowImageListViewOptions( this ),
88                  new ExecuteOperation( this ),
89                  new Zoomfit( this ),
90                  new ToggleZoomCursor( this )
91              };
92  
93      /**
94       * the marquee handler
95       */
96      private MMarqueeHandler marqueeHandler = new MMarqueeHandler( this );
97      /**
98       * the current document
99       * TODO MDI: make this to an array. introduce new index to current doc
100      */
101     private MDocument currDoc;
102     /**
103      * if one executes a popupaction, the position where the popupmenu is located is lost
104      * so we have to save it to this var
105      */
106     private Point popupPosition;
107 
108     /**
109      * constructor
110      */
111     public MApp()
112     {
113         // TODO configure the log4j engine
114         BasicConfigurator.configure();
115         // setup the actions
116         ActionFactory.setupActions( actions );
117     }
118 
119     public JPopupMenu createPopupMenu( Point p, Object cell )
120     {
121         JPopupMenu menu;
122         popupPosition = new Point(p);
123         // if the popup occured somewhere in the jgraph window (not above a cell)
124         // we generate a popup menu for the document
125         if( cell==null ) cell = this.getCurrentDoc();
126         // creates the popupmenu
127         menu = MenuController.getPopupMenu( cell );
128         return menu;
129     }
130 
131     /**
132      * creates a new document
133      * @return the new doc
134      */
135     public MDocument createDoc()
136     {
137         // first make a new Graph
138         MGraph graph = new MGraph( new MGraphModel( this ) );
139         // set the own marquee handler
140         graph.setMarqueeHandler( getMarqueeHandler() );
141         // finally create the document
142         currDoc = new MDocument( graph );
143         return currDoc;
144     }
145 
146     /**
147      * getter for the marquee handler
148      * @return
149      */
150     public MMarqueeHandler getMarqueeHandler()
151     {
152         return marqueeHandler;
153     }
154 
155     /**
156      * getter for the current document
157      * @return
158      */
159     public MDocument getCurrentDoc()
160     {
161         return currDoc;
162     }
163 
164     /**
165      * getter for the current graph
166      * @return
167      */
168     public MGraph getCurrentGraph()
169     {
170         if( currDoc==null ) return null;
171         return currDoc.getGraph();
172     }
173 
174     /**
175      * getter for popuppos
176      * @return
177      */
178     public Point getPopupPosition()
179     {
180         return popupPosition;
181     }
182 
183     public static void main( String[] args )
184     {
185         //register codec
186         ImageCodec.registerCodec( new GQECodec() );
187         ImageCodec.registerCodec( new XDRCodec() );
188 
189         MApp app = new MApp();
190 
191 
192         JFrame frame = new JFrame(ModamaVersionInfo.getVersion().getFullName());
193         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
194         frame.getContentPane().setLayout( new BorderLayout());
195         frame.getContentPane().add( app.createDoc(), BorderLayout.CENTER );
196         frame.getContentPane().add( ActionFactory.createToolBar(app), BorderLayout.NORTH );
197 
198         app.getCurrentGraph().setPreferredSize(new Dimension(640,480));
199 
200 
201         LayoutAlgorithm alg = new SpringEmbeddedLayoutAlgorithm();
202 /*
203         AbstractEntity e1 = new NumberEntity();
204         AbstractEntity e2 = new NumberEntity();
205         AbstractOperation o1 = new OpAdd();
206         MGraphModel m = (MGraphModel)app.getCurrentGraph().getModel();
207         m.connect(e1,o1);
208         m.connect(e2,o1);
209         LayoutAlgorithm alg = new SpringEmbeddedLayoutAlgorithm();
210         alg.perform( app.getCurrentGraph(), true, new Properties() );
211 */
212 
213         //app.getCurrentDoc().getModel().insert(, null);
214         //app.getCurrentDoc().getModel().insert(, new Point(130,130));
215 
216         frame.pack();
217         frame.setVisible( true );
218 
219     }
220 }