forked from geodmms/xdgnjobs

?? ?
2011-11-01 fc37383e498508fd3d40e39def99fc684e6f41f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package com.ximple.eofms;
 
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.awl.DefaultWizard;
import org.awl.DefaultWizardPageDescriptor;
import org.awl.NavigationAuthorization;
import org.awl.Wizard;
import org.awl.WizardPageDescriptor;
import org.awl.demo.AwlDemo;
import org.awl.header.AbstractWizardHeader;
import org.awl.header.EclipseWizardHeader;
import org.awl.message.MessageLevel;
 
public class XQuartzJobWizard extends DefaultWizard {
    static Log logger = LogFactory.getLog(XQuartzJobWizard.class);
 
    /**
     * header type none
     */
    private static final String HEADER_NONE = "none";
 
    /**
     * header type classic
     */
    private static final String HEADER_CLASSIC = "classic";
 
    /**
     * header type eclipse
     */
    private static final String HEADER_ECLIPSE = "eclipse";
 
    /**
     * first page
     */
    private WizardPageDescriptor firstPage = null;
 
    /**
     * second page
     */
    private WizardPageDescriptor secondPage = null;
 
    /**
     * third page
     */
    private WizardPageDescriptor thirdPage = null;
 
    /**
     * forth page
     */
    private WizardPageDescriptor fourthPage = null;
 
    /**
     * Creates a new instance of AwlDemo
     */
    public XQuartzJobWizard(JFrame frame) {
        super(frame);
 
        AbstractWizardHeader header = new EclipseWizardHeader();
        header.getIconLabel().setIcon(new ImageIcon(AwlDemo.class.getResource("/org/awl/rc/install.png")));
        this.setHeader(header);
 
        final ResourceBundle rb = ResourceBundle.getBundle(XQuartzJobWizard.class.getName());
 
        this.setTitle(rb.getString("title"));
 
        /** first page */
        this.firstPage = new DefaultWizardPageDescriptor();
        firstPage.setTitle(rb.getString("first.title"));
        firstPage.setDescription(rb.getString("first.description"));
 
        JLabel firstLabel = new JLabel(rb.getString("first.label.text"));
        firstLabel.setVerticalAlignment(SwingConstants.TOP);
        firstLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        firstPage.setComponent(firstLabel);
 
        firstPage.setNextDescriptorId("2");
 
        /** second page */
        this.secondPage = new DefaultWizardPageDescriptor();
        secondPage.setTitle(rb.getString("second.title"));
        secondPage.setDescription(rb.getString("second.description"));
 
        secondPage.setMessage(rb.getString("second.messageContent"), MessageLevel.INFO);
 
        URL licenseUrl = null;
        try {
            licenseUrl = new URL("http://www.gnu.org/licenses/lgpl-2.1.txt");
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
 
        JEditorPane licensePane = null;
        if (licenseUrl == null) {
            licensePane = new JEditorPane();
            licensePane.setText("license LGPL 2.1");
        } else {
            try {
                licensePane = new JEditorPane(licenseUrl);
            }
            catch (IOException e) {
                licensePane = new JEditorPane();
                licensePane.setText("license LGPL 2.1");
            }
        }
 
        licensePane.setPreferredSize(new Dimension(630, 350));
 
        final JRadioButton radioAcceptLicense = new JRadioButton(rb.getString("second.licenseAccepted"));
        final JRadioButton radioRefuseLicense = new JRadioButton(rb.getString("second.licenseRefused"));
 
        ButtonGroup licenseButtonGroup = new ButtonGroup();
        licenseButtonGroup.add(radioAcceptLicense);
        licenseButtonGroup.add(radioRefuseLicense);
 
        ChangeListener changeListener = new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                secondPage.setNextPageAuthorization(
                    radioAcceptLicense.isSelected() ? NavigationAuthorization.DEFAULT :
                        NavigationAuthorization.FORBIDDEN);
            }
        };
 
