1+ import java .io .*;
2+ import java .awt .*;
3+ import java .awt .Graphics ;
4+ import java .awt .Color ;
5+ import java .awt .BorderLayout ;
6+ import java .awt .GridLayout ;
7+ import java .awt .Insets ;
8+ import java .awt .Dimension ;
9+ import java .awt .event .ActionEvent ;
10+ import java .awt .event .*;
11+ import java .util .HashMap ;
12+ import java .util .Vector ;
13+
14+ import javax .swing .*;
15+ import javax .swing .JApplet ;
16+ import javax .swing .text .*;
17+ import javax .swing .event .*;
18+
19+ /* K. Lano 2010-2022
20+
21+ Adapted from Oracle example of JTextPane
22+
23+ * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
24+ *
25+ * Redistribution and use in source and binary forms, with or without
26+ * modification, are permitted provided that the following conditions
27+ * are met:
28+ *
29+ * - Redistributions of source code must retain the above copyright
30+ * notice, this list of conditions and the following disclaimer.
31+ *
32+ * - Redistributions in binary form must reproduce the above copyright
33+ * notice, this list of conditions and the following disclaimer in the
34+ * documentation and/or other materials provided with the distribution.
35+ *
36+ * - Neither the name of Oracle or the names of its
37+ * contributors may be used to endorse or promote products derived
38+ * from this software without specific prior written permission.
39+ *
40+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
41+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
42+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51+
52+ Package: GUI
53+ */
54+
55+ public class AgileUMLApp extends JApplet implements DocumentListener
56+ {
57+ JTextPane textPane ;
58+ AbstractDocument doc ;
59+ static final int MAX_CHARACTERS = 1000 ;
60+ JTextArea messageArea ;
61+ String newline = "\n " ;
62+ HashMap actions ;
63+
64+ String systemName = "app" ;
65+ private JLabel thisLabel ;
66+
67+ public void init ()
68+ {
69+ // types = new Vector();
70+
71+ textPane = new JTextPane ();
72+ textPane .setCaretPosition (0 );
73+ textPane .setMargin (new Insets (5 ,5 ,5 ,5 ));
74+ StyledDocument styledDoc = textPane .getStyledDocument ();
75+ if (styledDoc instanceof AbstractDocument )
76+ { doc = (AbstractDocument ) styledDoc ;
77+ doc .addDocumentListener (this );
78+ }
79+ else
80+ {
81+ System .err .println ("Error: invalid document" );
82+ }
83+
84+ SimpleAttributeSet [] attrs = initAttributes (4 );
85+
86+ try {
87+ doc .insertString (0 , "(expression (primary ident))" , attrs [1 ]);
88+ }
89+ catch (BadLocationException ble ) {
90+ System .err .println ("Couldn't insert code text." );
91+ }
92+
93+ JScrollPane scrollPane = new JScrollPane (textPane );
94+ scrollPane .setPreferredSize (new Dimension (350 , 500 ));
95+
96+ messageArea = new JTextArea (350 , 200 );
97+ messageArea .setEditable (false );
98+ JScrollPane scrollPaneForLog = new JScrollPane (messageArea );
99+
100+ JSplitPane splitPane = new JSplitPane (
101+ JSplitPane .VERTICAL_SPLIT ,
102+ scrollPane , scrollPaneForLog );
103+ splitPane .setOneTouchExpandable (true );
104+ splitPane .setDividerLocation (200 );
105+
106+ // JPanel statusPane = new JPanel(new GridLayout(2, 1));
107+
108+ getContentPane ().add (splitPane , BorderLayout .CENTER );
109+ // getContentPane().add(statusPane, BorderLayout.EAST);
110+ thisLabel =
111+ new JLabel ("Type & click within the framed area." );
112+ getContentPane ().add (thisLabel , BorderLayout .SOUTH );
113+
114+ actions = createActionTable (textPane );
115+ JMenu editMenu = createEditMenu ();
116+ JMenu absMenu = createAbstractionMenu ();
117+ JMenu transMenu = createTranslationMenu ();
118+ // JMenu styleMenu = createStyleMenu();
119+ JMenuBar mb = new JMenuBar ();
120+ mb .add (editMenu );
121+ mb .add (absMenu );
122+ mb .add (transMenu );
123+ // mb.add(styleMenu);
124+ setJMenuBar (mb );
125+
126+ textPane .setCaretPosition (0 );
127+
128+ // pack();
129+ setVisible (true );
130+ }
131+
132+ public void paint (Graphics g )
133+ { setVisible (true ); }
134+
135+ private javax .swing .Action getActionByName (String name )
136+ { return (javax .swing .Action ) actions .get (name ); }
137+
138+ protected JMenu createEditMenu ()
139+ { JMenu menu = new JMenu ("Edit" );
140+
141+ javax .swing .Action cAction = getActionByName (DefaultEditorKit .cutAction );
142+ // cAction.setValue(Action.NAME,"Cut");
143+ JMenuItem cutMI = menu .add (cAction );
144+ cutMI .setLabel ("Cut" );
145+
146+ javax .swing .Action cpyAction = getActionByName (DefaultEditorKit .copyAction );
147+ JMenuItem copyMI = menu .add (cpyAction );
148+ copyMI .setLabel ("Copy" );
149+
150+ javax .swing .Action pteAction = getActionByName (DefaultEditorKit .pasteAction );
151+ JMenuItem pasteMI = menu .add (pteAction );
152+ pasteMI .setLabel ("Paste" );
153+
154+ return menu ;
155+ }
156+
157+ protected JMenu createAbstractionMenu ()
158+ { JMenu menu = new JMenu ("Abstraction" );
159+
160+ javax .swing .Action fromCAction = new AbstractFromCAction ();
161+ // checkAction.setMnemonic(KeyEvent.VK_K);
162+ menu .add (fromCAction );
163+
164+ // javax.swing.Action fromJavaAction = new AbstractFromJavaAction();
165+ // checkAction.setMnemonic(KeyEvent.VK_K);
166+ // menu.add(fromJavaAction);
167+
168+ return menu ;
169+ }
170+
171+ protected JMenu createTranslationMenu ()
172+ { JMenu menu = new JMenu ("Translate" );
173+
174+ javax .swing .Action toJavaAction = new Translate2JavaAction ();
175+ // checkAction.setMnemonic(KeyEvent.VK_K);
176+ menu .add (toJavaAction );
177+
178+ javax .swing .Action toCSAction = new Translate2CSAction ();
179+ // checkAction.setMnemonic(KeyEvent.VK_K);
180+ menu .add (toCSAction );
181+
182+ return menu ;
183+ }
184+
185+
186+ public void changedUpdate (DocumentEvent e )
187+ { int offset = e .getOffset ();
188+ int pos = textPane .getCaretPosition ();
189+ System .out .println (">> Update event at: " + offset + " " + pos );
190+ }
191+
192+ public void removeUpdate (DocumentEvent e )
193+ { int offset = e .getOffset ();
194+ int pos = textPane .getCaretPosition ();
195+ System .out .println (">> Remove event at: " + offset + " " + pos );
196+ }
197+
198+ public void insertUpdate (DocumentEvent e )
199+ { int offset = e .getOffset ();
200+ int pos = textPane .getCaretPosition ();
201+ try {
202+ System .out .println (">> Insert event at: " + offset + " " + pos + " " + textPane .getText (pos ,1 ));
203+ } catch (Exception _e ) { }
204+ }
205+
206+ private HashMap createActionTable (JTextComponent textComponent )
207+ { HashMap actions = new HashMap ();
208+ javax .swing .Action [] actionsArray = textComponent .getActions ();
209+ for (int i = 0 ; i < actionsArray .length ; i ++)
210+ { javax .swing .Action a = actionsArray [i ];
211+ actions .put (a .getValue (javax .swing .Action .NAME ), a );
212+ }
213+ return actions ;
214+ }
215+
216+ protected SimpleAttributeSet [] initAttributes (int length )
217+ {
218+ SimpleAttributeSet [] attrs = new SimpleAttributeSet [length ];
219+
220+ attrs [0 ] = new SimpleAttributeSet ();
221+ StyleConstants .setFontFamily (attrs [0 ], "SansSerif" );
222+ StyleConstants .setFontSize (attrs [0 ], 16 );
223+
224+ attrs [1 ] = new SimpleAttributeSet (attrs [0 ]);
225+ StyleConstants .setBold (attrs [1 ], true );
226+
227+ attrs [2 ] = new SimpleAttributeSet (attrs [0 ]);
228+ StyleConstants .setItalic (attrs [2 ], true );
229+
230+ attrs [3 ] = new SimpleAttributeSet (attrs [0 ]);
231+ StyleConstants .setFontSize (attrs [3 ], 20 );
232+
233+ return attrs ;
234+ }
235+
236+
237+ class AbstractFromCAction extends javax .swing .AbstractAction
238+ { public AbstractFromCAction ()
239+ { super ("Abstract from C" ); }
240+
241+ public void actionPerformed (ActionEvent e )
242+ { int pos = doc .getLength ();
243+ // System.out.println("Document length = " + pos);
244+ int pos2 = textPane .getCaretPosition ();
245+ // System.out.println("Caret position = " + pos2);
246+ if (pos2 > pos )
247+ { pos = pos2 ; }
248+ if (pos == 0 ) { return ; }
249+ String txt = "" ;
250+ try
251+ { txt = textPane .getText (0 , pos +1 ); }
252+ catch (Exception _e ) { return ; }
253+
254+ Compiler2 comp = new Compiler2 ();
255+ ASTTerm xx = comp .parseGeneralAST (txt );
256+ java .util .Map m1 = new java .util .HashMap ();
257+ java .util .Map m2 = new java .util .HashMap ();
258+ Vector v1 = new Vector ();
259+ Vector v2 = new Vector ();
260+ // v1.addAll(types);
261+ // v2.addAll(entities);
262+
263+ if (xx == null )
264+ { System .err .println ("!! Invalid C AST" );
265+ return ;
266+ }
267+
268+ if (xx instanceof ASTCompositeTerm ) { }
269+ else
270+ { System .err .println ("!! Invalid C AST: " + xx );
271+ return ;
272+ }
273+
274+
275+ ((ASTCompositeTerm ) xx ).identifyCFunctions (null ,m1 ,m2 ,v1 ,v2 );
276+
277+ Vector mxs =
278+ ((ASTCompositeTerm ) xx ).cprogramToKM3 (null ,m1 ,m2 ,v1 ,v2 );
279+
280+ for (int i = 0 ; i < v1 .size (); i ++)
281+ { Type tt = (Type ) v1 .get (i );
282+ System .out .println (tt .getKM3 ());
283+ }
284+
285+ for (int i = 0 ; i < v2 .size (); i ++)
286+ { Entity newent = (Entity ) v2 .get (i );
287+ if (newent .isInterface () ||
288+ newent .hasConstructor ())
289+ { }
290+ else if (newent .isStruct ())
291+ { newent .addStaticConstructor (); }
292+ else
293+ { newent .addDefaultConstructor (); }
294+ }
295+
296+ for (int i = 0 ; i < v2 .size (); i ++)
297+ { Entity ent = (Entity ) v2 .get (i );
298+ System .out .println (ent .getKM3 ());
299+ }
300+ }
301+ }
302+
303+ class Translate2JavaAction extends javax .swing .AbstractAction
304+ { public Translate2JavaAction ()
305+ { super ("Translate to Java" ); }
306+
307+ public void actionPerformed (ActionEvent e )
308+ { int pos = doc .getLength ();
309+ // System.out.println("Document length = " + pos);
310+ int pos2 = textPane .getCaretPosition ();
311+ // System.out.println("Caret position = " + pos2);
312+ if (pos2 > pos )
313+ { pos = pos2 ; }
314+ if (pos == 0 ) { return ; }
315+ String txt = "" ;
316+ try
317+ { txt = textPane .getText (0 , pos +1 ); }
318+ catch (Exception _e ) { return ; }
319+
320+ Compiler2 comp = new Compiler2 ();
321+ ASTTerm trm = comp .parseGeneralAST (txt );
322+ System .out .println (trm );
323+ messageArea .append ("" + trm );
324+ }
325+ }
326+
327+
328+ class Translate2CSAction extends javax .swing .AbstractAction
329+ { public Translate2CSAction ()
330+ { super ("Translate to C#" ); }
331+
332+ public void actionPerformed (ActionEvent e )
333+ { int pos = doc .getLength ();
334+ // System.out.println("Document length = " + pos);
335+ int pos2 = textPane .getCaretPosition ();
336+ // System.out.println("Caret position = " + pos2);
337+ if (pos2 > pos )
338+ { pos = pos2 ; }
339+ if (pos == 0 ) { return ; }
340+ String txt = "" ;
341+ try
342+ { txt = textPane .getText (0 , pos +1 ); }
343+ catch (Exception _e ) { return ; }
344+
345+ Compiler2 comp = new Compiler2 ();
346+ ASTTerm trm = comp .parseGeneralAST (txt );
347+ System .out .println (trm );
348+ messageArea .append ("" + trm );
349+ }
350+ }
351+ }
0 commit comments