2009-10-20 4 views
0

그래서 저는 직장에서 플러그인을 작업 중이며 ContentProposalAdapter를 사용할 수있는 상황에 처했습니다. 기본적으로 누군가가 다른 사람의 이름을 입력하기 시작하면 현재 쿼리와 일치하는 이름 목록이 선행 형식 (a la Google)으로 반환됩니다. 그래서 IContentProposalProvider 클래스를 만들었습니다.이 클래스는 getProposals() 메서드를 호출 할 때 백그라운드에서 제안을 가져 오는 스레드를 처리합니다. 내가 겪고있는 문제는 HTTP를 통해 제안서를받는 처리가 이루어지는 경쟁 조건에 부딪혀 실제로 제안을 받기 전에 제안을 얻으려고한다는 것입니다.인터넷에서 JFace ContentProposalAdapter를 채우려면 어떻게해야합니까?

이제 스레드 지옥 문제에 부딪치지 않으려 고합니다. 어쨌든 저를 멀리하게하지는 않습니다. 그래서, 지금까지 제가 한 일이 있습니다. 누구든지 내가 할 수있는 것에 관해 어떤 제안이 있습니까? 좋아

public class ProfilesProposalProvider implements IContentProposalProvider, PropertyChangeListener { 

    private IContentProposal[] props; 

    @Override 
    public IContentProposal[] getProposals(String arg0, int arg1) { 
     Display display = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay(); 

     RunProfilesJobThread t1 = new RunProfilesJobThread(arg0, display); 
     t1.run(); 

     return props; 
    } 

    @Override 
    public void propertyChange(PropertyChangeEvent arg0) { 
     if (arg0.getSource() instanceof RunProfilesJobThread){ 
     RunProfilesJobThread thread = (RunProfilesJobThread)arg0.getSource(); 
     props = thread.getProps(); 

     } 
    } 
    } 



public class RunProfilesJobThread extends Thread { 

private ProfileProposal[] props; 
private Display display; 
private String query; 

public RunProfilesJobThread(String query, Display display){ 
    this.query = query; 
} 

@Override 
public void run() { 
    if (!(query.equals(""))){ 
    GetProfilesJob job = new GetProfilesJob("profiles", query); 
    job.schedule(); 

    try { 
    job.join(); 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 
    } 

    GetProfilesJobInfoThread thread = new GetProfilesJobInfoThread(job.getResults()); 

    try { 
    thread.join(); 
    } catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 


    props = thread.getProps(); 
    } 
} 

public ProfileProposal[] getProps(){ 
    return props; 
} 
} 

public class GetProfilesJobInfoThread extends Thread { 

    private ArrayList<String> names; 
    private ProfileProposal[] props; 

    public GetProfilesJobInfoThread(ArrayList<String> names){ 
     this.names = names; 
    } 

    @Override 
    public void run() { 
     if (names != null){ 
     props = new ProfileProposal[names.size()]; 
     for (int i = 0; i < props.length - 1; i++){ 
     ProfileProposal temp = new ProfileProposal(names.get(i), names.get(i)); 
     props[i] = temp; 
     } 
     } 
    } 

    public ProfileProposal[] getProps(){ 
     return props; 
    } 
    } 

답변

0

, 내가 그것을 시도 할 것이다 ...

나는 그것을 실행하려고하지 않은,하지만 그것은 어느 정도 작동합니다. 적어도 그것은 좋은 시작입니다. 질문이 있으시면 언제든지 물어보십시오.

public class ProfilesProposalProvider implements IContentProposalProvider { 
    private List<IContentProposal> proposals; 
    private String proposalQuery; 
    private Thread retrievalThread; 

    public void setProposals(List<IContentProposal> proposals, String query) { 
     synchronized(this) { 
      this.proposals = proposals; 
      this.proposalQuery = query; 
     } 
    } 

    public IContentProposal[] getProposals(String contents, int position) { 
     // Synchronize incoming thread and retrieval thread, so that the proposal list 
     // is not replaced while we're processing it. 
     synchronized(this) { 
      /** 
      * Get proposals if query is longer than one char, or if the current list of proposals does with a different 
      * prefix than the new query, and only if the current retrieval thread is finished. 
      */ 
      if (retrievalThread == null && contents.length() > 1 && (proposals == null || !contents.startsWith(proposalQuery))) { 
       getProposals(contents); 
      } 

      /** 
      * Select valid proposals from retrieved list. 
      */ 
      if (proposals != null) { 
       List<IContentProposal> validProposals = new ArrayList<IContentProposal>(); 
       for (IContentProposal prop : proposals) { 
        if(prop == null) { 
         continue; 
        } 
        String propVal = prop.getContent(); 
        if (isProposalValid(propVal, contents)) { 
         validProposals.add(prop); 
        } 
       } 
       return validProposals.toArray(new IContentProposal[ validProposals.size() ]); 
      } 
     } 

     return new IContentProposal[0]; 
    } 

    protected void getProposals(final String query) { 
     retrievalThread = new Thread() { 
      @Override 
      public void run() { 
       GetProfilesJob job = new GetProfilesJob("profiles", query); 
       job.schedule(); 

       try { 
        job.join(); 

        ArrayList<String> names = job.getResults(); 
        if (names != null){ 
         List<IContentProposal> props = new ArrayList<IContentProposal>(); 
         for (String name : names) { 
          props.add(new ProfileProposal(name, name)); 
         } 
         setProposals(props, query); 
        } 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       retrievalThread = null; 
      } 
     }; 
     retrievalThread.start(); 
    } 

    protected boolean isProposalValid(String proposalValue, String contents) { 
     return (proposalValue.length() >= contents.length() && proposalValue.substring(0, contents.length()).equalsIgnoreCase(contents)); 
    } 
} 
관련 문제