This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVentanaPrincipal.java
More file actions
executable file
·1098 lines (950 loc) · 36.8 KB
/
VentanaPrincipal.java
File metadata and controls
executable file
·1098 lines (950 loc) · 36.8 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
/**
* Clase principal del programa
*/
public class VentanaPrincipal extends JFrame {
//Constantes
private final int WINDOW_LENGTHT = 1280; //Tamaño de la Vetana (frame)
private final int WINDOW_HEIGTH = 720;
private final Color PANEL_BACKGROUND = new Color(161,175,224);
private final Color SUBPANEL_BACKGROUND = new Color(140,154,206);
//Variables
private Grafo grafo;
private String ruta = "Sin Titulo.acm"; //Ruta del archivo abierto
private boolean cambios = false; //Variable de cambios en el archivo actual
private boolean botonCrearVerticeActivado = false; //Si fue activado el botón de Creación de Vertices
private boolean botonCrearAristaActivado = false; //Si fue activado el botón de Creación de Aristas
private boolean modoEditor;
private Vertice newOrg = null;
private Vertice newTerm = null;
//Componentes Graficos
//Contenedor Principal
private Container contenedorPrincipal;
//Paneles
private WorkPanel workPanel;
private JPanel mainPanel;
private JPanel panelA;
private JPanel panelB;
private JPanel panelEditorGrafo;
private JPanel panelEditorCamino;
private JPanel panelInformacionGrafo;
private JPanel panelPropiedadesGrafo;
private JPanel panelInformacionArista;
private JPanel panelInformacionVertice;
private JPanel panelInfoGeneral;
//Etiquetas
private JLabel labelCrear;
private JLabel labelBorrar;
private JLabel infoLabel;
private JLabel infoPuntosPanelLabel;
private JLabel infoAristasPanelLabel;
//Botones
//Sección Editor de Grafo
private JButton botonSeleccionarEditor;
private JButton botonCrearVertice;
private JButton botonCrearArista;
//Seccion Propiedades del Grafo
private JButton botonSeleccionarProp;
private JButton botonMatrizAdyacencia;
private JButton botonTrivial;
private JButton botonCompleto;
private JButton botonConexo;
private JButton botonRecorridoEuler;
private JButton botonCircuitoEuler;
private JPanel infoPuntosPanel;
private JPanel infoAristasPanel;
private ArrayList<PanelDetalles> detallesVertices = new ArrayList<>();
private ArrayList<PanelDetalles> detallesAristas = new ArrayList<>();
private JScrollPane infoPuntosPanelScrollPane;
private JScrollPane infoAristasPanelScrollPane;
//Menus
private JMenuBar menuPrincipal;
private JMenu menuArchivo;
private JMenuItem menuArchivoNuevo;
private JMenuItem menuArchivoAbrir;
private JMenuItem menuArchivoGuardar;
private JMenuItem menuArchivoGuardarComo;
private JMenu menuGrafo;
private JMenuItem menuGrafoBorrar;
private JMenuItem menuGrafoEditor;
private JMenu menuCamino;
private JMenuItem menuCaminoEditor;
//Constructor
public VentanaPrincipal() {
Image icon = new ImageIcon(System.getProperty("user.dir") + "\\lib\\icon.png").getImage();
setIconImage(icon);
setTitle("AmadeusGraphs - " + ruta);
setSize(WINDOW_LENGTHT, WINDOW_HEIGTH);
setResizable(false);
setLocationRelativeTo(null);
initComponents();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); //Evita el cierre automático de la ventana
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if (cambios) { //Ya se realizaron cambios
int jop = mostrarSeleccion("El archivo tiene cambios sin guardar, ¿Deseas guardar los cambios?");
if( jop == JOptionPane.YES_OPTION) {
if(ruta.equals("Sin Titulo.acm")) guardarComoArchivo();
else guardarArchivo();
salir();
}
else if (jop == JOptionPane.NO_OPTION) salir();
}
else salir();
}
});
setVisible(true);
}
//
//Inicialización de Componentes
//
public void initComponents(){
//Contenedor
Container contenedorPrincipal = getContentPane();
contenedorPrincipal.setLayout(new BorderLayout());
// Menu
initMenus();
// Panel de Trabajo
workPanel = new WorkPanel();
workPanel.setLayout(null);
workPanel.setBackground(new Color(225,225,225));
workPanel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, new Color(51, 51, 51), null, null));
workPanel.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent evt){
workPanelMouseMoved(evt);
}
});
workPanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt){
workPanelMouseClicked(evt);
}
});
contenedorPrincipal.add(workPanel,BorderLayout.CENTER);
//Panel InfoGeneral Panel de abajo
panelInfoGeneral = new JPanel();
panelInfoGeneral.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, new Color(51, 51, 51), null, null));
//Etiqueta de Estado
infoLabel = new JLabel("Posicion...");
infoLabel.setSize(700,50);
panelInfoGeneral.add(infoLabel);
contenedorPrincipal.add(panelInfoGeneral,BorderLayout.SOUTH);
//Panel Izquierdo (a)
panelA = new JPanel();
panelA.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, new Color(51, 51, 51), null, null));
initPanelA();
contenedorPrincipal.add(panelA,BorderLayout.WEST);
//Panel Derecho (b)
panelB = new JPanel();
panelB.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, new Color(51, 51, 51), null, null));
initPanelB();
contenedorPrincipal.add(panelB,BorderLayout.EAST);
}
//
//Inicialización de los Menús
//
private void initMenus(){
//Fuentes
Font MENU_FONT = new Font("Arial", Font.PLAIN, 14);
Font MENUITEM_FONT = new Font("Arial", Font.PLAIN, 12);
//Menu Principal
menuPrincipal = new JMenuBar();
//Menu Archivo
menuArchivo = new JMenu("Archivo");
menuArchivo.setFont(MENU_FONT);
//Componentes Menu Archivo
//Submenu Nuevo
menuArchivoNuevo = new JMenuItem("Nuevo");
menuArchivoNuevo.setFont(MENUITEM_FONT);
menuArchivoNuevo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
menuArchivoNuevo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuArchivoNuevoMouseClicked(evt);
}
});
menuArchivo.add(menuArchivoNuevo);
//Submenu Arbrir
menuArchivoAbrir = new JMenuItem ("Abrir");
menuArchivoAbrir.setFont(MENUITEM_FONT);
menuArchivoAbrir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
menuArchivoAbrir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuArchivoAbrirMouseClicked(evt);
}
});
menuArchivo.add(menuArchivoAbrir);
menuArchivo.add(new JPopupMenu.Separator());
//Submenu Guardar
menuArchivoGuardar = new JMenuItem ("Guardar");
menuArchivoGuardar.setFont(MENUITEM_FONT);
menuArchivoGuardar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
menuArchivoGuardar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuArchivoGuardarMouseClicked(evt);
}
});
menuArchivo.add (menuArchivoGuardar);
//Submenu Guardar Como
menuArchivoGuardarComo = new JMenuItem ("Guardar como");
menuArchivoGuardarComo.setFont(MENUITEM_FONT);
menuArchivoGuardarComo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK));
menuArchivoGuardarComo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuArchivoGuardarComoMouseClicked(evt);
}
});
menuArchivo.add (menuArchivoGuardarComo);
//Menu Grafo
menuGrafo = new JMenu("Grafo");
menuGrafo.setFont(MENU_FONT);
//Componentes Menu Grafo
//Submenu Borrar Grafo
menuGrafoBorrar = new JMenuItem("Borrar Grafo");
menuGrafoBorrar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK));
menuGrafoBorrar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
menuGrafoBorrarMouseClicked(evt);
}
});
menuGrafo.add(menuGrafoBorrar);
menuGrafo.add(new JPopupMenu.Separator());
//Submenu Editor de Grafos
menuGrafoEditor = new JMenuItem("Editor de Grafos");
menuGrafoEditor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
menuGrafoEditor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//Panel A
panelEditorGrafo.setVisible(true);
panelEditorCamino.setVisible(false);
//Panel B
panelPropiedadesGrafo.setVisible(false);
panelInformacionGrafo.setVisible(true);
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); //Establece el cursor del sistema como el DEFAULT_CURSOR
modoEditor = true;
botonCrearAristaActivado = false;
botonCrearVerticeActivado = false;
newOrg = newTerm = null;
seleccionarElemento(null);
}
});
menuGrafo.add(menuGrafoEditor);
menuGrafo.add(new JPopupMenu.Separator());
modoEditor = true;
//Menu Caminos
menuCamino = new JMenu("Propiedades");
menuCamino.setFont(MENU_FONT);
//Componentes menu Caminos
//Submenu Editor de Caminos
menuCaminoEditor = new JMenuItem("Propiedades del Grafo");
menuCaminoEditor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
menuCaminoEditor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//Panel A
panelEditorGrafo.setVisible(false);
panelEditorCamino.setVisible(true);
//Panel B
panelPropiedadesGrafo.setVisible(true);
panelInformacionGrafo.setVisible(false);
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); //Establece el cursor del sistema como el DEFAULT_CURSOR
modoEditor = false;
botonCrearAristaActivado = false;
botonCrearVerticeActivado = false;
newOrg = newTerm = null;
seleccionarElemento(null);
//Se crea el grafo correspondiente
grafo = new Grafo(workPanel.getVertices(), workPanel.getAristas());
}
});
menuCamino.add(menuCaminoEditor);
//Se añaden todos los menus
menuPrincipal.add(menuArchivo);
menuPrincipal.add(menuGrafo);
menuPrincipal.add(menuCamino);
this.setJMenuBar(menuPrincipal);
}
//
//Panel de la Izquierda (a)
//
private void initPanelA(){
//Panel Editor Grafo
panelEditorGrafo = new JPanel();
panelEditorGrafo.setPreferredSize(new Dimension(120,200));
panelEditorGrafo.setLayout(new GridLayout(4,1));
//Boton de Selección 1
botonSeleccionarEditor = new JButton("Seleccionar");
botonSeleccionarEditor.setSize(80,30);
botonSeleccionarEditor.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonSeleccionarMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorGrafo.add(botonSeleccionarEditor);
//Etiqueta Crear: 2
labelCrear = new JLabel("Herrramientas");
labelCrear.setSize(80,30);
labelCrear.setBackground(new Color(37,39,41));
panelEditorGrafo.add(labelCrear);
//Boton de Creación de Vertices 3
botonCrearVertice = new JButton("Crear Vertice");
botonCrearVertice.setSize(80,30);
botonCrearVertice.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt){
botonCrearVerticeMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt){
botonMouseEntered(evt);
}
});
panelEditorGrafo.add(botonCrearVertice);
//Botón de Creación de Aristas 4
botonCrearArista = new JButton("Crear Arista");
botonCrearArista.setSize(80,30);
botonCrearArista.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt){
botonCrearAristaMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt){
botonMouseEntered(evt);
}
});
panelEditorGrafo.add(botonCrearArista);
//Panel Editor De Caminos
panelEditorCamino = new JPanel();
panelEditorCamino.setPreferredSize(new Dimension(120,400));
panelEditorCamino.setLayout(new GridLayout(9,1));
//Se pone invisible para no estorbar
//Solo se cambia cuando se da clic en editor de caminos o ctl q
panelEditorCamino.setVisible(false);
//Boton de Selección
botonSeleccionarProp = new JButton("Seleccionar");
botonSeleccionarProp.setSize(80,30);
botonSeleccionarProp.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonSeleccionarMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorCamino.add(botonSeleccionarProp);
panelEditorCamino.add(new JLabel("Propiedades"));
//Botón de Grado del Grafo
botonMatrizAdyacencia = new JButton("M. Adyacencia");
botonMatrizAdyacencia.setSize(80,30);
botonMatrizAdyacencia.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonMatrizAdyacenciaMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorCamino.add(botonMatrizAdyacencia);
//Botón de determinación trivial
botonTrivial = new JButton("Trivialidad");
botonTrivial.setSize(80,30);
botonTrivial.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonTrivialMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorCamino.add(botonTrivial);
//Botón de determinación de Grafo Completo
botonCompleto = new JButton("Completo");
botonCompleto.setSize(80,30);
botonCompleto.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonCompletoMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorCamino.add(botonCompleto);
//Botón de Conexidad
botonConexo = new JButton("Conexidad");
botonConexo.setSize(80,30);
botonConexo.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonConexoMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorCamino.add(botonConexo);
panelEditorCamino.add(new JLabel("Eulerianas"));
//Botón de determinación de Recorrido Euleriano
botonRecorridoEuler = new JButton("Recorrido");
botonRecorridoEuler.setSize(80,30);
botonRecorridoEuler.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonRecorridoEulerMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorCamino.add(botonRecorridoEuler);
//Botón de
botonCircuitoEuler = new JButton("Circuito");
botonCircuitoEuler.setSize(80,30);
botonCircuitoEuler.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonCircuitoEulerMouseClicked(evt);
}
public void mouseEntered(MouseEvent evt) {
botonMouseEntered(evt);
}
});
panelEditorCamino.add(botonCircuitoEuler);
//Se Agregan los dos paneles al panel A
panelA.add(panelEditorGrafo);
panelA.add(panelEditorCamino);
}
//
//Panel Derecho (b)
//
private void initPanelB(){
//Panel de Informacion del Grafo
panelInformacionGrafo = new JPanel();
panelInformacionGrafo.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, new Color(51, 51, 51), null, null));
panelInformacionGrafo.setLayout(new BoxLayout(panelInformacionGrafo, BoxLayout.Y_AXIS));
//Etiqueta Area de Infomación de Puntos
infoPuntosPanelLabel = new JLabel("Vertices: 0");
infoPuntosPanelLabel.setForeground(new Color(37,39,41));
panelInformacionGrafo.add(infoPuntosPanelLabel);
//Area de Texto para Información de Puntos
infoPuntosPanel = new JPanel();
infoPuntosPanel.setLayout(new BoxLayout(infoPuntosPanel, BoxLayout.Y_AXIS));
infoPuntosPanelScrollPane = new JScrollPane(infoPuntosPanel);
infoPuntosPanelScrollPane.setPreferredSize(new Dimension(155,280));
infoPuntosPanelScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
infoPuntosPanelScrollPane.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
infoPuntosPanelScrollPaneMouseEntered(evt);
}
});
panelInformacionGrafo.add(infoPuntosPanelScrollPane);
//Etiqueta Area de Infomación de Aristas
infoAristasPanelLabel = new JLabel("Aristas: 0");
infoAristasPanelLabel.setForeground(new Color(37,39,41));
panelInformacionGrafo.add(infoAristasPanelLabel);
//Area de Texto para Información de Aristas
infoAristasPanel = new JPanel();
infoAristasPanel.setLayout(new BoxLayout(infoAristasPanel, BoxLayout.Y_AXIS));
infoAristasPanelScrollPane = new JScrollPane(infoAristasPanel);
infoAristasPanelScrollPane.setPreferredSize(new Dimension(155,280));
infoAristasPanelScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
infoAristasPanelScrollPane.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
infoAristasPanelScrollPaneMouseEntered(evt);
}
});
panelInformacionGrafo.add(infoAristasPanelScrollPane);
//Area de Información de un Camino
panelPropiedadesGrafo = new JPanel();
panelPropiedadesGrafo.setLayout(new BoxLayout(panelPropiedadesGrafo, BoxLayout.Y_AXIS));
panelPropiedadesGrafo.setVisible(false);
panelPropiedadesGrafo.setPreferredSize(new Dimension(155,280));
panelPropiedadesGrafo.add(new JLabel("Propiedades"));
//Se añaden los paneles al Panel B
panelB.add(panelInformacionGrafo);
panelB.add(panelPropiedadesGrafo);
}
//Actualización del Ambiente en modo Editor Grafo
public void actualizarInfoEditorGrafo() {
//Actualización del TextField de Vértices
infoPuntosPanelLabel.setText("Vertices: " + workPanel.getVertices().size());
infoPuntosPanel.removeAll();
for(PanelDetalles panelDetalle : detallesVertices) {
panelDetalle.setBotonEliminarHabilitado(true); //Se rehabilita el boton de eliminar
panelDetalle.setInformacionDetallada(false);
infoPuntosPanel.add(panelDetalle);
}
infoPuntosPanel.updateUI();
//Actualización del TextField de Aristas
infoAristasPanelLabel.setText("Aristas: " + workPanel.getAristas().size());
infoAristasPanel.removeAll();
for(PanelDetalles panelDetalle : detallesAristas) {
panelDetalle.setBotonEliminarHabilitado(true); //Se rehabilita el boton de eliminar
infoAristasPanel.add(panelDetalle);
}
infoAristasPanel.updateUI();
}
//Guardar archivo Como
public void guardarComoArchivo(){
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Guardar como: ");
jfc.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACM", "acm");
jfc.addChoosableFileFilter(filter);
do {
int returnValue = jfc.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION){
ruta = jfc.getSelectedFile().getAbsolutePath().concat(".acm");
File file = new File(ruta);
if(file.exists()) { //El archivo existe?
int jop = this.mostrarSeleccion("Desea sobreescribir el archivo existente");
//Verificar si desea sobre escribir
if (jop == JOptionPane.YES_OPTION) {
guardarArchivo();
break;
}
}else{ //No existe el archivo se crea y se guarda
guardarArchivo();
break;
}
}
else break;
} while (true);
}
//Guardar Archivo
public void guardarArchivo(){
//Abrir Archivo
if(Archivo.fopen(ruta, 'w')){
ArrayList<ElementoGrafo> auxArray = new ArrayList<>();
auxArray.addAll(workPanel.getVertices());
auxArray.addAll(workPanel.getAristas());
//Escribir Archivo
if(!Archivo.fwrite((Object) auxArray))
mostrarAdvertencia("ERROR: No se puedo guardar el archivo");
//Cerrar Archivo
Archivo.fclose('w');
cambios(false);
}else
mostrarAdvertencia("ERROR: No se puedo salvar el archivo ");
}
//Abrir Archivo
public void abrirArchivo(){
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Seleecione un archivo .acm");
jfc.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACM", "acm");
jfc.addChoosableFileFilter(filter);
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
reiniciarFrame(); //Reiniciar componentes
ruta = jfc.getSelectedFile().getPath();
//Abrir Archivo
if(Archivo.fopen(ruta, 'r')){
//ArrayList's Auxiliares
ArrayList<Arista> auxAristas = new ArrayList<>();
ArrayList<Vertice> auxVertices = new ArrayList<>();;
ArrayList<Object> auxArray = (ArrayList<Object>) Archivo.fread();
for (Object elemento : auxArray) {
if (elemento instanceof Vertice)
auxVertices.add((Vertice) elemento);
else
auxAristas.add((Arista) elemento);
}
if (auxVertices == null)
System.out.println("Vertices");
if (auxAristas == null)
System.out.println("Aristas");
workPanel.setVertices(auxVertices);
workPanel.setAristas(auxAristas);
//Cerrar archivo
Archivo.fclose('r');
//Inicializar Vertices y Aristas
for(Vertice vertice : workPanel.getVertices()){
PanelDetalles nuevoPanelDetalle = new PanelDetalles(vertice);
detallesVertices.add(nuevoPanelDetalle);
//Implementacion del boton eliminar
nuevoPanelDetalle.getBotonEliminar().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
botonEliminarMouseClicked(evt, nuevoPanelDetalle);
}
});
}
for(Arista arista : workPanel.getAristas()){
PanelDetalles nuevoPanelDetalle = new PanelDetalles(arista);
detallesAristas.add(nuevoPanelDetalle);
//Implementacion del boton eliminar
nuevoPanelDetalle.getBotonEliminar().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
botonEliminarMouseClicked(evt, nuevoPanelDetalle);
}
});
}
actualizarInfoEditorGrafo();
seleccionarElemento(null);
if (auxAristas.size() != 0)
Arista.setContID(workPanel.getAristas().get(workPanel.getAristas().size()-1).getID()); //ID SE INICIALIZA EN LA ULTIMA ARISTA
if (auxVertices.size() != 0)
Vertice.setContID(workPanel.getVertices().get(workPanel.getVertices().size()-1).getID()); //ID SE INICIALIZA EN EL ULTIMO VERTICE
cambios(false);
}
}
}
//Abrir Archivo nuevo
public void reiniciarFrame() {
workPanel.reiniciar();
detallesAristas = new ArrayList<>();
detallesVertices = new ArrayList<>();
actualizarInfoEditorGrafo();
ruta = "Sin Titulo.acm";
cambios = false;
setTitle("AmadeusGraphs - " + ruta);
}
//Salida del Programa
private void salir() {
setVisible(false); //Esconde la ventana
dispose(); //Elimina los componentes gráficos y retorna la memoria al SO
System.exit(0); //Termina el Proceso
}
//Establecer cambios
public void cambios(boolean valor) {
if (valor) setTitle("* AmadeusGraphs - " + ruta);
else setTitle("AmadeusGraphs - " + ruta);
cambios = valor;
}
//Mensaje de Advertencia
public int mostrarSeleccion(String control) {
return JOptionPane.showConfirmDialog(null, control, ruta, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
}
//Mensaje de Error
public void mostrarAdvertencia(String control) {
JOptionPane.showMessageDialog(this, control, "Error", JOptionPane.ERROR_MESSAGE);
}
//
//Eventos
//
//Eventos de workPanel
public void workPanelMouseMoved(MouseEvent evt){
infoLabel.setText("(" + evt.getX() + "," + evt.getY() + ")");
}
public void workPanelMouseClicked(MouseEvent evt){
infoLabel.setText("Click en (" + evt.getX() + "," + evt.getY() + ")");
if (!botonCrearVerticeActivado && !botonCrearAristaActivado) {
//Se busca primero un vértice
boolean esVertice = false;
boolean esArista = false;
for(Vertice vertice : workPanel.getVertices()){
double dist = Math.sqrt(Math.pow(vertice.getX() - evt.getX(), 2) + Math.pow(vertice.getY() - evt.getY(), 2)); //Distancia entre ambos centros (vertice y newVertex)
if(dist <= Vertice.getDiametro()/2){ //La distancia debe maxima de un radio
esVertice = true;
seleccionarElemento(vertice);
break;
}
}
//Si no, se busca una arista, se toma la primera ocurrencia
if (!esVertice) {
for (Arista arista : workPanel.getAristas()) {
int x1 = arista.getOrigen().getX();
int y1 = arista.getOrigen().getY();
int x = evt.getX();
int y = evt.getY();
int x2 = arista.getTerminal().getX();
int y2 = arista.getTerminal().getY();
//Se busca el click dentro del rectángulo que forman los vértices de la arista como esquinas opuestas
if (x1 < x && x < x2){
if ((y1 < y && y < y2) || (y2 < y && y < y1)) {
seleccionarElemento(arista);
esArista = true;
break;
}
}
else if (x2 < x && x < x1)
if ((y1 < y && y < y2) || (y2 < y && y < y1)) {
seleccionarElemento(arista);
esArista = true;
break;
}
}
if (!esArista) seleccionarElemento(null); //Deselecciona del panel
}
}
if (modoEditor) {
procesamientoEditor(evt);
}
else {
procesamientoPropiedades(evt);
}
}
//Procesamiento en modo Editor
public void procesamientoEditor(MouseEvent evt) {
if (botonCrearVerticeActivado) { //Creación de Vértices
boolean correctVertex = true;
for (Vertice vertice : workPanel.getVertices()) {
double dist = Math.sqrt(Math.pow(vertice.getX() - evt.getX(), 2) + Math.pow(vertice.getY() - evt.getY(), 2)); //Distancia entre ambos centros (vertice y newVertex)
if (dist < Vertice.getDiametro()) {
correctVertex = false;
break;
} //La distancia debe ser de al menos dos diametros
}
if (!correctVertex) infoLabel.setText("No es posible encimar Vertices. Intente colocando el Vertice en otro punto.");
else {
//Adición al registro de Vertices
Vertice newVertex = new Vertice(evt.getX(), evt.getY());
workPanel.addVertice(newVertex);
//Se crea el panel de Detalles y se añade al sistema
PanelDetalles nuevoElementoPanel = new PanelDetalles(newVertex);
nuevoElementoPanel.getBotonEliminar().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonEliminarMouseClicked(evt, nuevoElementoPanel);
}
});
detallesVertices.add(nuevoElementoPanel);
//Se deja solo seleccionado el nuevo vértice
seleccionarElemento(newVertex);
//Actualización del Ambiente Gráfico
actualizarInfoEditorGrafo();
cambios(true);
}
}
else if(botonCrearAristaActivado){ //Creación de Aristas
boolean correctVertex = false;
Vertice vertex = null;
Arista edge = null;
for(Vertice vertice : workPanel.getVertices()){
double dist = Math.sqrt(Math.pow(vertice.getX() - evt.getX(), 2) + Math.pow(vertice.getY() - evt.getY(), 2)); //Distancia entre ambos centros (vertice y newVertex)
if(dist <= Vertice.getDiametro()/2){ //La distancia debe maxima de un radio
correctVertex = true;
vertex = vertice; //Se guarda la referencia del vértice
break;
}
}
if(correctVertex){
//Primera vez, se genera un Origen
if (newOrg == null) newOrg = vertex;
//Segunda vez, se genera un Terminal, se construye la arista y se resetean el Orgien y el Terminal
else if (newOrg != null && newTerm == null && vertex != newOrg) {
newTerm = vertex;
edge = new Arista(newOrg, newTerm); //Creación de la Arista
newOrg.setGrado(newOrg.getGrado() + 1);
newTerm.setGrado(newTerm.getGrado() + 1);
newOrg = newTerm = null;
//Adición al Arreglo de Aristas
workPanel.addArista(edge);
//Se crea el panel de Detalles y se añade al sistema
PanelDetalles nuevoElementoPanel = new PanelDetalles(edge);
nuevoElementoPanel.getBotonEliminar().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
botonEliminarMouseClicked(evt, nuevoElementoPanel);
}
});
detallesAristas.add(nuevoElementoPanel);
//Se deja solo seleccionada la nueva Arista
seleccionarElemento(edge);
//Actualización del Ambiente Gráfico
actualizarInfoEditorGrafo();
cambios(true);
}
}
else infoLabel.setText("No se selecciono ningun punto.");
}// fin de las funciones de workPanel
}
//Procesamiento en modo Propiedades
public void procesamientoPropiedades(MouseEvent evt) {
/*
* Futuras Actualizaciones
*/
}
//Atencion sobre el panel del elemento seleccionado
private void seleccionarElemento(ElementoGrafo elemento) {
workPanel.seleccionarElemento(elemento);
if (modoEditor){
//Se actualizan los paneles de detalles
for (PanelDetalles detalle : detallesAristas)
if (detalle.esPanelDe(elemento))
detalle.panelSeleccionado(true);
else
detalle.panelSeleccionado(false);
for (PanelDetalles detalle : detallesVertices)
if (detalle.esPanelDe(elemento))
detalle.panelSeleccionado(true);
else
detalle.panelSeleccionado(false);
actualizarInfoEditorGrafo();
}else{
//Se muestra el panel de detalles del elemento seleccionado
panelPropiedadesGrafo.removeAll();
if (elemento == null)
panelPropiedadesGrafo.add(new JLabel("Propiedades"));
else{
for (PanelDetalles detalle : detallesAristas)
if (detalle.esPanelDe(elemento)) {
detalle.panelSeleccionado(true);
detalle.setBotonEliminarHabilitado(false); //Se impide la eliminación
panelPropiedadesGrafo.add(detalle);
}
for (PanelDetalles detalle : detallesVertices)
if (detalle.esPanelDe(elemento)) {
detalle.panelSeleccionado(true);
detalle.setBotonEliminarHabilitado(false); //Se impide la eliminación
detalle.setInformacionDetallada(true);
panelPropiedadesGrafo.add(detalle);
}
}
panelPropiedadesGrafo.updateUI();
}
}
//Eventos de Menú
//Click sobre menuArchivoNuevo
public void menuArchivoNuevoMouseClicked(ActionEvent evt) {
if (cambios) {
int jop = mostrarSeleccion("El archivo tiene cambios sin guardar, ¿Deseas guardar los cambios?");
if (jop == JOptionPane.YES_OPTION) {
guardarArchivo();
reiniciarFrame();
}else if
(jop == JOptionPane.NO_OPTION) reiniciarFrame();
}else
reiniciarFrame();
}
//Click sobre menuArchivoAbrir
public void menuArchivoAbrirMouseClicked(ActionEvent evt){
if (cambios) { //Ya se realizaron cambios
int jop = mostrarSeleccion("El archivo tiene cambios sin guardar, ¿Deseas guardar los cambios?");
if( jop == JOptionPane.YES_OPTION) {
guardarArchivo();
abrirArchivo();
}
else if (jop == JOptionPane.NO_OPTION) abrirArchivo();
}else
abrirArchivo();
}
//Click sobre menuArchivoGuardar
public void menuArchivoGuardarMouseClicked(ActionEvent evt){
if
(ruta.equals("Sin Titulo.acm")) guardarComoArchivo();
else
guardarArchivo();
}
//Click sobre menuArchivoGuardarComo
public void menuArchivoGuardarComoMouseClicked(ActionEvent evt){
guardarComoArchivo();
}
//Click sobre menuGrafoBorrar
public void menuGrafoBorrarMouseClicked(ActionEvent evt) {
reiniciarFrame();
}
//Eventos de botonSeleccionar
public void botonSeleccionarMouseClicked(MouseEvent evt) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); //Establece el cursor del sistema como el DEFAULT_CURSOR
((JButton) evt.getSource()).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
botonCrearAristaActivado = false;
botonCrearVerticeActivado = false;
newOrg = newTerm = null;
}
//Eventos de botonCrearVertice
public void botonCrearVerticeMouseClicked(MouseEvent evt) {
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); //Establece el cursor del sistema como el CROSSHAIR_CURSOR
botonCrearVertice.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
botonCrearAristaActivado = false;
botonCrearVerticeActivado = true;
newOrg = newTerm = null;
}
//Eventos de botonCrearArista
public void botonCrearAristaMouseClicked(MouseEvent evt) {
setCursor(new Cursor(Cursor.MOVE_CURSOR)); //Establece el cursor del sistema como el MOVE_CURSOR
botonCrearArista.setCursor(new Cursor(Cursor.MOVE_CURSOR));
botonCrearAristaActivado = true;
botonCrearVerticeActivado = false;
newOrg = newTerm = null;
}
//Eventos de infoPuntosPanelScrollPane
public void infoPuntosPanelScrollPaneMouseEntered(MouseEvent evt) {
infoPuntosPanelScrollPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
//Eventos de infoAristasPanelScrollPane
public void infoAristasPanelScrollPaneMouseEntered(MouseEvent evt) {
infoAristasPanelScrollPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}