1 /*
2  * File    : ConfigPanel*.java
3  * Created : 11 mar. 2004
4  * By      : TuxPaper
5  *
6  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details ( see the LICENSE file ).
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 package org.gudy.azureus2.ui.swt.views.configsections;
24 
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.*;
31 import org.gudy.azureus2.core3.config.COConfigurationManager;
32 import org.gudy.azureus2.core3.internat.MessageText;
33 import org.gudy.azureus2.core3.util.AEJavaManagement;
34 import org.gudy.azureus2.core3.util.Debug;
35 import org.gudy.azureus2.core3.util.DisplayFormatters;
36 import org.gudy.azureus2.platform.PlatformManager;
37 import org.gudy.azureus2.platform.PlatformManagerFactory;
38 import org.gudy.azureus2.platform.PlatformManagerCapabilities;
39 import org.gudy.azureus2.plugins.platform.PlatformManagerException;
40 import org.gudy.azureus2.plugins.ui.config.ConfigSection;
41 import org.gudy.azureus2.ui.swt.Messages;
42 import org.gudy.azureus2.ui.swt.Utils;
43 import org.gudy.azureus2.ui.swt.components.LinkLabel;
44 import org.gudy.azureus2.ui.swt.config.*;
45 import org.gudy.azureus2.ui.swt.plugins.UISWTConfigSection;
46 import org.gudy.azureus2.ui.swt.shells.MessageBoxShell;
47 import org.gudy.azureus2.ui.swt.views.utils.ManagerUtils;
48 
49 import java.io.File;
50 import java.util.ArrayList;
51 import java.util.List;
52 
53 public class ConfigSectionStartShutdown implements UISWTConfigSection {
54 
55 	private final static String LBLKEY_PREFIX = "ConfigView.label.";
56 
configSectionGetParentSection()57 	public String configSectionGetParentSection() {
58 		return ConfigSection.SECTION_ROOT;
59 	}
60 
configSectionGetName()61 	public String configSectionGetName() {
62 		return "startstop";
63 	}
64 
configSectionSave()65 	public void configSectionSave() {
66 	}
67 
configSectionDelete()68 	public void configSectionDelete() {
69 	}
70 
maxUserMode()71 	public int maxUserMode() {
72 		return 1;
73 	}
74 
75 
configSectionCreate(final Composite parent)76 	public Composite configSectionCreate(final Composite parent) {
77 
78 		GridData gridData;
79 		GridLayout layout;
80 		Label label;
81 
82 		final Composite cDisplay = new Composite(parent, SWT.NULL);
83 
84 		gridData = new GridData(GridData.VERTICAL_ALIGN_FILL
85 				| GridData.HORIZONTAL_ALIGN_FILL);
86 		Utils.setLayoutData(cDisplay, gridData);
87 		layout = new GridLayout();
88 		layout.numColumns = 1;
89 		layout.marginWidth = 0;
90 		layout.marginHeight = 0;
91 		cDisplay.setLayout(layout);
92 
93 		final PlatformManager platform = PlatformManagerFactory.getPlatformManager();
94 
95 		int userMode = COConfigurationManager.getIntParameter("User Mode");
96 
97 			// ***** start group
98 
99 		boolean can_ral = platform.hasCapability(PlatformManagerCapabilities.RunAtLogin );
100 
101 		if ( can_ral || userMode > 0 ){
102 
103 			Group gStartStop = new Group(cDisplay, SWT.NULL);
104 			Messages.setLanguageText(gStartStop, LBLKEY_PREFIX + "start");
105 			layout = new GridLayout(2, false);
106 			gStartStop.setLayout(layout);
107 			Utils.setLayoutData(gStartStop, new GridData( GridData.FILL_HORIZONTAL ));
108 
109 			if ( can_ral ){
110 
111 				gridData = new GridData();
112 				gridData.horizontalSpan = 2;
113 				BooleanParameter start_on_login = new BooleanParameter(gStartStop, "Start On Login", LBLKEY_PREFIX + "start.onlogin");
114 
115 				try{
116 					start_on_login.setSelected( platform.getRunAtLogin());
117 
118 					start_on_login.addChangeListener(
119 						new ParameterChangeAdapter()
120 						{
121 							public void
122 							booleanParameterChanging(
123 								Parameter p,
124 								boolean toValue)
125 							{
126 								try{
127 									platform.setRunAtLogin( toValue );
128 
129 								}catch( Throwable e ){
130 
131 									Debug.out( e );
132 								}
133 							}
134 						});
135 
136 				}catch( Throwable e ){
137 
138 					start_on_login.setEnabled( false );
139 
140 					Debug.out( e );
141 				}
142 
143 				start_on_login.setLayoutData(gridData);
144 			}
145 
146 			if ( userMode > 0 ){
147 
148 				gridData = new GridData();
149 				gridData.horizontalSpan = 2;
150 
151 				BooleanParameter start_in_lr_mode = new BooleanParameter(gStartStop, "Start In Low Resource Mode", LBLKEY_PREFIX + "start.inlrm");
152 
153 				start_in_lr_mode.setLayoutData(gridData);
154 			}
155 		}
156 
157 		if ( platform.hasCapability( PlatformManagerCapabilities.PreventComputerSleep )){
158 
159 			Group gSleep = new Group(cDisplay, SWT.NULL);
160 			Messages.setLanguageText(gSleep, LBLKEY_PREFIX + "sleep");
161 			layout = new GridLayout(2, false);
162 			gSleep.setLayout(layout);
163 			Utils.setLayoutData(gSleep, new GridData( GridData.FILL_HORIZONTAL ));
164 
165 			gridData = new GridData();
166 			gridData.horizontalSpan = 2;
167 			label = new Label(gSleep, SWT.NULL);
168 		    Messages.setLanguageText(label, "ConfigView.label.sleep.info");
169 		    Utils.setLayoutData(label,  gridData );
170 
171 			gridData = new GridData();
172 			gridData.horizontalSpan = 2;
173 			BooleanParameter no_sleep_dl = new BooleanParameter(gSleep, "Prevent Sleep Downloading", LBLKEY_PREFIX + "sleep.download");
174 			no_sleep_dl.setLayoutData(gridData);
175 
176 			gridData = new GridData();
177 			gridData.horizontalSpan = 2;
178 			BooleanParameter no_sleep_se = new BooleanParameter(gSleep, "Prevent Sleep FP Seeding", LBLKEY_PREFIX + "sleep.fpseed");
179 			no_sleep_se.setLayoutData(gridData);
180 		}
181 
182 		if ( userMode > 0 ){
183 
184 			Group gPR = new Group(cDisplay, SWT.NULL);
185 			Messages.setLanguageText(gPR, LBLKEY_PREFIX + "pauseresume");
186 			layout = new GridLayout(2, false);
187 			gPR.setLayout(layout);
188 			Utils.setLayoutData(gPR, new GridData( GridData.FILL_HORIZONTAL ));
189 
190 			gridData = new GridData();
191 			gridData.horizontalSpan = 2;
192 			BooleanParameter pauseOnExit = new BooleanParameter(gPR,
193 					"Pause Downloads On Exit", "ConfigView.label.pause.downloads.on.exit");
194 			pauseOnExit.setLayoutData(gridData);
195 
196 			gridData = new GridData();
197 			gridData.horizontalSpan = 2;
198 			BooleanParameter resumeOnStart = new BooleanParameter(gPR,
199 					"Resume Downloads On Start", "ConfigView.label.resume.downloads.on.start");
200 			resumeOnStart.setLayoutData(gridData);
201 		}
202 
203 		if ( userMode >= 0 ){
204 
205 			Group gStop = new Group(cDisplay, SWT.NULL);
206 			Messages.setLanguageText(gStop, LBLKEY_PREFIX + "stop");
207 			layout = new GridLayout(5, false);
208 			gStop.setLayout(layout);
209 			Utils.setLayoutData(gStop, new GridData( GridData.FILL_HORIZONTAL ));
210 
211 				// done downloading
212 
213 			addDoneDownloadingOption( gStop, true );
214 
215 				// done seeding
216 
217 			addDoneSeedingOption( gStop, true );
218 
219 		    	// reset on trigger
220 
221 			gridData = new GridData();
222 			gridData.horizontalSpan = 2;
223 			BooleanParameter resetOnTrigger =
224 				new BooleanParameter(
225 					gStop, "Stop Triggers Auto Reset",
226 					"!" + MessageText.getString(
227 						"ConfigView.label.stop.autoreset",
228 						new String[]{ MessageText.getString( "ConfigView.label.stop.Nothing" )})+ "!");
229 
230 			resetOnTrigger.setLayoutData(gridData);
231 
232 		    	// prompt to allow abort
233 
234 			gridData = new GridData();
235 			gridData.horizontalSpan = 2;
236 			BooleanParameter enablePrompt =	new BooleanParameter( gStop, "Prompt To Abort Shutdown", "ConfigView.label.prompt.abort" );
237 			enablePrompt.setLayoutData(gridData);
238 		}
239 
240 		if ( userMode > 0 ){
241 
242 			Group gRestart = new Group(cDisplay, SWT.NULL);
243 			Messages.setLanguageText(gRestart, "label.restart");
244 			layout = new GridLayout(2, false);
245 			gRestart.setLayout(layout);
246 			Utils.setLayoutData(gRestart, new GridData( GridData.FILL_HORIZONTAL ));
247 
248 			label = new Label(gRestart, SWT.NULL);
249 			Messages.setLanguageText(label, "ConfigView.label.restart.auto");
250 
251 			new IntParameter(gRestart, "Auto Restart When Idle", 0, 100000 );
252 		}
253 
254 		if ( userMode > 0 && platform.hasCapability( PlatformManagerCapabilities.AccessExplicitVMOptions )){
255 
256 			Group gJVM = new Group(cDisplay, SWT.NULL);
257 			Messages.setLanguageText(gJVM, LBLKEY_PREFIX + "jvm");
258 			layout = new GridLayout(2, false);
259 			gJVM.setLayout(layout);
260 			Utils.setLayoutData(gJVM, new GridData( GridData.FILL_HORIZONTAL ));
261 
262 				// wiki link
263 
264 			gridData = new GridData();
265 			gridData.horizontalSpan = 2;
266 
267 			LinkLabel link = new LinkLabel(	gJVM, gridData, "ConfigView.label.please.visit.here",
268 											"http://wiki.vuze.com/w/Java_VM_memory_usage");
269 
270 				// info
271 
272 			label = new Label(gJVM, SWT.NULL);
273 			Messages.setLanguageText(label, "jvm.info");
274 			gridData = new GridData();
275 			gridData.horizontalSpan = 2;
276 			Utils.setLayoutData(label,  gridData );
277 
278 			try{
279 				final File option_file = platform.getVMOptionFile();
280 
281 				final Group gJVMOptions = new Group(gJVM, SWT.NULL);
282 				layout = new GridLayout(3, false);
283 				gJVMOptions.setLayout(layout);
284 				gridData = new GridData( GridData.FILL_HORIZONTAL );
285 				gridData.horizontalSpan = 2;
286 				Utils.setLayoutData(gJVMOptions,  gridData );
287 
288 				buildOptions( cDisplay, platform, gJVMOptions, false );
289 
290 					// show option file
291 
292 				label = new Label(gJVM, SWT.NULL);
293 				Messages.setLanguageText(label, "jvm.show.file", new String[]{ option_file.getAbsolutePath() });
294 
295 				Button show_folder_button = new Button( gJVM, SWT.PUSH );
296 
297 			 	Messages.setLanguageText( show_folder_button, "MyTorrentsView.menu.explore");
298 
299 			 	show_folder_button.addSelectionListener(
300 			 		new SelectionAdapter()
301 			 		{
302 			 			public void
303 			 			widgetSelected(
304 			 				SelectionEvent e )
305 			 			{
306 			 				ManagerUtils.open( option_file );
307 			 			}
308 			 		});
309 
310 			 	label = new Label(gJVM, SWT.NULL);
311 				Messages.setLanguageText(label, "jvm.reset");
312 
313 				Button reset_button = new Button( gJVM, SWT.PUSH );
314 
315 			 	Messages.setLanguageText( reset_button, "Button.reset");
316 
317 			 	reset_button.addSelectionListener(
318 			 		new SelectionAdapter()
319 			 		{
320 			 			public void
321 			 			widgetSelected(
322 			 				SelectionEvent event )
323 			 			{
324 			 				try{
325 			 					platform.setExplicitVMOptions( new String[0] );
326 
327 			 					buildOptions( cDisplay, platform, gJVMOptions, true );
328 
329 			 				}catch( Throwable e ){
330 
331 			 					Debug.out( e );
332 			 				}
333 			 			}
334 			 		});
335 
336 			}catch( Throwable e ){
337 
338 				Debug.out( e );
339 
340 				label = new Label(gJVM, SWT.NULL);
341 				Messages.setLanguageText(label, "jvm.error", new String[]{ Debug.getNestedExceptionMessage(e) });
342 				gridData = new GridData();
343 				gridData.horizontalSpan = 2;
344 				Utils.setLayoutData(label,  gridData );
345 			}
346 		}
347 
348 		return cDisplay;
349 	}
350 
351 	private void
buildOptions( final Composite parent, final PlatformManager platform, final Composite area, boolean rebuild )352 	buildOptions(
353 		final Composite			parent,
354 		final PlatformManager	platform,
355 		final Composite			area,
356 		boolean					rebuild )
357 
358 		throws PlatformManagerException
359 	{
360 		if ( rebuild ){
361 
362 			Control[] kids = area.getChildren();
363 
364 			for ( Control k: kids ){
365 				k.dispose();
366 			}
367 		}
368 
369 		String[] options = platform.getExplicitVMOptions();
370 
371 		{
372 				// max mem
373 
374 			long	max_mem = AEJavaManagement.getJVMLongOption( options, "-Xmx" );
375 
376 			final int MIN_MAX_JVM = 32*1024*1024;
377 
378 			GridData gridData = new GridData();
379 			Label label = new Label(area, SWT.NULL);
380 			Utils.setLayoutData(label, gridData);
381 			Messages.setLanguageText(label,	"jvm.max.mem", new String[]{encodeDisplayLong(MIN_MAX_JVM)});
382 
383 			gridData = new GridData();
384 			gridData.widthHint = 125;
385 			final StringParameter max_vm = new StringParameter(area, "jvm.max.mem", "", false );
386 			max_vm.setLayoutData(gridData);
387 
388 			max_vm.setValue( max_mem == -1?"":encodeDisplayLong( max_mem ));
389 
390 			max_vm.addChangeListener(
391 				new ParameterChangeAdapter()
392 				{
393 					private String	last_value;
394 
395 					public void
396 					parameterChanged(
397 						Parameter	p,
398 						boolean		caused_internally )
399 					{
400 						if ( max_vm.isDisposed()){
401 
402 							max_vm.removeChangeListener( this );
403 
404 							return;
405 						}
406 
407 						String val = max_vm.getValue();
408 
409 						if ( last_value != null && last_value.equals( val )){
410 
411 							return;
412 						}
413 
414 						last_value = val;
415 
416 						try{
417 							long max_mem = decodeDisplayLong( val );
418 
419 							if ( max_mem < MIN_MAX_JVM ){
420 
421 								throw( new Exception( "Min=" + encodeDisplayLong( MIN_MAX_JVM )));
422 							}
423 
424 							String[] options = platform.getExplicitVMOptions();
425 
426 							options = AEJavaManagement.setJVMLongOption( options, "-Xmx", max_mem );
427 
428 							long	min_mem = AEJavaManagement.getJVMLongOption( options, "-Xms" );
429 
430 							if ( min_mem == -1 || min_mem > max_mem ){
431 
432 								options = AEJavaManagement.setJVMLongOption( options, "-Xms", max_mem );
433 							}
434 
435 							platform.setExplicitVMOptions( options );
436 
437 							buildOptions( parent, platform, area, true );
438 
439 						}catch( Throwable e ){
440 
441 							String param_name = MessageText.getString( "jvm.max.mem" );
442 
443 							int	pos = param_name.indexOf( '[' );
444 
445 							if ( pos != -1 ){
446 
447 								param_name = param_name.substring( 0, pos ).trim();
448 							}
449 
450 							MessageBoxShell mb =
451 								new MessageBoxShell(
452 									SWT.ICON_ERROR | SWT.OK,
453 									MessageText.getString( "ConfigView.section.invalid.value.title"),
454 									MessageText.getString(
455 										"ConfigView.section.invalid.value",
456 										new String[]{ val, param_name, Debug.getNestedExceptionMessage(e)}));
457 
458 									mb.setParent( parent.getShell());
459 									mb.open(null);
460 						}
461 					}
462 				});
463 
464 			label = new Label(area, SWT.NULL);
465 			gridData = new GridData(GridData.FILL_HORIZONTAL);
466 			Utils.setLayoutData(label, gridData);
467 
468 			Long max_heap_mb = AEJavaManagement.getMaxHeapMB();
469 
470 			if ( max_heap_mb > 0 ){
471 
472 				Messages.setLanguageText(label,	"jvm.max.mem.current", new String[]{ DisplayFormatters.formatByteCountToKiBEtc( max_heap_mb*1024*1024, true )});
473 			}
474 		}
475 
476 		{
477 				// min mem
478 
479 			final int MIN_MIN_JVM = 8*1024*1024;
480 
481 			long	min_mem = AEJavaManagement.getJVMLongOption( options, "-Xms" );
482 
483 			GridData gridData = new GridData();
484 			Label label = new Label(area, SWT.NULL);
485 			Utils.setLayoutData(label, gridData);
486 			Messages.setLanguageText(label,	"jvm.min.mem", new String[]{encodeDisplayLong(MIN_MIN_JVM)});
487 
488 			gridData = new GridData();
489 			gridData.widthHint = 125;
490 			final StringParameter min_vm = new StringParameter(area, "jvm.min.mem", "", false );
491 			min_vm.setLayoutData(gridData);
492 
493 			min_vm.setValue( min_mem == -1?"":encodeDisplayLong( min_mem ));
494 
495 			min_vm.addChangeListener(
496 				new ParameterChangeAdapter()
497 				{
498 					private String	last_value;
499 
500 					public void
501 					parameterChanged(
502 						Parameter	p,
503 						boolean		caused_internally )
504 					{
505 						if ( min_vm.isDisposed()){
506 
507 							min_vm.removeChangeListener( this );
508 
509 							return;
510 						}
511 
512 						String val = min_vm.getValue();
513 
514 						if ( last_value != null && last_value.equals( val )){
515 
516 							return;
517 						}
518 
519 						last_value = val;
520 
521 						try{
522 							long min_mem = decodeDisplayLong( val );
523 
524 							if ( min_mem < MIN_MIN_JVM ){
525 
526 								throw( new Exception( "Min=" + encodeDisplayLong( MIN_MIN_JVM )));
527 							}
528 
529 							String[] options = platform.getExplicitVMOptions();
530 
531 							options = AEJavaManagement.setJVMLongOption( options, "-Xms", min_mem );
532 
533 							long	max_mem = AEJavaManagement.getJVMLongOption( options, "-Xmx" );
534 
535 							if ( max_mem == -1 || max_mem < min_mem ){
536 
537 								options = AEJavaManagement.setJVMLongOption( options, "-Xmx", min_mem );
538 							}
539 
540 							platform.setExplicitVMOptions( options );
541 
542 							buildOptions( parent, platform, area, true );
543 
544 						}catch( Throwable e ){
545 
546 							String param_name = MessageText.getString( "jvm.min.mem" );
547 
548 							int	pos = param_name.indexOf( '[' );
549 
550 							if ( pos != -1 ){
551 
552 								param_name = param_name.substring( 0, pos ).trim();
553 							}
554 
555 							MessageBoxShell mb =
556 								new MessageBoxShell(
557 									SWT.ICON_ERROR | SWT.OK,
558 									MessageText.getString( "ConfigView.section.invalid.value.title"),
559 									MessageText.getString(
560 										"ConfigView.section.invalid.value",
561 										new String[]{ val, param_name, Debug.getNestedExceptionMessage(e)}));
562 
563 									mb.setParent( parent.getShell());
564 									mb.open(null);
565 						}
566 					}
567 				});
568 
569 			label = new Label(area, SWT.NULL);
570 			gridData = new GridData(GridData.FILL_HORIZONTAL);
571 			Utils.setLayoutData(label, gridData);
572 		}
573 
574 		{
575 				// max DIRECT mem
576 
577 			final int MIN_DIRECT_JVM = 32*1024*1024;
578 
579 			final String OPTION_KEY = "-XX:MaxDirectMemorySize=";
580 
581 			long	max_direct = AEJavaManagement.getJVMLongOption( options, OPTION_KEY );
582 
583 			GridData gridData = new GridData();
584 			Label label = new Label(area, SWT.NULL);
585 			Utils.setLayoutData(label, gridData);
586 			Messages.setLanguageText(label,	"jvm.max.direct.mem", new String[]{encodeDisplayLong(MIN_DIRECT_JVM)});
587 
588 			gridData = new GridData();
589 			gridData.widthHint = 125;
590 			final StringParameter max_direct_vm = new StringParameter(area, "jvm.max.direct.mem", "", false );
591 			max_direct_vm.setLayoutData(gridData);
592 
593 			max_direct_vm.setValue( max_direct == -1?"":encodeDisplayLong( max_direct ));
594 
595 			max_direct_vm.addChangeListener(
596 				new ParameterChangeAdapter()
597 				{
598 					private String	last_value;
599 
600 					public void
601 					parameterChanged(
602 						Parameter	p,
603 						boolean		caused_internally )
604 					{
605 						if ( max_direct_vm.isDisposed()){
606 
607 							max_direct_vm.removeChangeListener( this );
608 
609 							return;
610 						}
611 
612 						String val = max_direct_vm.getValue();
613 
614 						if ( last_value != null && last_value.equals( val )){
615 
616 							return;
617 						}
618 
619 						last_value = val;
620 
621 						try{
622 							long max_direct = decodeDisplayLong( val );
623 
624 							if ( max_direct < MIN_DIRECT_JVM ){
625 
626 								throw( new Exception( "Min=" + encodeDisplayLong( MIN_DIRECT_JVM )));
627 							}
628 
629 							String[] options = platform.getExplicitVMOptions();
630 
631 							options = AEJavaManagement.setJVMLongOption( options, OPTION_KEY, max_direct );
632 
633 							platform.setExplicitVMOptions( options );
634 
635 							buildOptions( parent, platform, area, true );
636 
637 						}catch( Throwable e ){
638 
639 							String param_name = MessageText.getString( "jvm.max.direct.mem" );
640 
641 							int	pos = param_name.indexOf( '[' );
642 
643 							if ( pos != -1 ){
644 
645 								param_name = param_name.substring( 0, pos ).trim();
646 							}
647 
648 							MessageBoxShell mb =
649 								new MessageBoxShell(
650 									SWT.ICON_ERROR | SWT.OK,
651 									MessageText.getString( "ConfigView.section.invalid.value.title"),
652 									MessageText.getString(
653 										"ConfigView.section.invalid.value",
654 										new String[]{ val, param_name, Debug.getNestedExceptionMessage(e)}));
655 
656 									mb.setParent( parent.getShell());
657 									mb.open(null);
658 						}
659 					}
660 				});
661 
662 			label = new Label(area, SWT.NULL);
663 			gridData = new GridData(GridData.FILL_HORIZONTAL);
664 			Utils.setLayoutData(label, gridData);
665 			Messages.setLanguageText(label,	"jvm.max.direct.mem.info" );
666 		}
667 
668 			// all options
669 
670 		Label label = new Label(area, SWT.NULL);
671 		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
672 		gridData.horizontalSpan = 3;
673 		Utils.setLayoutData(label, gridData);
674 		Messages.setLanguageText(label,	"jvm.options.summary" );
675 
676 		for ( String option: options ){
677 
678 			label = new Label(area, SWT.NULL);
679 			label.setText( option );
680 			gridData = new GridData( );
681 			gridData.horizontalSpan = 3;
682 			gridData.horizontalIndent = 20;
683 			Utils.setLayoutData(label,  gridData );
684 		}
685 
686 		if ( rebuild ){
687 
688 			parent.layout( true, true );
689 		}
690 	}
691 
692 	private static  String
encodeDisplayLong( long val )693 	encodeDisplayLong(
694 		long		val )
695 	{
696 		if ( val < 1024 ){
697 
698 			return( String.valueOf( val ));
699 		}
700 
701 		val = val/1024;
702 
703 		if ( val < 1024 ){
704 
705 			return( String.valueOf( val ) + " KB" );
706 		}
707 
708 		val = val/1024;
709 
710 		if ( val < 1024 ){
711 
712 			return( String.valueOf( val ) + " MB" );
713 		}
714 
715 		val = val/1024;
716 
717 		return( String.valueOf( val ) + " GB" );
718 	}
719 
720 	private static  long
decodeDisplayLong( String val )721 	decodeDisplayLong(
722 		String		val )
723 
724 		throws Exception
725 	{
726 		char[] chars = val.trim().toCharArray();
727 
728 		String	digits = "";
729 		String	units = "";
730 
731 		for ( char c: chars ){
732 
733 			if ( Character.isDigit( c )){
734 
735 				if ( units.length() > 0 ){
736 
737 					throw( new Exception( "Invalid unit" ));
738 				}
739 
740 				digits += c;
741 
742 			}else{
743 
744 				if ( digits.length() == 0 ){
745 
746 					throw( new Exception( "Missing digits" ));
747 
748 				}else if ( units.length() == 0 && Character.isWhitespace( c )){
749 
750 				}else{
751 
752 					units += c;
753 				}
754 			}
755 		}
756 
757 		long value = Long.parseLong( digits );
758 
759 		if( units.length() == 0 ){
760 
761 			units = "m";
762 		}
763 
764 		if ( units.length() > 0 ){
765 
766 			char c = Character.toLowerCase( units.charAt(0));
767 
768 			if ( c == 'k' ){
769 
770 				value = value * 1024;
771 
772 			}else if ( c == 'm' ){
773 
774 				value = value * 1024 * 1024;
775 
776 			}else if ( c == 'g' ){
777 
778 				value = value * 1024 * 1024 * 1024;
779 
780 			}else{
781 
782 				throw( new Exception( "Invalid size unit '" + units + "'" ));
783 			}
784 		}
785 
786 		return( value );
787 	}
788 
789 	private static String[][]
getActionDetails()790 	getActionDetails()
791 	{
792 		final PlatformManager platform = PlatformManagerFactory.getPlatformManager();
793 
794 	    int	shutdown_types = platform.getShutdownTypes();
795 
796 		List<String>	l_action_values = new ArrayList<String>();
797 		List<String>	l_action_descs 	= new ArrayList<String>();
798 
799 		l_action_values.add( "Nothing" );
800 		l_action_values.add( "QuitVuze" );
801 
802 		if (( shutdown_types & PlatformManager.SD_SLEEP ) != 0 ){
803 
804 			l_action_values.add( "Sleep" );
805 		}
806 		if (( shutdown_types & PlatformManager.SD_HIBERNATE ) != 0 ){
807 
808 			l_action_values.add( "Hibernate" );
809 		}
810 		if (( shutdown_types & PlatformManager.SD_SHUTDOWN ) != 0 ){
811 
812 			l_action_values.add( "Shutdown" );
813 		}
814 
815 		l_action_values.add( "RunScript" );
816 		l_action_values.add( "RunScriptAndClose" );
817 
818 		String[] action_values = l_action_values.toArray( new String[ l_action_values.size()]);
819 
820 		for ( String s: action_values ){
821 
822 			l_action_descs.add( MessageText.getString( "ConfigView.label.stop." + s ));
823 		}
824 
825 		String[] action_descs = l_action_descs.toArray( new String[ l_action_descs.size()]);
826 
827 		return( new String[][]{ action_descs, action_values });
828 	}
829 
830 	public static void
addDoneDownloadingOption( Composite comp, boolean include_script_setting )831 	addDoneDownloadingOption(
832 		Composite		comp,
833 		boolean			include_script_setting )
834 	{
835 		GridData gridData = new GridData();
836 		Label label = new Label(comp, SWT.NULL);
837 	    Messages.setLanguageText(label, "ConfigView.label.stop.downcomp");
838 	    Utils.setLayoutData(label,  gridData );
839 
840 	    String[][]	action_details = getActionDetails();
841 
842 		final StringListParameter dc = new StringListParameter(comp, "On Downloading Complete Do", "Nothing", action_details[0], action_details[1] );
843 
844 		if ( include_script_setting ){
845 
846 			final Label dc_label = new Label(comp, SWT.NONE);
847 			Messages.setLanguageText(dc_label, "label.script.to.run");
848 			Utils.setLayoutData(dc_label, new GridData());
849 
850 			gridData = new GridData(GridData.FILL_HORIZONTAL);
851 			final FileParameter dc_script = new FileParameter(comp, "On Downloading Complete Script", "", new String[0]);
852 			dc_script.setLayoutData(gridData);
853 
854 			boolean	is_script = dc.getValue().startsWith( "RunScript" );
855 
856 			dc_label.setEnabled( is_script );
857 			dc_script.setEnabled( is_script );
858 
859 			dc.addChangeListener(
860 				new ParameterChangeAdapter()
861 				{
862 					public void
863 					parameterChanged(
864 						Parameter	p,
865 						boolean		caused_internally )
866 					{
867 						boolean	is_script = dc.getValue().startsWith( "RunScript" );
868 
869 						dc_label.setEnabled( is_script );
870 						dc_script.setEnabled( is_script );
871 					}
872 				});
873 		}
874 	}
875 
876 	private static void
addDoneSeedingOption( Composite comp, boolean include_script_setting )877 	addDoneSeedingOption(
878 		Composite		comp,
879 		boolean			include_script_setting )
880 	{
881 		GridData gridData = new GridData();
882 		Label label = new Label(comp, SWT.NULL);
883 	    Messages.setLanguageText(label, "ConfigView.label.stop.seedcomp");
884 	    Utils.setLayoutData(label,  gridData );
885 
886 	    String[][]	action_details = getActionDetails();
887 
888 	    final StringListParameter sc = new StringListParameter(comp, "On Seeding Complete Do", "Nothing", action_details[0], action_details[1] );
889 
890 	    if ( include_script_setting ){
891 
892 			final Label sc_label = new Label(comp, SWT.NONE);
893 			Messages.setLanguageText(sc_label, "label.script.to.run");
894 			Utils.setLayoutData(sc_label, new GridData());
895 			gridData = new GridData(GridData.FILL_HORIZONTAL);
896 			final FileParameter sc_script = new FileParameter(comp, "On Seeding Complete Script", "", new String[0]);
897 			sc_script.setLayoutData(gridData);
898 
899 			boolean is_script = sc.getValue().startsWith( "RunScript" );
900 
901 			sc_label.setEnabled( is_script );
902 			sc_script.setEnabled( is_script );
903 
904 			sc.addChangeListener(
905 				new ParameterChangeAdapter()
906 				{
907 					public void
908 					parameterChanged(
909 						Parameter	p,
910 						boolean		caused_internally )
911 					{
912 						boolean	is_script = sc.getValue().startsWith( "RunScript" );
913 
914 						sc_label.setEnabled( is_script );
915 						sc_script.setEnabled( is_script );
916 					}
917 				});
918 	    }
919 	}
920 }
921