11package club .bytecode .the .jda .gui .fileviewer ;
22
33import club .bytecode .the .jda .FileChangeNotifier ;
4- import club .bytecode .the .jda .FileContainer ;
54import club .bytecode .the .jda .JDA ;
65import club .bytecode .the .jda .Resources ;
76import club .bytecode .the .jda .gui .JDAWindow ;
1211
1312import javax .swing .*;
1413import java .awt .*;
15- import java .awt .event .ActionEvent ;
16- import java .awt .event .ActionListener ;
1714import java .awt .event .ContainerEvent ;
1815import java .awt .event .ContainerListener ;
19- import java .util .HashMap ;
16+ import java .util .ArrayList ;
17+ import java .util .List ;
18+ import java .util .function .Supplier ;
2019
2120/**
2221 * The pane that contains all of the classes as tabs.
2524 * @author WaterWolf
2625 */
2726
28- public class FileViewerPane extends JDAWindow implements ActionListener {
27+ public class FileViewerPane extends JDAWindow {
2928
3029 private static final long serialVersionUID = 6542337997679487946L ;
3130
@@ -35,7 +34,8 @@ public class FileViewerPane extends JDAWindow implements ActionListener {
3534 JPanel buttonPanel ;
3635 public JButton refreshClass ;
3736
38- HashMap <String , Integer > workingOn = new HashMap <>();
37+ // todo: once we move to mapleir, we can convert this to an indexedlist!
38+ List <ViewerFile > workingOn = new ArrayList <>();
3939
4040 public FileViewerPane (final FileChangeNotifier fcn ) {
4141 super ("WorkPanel" , "Work Space" , Resources .fileNavigatorIcon , (MainViewerGUI ) fcn );
@@ -50,7 +50,16 @@ public FileViewerPane(final FileChangeNotifier fcn) {
5050 buttonPanel = new JPanel (new FlowLayout ());
5151
5252 refreshClass = new JButton ("Refresh" );
53- refreshClass .addActionListener (this );
53+ refreshClass .addActionListener (e -> (new Thread (() -> {
54+ final Component tabComp = tabs .getSelectedComponent ();
55+ if (tabComp != null ) {
56+ assert (tabComp instanceof Viewer );
57+ Viewer viewer = (Viewer ) tabComp ;
58+ JDA .viewer .setIcon (true );
59+ viewer .refresh (refreshClass );
60+ JDA .viewer .setIcon (false );
61+ }
62+ })).start ());
5463
5564 buttonPanel .add (refreshClass );
5665
@@ -66,13 +75,9 @@ public void componentAdded(final ContainerEvent e) {
6675 @ Override
6776 public void componentRemoved (final ContainerEvent e ) {
6877 final Component c = e .getChild ();
69- if (c instanceof ClassViewer ) {
70- ClassViewer cv = (ClassViewer ) c ;
71- workingOn .remove (cv .container + "$" + cv .name );
72- }
73- if (c instanceof FileViewer ) {
74- FileViewer fv = (FileViewer ) c ;
75- workingOn .remove (fv .container + "$" + fv .name );
78+ if (c instanceof Viewer ) {
79+ Viewer v = (Viewer ) c ;
80+ workingOn .remove (v .getFile ());
7681 }
7782 }
7883
@@ -82,7 +87,7 @@ public void componentRemoved(final ContainerEvent e) {
8287 this .setVisible (true );
8388
8489 }
85-
90+
8691 public static Dimension defaultDimension = new Dimension (-FileNavigationPane .defaultDimension .width , -35 );
8792 public static Point defaultPosition = new Point (FileNavigationPane .defaultDimension .width , 0 );
8893
@@ -96,82 +101,52 @@ public Point getDefaultPosition() {
96101 return defaultPosition ;
97102 }
98103
99- int tabCount = 0 ;
100-
101- public void addWorkingFile (final String name , FileContainer container , final ClassNode cn ) {
102- String key = container + "$" + name ;
103- if (!workingOn .containsKey (key )) {
104- final JPanel tabComp = new ClassViewer (name , container , cn );
104+ private void addFile (ViewerFile file , Supplier <Viewer > viewerFactory ) {
105+ if (!workingOn .contains (file )) {
106+ final JPanel tabComp = viewerFactory .get ();
105107 tabs .add (tabComp );
106108 final int tabCount = tabs .indexOfComponent (tabComp );
107- workingOn .put ( key , tabCount );
108- tabs .setTabComponentAt (tabCount , new TabbedPane (name , tabs ));
109+ workingOn .add ( tabCount , file );
110+ tabs .setTabComponentAt (tabCount , new TabbedPane (file . name , tabs ));
109111 tabs .setSelectedIndex (tabCount );
110112 } else {
111- tabs .setSelectedIndex (workingOn .get ( key ));
113+ tabs .setSelectedIndex (workingOn .indexOf ( file ));
112114 }
113115 }
116+
117+ public void addWorkingFile (ViewerFile file , final ClassNode cn ) {
118+ addFile (file , () -> new ClassViewer (file , cn ));
119+ }
114120
115- public void addFile (final String name , FileContainer container , byte [] contents ) {
116- if (contents == null ) //a directory
117- return ;
118-
119- String key = container + "$" + name ;
120- if (!workingOn .containsKey (key )) {
121- final Component tabComp = new FileViewer (name , container , contents );
122- tabs .add (tabComp );
123- final int tabCount = tabs .indexOfComponent (tabComp );
124- workingOn .put (key , tabCount );
125- tabs .setTabComponentAt (tabCount , new TabbedPane (name , tabs ));
126- tabs .setSelectedIndex (tabCount );
127- } else {
128- tabs .setSelectedIndex (workingOn .get (key ));
129- }
121+ public void addFile (ViewerFile file , byte [] contents ) {
122+ addFile (file , () -> new FileViewer (file , contents ));
130123 }
131124
132125 @ Override
133- public void openClassFile (final String name , FileContainer container , final ClassNode cn ) {
134- addWorkingFile (name , container , cn );
126+ public void openClassFile (ViewerFile file , final ClassNode cn ) {
127+ addWorkingFile (file , cn );
135128 }
136129
137130 @ Override
138- public void openFile (final String name , FileContainer container , byte [] content ) {
139- addFile (name , container , content );
131+ public void openFile (ViewerFile file , byte [] content ) {
132+ addFile (file , content );
140133 }
141134
142135 public Viewer getCurrentViewer () {
143136 return (Viewer ) tabs .getSelectedComponent ();
144137 }
145-
146- public java . awt . Component [] getLoadedViewers () {
147- return tabs .getComponents ();
138+
139+ public Viewer [] getLoadedViewers () {
140+ return ( Viewer []) tabs .getComponents ();
148141 }
149142
150- @ Override
151- public void actionPerformed (final ActionEvent arg0 ) {
152- Thread t = new Thread () {
153- public void run () {
154- final JButton src = (JButton ) arg0 .getSource ();
155- if (src == refreshClass ) {
156- final Component tabComp = tabs .getSelectedComponent ();
157- if (tabComp != null ) {
158- if (tabComp instanceof ClassViewer ) {
159- JDA .viewer .setIcon (true );
160- ((ClassViewer ) tabComp ).startPaneUpdater (src );
161- JDA .viewer .setIcon (false );
162- } else if (tabComp instanceof FileViewer ) {
163- src .setEnabled (false );
164- JDA .viewer .setIcon (true );
165- ((FileViewer ) tabComp ).refresh (src );
166- JDA .viewer .setIcon (false );
167- }
168- }
169- }
170- }
171- };
172- t .start ();
143+ /**
144+ * @return a copy of the files currently open
145+ */
146+ public List <ViewerFile > getOpenFiles () {
147+ return new ArrayList <>(workingOn );
173148 }
174-
149+
175150 public void resetWorkspace () {
176151 for (Component component : tabs .getComponents ()) {
177152 if (component instanceof ClassViewer )
0 commit comments