1 | package org.greenstone.gatherer.gui;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.awt.event.*;
|
---|
5 | import java.io.*;
|
---|
6 | import javax.swing.*;
|
---|
7 | import org.greenstone.gatherer.Dictionary;
|
---|
8 | import org.greenstone.gatherer.Gatherer;
|
---|
9 | import org.greenstone.gatherer.gui.TextFieldLabel;
|
---|
10 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
11 | import org.greenstone.gatherer.util.Utility;
|
---|
12 | import org.greenstone.gatherer.gui.SimpleMenuBar;
|
---|
13 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
14 | import org.w3c.dom.*;
|
---|
15 |
|
---|
16 | /** A lock file dialog is shown whenever GLI detects an existing lock on a collection it is trying to open. The user can look at the details provided with the dialog, and decide whether to 'steal' the lock or to cancel the loading. While I would have loved to make this one of the modal-but-not-really-modal dialog boxes, it turns out this doesn't work as there is theoretically no parent frame if this dialog is created during GLI startup. So instead I'll use a boring old JDialog and hope that nobody ever asks for a help item to be added to this dialog. */
|
---|
17 | public class LockFileDialog
|
---|
18 | extends JDialog {
|
---|
19 | // extends ModalDialog {
|
---|
20 |
|
---|
21 | private int choice;
|
---|
22 | private Document document;
|
---|
23 | private JButton cancel_button;
|
---|
24 | private JButton ok_button;
|
---|
25 | private LockFileDialog self;
|
---|
26 |
|
---|
27 | static final public int NO_OPTION = 0;
|
---|
28 | static final public int YES_OPTION = 1;
|
---|
29 |
|
---|
30 | static final private Dimension LABEL_SIZE = new Dimension(100, 25);
|
---|
31 | static final private Dimension SIZE = new Dimension(500, 250);
|
---|
32 | static final private int ICON_SIZE = 30;
|
---|
33 |
|
---|
34 | public LockFileDialog(JFrame parent, String name, File lock_file) {
|
---|
35 | super(parent, get("Title"), true);
|
---|
36 | setSize(SIZE);
|
---|
37 | setJMenuBar(new SimpleMenuBar("2.3"));
|
---|
38 | this.self = this;
|
---|
39 | // Parse the lock file, but do so quietly so that if the XML is poorly formed it doesn't show exception.
|
---|
40 | document = Utility.parse(lock_file, false);
|
---|
41 | // Creation
|
---|
42 | JPanel content_pane = (JPanel) getContentPane();
|
---|
43 | JPanel upper_pane = new JPanel();
|
---|
44 | ImageIcon icon = Utility.getImage("lcolicn.gif");
|
---|
45 | ImageIcon scaled_icon = new ImageIcon(icon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_DEFAULT));
|
---|
46 | JLabel icon_label = new JLabel(scaled_icon);
|
---|
47 | JPanel title_pane = new JPanel();
|
---|
48 | JLabel title_one_label = new JLabel(get("General.Warning"));
|
---|
49 | JLabel title_two_label = new JLabel(get("Lockfile_Message_One"));
|
---|
50 | JPanel central_pane = new JPanel();
|
---|
51 | JPanel name_pane = new JPanel();
|
---|
52 | JLabel name_label = new JLabel(get("Name"));
|
---|
53 | name_label.setPreferredSize(LABEL_SIZE);
|
---|
54 | TextFieldLabel name_field = new TextFieldLabel(name);
|
---|
55 | JPanel person_pane = new JPanel();
|
---|
56 | JLabel person_label = new JLabel(get("User"));
|
---|
57 | person_label.setPreferredSize(LABEL_SIZE);
|
---|
58 | TextFieldLabel person_field = new TextFieldLabel(getValue("User"));
|
---|
59 | JPanel machine_pane = new JPanel();
|
---|
60 | JLabel machine_label = new JLabel(get("Machine"));
|
---|
61 | machine_label.setPreferredSize(LABEL_SIZE);
|
---|
62 | TextFieldLabel machine_field = new TextFieldLabel(getValue("Machine"));
|
---|
63 | JPanel created_pane = new JPanel();
|
---|
64 | JLabel created_label = new JLabel(get("Date"));
|
---|
65 | created_label.setPreferredSize(LABEL_SIZE);
|
---|
66 | TextFieldLabel created_field = new TextFieldLabel(getValue("Date"));
|
---|
67 | JLabel central_label = new JLabel(get("Lockfile_Message_Two"));
|
---|
68 | JPanel button_pane = new JPanel();
|
---|
69 | ok_button = new JButton(get("General.OK"));
|
---|
70 | cancel_button = new JButton(get("General.Cancel"));
|
---|
71 | // Connection
|
---|
72 | ok_button.addActionListener(new MyActionListener());
|
---|
73 | cancel_button.addActionListener(new MyActionListener());
|
---|
74 | // Layout
|
---|
75 | icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,10));
|
---|
76 |
|
---|
77 | title_pane.setLayout(new GridLayout(2,1,0,5));
|
---|
78 | title_pane.add(title_one_label);
|
---|
79 | title_pane.add(title_two_label);
|
---|
80 |
|
---|
81 | upper_pane.setLayout(new BorderLayout());
|
---|
82 | upper_pane.add(icon_label, BorderLayout.WEST);
|
---|
83 | upper_pane.add(title_pane, BorderLayout.CENTER);
|
---|
84 |
|
---|
85 | name_pane.setLayout(new BorderLayout());
|
---|
86 | name_pane.add(name_label, BorderLayout.WEST);
|
---|
87 | name_pane.add(name_field, BorderLayout.CENTER);
|
---|
88 |
|
---|
89 | person_pane.setLayout(new BorderLayout());
|
---|
90 | person_pane.add(person_label, BorderLayout.WEST);
|
---|
91 | person_pane.add(person_field, BorderLayout.CENTER);
|
---|
92 |
|
---|
93 | machine_pane.setLayout(new BorderLayout());
|
---|
94 | machine_pane.add(machine_label, BorderLayout.WEST);
|
---|
95 | machine_pane.add(machine_field, BorderLayout.CENTER);
|
---|
96 |
|
---|
97 | created_pane.setLayout(new BorderLayout());
|
---|
98 | created_pane.add(created_label, BorderLayout.WEST);
|
---|
99 | created_pane.add(created_field, BorderLayout.CENTER);
|
---|
100 |
|
---|
101 | central_pane.setLayout(new GridLayout(5,1,0,5));
|
---|
102 | central_pane.add(name_pane);
|
---|
103 | central_pane.add(person_pane);
|
---|
104 | central_pane.add(machine_pane);
|
---|
105 | central_pane.add(created_pane);
|
---|
106 | central_pane.add(central_label);
|
---|
107 |
|
---|
108 | button_pane.setLayout(new GridLayout(1,2,5,0));
|
---|
109 | button_pane.add(ok_button);
|
---|
110 | button_pane.add(cancel_button);
|
---|
111 |
|
---|
112 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
113 | content_pane.setLayout(new BorderLayout());
|
---|
114 | content_pane.add(upper_pane, BorderLayout.NORTH);
|
---|
115 | content_pane.add(central_pane, BorderLayout.CENTER);
|
---|
116 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
117 | // Display
|
---|
118 | if(parent != null) {
|
---|
119 | Rectangle frame_bounds = parent.getBounds();
|
---|
120 | setLocation(frame_bounds.x + (frame_bounds.width - SIZE.width) / 2, frame_bounds.y + (frame_bounds.height - SIZE.height) / 2);
|
---|
121 | }
|
---|
122 | else {
|
---|
123 | Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
124 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public int getChoice() {
|
---|
129 | show();
|
---|
130 | ///ystem.err.println("Get choice... blocks here.");
|
---|
131 | ///setVisible(true);
|
---|
132 | ///ystem.err.println("Dispose recieved, return.");
|
---|
133 | return choice;
|
---|
134 | }
|
---|
135 |
|
---|
136 | private String getValue(String name) {
|
---|
137 | String value = "";
|
---|
138 | if(document != null) {
|
---|
139 | try {
|
---|
140 | Element value_element = (Element) MSMUtils.getNodeFromNamed(document.getDocumentElement(), name);
|
---|
141 | value = MSMUtils.getValue(value_element);
|
---|
142 | }
|
---|
143 | catch (Exception error) {
|
---|
144 | }
|
---|
145 | }
|
---|
146 | else {
|
---|
147 | value = get("Error");
|
---|
148 | }
|
---|
149 | return value;
|
---|
150 | }
|
---|
151 |
|
---|
152 | private class MyActionListener
|
---|
153 | implements ActionListener {
|
---|
154 | public void actionPerformed(ActionEvent event) {
|
---|
155 | if(event.getSource() == ok_button) {
|
---|
156 | choice = YES_OPTION;
|
---|
157 | }
|
---|
158 | else {
|
---|
159 | choice = NO_OPTION;
|
---|
160 | }
|
---|
161 | ///ystem.err.println("setVisible(false)");
|
---|
162 | ///self.setVisible(false);
|
---|
163 | ///ystem.err.println("dispose()");
|
---|
164 | self.dispose();
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | static public void main(String[] args) {
|
---|
169 | JFrame frame = new JFrame();
|
---|
170 | frame.setSize(640,480);
|
---|
171 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
172 | frame.show();
|
---|
173 | LockFileDialog dialog = new LockFileDialog(frame, "Test", new File(args[0]));
|
---|
174 | int choice = dialog.getChoice();
|
---|
175 | }
|
---|
176 |
|
---|
177 | static private String get(String key) {
|
---|
178 | if(key.indexOf(".") == -1) {
|
---|
179 | key = "LockFileDialog." + key;
|
---|
180 | }
|
---|
181 | return Gatherer.dictionary.get(key);
|
---|
182 | }
|
---|
183 | }
|
---|