        radioAcceptLicense.addChangeListener(changeListener);
        radioRefuseLicense.addChangeListener(changeListener);
 
        radioRefuseLicense.setSelected(true);
 
        JPanel secondPageComponent = new JPanel();
        GridBagLayout secondPageLayout = new GridBagLayout();
        secondPageComponent.setLayout(secondPageLayout);
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0f;
        gbc.weighty = 1.0f;
        JScrollPane scrollLicense = new JScrollPane(licensePane);
        scrollLicense.setBorder(BorderFactory.createLoweredBevelBorder());
        secondPageComponent.add(scrollLicense, gbc);
 
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0f;
        gbc.weighty = 0.0f;
        secondPageComponent.add(new JToolBar.Separator(new Dimension(10, 10)), gbc);
 
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        secondPageComponent.add(radioAcceptLicense, gbc);
 
        gbc.gridx = 1;
        gbc.gridy = 4;
        secondPageComponent.add(radioRefuseLicense, gbc);
 
        secondPageComponent.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
        secondPage.setComponent(secondPageComponent);
 
        secondPage.setPreviousDescriptorId("1");
        secondPage.setNextDescriptorId("3");
 
        /** third page */
        final JTextField locationField = new JTextField(20);
        final Action chooseLocation = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                File current = null;
                String text = locationField.getText();
                if (text != null && text.trim().length() > 0) {
                    try {
                        current = new File(text);
                        if (!current.exists()) {
                            current = null;
                        }
                    }
                    catch (Exception ex) {
                    }
                }
 
                JFileChooser chooser = new JFileChooser(current);
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
                    public boolean accept(File f) {
                        boolean result = false;
 
                        if (f != null && f.isDirectory()) {
                            result = true;
                        }
 
                        return result;
                    }
 
                    public String getDescription() {
                        return rb.getString("third.fileChooser.description");
                    }
                });
                chooser.setMultiSelectionEnabled(false);
                int answer = chooser.showDialog(XQuartzJobWizard.this, rb.getString("third.fileChooser.selectLabel"));
 
                if (answer == JFileChooser.CANCEL_OPTION) {
                    locationField.setText("");
                    thirdPage.setMessage(rb.getString("third.messageContent"), MessageLevel.WARN);
                } else // aprove
                {
                    locationField.setText(chooser.getSelectedFile().getPath());
                }
            }
        };
        this.thirdPage = new DefaultWizardPageDescriptor() {
            public void displayingPanel(Wizard wizard) {
                /** open file dialog box */
                if (locationField.getText().trim().length() == 0) {
                    chooseLocation.actionPerformed(null);
                }
            }
        };
        chooseLocation.putValue(Action.SMALL_ICON, new ImageIcon(AwlDemo.class.getResource("/org/awl/rc/folder.png")));
        thirdPage.setNextPageAuthorization(NavigationAuthorization.FORBIDDEN);
 
        locationField.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) { }
 
            public void insertUpdate(DocumentEvent e) {
                this.changeNavigation();
            }
 
            public void removeUpdate(DocumentEvent e) {
                this.changeNavigation();
            }
 
            private void changeNavigation() {
                thirdPage.setNextPageAuthorization(locationField.getText().trim().length() > 0 ? NavigationAuthorization.DEFAULT :
                    NavigationAuthorization.FORBIDDEN);
            }
        });
 
        thirdPage.setTitle(rb.getString("third.title"));
        thirdPage.setDescription(rb.getString("third.description"));
 
        JPanel thirdSubPanel = new JPanel();
 
        JPanel thirdPanel = new JPanel();
        JLabel installLocationLabel = new JLabel(rb.getString("third.label.text"));
        installLocationLabel.setHorizontalAlignment(SwingConstants.LEFT);
        thirdSubPanel.add(installLocationLabel);
        locationField.setEnabled(false);
        locationField.setHorizontalAlignment(SwingConstants.LEFT);
        thirdSubPanel.add(locationField);
        JButton locationButton = new JButton(chooseLocation);
        locationButton.setHorizontalAlignment(SwingConstants.LEFT);
        Dimension buttonPrefSize = new Dimension(locationButton.getPreferredSize());
        buttonPrefSize.height = locationField.getPreferredSize().height;
