~motu-torrent/azureus/upstream-stable

« back to all changes in this revision

Viewing changes to org/gudy/azureus2/ui/swt/components/StringListChooser.java

  • Committer: John Dong
  • Date: 2007-10-22 04:54:13 UTC
  • Revision ID: john.dong@gmail.com-20071022045413-3ovb11u82rrcokxx
Commit 3.0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2005, 2006 Aelitis, All Rights Reserved.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License
6
 
 * as published by the Free Software Foundation; either version 2
7
 
 * of the License, or (at your option) any later version.
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 * You should have received a copy of the GNU General Public License
13
 
 * along with this program; if not, write to the Free Software
14
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15
 
 *
16
 
 * AELITIS, SAS au capital de 46,603.30 euros
17
 
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
18
 
 */
19
 
package org.gudy.azureus2.ui.swt.components;
20
 
 
21
 
import org.eclipse.swt.SWT;
22
 
import org.eclipse.swt.layout.GridData;
23
 
import org.eclipse.swt.layout.GridLayout;
24
 
import org.eclipse.swt.widgets.Button;
25
 
import org.eclipse.swt.widgets.Combo;
26
 
import org.eclipse.swt.widgets.Display;
27
 
import org.eclipse.swt.widgets.Event;
28
 
import org.eclipse.swt.widgets.Label;
29
 
import org.eclipse.swt.widgets.Listener;
30
 
import org.eclipse.swt.widgets.Shell;
31
 
import org.gudy.azureus2.core3.internat.MessageText;
32
 
import org.gudy.azureus2.core3.util.AERunnable;
33
 
import org.gudy.azureus2.core3.util.Constants;
34
 
import org.gudy.azureus2.core3.util.Debug;
35
 
import org.gudy.azureus2.ui.swt.ImageRepository;
36
 
import org.gudy.azureus2.ui.swt.Utils;
37
 
import org.gudy.azureus2.ui.swt.components.shell.ShellFactory;
38
 
 
39
 
public class StringListChooser {
40
 
 
41
 
  private Display display;  
42
 
  private Shell shell;
43
 
  private Label label;
44
 
  private Combo combo;
45
 
  
46
 
  private String result;  
47
 
  
48
 
  public StringListChooser(final Shell parentShell) {
49
 
    result = null;
50
 
    
51
 
    display = parentShell.getDisplay();
52
 
    if(display == null || display.isDisposed()) return;
53
 
    display.syncExec(new Runnable() {
54
 
      public void run() {
55
 
       createShell(parentShell); 
56
 
      }
57
 
    });
58
 
  }
59
 
  
60
 
  private void createShell(Shell parentShell) {
61
 
      
62
 
    shell = ShellFactory.createShell(display,SWT.APPLICATION_MODAL | SWT.BORDER | SWT.TITLE | SWT.CLOSE);
63
 
    if(!Constants.isOSX) {
64
 
      shell.setImage(ImageRepository.getImage("azureus"));
65
 
    }
66
 
    
67
 
    GridLayout layout = new GridLayout();    
68
 
    layout.numColumns = 2;
69
 
    shell.setLayout(layout);
70
 
    GridData data;
71
 
    
72
 
    label = new Label(shell,SWT.WRAP);
73
 
    
74
 
    combo = new Combo(shell,SWT.READ_ONLY);
75
 
    
76
 
    Button ok = new Button(shell,SWT.PUSH);
77
 
    ok.addListener(SWT.Selection, new Listener() {
78
 
      public void handleEvent(Event arg0) {
79
 
       result = combo.getText();
80
 
       shell.dispose();       
81
 
      }
82
 
    });
83
 
    ok.setText(MessageText.getString("Button.ok"));
84
 
    
85
 
    Button cancel = new Button(shell,SWT.PUSH);
86
 
    cancel.addListener(SWT.Selection, new Listener() {
87
 
      public void handleEvent(Event arg0) {
88
 
          
89
 
          result = null;
90
 
       
91
 
       shell.dispose();       
92
 
      }
93
 
    });
94
 
    cancel.setText(MessageText.getString("Button.cancel"));
95
 
    
96
 
    
97
 
    shell.addListener(SWT.Dispose,new Listener() {
98
 
      public void handleEvent(Event arg0) {
99
 
      }
100
 
    });
101
 
    
102
 
    data = new GridData(GridData.FILL_HORIZONTAL);
103
 
    data.horizontalSpan = 2;
104
 
    data.heightHint = 30;
105
 
    label.setLayoutData(data);
106
 
    
107
 
    data = new GridData(GridData.FILL_HORIZONTAL);
108
 
    data.horizontalSpan = 2;
109
 
    combo.setLayoutData(data);
110
 
        
111
 
    data = new GridData();
112
 
    data.widthHint = 80;
113
 
    data.grabExcessHorizontalSpace = true;
114
 
    data.grabExcessVerticalSpace = true;
115
 
    data.verticalAlignment = SWT.END;
116
 
    data.horizontalAlignment = SWT.END;
117
 
    ok.setLayoutData(data);
118
 
    
119
 
    data = new GridData();
120
 
    data.grabExcessVerticalSpace = true;
121
 
    data.verticalAlignment = SWT.END;
122
 
    data.widthHint = 80;    
123
 
    cancel.setLayoutData(data);
124
 
    
125
 
    shell.setSize(300,150);
126
 
    shell.layout();
127
 
    
128
 
    Utils.centerWindowRelativeTo(shell,parentShell);
129
 
    
130
 
  }
131
 
  
132
 
