This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
forked from vimalaad/DANA-Laboratory.java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DanaClientFrame.java
420 lines (391 loc) · 12.3 KB
/
DanaClientFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.*;
import java.util.Iterator;
import java.io.*;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import org.json.*;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import javax.swing.JToggleButton;
public class DanaClientFrame extends JFrame {
public class JOptionPaneMultiInput {
JOptionPaneMultiInput(JSONObject args,String message) {
JSONArray names;
JPanel myPanel = new JPanel();
try {
names=args.getJSONArray("names");
if (names.length()>10)
myPanel.setLayout(new GridLayout((int)(names.length()/2)+1, 4));
else
myPanel.setLayout(new GridLayout(names.length(),2));
for (int i=0;i<names.length();i++)
{
myPanel.add(new JLabel(" ".concat(names.getString(i))));
myPanel.add(new JTextField(5));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int result = JOptionPane.showConfirmDialog(null, myPanel,
message, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
}
}
}
/**
*
*/
private static final long serialVersionUID = 1L;
private int MaxBufferLength=102400;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DanaClientFrame.setDefaultLookAndFeelDecorated(true);
DanaClientFrame frame = new DanaClientFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
static public Frame GetFrame(Component c)
{
if(c instanceof Frame || null==c)
return c==null ? null : (Frame)c;
return GetFrame(c.getParent());
}
/**
* Create the frame.
*/
public DanaClientFrame() {
setTitle("Dana Virtual Laboratory");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 900, 450);
menuBar = new JMenuBar();
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane();
textPane = new JTextPane();
textPane.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyChar()==10)
{
String ss=textPane.getText();
lastLine="";
int i=ss.length()-1;
while (true && i>0)
{
if(ss.charAt(i-1)=='\n')
{
break;
}
i--;
}
lastLine=ss.substring(i,ss.length()-2);
if (out!=null && danaSocket!=null && !danaSocket.isClosed() && !danaSocket.isOutputShutdown())
out.println(lastLine);
}
}
});
scrollPane.setViewportView(textPane);
toolBar_1 = new JToolBar();
contentPane.add(toolBar_1);
menuBar.setMaximumSize(new Dimension(4000,5));
menuBar.setVisible(false);
contentPane.add(menuBar);
contentPane.add(scrollPane);
toolBar_1.setAlignmentX(Component.LEFT_ALIGNMENT);
menuBar.setAlignmentX(Component.LEFT_ALIGNMENT);
scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
tglbtnDanamodels = new JToggleButton("DanaModels");
tglbtnDanamodels.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (jsonMenuStructure==null)
{
createDanaModelsMenu();
addJsonMenu(jsonMenuStructure,menuBar);
menuBar.setVisible(true);
}
if (tglbtnDanamodels.isSelected())
menuBar.setVisible(true);
else
menuBar.setVisible(false);
}
});
toolBar_1.add(tglbtnDanamodels);
doc = textPane.getStyledDocument();
defineTextAttributres();
mnItmActionListener=new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JMenuItem mnDanaModelItm=(JMenuItem)arg0.getSource();
JSONObject jo = sendReqestAndgetJsonResponse(mnDanaModelItm.getName());
// Create simple option pane
//JOptionPaneMultiInput mop=new JOptionPaneMultiInput(jo,mnDanaModelItm.getName());
// Create tabbed pane
ModelDataFrame mainFrame = new ModelDataFrame(Frame.getFrames()[0],jo,mnDanaModelItm.getName());
mainFrame.setVisible( true );
}
};
}
private void addJsonToolbar(JSONObject jo,JToolBar toolBar)
{
JSONArray ja=jo.names();
//JPopupMenu popupMenu = mnu.getPopupMenu();
//int clmnCount=ja.length()/20+1;
//int rowCount=(ja.length()<20) ? ja.length() : ja.length()/clmnCount+1;
//popupMenu.setLayout(new GridLayout(rowCount, clmnCount));
for (int i=0;i<ja.length();i++)
{
try {
Object obj=jo.get(ja.getString(i));
int j=0;
int s=toolBar.getComponentCount();
if (obj instanceof JSONObject)
{
JButton mnDanaModel = new JButton(ja.getString(i).substring(ja.getString(i).lastIndexOf('.')+1));
mnDanaModel.setName(ja.getString(i));
while (j<s && ((JButton)toolBar.getComponentAtIndex(j)).getText().compareTo(mnDanaModel.getText())<0)
j++;
toolBar.add(mnDanaModel,j);
//addJsonMenu((JSONObject)(obj),mnDanaModel);
}
else
{
// JMenuItem mnDanaModelItm = new JMenuItem(ja.getString(i));
// //TODO text for types must contains simple name
// mnDanaModelItm.setName(mnu.getName().concat(".").concat(ja.getString(i)));
// while (j<s && ((JMenuItem)mnu.getMenuComponent(j)).getText().compareTo(mnDanaModelItm.getText())<0)
// j++;
// mnu.add(mnDanaModelItm,j);
// mnDanaModelItm.setToolTipText(createHtmlToolTip(sendReqestAndgetJsonResponse(mnDanaModelItm.getName())));
// mnDanaModelItm.addActionListener(mnItmActionListener);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void addJsonMenu(JSONObject jo,Component comp)
{
JSONArray ja=jo.names();
for (int i=0;i<ja.length();i++)
{
try {
Object obj=jo.get(ja.getString(i));
if (comp instanceof JMenuBar)
{
JMenuBar mnuB=(JMenuBar) comp;
if (obj instanceof JSONObject)
{
JMenu mnDanaModel = new JMenu(ja.getString(i).substring(ja.getString(i).lastIndexOf('.')+1));
mnDanaModel.setName(ja.getString(i));
int j=0;
int s=mnuB.getMenuCount();
while (j<s && ((JMenu)mnuB.getMenu(j)).getText().compareTo(mnDanaModel.getText())<0)
j++;
mnuB.add(mnDanaModel,j);
addJsonMenu((JSONObject)(obj),mnDanaModel);
}
}
if (comp instanceof JMenu)
{
JMenu mnu=(JMenu)comp;
JPopupMenu popupMenu = mnu.getPopupMenu();
int clmnCount=ja.length()/20+1;
int rowCount=(ja.length()<20) ? ja.length() : ja.length()/clmnCount+1;
popupMenu.setLayout(new GridLayout(rowCount, clmnCount));
int j=0;
int s=mnu.getItemCount();
if (obj instanceof JSONObject)
{
JMenu mnDanaModel = new JMenu(ja.getString(i).substring(ja.getString(i).lastIndexOf('.')+1));
mnDanaModel.setName(ja.getString(i));
while (j<s && ((JMenu)mnu.getMenuComponent(j)).getText().compareTo(mnDanaModel.getText())<0)
j++;
mnu.add(mnDanaModel,j);
addJsonMenu((JSONObject)(obj),mnDanaModel);
}
else
{
JMenuItem mnDanaModelItm = new JMenuItem(ja.getString(i));
//TODO text for types must contains simple name
mnDanaModelItm.setName(mnu.getName().concat(".").concat(ja.getString(i)));
while (j<s && ((JMenuItem)mnu.getMenuComponent(j)).getText().compareTo(mnDanaModelItm.getText())<0)
j++;
mnu.add(mnDanaModelItm,j);
mnDanaModelItm.setToolTipText(createHtmlToolTip(sendReqestAndgetJsonResponse(mnDanaModelItm.getName())));
mnDanaModelItm.addActionListener(mnItmActionListener);
}
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private Socket danaSocket;
private Integer danaPort=2001;
private PrintWriter out;
private BufferedReader in;
private JTextPane textPane;
private StyledDocument doc;
private SimpleAttributeSet attribkeyWord;
private SimpleAttributeSet attribError;
private SimpleAttributeSet attribSucc;
private SimpleAttributeSet attribAlert;
private SimpleAttributeSet attribNormal;
private String lastLine;
private ActionListener mnItmActionListener;
private JToolBar toolBar_1;
private JToggleButton tglbtnDanamodels;
private JMenuBar menuBar;
private JMenu mnTest;
private JSONObject jsonMenuStructure=null;
private void defineTextAttributres()
{
// Define a keyword attribute
attribkeyWord = new SimpleAttributeSet();
StyleConstants.setForeground(attribkeyWord, Color.RED);
StyleConstants.setBackground(attribkeyWord, Color.YELLOW);
StyleConstants.setBold(attribkeyWord, true);
// Define a error attribute
attribError = new SimpleAttributeSet();
StyleConstants.setForeground(attribError, Color.RED);
// Define a success attribute
attribSucc = new SimpleAttributeSet();
StyleConstants.setForeground(attribSucc, Color.GREEN);
// Define a success attribute
attribAlert = new SimpleAttributeSet();
StyleConstants.setForeground(attribAlert, Color.ORANGE);
// Define a normal attribute
attribNormal = new SimpleAttributeSet();
StyleConstants.setForeground(attribNormal, Color.BLACK);
}
//print text
private void printout(String txt,SimpleAttributeSet attrib)
{
try
{
doc.insertString(doc.getLength(), txt, attrib );
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void createDanaModelsMenu()
{
String strResponce="";
try
{
danaSocket = new Socket("localhost" , danaPort);
out = new PrintWriter(danaSocket.getOutputStream(), true);
in = new BufferedReader( new InputStreamReader(danaSocket.getInputStream()));
printout("Successfully connect to server\n",attribSucc);
char[] chBuf=new char[MaxBufferLength];
out.println("DanaModels");
strResponce=String.valueOf(chBuf, 0, in.read(chBuf));
}
catch(IOException e)
{
e.printStackTrace();
printout("Failed to establish server connection\n",attribError);
return;
}
try {
jsonMenuStructure=new JSONObject(strResponce);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//addJsonToolbar(jo,toolBar_1);
}
private String createHtmlToolTip(JSONObject jsonArgs)
{
Iterator<?> keys = jsonArgs.keys();
String htmlToolTip="<html>";
while( keys.hasNext() ){
String key = (String)keys.next();
try {
Object val=jsonArgs.get(key);
htmlToolTip+="<b>"+key+"</b> "+val.toString()+"<br/>";
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return htmlToolTip;
}
private JSONObject sendReqestAndgetJsonResponse(String request)
{
String strResponce="";
char[] chBuf=new char[MaxBufferLength];
int rSize=0;
if (out!=null && danaSocket!=null && !danaSocket.isClosed() && !danaSocket.isOutputShutdown())
{
out.println(request);
try {
rSize=in.read(chBuf);
strResponce=String.valueOf(chBuf, 0, rSize);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JSONObject jsonResponse = null;
try {
jsonResponse=new JSONObject(strResponce);
// Create simple option pane
//JOptionPaneMultiInput mop=new JOptionPaneMultiInput(jo,mnDanaModelItm.getName());
// Create tabbed pane
} catch (JSONException e) {
e.printStackTrace();
printout("\nInValid server response! response length=".concat(String.valueOf(rSize)).concat(" "),attribError);
if (rSize==1)
printout("Please try again.",attribNormal);
else
printout(strResponce,attribError);
}
return jsonResponse;
}
}