1 | /*
|
---|
2 | * SWT008_02.java
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (c) 2002, 2005 EclipseOS2 Team.
|
---|
7 | * This file is made available under the terms of the Common Public License v1.0
|
---|
8 | * which accompanies this distribution, and is available at
|
---|
9 | * http://www.eclipse.org/legal/cpl-v10.html
|
---|
10 | */
|
---|
11 |
|
---|
12 | import org.eclipse.swt.SWT;
|
---|
13 | import org.eclipse.swt.widgets.*;
|
---|
14 | import org.eclipse.swt.events.*;
|
---|
15 | import org.eclipse.swt.layout.*;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * This example demonstrates the FileDialog.
|
---|
19 | * Valid combinations are:
|
---|
20 | * <ul>
|
---|
21 | * <li>SAVE (with or without MULTI)</li>
|
---|
22 | * <li>OPEN (with or without MULTI)</li>
|
---|
23 | * </ul>
|
---|
24 | */
|
---|
25 |
|
---|
26 | public class SWT008_03 extends SWTTestCase {
|
---|
27 |
|
---|
28 | static {
|
---|
29 | STEP = "008";
|
---|
30 | TEST = "03";
|
---|
31 | DESC = "File Dialog";
|
---|
32 | }
|
---|
33 |
|
---|
34 | static final String SAVE = "Save Dialog";
|
---|
35 | static final String OPEN = "Open Dialog";
|
---|
36 | static final String MULTI = "Select Multiple";
|
---|
37 |
|
---|
38 | private Button show, exit;
|
---|
39 | // no sense in having a container holding the buttons as there are only three buttons.
|
---|
40 | private Button save, open, multi;
|
---|
41 |
|
---|
42 | final static RowLayout dialogTypeLayout = new RowLayout (SWT.VERTICAL);
|
---|
43 | static {
|
---|
44 | dialogTypeLayout.marginLeft = dialogTypeLayout.marginRight =
|
---|
45 | dialogTypeLayout.marginTop = dialogTypeLayout.marginBottom = 12;
|
---|
46 | dialogTypeLayout.spacing = 4;
|
---|
47 | }
|
---|
48 | final static RowLayout multiSelLayout = new RowLayout (SWT.VERTICAL);
|
---|
49 | static {
|
---|
50 | multiSelLayout.marginLeft = multiSelLayout.marginRight =
|
---|
51 | multiSelLayout.marginTop = multiSelLayout.marginBottom = 12;
|
---|
52 | multiSelLayout.spacing = 4;
|
---|
53 | }
|
---|
54 |
|
---|
55 | class MySelectionListener implements SelectionListener {
|
---|
56 | public void widgetDefaultSelected (SelectionEvent e) {
|
---|
57 | System.out.println("DefaultSelected: " + e.widget + ", (" + e.item +"), [" +
|
---|
58 | e.x + "," + e.y + ";" + e.width + "," + e.height + "], " +
|
---|
59 | "selected=" + ((Button)e.widget).getSelection());
|
---|
60 | }
|
---|
61 | public void widgetSelected (SelectionEvent e) {
|
---|
62 | System.out.println("Selected: " + e.widget + ", (" + e.item +"), [" +
|
---|
63 | e.x + "," + e.y + ";" + e.width + "," + e.height + "], " +
|
---|
64 | "selected=" + ((Button)e.widget).getSelection());
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static void main (String[] args) {
|
---|
69 | go (new SWT008_03());
|
---|
70 | }
|
---|
71 |
|
---|
72 | Shell createTopShell (Display display) {
|
---|
73 | return new Shell(display, SWT.SHELL_TRIM | SWT.NO_REDRAW_RESIZE);
|
---|
74 | }
|
---|
75 |
|
---|
76 | void createChoices (Composite dialogTypeComp, Composite multiSelComp)
|
---|
77 | {
|
---|
78 | save = new Button (dialogTypeComp, SWT.RADIO);
|
---|
79 | save.setText (SAVE);
|
---|
80 | save.addSelectionListener (new MySelectionListener());
|
---|
81 | open = new Button (dialogTypeComp, SWT.RADIO);
|
---|
82 | open.setText (OPEN);
|
---|
83 | open.addSelectionListener (new MySelectionListener());
|
---|
84 | multi = new Button (multiSelComp, SWT.CHECK);
|
---|
85 | multi.setText (MULTI);
|
---|
86 | multi.addSelectionListener (new MySelectionListener());
|
---|
87 | }
|
---|
88 |
|
---|
89 | void initComponents() {
|
---|
90 | shell.setLayout (new FormLayout());
|
---|
91 | setTitle (DESC + " Test");
|
---|
92 |
|
---|
93 | FormData formData = new FormData();
|
---|
94 |
|
---|
95 | Composite dialogTypeComp = new Composite (shell, SWT.NONE);
|
---|
96 | dialogTypeComp.setLayout (dialogTypeLayout);
|
---|
97 | formData.top = new FormAttachment (0, 5);
|
---|
98 | formData.left = new FormAttachment (0, 5);
|
---|
99 | formData.bottom = new FormAttachment (75, -5);
|
---|
100 | formData.right = new FormAttachment (33, -5);
|
---|
101 | dialogTypeComp.setLayoutData (formData);
|
---|
102 |
|
---|
103 | Composite multiSelComp = new Composite (shell, SWT.NONE);
|
---|
104 | multiSelComp.setLayout (multiSelLayout);
|
---|
105 | formData = new FormData();
|
---|
106 | formData.top = new FormAttachment (0, 5);
|
---|
107 | formData.left = new FormAttachment (dialogTypeComp, 5);
|
---|
108 | formData.bottom = new FormAttachment (75, -5);
|
---|
109 | formData.right = new FormAttachment (66, -5);
|
---|
110 | multiSelComp.setLayoutData (formData);
|
---|
111 |
|
---|
112 | Composite shellControls = new Composite (shell, SWT.NONE);
|
---|
113 | shellControls.setLayout (new RowLayout (SWT.HORIZONTAL));
|
---|
114 | formData = new FormData();
|
---|
115 | formData.top = new FormAttachment (dialogTypeComp, 5);
|
---|
116 | formData.left = new FormAttachment (33, 5);
|
---|
117 | formData.bottom = new FormAttachment (100, -5);
|
---|
118 | formData.right = new FormAttachment (100, -5);
|
---|
119 | shellControls.setLayoutData (formData);
|
---|
120 |
|
---|
121 | show = new Button (shellControls, SWT.PUSH);
|
---|
122 | show.setText ("S&how");
|
---|
123 | show.setLayoutData (new RowData (50, 25));
|
---|
124 | show.addSelectionListener (new SelectionAdapter() {
|
---|
125 | public void widgetSelected (SelectionEvent e) {
|
---|
126 | int dialogTypeBits = SWT.NONE, multiSelBits = SWT.NONE;
|
---|
127 |
|
---|
128 | if (save != null && save.getSelection()) {
|
---|
129 | dialogTypeBits = SWT.SAVE;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (open != null && open.getSelection()) {
|
---|
133 | dialogTypeBits = SWT.OPEN;
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (multi != null && multi.getSelection()) {
|
---|
137 | multiSelBits = SWT.MULTI;
|
---|
138 | }
|
---|
139 |
|
---|
140 | FileDialog fd = new FileDialog (shell, dialogTypeBits | multiSelBits);
|
---|
141 | String dialogTitle = "", file = null;
|
---|
142 | switch (dialogTypeBits) {
|
---|
143 | case SWT.OPEN:
|
---|
144 | if (multiSelBits == SWT.MULTI) dialogTitle = OPEN + " " + MULTI;
|
---|
145 | else dialogTitle = OPEN;
|
---|
146 | break;
|
---|
147 | case SWT.SAVE:
|
---|
148 | if (multiSelBits == SWT.MULTI) dialogTitle = SAVE + " " + MULTI;
|
---|
149 | else dialogTitle = SAVE;
|
---|
150 | break;
|
---|
151 | default: break;
|
---|
152 | }
|
---|
153 | fd.setText (dialogTitle);
|
---|
154 | file = fd.open();
|
---|
155 | String[] stringArray = fd.getFileNames();
|
---|
156 | if (stringArray != null && stringArray.length > 0) {
|
---|
157 | say ("File selected list: ");
|
---|
158 | for (int i = 0; i < stringArray.length; i++) {
|
---|
159 | say (" " + stringArray[i]);
|
---|
160 | }
|
---|
161 | } else {
|
---|
162 | say ("File selected: " + fd.getFileName());
|
---|
163 | }
|
---|
164 | stringArray = fd.getFilterExtensions();
|
---|
165 | if (stringArray != null && stringArray.length > 0) {
|
---|
166 | say ("Filter extension list: ");
|
---|
167 | for (int i = 0; i < stringArray.length; i++) {
|
---|
168 | say (" " + stringArray[i]);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | stringArray = fd.getFilterNames();
|
---|
172 | if (stringArray != null && stringArray.length > 0) {
|
---|
173 | say ("Filter name list: ");
|
---|
174 | for (int i = 0; i < stringArray.length; i++) {
|
---|
175 | say (" " + stringArray[i]);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (fd.getFilterPath() != null && fd.getFilterPath().length() > 0) {
|
---|
180 | say ("Filter path " + fd.getFilterPath());
|
---|
181 | }
|
---|
182 |
|
---|
183 | fd = null;
|
---|
184 | MessageBox mb = new MessageBox (shell, SWT.OK);
|
---|
185 | mb.setText (DESC + " test");
|
---|
186 | mb.setMessage ("file/filter returned " + file);
|
---|
187 | mb.open();
|
---|
188 | mb = null;
|
---|
189 | /*
|
---|
190 | System.out.println(strButton + ", " + strIcon + ", " + strModal + " - " + getReturnString(mbrc) + ".");
|
---|
191 | */
|
---|
192 | }
|
---|
193 | });
|
---|
194 | exit = new Button (shellControls, SWT.PUSH);
|
---|
195 | exit.setText ("E&xit");
|
---|
196 | exit.setLayoutData (new RowData (50, 25));
|
---|
197 | exit.addSelectionListener (new SelectionAdapter() {
|
---|
198 | public void widgetSelected (SelectionEvent e) {
|
---|
199 | shell.getDisplay().dispose();
|
---|
200 | }
|
---|
201 | });
|
---|
202 |
|
---|
203 | createChoices (dialogTypeComp, multiSelComp);
|
---|
204 |
|
---|
205 | shell.pack();
|
---|
206 | }
|
---|
207 |
|
---|
208 | private int getFlag (String buttonText)
|
---|
209 | {
|
---|
210 | if (buttonText.equals (SAVE))
|
---|
211 | return SWT.SAVE;
|
---|
212 | if (buttonText.equals (OPEN))
|
---|
213 | return SWT.OPEN;
|
---|
214 | if (buttonText.equals (MULTI))
|
---|
215 | return SWT.MULTI;
|
---|
216 |
|
---|
217 | return SWT.NONE;
|
---|
218 | }
|
---|
219 |
|
---|
220 | }
|
---|