//        buttonPrefSize.width = ((Icon)chooseLocation.getValue(Action.SMALL_ICON)).getIconWidth() + 4;
        locationButton.setPreferredSize(buttonPrefSize);
        thirdSubPanel.add(locationButton);
 
        thirdSubPanel.setAlignmentX(0.0f);
        thirdPanel.setAlignmentX(0.0f);
 
        thirdPanel.setLayout(new GridBagLayout());
 
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 0.0f;
        gbc.weighty = 0.0f;
        gbc.fill = GridBagConstraints.NONE;
        thirdPanel.add(thirdSubPanel, gbc);
 
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.weightx = 1.0f;
        gbc.weighty = 1.0f;
        gbc.fill = GridBagConstraints.BOTH;
        thirdPanel.add(new JToolBar.Separator(new Dimension(5, 5)), gbc);
 
        thirdPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
        thirdPage.setComponent(thirdPanel);
 
        thirdPage.setPreviousDescriptorId("2");
        thirdPage.setNextDescriptorId("4");
 
        /** fourth page */
        final JProgressBar installProgressBar = new JProgressBar();
        final JLabel progressLabel = new JLabel(rb.getString("fourth.inProgress.text"));
 
        this.fourthPage = new DefaultWizardPageDescriptor() {
            public void displayingPanel(Wizard wizard) {
                /* begin installation */
                this.setPreviousPageAuthorization(NavigationAuthorization.FORBIDDEN);
                this.setFinishAuthorization(NavigationAuthorization.FORBIDDEN);
 
                /* begin new thread */
                Runnable runnable = new Runnable() {
                    public void run() {
                        while (installProgressBar.getValue() < installProgressBar.getMaximum()) {
                            Runnable r = new Runnable() {
                                public void run() { installProgressBar.setValue(installProgressBar.getValue() + 1); }
                            };
                            SwingUtilities.invokeLater(r);
 
                            try {
                                Thread.sleep(50);
                            }
                            catch (InterruptedException e) {
                                Runnable r1 = new Runnable() {
                                    public void run() { installProgressBar.setValue(installProgressBar.getMaximum()); }
                                };
                                SwingUtilities.invokeLater(r1);
 
                                break;
                            }
                        }
 
                        Runnable r2 = new Runnable() {
                            public void run() { progressLabel.setText(rb.getString("fourth.finished.text")); }
                        };
                        SwingUtilities.invokeLater(r2);
 
                        setFinishAuthorization(NavigationAuthorization.DEFAULT);
                        setCancelAuthorization(NavigationAuthorization.FORBIDDEN);
                    }
                };
 
                new Thread(runnable).start();
            }
        };
        fourthPage.setTitle(rb.getString("fourth.title"));
        fourthPage.setDescription(rb.getString("fourth.description"));
 
        JPanel fourthPanel = new JPanel();
        BoxLayout fourthLayout = new BoxLayout(fourthPanel, BoxLayout.PAGE_AXIS);
        fourthPanel.setLayout(fourthLayout);
 
        fourthPanel.add(installProgressBar);
        fourthPanel.add(progressLabel);
 
        fourthPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
        fourthPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
        fourthPage.setComponent(fourthPanel);
 
        fourthPage.setPreviousDescriptorId("3");
 
        /** register */
        this.addPage(firstPage, "1");
        this.addPage(secondPage, "2");
        this.addPage(thirdPage, "3");
        this.addPage(fourthPage, "4");
    }
 
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                Wizard wizard = new XQuartzJobWizard((JFrame) null);
 
                wizard.pack();
                wizard.setVisibleOnCenterOfScreen();
            }
        };
 
        try {
            SwingUtilities.invokeLater(runnable);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}