99import java .awt .event .WindowEvent ;
1010
1111import javax .swing .BorderFactory ;
12+ import javax .swing .JButton ;
1213import javax .swing .JComboBox ;
1314import javax .swing .JFrame ;
15+ import javax .swing .JList ;
1416import javax .swing .JTabbedPane ;
1517import javax .swing .JTextArea ;
1618import javax .swing .UIManager ;
2729import me .coley .clicker .Stats ;
2830import me .coley .clicker .Values ;
2931import me .coley .clicker .agent .Agent ;
32+ import me .coley .clicker .agent .AttatchListener ;
3033import me .coley .clicker .jna .KeyHandler ;
3134import me .coley .clicker .ui .controls .LabeledBindButton ;
3235import me .coley .clicker .ui .controls .LabeledCheckbox ;
3942import me .coley .simplejna .hook .key .KeyHook ;
4043
4144import java .awt .BorderLayout ;
42- import java .awt .Color ;
4345import java .awt .Dimension ;
4446
4547import javax .swing .JPanel ;
4648import javax .swing .JScrollPane ;
4749
4850import java .util .Random ;
51+ import java .util .concurrent .atomic .AtomicBoolean ;
4952import java .util .logging .Level ;
5053import java .util .logging .Logger ;
5154import javax .swing .JSplitPane ;
5255import javax .swing .JTree ;
56+ import javax .swing .ListSelectionModel ;
57+
5358import java .awt .FlowLayout ;
5459
5560public class MainGUI {
@@ -59,6 +64,7 @@ public class MainGUI {
5964 public final Clicker clicker = new Clicker (this );
6065 public final Stats stats = new Stats (this );
6166 private final KeyHandler handler = new KeyHandler (this );
67+ private final boolean isAttatched ;
6268 public GraphingPanel graph ;
6369 private JFrame frmClicker ;
6470 private JTextArea txtStats ;
@@ -71,18 +77,23 @@ public static void main(String[] args) {
7177 String curDir = System .getProperty ("user.dir" );
7278 System .setProperty ("settings.dir" , curDir );
7379 // Handle args if there are any
74- if (args .length >= 2 ) {
75- String arg1 = args [0 ];
76- String arg2 = args [1 ];
77- if (arg1 .contains ("agent" )) {
78- // Agent that runs the program in another VM.
79- String target = arg2 ;
80- Agent .loadAgentToTarget (target , "dir:" + curDir );
81- return ;
82- } else if (arg1 .contains ("dir" )) {
83- // Adjusts the settings.dir property.
84- String dir = arg2 ;
85- System .setProperty ("settings.dir" , dir );
80+ AtomicBoolean displayAgent = new AtomicBoolean (true );
81+ if (args .length >= 0 ) {
82+ for (String arg : args ) {
83+ if (arg .startsWith ("agentTarget:" )) {
84+ // Agent that runs the program in another VM.
85+ String target = arg .substring ("agentTarget:" .length ());
86+ Agent .loadAgentToTarget (target , "dir:" + curDir );
87+ return ;
88+ } else if (arg .contains ("dir:" )) {
89+ // Adjusts the settings.dir property.
90+ String dir = arg .substring ("dir:" .length ());
91+ System .setProperty ("settings.dir" , dir );
92+ } else if (arg .contains ("displayAgentTab:" )) {
93+ //
94+ String val = arg .substring ("displayAgentTab:" .length ());
95+ displayAgent .set (Boolean .parseBoolean (val ));
96+ }
8697 }
8798 }
8899 try {
@@ -96,7 +107,7 @@ public static void main(String[] args) {
96107 EventQueue .invokeLater (new Runnable () {
97108 public void run () {
98109 try {
99- MainGUI gui = new MainGUI ();
110+ MainGUI gui = new MainGUI (! displayAgent . get () );
100111 gui .initSettings ();
101112 gui .initGui ();
102113 gui .loadSettings ();
@@ -107,6 +118,10 @@ public void run() {
107118 });
108119 }
109120
121+ public MainGUI (boolean isAttatched ) {
122+ this .isAttatched = isAttatched ;
123+ }
124+
110125 /**
111126 * Initialize the contents of the frame.
112127 */
@@ -118,13 +133,24 @@ private void initGui() {
118133 frmClicker .setBounds (100 , 100 , 422 , 398 );
119134 frmClicker .setDefaultCloseOperation (JFrame .DO_NOTHING_ON_CLOSE );
120135 frmClicker .addWindowListener (new WindowAdapter () {
121-
122136 @ Override
123137 public void windowClosing (WindowEvent e ) {
124138 log .log (Level .INFO , "Saving settings..." );
125139 settings .save ();
126140 keybinds .save ();
127- System .exit (0 );
141+ if (!isAttatched ) {
142+ // Not attached, just suicide the program
143+ System .exit (0 );
144+ } else {
145+ // Attached, exiting would kill the parent program.
146+ // Carefully disable / dispose instead.
147+ if (clicker .getStatus ())
148+ clicker .toggle ();
149+ if (stats .getStatus ())
150+ stats .toggle ();
151+ KeyHook .unhook (handler );
152+ frmClicker .dispose ();
153+ }
128154 }
129155 });
130156 JTabbedPane tabs = new JTabbedPane (JTabbedPane .TOP );
@@ -194,7 +220,6 @@ public void itemStateChanged(ItemEvent e) {
194220 tabSettings .add (combo );
195221
196222 }
197-
198223 JPanel tabStatistics = new JPanel ();
199224 tabs .addTab (Lang .get (Lang .STATISTICS_TITLE ), null , tabStatistics , null );
200225 tabStatistics .setLayout (new BorderLayout ());
@@ -214,6 +239,29 @@ public void itemStateChanged(ItemEvent e) {
214239 sp .setDividerLocation (frmClicker .getHeight () / 2 );
215240 tabStatistics .add (sp , BorderLayout .CENTER );
216241 }
242+ if (!isAttatched ) {
243+ JPanel tabAgent = new JPanel ();
244+ tabs .addTab (Lang .get (Lang .AGENT_TITLE ), null , tabAgent , null );
245+ tabAgent .setLayout (new BorderLayout ());
246+ JButton btnAction = new JButton (Lang .get (Lang .AGENT_ATTATCH_VM ));
247+ JList <String > list = new JList <String >();
248+ list .setModel (Agent .getJVMS ());
249+ list .setSelectionMode (ListSelectionModel .SINGLE_INTERVAL_SELECTION );
250+ list .setLayoutOrientation (JList .HORIZONTAL_WRAP );
251+ btnAction .addActionListener (new AttatchListener (list ));
252+ JScrollPane listScroller = new JScrollPane (list );
253+ tabAgent .add (listScroller , BorderLayout .CENTER );
254+ tabAgent .add (btnAction , BorderLayout .NORTH );
255+ /*
256+ * list = new JList(data); //data has type Object[]
257+ * list.setSelectionMode(ListSelectionModel.
258+ * SINGLE_INTERVAL_SELECTION);
259+ * list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
260+ * list.setVisibleRowCount(-1); ... JScrollPane listScroller = new
261+ * JScrollPane(list); listScroller.setPreferredSize(new
262+ * Dimension(250, 80));
263+ */
264+ }
217265 log .log (Level .INFO , "Displaying gui" );
218266 frmClicker .setVisible (true );
219267 }
0 commit comments