@@ -4,99 +4,145 @@ import scala.swing._
4
4
5
5
import scala .swing .event .ButtonClicked
6
6
7
- import javax . swing .{ JFrame , JScrollPane , UIManager }
7
+ import java . awt .{ Color , Toolkit }
8
8
9
- import java . awt . Dimension
9
+ import javax . swing .{ JFrame , JScrollPane , UIManager }
10
10
11
11
import javax .swing .filechooser .FileNameExtensionFilter
12
12
13
13
import java .io .{ File , IOException }
14
14
15
- import org .jgrapht .ext .JGraphModelAdapter
16
-
17
- import org .jgrapht .graph .DefaultEdge
18
-
19
- import org .jgraph .JGraph
20
-
21
15
22
16
/**
23
17
* Read XML files and generate the graph
24
18
*/
25
- object App extends SimpleGUIApplication {
26
-
27
- final val WIDTH = 1600
28
-
29
- final val HEIGHT = 1024
19
+ object App extends SimpleSwingApplication {
20
+
21
+ // val reader = new StrutsXmlReader
22
+ val reader = new StrutsReader
30
23
31
- val reader = new StrutsXmlReader
32
-
33
- val graphCreator = new GraphCreator
24
+ // val graphCreator = new GraphCreator
25
+ val jung = new JUNGFacade
26
+
27
+ val screenSize = Toolkit .getDefaultToolkit().getScreenSize()
34
28
35
29
var currentFiles : Seq [File ] = null
36
-
37
- val mainPanel = new BoxPanel (Orientation .Vertical ) {
30
+
31
+ val buttonPanel = new BoxPanel (Orientation .Horizontal ) {
32
+
33
+ border = Swing .EmptyBorder (10 ,10 ,10 ,10 )
34
+
38
35
val openFilesButton = new Button {
39
36
text = " Open file(s)"
40
37
}
38
+ val saveGraphAsButton = new Button {
39
+ text = " Save graph as ..."
40
+ visible = false
41
+ }
42
+
41
43
contents += openFilesButton
44
+ contents += saveGraphAsButton
42
45
43
46
listenTo(openFilesButton)
47
+ listenTo(saveGraphAsButton)
48
+
49
+ reactions += {
50
+ case ButtonClicked (b) =>
51
+ if (b == openFilesButton) openFile
52
+ if (b == saveGraphAsButton) saveGraphAsFile
53
+ }
54
+ }
55
+
56
+ val infoPanel = new BoxPanel (Orientation .Vertical ) {
57
+
58
+ contents += new Label (" Interactive modes:" )
59
+ contents += new TextArea {
60
+ background = Color .lightGray
61
+ text = " [p] - picking mode\n " +
62
+ " * drag and drop vertices\n " +
63
+ " * press [Shift] to select multiple vertices\n " +
64
+ " * press [CTRL] to center a specific vertex\n\n " +
65
+ " [t] - transforming mode\n " +
66
+ " * move graph around\n " +
67
+ " * scroll to zoom in and out\n " +
68
+ " * press [Shift] or [CTRL] to transform graph"
69
+ }
70
+ }
71
+
72
+ val drawPanel = new BoxPanel (Orientation .Vertical ) {
73
+
74
+ background = Color .white
75
+
76
+ def draw (vertices : Map [String , VertexRepresentator ], edges : Map [Int , EdgeRepresentator ]) = {
77
+ contents += Component .wrap(jung.buildGraph(vertices, edges).prepareLayout.formDesign.activateControllers.plot)
78
+ }
79
+
80
+ }
81
+
82
+ def top = new MainFrame {
83
+ title = " Struts Actions and Forwards"
44
84
45
- def getFileChooser = {
85
+ UIManager setLookAndFeel UIManager .getSystemLookAndFeelClassName
86
+
87
+ JFrame setDefaultLookAndFeelDecorated true
88
+
89
+ minimumSize = screenSize
90
+
91
+ contents = new BorderPanel {
92
+ layout += buttonPanel -> BorderPanel .Position .North
93
+ layout += drawPanel -> BorderPanel .Position .Center
94
+ layout += infoPanel -> BorderPanel .Position .East
95
+ }
96
+ }
97
+
98
+ def getFileChooser = {
46
99
val dir = if (currentFiles == null ) new File (System .getProperty(" user.home" )) else currentFiles.first
47
100
new FileChooser (dir) {
48
- fileFilter = new FileNameExtensionFilter (" Struts XML " , " xml" )
49
- // strange behaviour of multiSelectionEnable = true, had to use _
50
- multiSelectionEnable_= ( true )
101
+ fileFilter = new FileNameExtensionFilter (" Struts Files " , " xml" , " jsp " , " jspf " )
102
+
103
+ multiSelectionEnabled = true
51
104
}
52
- }
53
-
54
- def openFile : Unit = {
55
- val fileChoser = getFileChooser
56
- fileChoser.title = " Choose Struts XML file(s) to analyse"
57
- fileChoser.showOpenDialog(this ) match {
58
- case FileChooser .Result .Approve =>
59
- try {
60
- if (fileChoser.selectedFile.exists) {
61
- currentFiles = fileChoser.selectedFiles
62
- val fileNames = for (currentFile <- currentFiles) yield currentFile.getAbsolutePath
63
-
64
- val fromXML = reader.fromXML(fileNames:_* )
65
- val graph = graphCreator.toGraph(fromXML)
66
- val adapter = new JGraphModelAdapter (graph);
67
- val jScrollPane = new JScrollPane (new JGraph (adapter))
68
- jScrollPane.setMinimumSize(new Dimension (WIDTH , HEIGHT ))
69
-
70
- val vertexPositioner = new VertexPositioner [String , DefaultEdge ]
71
- vertexPositioner.positionVertices(adapter, fromXML, (WIDTH , HEIGHT ))
105
+ }
106
+
107
+ def openFile : Unit = {
108
+ val fileChooser = getFileChooser
109
+ fileChooser.title = " Choose Struts file(s) to analyse"
110
+ fileChooser.showOpenDialog(drawPanel) match {
111
+ case FileChooser .Result .Approve =>
112
+ try {
113
+ if (fileChooser.selectedFile.exists) {
114
+
115
+ currentFiles = fileChooser.selectedFiles
72
116
73
- contents += Component .wrap(jScrollPane)
117
+ val fileNames = for (currentFile <- currentFiles) yield currentFile.getAbsolutePath
118
+
119
+ // XML & JSP
120
+ val (vertices, edges) = reader.readFiles(fileNames:_* )
121
+
122
+ drawPanel.draw(vertices, edges)
74
123
75
- jScrollPane.updateUI
124
+ buttonPanel.saveGraphAsButton.visible = true
76
125
}
77
126
} catch {
78
127
case e : IOException =>
79
- Dialog .showMessage(this , " Error loading File: " + e.getMessage)
128
+ Dialog .showMessage(drawPanel, " Error loading File: " + e.getMessage)
129
+
80
130
}
81
131
case FileChooser .Result .Cancel => ;
82
132
case FileChooser .Result .Error => ;
83
133
}
84
- }
85
-
86
- reactions += {
87
- case ButtonClicked (openFilesButton) => openFile
88
- }
89
134
}
90
-
91
- def top = new MainFrame {
92
- title = " Struts Actions and Forwards"
93
-
94
- UIManager setLookAndFeel UIManager .getSystemLookAndFeelClassName
95
-
96
- JFrame setDefaultLookAndFeelDecorated true
97
-
98
- minimumSize = new Dimension (WIDTH , HEIGHT )
99
-
100
- contents = mainPanel
101
- }
135
+
136
+ def saveGraphAsFile : Unit = {
137
+ val dir = new File (System .getProperty(" user.home" ))
138
+ val fileChooser = new FileChooser (dir) {
139
+ fileFilter = new FileNameExtensionFilter (" Image (*.jpg, *.png, *.gif)" , " jpg" , " png" , " gif" , " jpeg" )
140
+ title = " Save graph as file"
141
+ }
142
+ fileChooser.showSaveDialog(drawPanel) match {
143
+ case FileChooser .Result .Approve => jung.saveGraphAs(fileChooser.selectedFile)
144
+ case FileChooser .Result .Cancel => ;
145
+ case FileChooser .Result .Error => ;
146
+ }
147
+ }
102
148
}
0 commit comments