  public void setTitle(final String title) {
133
 
    Utils.execSWTThread(new AERunnable() {    
134
 
      public void runSupport() {
135
 
        if(display == null || display.isDisposed()) return;
136
 
        shell.setText(title);
137
 
      }    
138
 
    });
139
 
  }
140
 
  
141
 
  public void setText(final String text) {
142
 
        Utils.execSWTThread(new AERunnable() {    
143
 
      public void runSupport() {
144
 
        if(display == null || display.isDisposed()) return;
145
 
        label.setText(text.replaceAll("&", "&&"));
146
 
      }    
147
 
    });
148
 
  }
149
 
  
150
 
  public void addOption(final String option) {
151
 
    Utils.execSWTThread(new AERunnable() {    
152
 
      public void runSupport() {
153
 
        if(display == null || display.isDisposed()) return;
154
 
        combo.add(option);
155
 
        if(combo.getItemCount() == 1) {
156
 
          combo.setText(option);
157
 
        }
158
 
      }    
159
 
    });
160
 
  }
161
 
  
162
 
  public String open() {
163
 
    if(display == null || display.isDisposed()) return null;    
164
 
    Utils.execSWTThread(new AERunnable() {    
165
 
      public void 
166
 
      runSupport() 
167
 
      {
168
 
        if(display == null || display.isDisposed()) {
169
 
                return;    
170
 
        }
171
 
          try{
172
 
                  shell.open();
173
 
            while (!shell.isDisposed())
174
 
              if (!display.readAndDispatch()) display.sleep();
175
 
                  
176
 
          }catch( Throwable e ){
177
 
                  
178
 
                  Debug.printStackTrace( e );
179
 
          }
180
 
      }    
181
 
    });
182
 
    
183
 
    return result;
184
 
  }
185
 
  
186
 
}
 
1
/*
 
2
 * Copyright (C) 2005, 2006 Aelitis, All Rights Reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 * You should have received a copy of the GNU General Public License
 
13
 * along with this program; if not, write to the Free Software
 
14
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
15
 *
 
16
 * AELITIS, SAS au capital de 46,603.30 euros
 
17
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 
18
 */
 
19
package org.gudy.azureus2.ui.swt.components;
 
20
 
 
21
import org.eclipse.swt.SWT;
 
22
import org.eclipse.swt.layout.GridData;
 
23
import org.eclipse.swt.layout.GridLayout;
 
24
import org.eclipse.swt.widgets.Button;
 
25
import org.eclipse.swt.widgets.Combo;
 
26
import org.eclipse.swt.widgets.Display;
 
27
import org.eclipse.swt.widgets.Event;
 
28
import org.eclipse.swt.widgets.Label;
 
29
import org.eclipse.swt.widgets.Listener;
 
30
import org.eclipse.swt.widgets.Shell;
 
31
import org.gudy.azureus2.core3.internat.MessageText;
 
32
import org.gudy.azureus2.core3.util.AERunnable;
 
33
import org.gudy.azureus2.core3.util.Constants;
 
34
import org.gudy.azureus2.core3.util.Debug;
 
35
import org.gudy.azureus2.ui.swt.ImageRepository;
 
36
import org.gudy.azureus2.ui.swt.Utils;
 
37
import org.gudy.azureus2.ui.swt.components.shell.ShellFactory;
 
