FileDocCategorySizeDatePackage
LocaleUtilSWT.javaAPI DocAzureus 3.0.3.410013Thu Jul 13 09:17:24 BST 2006org.gudy.azureus2.ui.swt

LocaleUtilSWT

public class LocaleUtilSWT extends Object implements LocaleUtilListener
author
Tobias Minich

Fields Summary
protected static boolean
rememberEncodingDecision
protected static LocaleUtilDecoder
rememberedDecoder
protected static Object
remembered_on_behalf_of
Constructors Summary
public LocaleUtilSWT(com.aelitis.azureus.core.AzureusCore core)

  

   
  
  			  
  
  	LocaleTorrentUtil.addListener( this );
  
Methods Summary
private voidabandonSelection(Shell s)

    s.dispose();
  
public LocaleUtilDecoderCandidateselectDecoder(LocaleUtil locale_util, java.lang.Object decision_owner, LocaleUtilDecoderCandidate[] candidates)

  	if ( decision_owner != remembered_on_behalf_of ){
  		
  		remembered_on_behalf_of		= decision_owner;
  		rememberedDecoder			= null;
  	}
  	
    if( rememberEncodingDecision && rememberedDecoder != null) {
    	
      for (int i = 0; i < candidates.length; i++) {
      	
        if(candidates[i].getValue() != null && rememberedDecoder == candidates[i].getDecoder()) {
          return( candidates[i] );
        }
      }
    }

    LocaleUtilDecoderCandidate	default_candidate	= candidates[0];
    
    String defaultString = candidates[0].getValue();

    Arrays.sort(candidates);
   
    boolean always_prompt = COConfigurationManager.getBooleanParameter("File.Decoder.Prompt", false );
    
    if ( !always_prompt ){
    	
    	int minlength = candidates[0].getValue().length();
   
	    // If the default string length == minlength assumes that
	    // the array encoding is from default charset
    	
	    if (defaultString != null && defaultString.length() == minlength) {
	      return( null );
	    }

	    	// see if we can try and apply a default encoding
	    
	    String	default_name = COConfigurationManager.getStringParameter( "File.Decoder.Default", "" );
	    
	    if ( default_name.length() > 0 ){
			for (int i = 0; i < candidates.length; i++) {
			  if(candidates[i].getValue() != null && candidates[i].getDecoder().getName().equals( default_name )) {
	        	
				return( candidates[i] );
			  }
			}
		}
    }
    
    ArrayList choosableCandidates = new ArrayList();
    
    	// Always stick the default candidate in position 0 if valid
    
    if ( defaultString != null ){
    	
    	choosableCandidates.add( default_candidate );  	
    }
    
    LocaleUtilDecoder[]	general_decoders = locale_util.getGeneralDecoders();
    
    	// 	add all general candidates with names not already in the list
    
    for (int j = 0; j < general_decoders.length; j++) {
    	
      for (int i = 0; i < candidates.length; i++) {
      	
      	if (candidates[i].getValue()==null || candidates[i].getDecoder()==null) continue;
      	
	        if(		general_decoders[j] != null && 
	        		general_decoders[j].getName().equals(candidates[i].getDecoder().getName())){
	        	
	        	if (!choosableCandidates.contains(candidates[i])) {
	       
	        		choosableCandidates.add(candidates[i]);
	        		
	        		break;
	        	}
	        }
      	}
    }
    
    	// add the remaining possible locales
    
   	for (int i = 0; i < candidates.length; i++){
   		
   		if (candidates[i].getValue()==null || candidates[i].getDecoder()==null) continue;
   		
   		if (!choosableCandidates.contains(candidates[i])){
   			
   			choosableCandidates.add(candidates[i]);
   		}
   	}
 

    final LocaleUtilDecoderCandidate[] candidatesToChoose = (LocaleUtilDecoderCandidate[]) choosableCandidates.toArray(new LocaleUtilDecoderCandidate[choosableCandidates.size()]);
    final LocaleUtilDecoderCandidate[] selected_candidate = {null};
        
    // Run Synchronously, since we want the results
    Utils.execSWTThread(new AERunnable() {
      public void runSupport() {
      	try{
        	showChoosableEncodingWindow(Utils.findAnyShell(), 
        			candidatesToChoose,selected_candidate);
        	
      	}catch( Throwable e ){
      		
      		Debug.printStackTrace( e );
      		
        }
      }
    }, false);
    
    if ( selected_candidate[0] == null ){
    
    	throw( new LocaleUtilEncodingException( true ));
    }else{

    	return ( selected_candidate[0] );
    }
  
private voidsetSelectedIndex(Shell s, Table table, Button checkBox, LocaleUtilDecoderCandidate[] candidates, LocaleUtilDecoderCandidate[] selected_candidate)

    int selectedIndex = table.getSelectionIndex();
    
    if(-1 == selectedIndex) 
      return;
    
    rememberEncodingDecision = checkBox.getSelection();
        
    selected_candidate[0]	= candidates[selectedIndex];
    
	if ( rememberEncodingDecision ){
		
		rememberedDecoder = selected_candidate[0].getDecoder();
	}else{
		rememberedDecoder = null;
	}

    s.dispose();
  
private voidshowChoosableEncodingWindow(Shell shell, LocaleUtilDecoderCandidate[] candidates, LocaleUtilDecoderCandidate[] selected_candidate)

    final Shell s = org.gudy.azureus2.ui.swt.components.shell.ShellFactory
				.createShell(shell, SWT.RESIZE | SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
    Utils.setShellIcon(s);
    s.setText(MessageText.getString("LocaleUtil.title")); //$NON-NLS-1$
    GridData gridData;
    s.setLayout(new GridLayout(1, true));
    
    Label label = new Label(s, SWT.LEFT);
    Messages.setLanguageText(label, "LocaleUtil.label.chooseencoding"); //$NON-NLS-1$

    final Table table = new Table(s, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL);
    gridData = new GridData( GridData.FILL_BOTH );
    table.setLayoutData(gridData);
    
    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    String[] titlesPieces = { "encoding", "text" };
    for (int i = 0; i < titlesPieces.length; i++) {
      TableColumn column = new TableColumn(table, SWT.LEFT);
      Messages.setLanguageText(column, "LocaleUtil.column." + titlesPieces[i]);
    }

    // add candidates to table
    for (int i = 0; i < candidates.length; i++) {
      TableItem item = new TableItem(table, SWT.NULL);
      String name = candidates[i].getDecoder().getName();
      item.setText(0, name);
      item.setText(1, candidates[i].getValue());
    }
    int lastSelectedIndex = 0;
    for (int i = 1; i < candidates.length; i++) {
      if(candidates[i].getValue() != null && candidates[i].getDecoder() == rememberedDecoder ) {
        lastSelectedIndex = i;
        break;
      }
    }
    table.select(lastSelectedIndex);

    // resize all columns to fit the widest entry 
    table.getColumn(0).pack();
    table.getColumn(1).pack();

    label = new Label(s, SWT.LEFT);
    Messages.setLanguageText(label, "LocaleUtil.label.hint.doubleclick"); //$NON-NLS-1$

    Composite composite = new Composite(s,SWT.NULL);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    composite.setLayoutData(gridData);
    
    GridLayout subLayout  = new GridLayout();
    subLayout.numColumns = 2;
    
    composite.setLayout(subLayout);
    
    final Button checkBox = new Button(composite, SWT.CHECK);
    checkBox.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
    checkBox.setSelection(rememberEncodingDecision);
    Messages.setLanguageText(checkBox, "LocaleUtil.label.checkbox.rememberdecision"); //$NON-NLS-1$, "LocaleUtil.label.checkbox.rememberdecision"); //$NON-NLS-1$
       
    final Button ok = new Button(composite, SWT.PUSH);
    ok.setText(" ".concat(MessageText.getString("Button.next")).concat(" ")); //$NON-NLS-1$ //$NON-NLS-3$ //$NON-NLS-2$
    gridData = new GridData(GridData.END);
    gridData.widthHint = 100;
    ok.setLayoutData(gridData);
    

    
    s.setSize(500,500);
    s.layout();
    
    Utils.centreWindow(s);
 
    ok.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
      	//abandonSelection(s);
        setSelectedIndex(s, table, checkBox, candidates,selected_candidate);
        s.dispose();
      }
    });

    table.addMouseListener(new MouseAdapter() {
      public void mouseDoubleClick(MouseEvent mEvent) {
        setSelectedIndex(s, table, checkBox, candidates,selected_candidate);
        s.dispose();
      }
    });

    s.open();
    while (!s.isDisposed()) {
      if (!s.getDisplay().readAndDispatch()) {
      	s.getDisplay().sleep();
      }
    }