38
 
 
39
public class StringListChooser {
 
40
 
 
41
  private Display display;  
 
42
  private Shell shell;
 
43
  private Label label;
 
44
  private Combo combo;
 
45
  
 
46
  private String result;  
 
47
  
 
48
  public StringListChooser(final Shell parentShell) {
 
49
    result = null;
 
50
    
 
51
    display = parentShell.getDisplay();
 
52
    if(display == null || display.isDisposed()) return;
 
53
    display.syncExec(new Runnable() {
 
54
      public void run() {
 
55
       createShell(parentShell); 
 
56
      }
 
57
    });
 
58
  }
 
59
  
 
60
  private void createShell(Shell parentShell) {
 
61
      
 
62
    shell = ShellFactory.createShell(display,SWT.APPLICATION_MODAL | SWT.BORDER | SWT.TITLE | SWT.CLOSE);
 
63
    if(!Constants.isOSX) {
 
64
      shell.setImage(ImageRepository.getImage("azureus"));
 
65
    }
 
66
    
 
67
    GridLayout layout = new GridLayout();    
 
68
    layout.numColumns = 2;
 
69
    shell.setLayout(layout);
 
70
    GridData data;
 
71
    
 
72
    label = new Label(shell,SWT.WRAP);
 
73
    
 
74
    combo = new Combo(shell,SWT.READ_ONLY);
 
75
    
 
76
    Button ok = new Button(shell,SWT.PUSH);
 
77
    ok.addListener(SWT.Selection, new Listener() {
 
78
      public void handleEvent(Event arg0) {
 
79
       result = combo.getText();
 
80
       shell.dispose();       
 
81
      }
 
82
    });
 
83
    ok.setText(MessageText.getString("Button.ok"));
 
84
    
 
85
    Button cancel = new Button(shell,SWT.PUSH);
 
86
    cancel.addListener(SWT.Selection, new Listener() {
 
87
      public void handleEvent(Event arg0) {
 
88
          
 
89
          result = null;
 
90
       
 
91
       shell.dispose();       
 
92
      }
 
93
    });
 
94
    cancel.setText(MessageText.getString("Button.cancel"));
 
95
    
 
96
    
 
97
    shell.addListener(SWT.Dispose,new Listener() {
 
98
      public void handleEvent(Event arg0) {
 
99
      }
 
100
    });
 
101
    
 
102
    data = new GridData(GridData.FILL_HORIZONTAL);
 
103
    data.horizontalSpan = 2;
 
104
    data.heightHint = 30;
 
105
    label.setLayoutData(data);
 
106
    
 
107
    data = new GridData(GridData.FILL_HORIZONTAL);
 
108
    data.horizontalSpan = 2;
 
109
    combo.setLayoutData(data);
 
110
        
 
111
    data = new GridData();
 
112
    data.widthHint = 80;
 
113
    data.grabExcessHorizontalSpace = true;
 
114
    data.grabExcessVerticalSpace = true;
 
115
    data.verticalAlignment = SWT.END;
 
116
    data.horizontalAlignment = SWT.END;
 
117
    ok.setLayoutData(data);
 
118
    
 
119
    data = new GridData();
 
120
    data.grabExcessVerticalSpace = true;
 
121
    data.verticalAlignment = SWT.END;
 
122
    data.widthHint = 80;    
 
123
    cancel.setLayoutData(data);
 
124
    
 
125
    shell.setSize(300,150);
 
126
    shell.layout();
 
127
    
 
128
    Utils.centerWindowRelativeTo(shell,parentShell);
 
129
    
 
130
  }
 
131
  
 
132
  public void setTitle(final String title) {
 
133
    Utils.execSWTThread(new AERunnable() {    
 
134
      public void runSupport() {
 
135
        if(display == null || display.isDisposed()) return;
 
136
        shell.setText(title);
 
137
      }    
 
138
    });
 
139
  }
 
140
  
 
141
  public void setText(final String text) {
 
142
        Utils.execSWTThread(new AERunnable() {    
 
143
      public void runSupport() {
 
144
        if(display == null || display.isDisposed()) return;
 
145
        label.setText(text.replaceAll("&", "&&"));
 
146
      }    
 
147
    });
 
148
  }
 
149
  
 
150
  public void addOption(final String option) {
 
151
    Utils.execSWTThread(new AERunnable() {    
 
152
      public void runSupport() {
 
153
        if(display == null || display.isDisposed()) return;
 
154
        combo.add(option);
 
155
        if(combo.getItemCount() == 1) {
 
156
          combo.setText(option);
 
157
        }
 
158
      }    
 
159
    });
 
160
  }
 
161
  
 
162
  public String open() {
 
163
    if(display == null || display.isDisposed()) return null;    
 
164
    Utils.execSWTThread(new AERunnable() {    
 
165
      public void 
 
166
      runSupport() 
 
167
      {
 
168
        if(display == null || display.isDisposed()) {
 
169
                return;    
 
170
        }
 
171
          try{
 
172
                  shell.open();
 
173
            while (!shell.isDisposed())
 
174
              if (!display.readAndDispatch()) display.sleep();
 
175
                  
 
176
          }catch( Throwable e ){
 
177
                  
 
178
                  Debug.printStackTrace( e );
 
179
          }
 
180
      }    
 
181
    });
 
182
    
 
183
    return result;
 
184
  }
 
185
  
 
186
}