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.SimpleMenuBar;
|
---|
10 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
11 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
12 | import org.greenstone.gatherer.util.Utility;
|
---|
13 | import org.w3c.dom.*;
|
---|
14 |
|
---|
15 | /** 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. */
|
---|
16 | public class LockFileDialog
|
---|
17 | extends ModalDialog {
|
---|
18 |
|
---|
19 | private int choice;
|
---|
20 | private Document document;
|
---|
21 | private JButton cancel_button;
|
---|
22 | private JButton ok_button;
|
---|
23 | private LockFileDialog self;
|
---|
24 |
|
---|
25 | static final public int NO_OPTION = 0;
|
---|
26 | static final public int YES_OPTION = 1;
|
---|
27 |
|
---|
28 | static final private Dimension LABEL_SIZE = new Dimension(100, 25);
|
---|
29 | static final private Dimension SIZE = new Dimension(500, 250);
|
---|
30 | static final private int ICON_SIZE = 30;
|
---|
31 |
|
---|
32 | public LockFileDialog(JFrame parent, String name, File lock_file) {
|
---|
33 | super(parent, Dictionary.newget("LockFileDialog.Title"), true);
|
---|
34 | setSize(SIZE);
|
---|
35 | setJMenuBar(new SimpleMenuBar("openingacollection"));
|
---|
36 | this.self = this;
|
---|
37 |
|
---|
38 | // Parse the lock file, but do so quietly so that if the XML is poorly formed it doesn't show exception.
|
---|
39 | document = Utility.parse(lock_file, false);
|
---|
40 |
|
---|
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();
|
---|
49 | Dictionary.setText(title_one_label, "General.Warning");
|
---|
50 | JLabel title_two_label = new JLabel();
|
---|
51 | Dictionary.setText(title_two_label, "LockFileDialog.Lockfile_Message_One");
|
---|
52 | JPanel central_pane = new JPanel();
|
---|
53 |
|
---|
54 | JPanel name_pane = new JPanel();
|
---|
55 | JLabel name_label = new JLabel();
|
---|
56 | name_label.setPreferredSize(LABEL_SIZE);
|
---|
57 | Dictionary.setText(name_label, "LockFileDialog.Name");
|
---|
58 | JTextField name_field = new JTextField(name);
|
---|
59 | name_field.setEditable(false);
|
---|
60 | name_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
|
---|
61 |
|
---|
62 | JPanel person_pane = new JPanel();
|
---|
63 | JLabel person_label = new JLabel();
|
---|
64 | person_label.setPreferredSize(LABEL_SIZE);
|
---|
65 | Dictionary.setText(person_label, "LockFileDialog.User");
|
---|
66 | JTextField person_field = new JTextField(getValue("User"));
|
---|
67 | person_field.setEditable(false);
|
---|
68 | person_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
|
---|
69 |
|
---|
70 | JPanel machine_pane = new JPanel();
|
---|
71 | JLabel machine_label = new JLabel();
|
---|
72 | machine_label.setPreferredSize(LABEL_SIZE);
|
---|
73 | Dictionary.setText(machine_label, "LockFileDialog.Machine");
|
---|
74 | JTextField machine_field = new JTextField(getValue("Machine"));
|
---|
75 | machine_field.setEditable(false);
|
---|
76 | machine_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
|
---|
77 |
|
---|
78 | JPanel created_pane = new JPanel();
|
---|
79 | JLabel created_label = new JLabel();
|
---|
80 | created_label.setPreferredSize(LABEL_SIZE);
|
---|
81 | Dictionary.setText(created_label, "LockFileDialog.Date");
|
---|
82 | JTextField created_field = new JTextField(getValue("Date"));
|
---|
83 | created_field.setEditable(false);
|
---|
84 | created_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
|
---|
85 |
|
---|
86 | JLabel central_label = new JLabel();
|
---|
87 | Dictionary.setText(central_label, "LockFileDialog.Lockfile_Message_Two");
|
---|
88 | JPanel button_pane = new JPanel();
|
---|
89 | ok_button = new JButton();
|
---|
90 | Dictionary.setBoth(ok_button, "General.OK", "LockFileDialog.OK_Tooltip");
|
---|
91 | cancel_button = new JButton();
|
---|
92 | Dictionary.setBoth(cancel_button, "General.Cancel", "LockFileDialog.Cancel_Tooltip");
|
---|
93 |
|
---|
94 | // Connection
|
---|
95 | ok_button.addActionListener(new MyActionListener());
|
---|
96 | cancel_button.addActionListener(new MyActionListener());
|
---|
97 |
|
---|
98 | // Layout
|
---|
99 | icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,10));
|
---|
100 |
|
---|
101 | title_pane.setLayout(new GridLayout(2,1,0,5));
|
---|
102 | title_pane.add(title_one_label);
|
---|
103 | title_pane.add(title_two_label);
|
---|
104 |
|
---|
105 | upper_pane.setLayout(new BorderLayout());
|
---|
106 | upper_pane.add(icon_label, BorderLayout.WEST);
|
---|
107 | upper_pane.add(title_pane, BorderLayout.CENTER);
|
---|
108 |
|
---|
109 | name_pane.setLayout(new BorderLayout());
|
---|
110 | name_pane.add(name_label, BorderLayout.WEST);
|
---|
111 | name_pane.add(name_field, BorderLayout.CENTER);
|
---|
112 |
|
---|
113 | person_pane.setLayout(new BorderLayout());
|
---|
114 | person_pane.add(person_label, BorderLayout.WEST);
|
---|
115 | person_pane.add(person_field, BorderLayout.CENTER);
|
---|
116 |
|
---|
117 | machine_pane.setLayout(new BorderLayout());
|
---|
118 | machine_pane.add(machine_label, BorderLayout.WEST);
|
---|
119 | machine_pane.add(machine_field, BorderLayout.CENTER);
|
---|
120 |
|
---|
121 | created_pane.setLayout(new BorderLayout());
|
---|
122 | created_pane.add(created_label, BorderLayout.WEST);
|
---|
123 | created_pane.add(created_field, BorderLayout.CENTER);
|
---|
124 |
|
---|
125 | central_pane.setLayout(new GridLayout(5,1,0,5));
|
---|
126 | central_pane.add(name_pane);
|
---|
127 | central_pane.add(person_pane);
|
---|
128 | central_pane.add(machine_pane);
|
---|
129 | central_pane.add(created_pane);
|
---|
130 | central_pane.add(central_label);
|
---|
131 |
|
---|
132 | button_pane.setLayout(new GridLayout(1,2,5,0));
|
---|
133 | button_pane.add(ok_button);
|
---|
134 | button_pane.add(cancel_button);
|
---|
135 |
|
---|
136 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
137 | content_pane.setLayout(new BorderLayout());
|
---|
138 | content_pane.add(upper_pane, BorderLayout.NORTH);
|
---|
139 | content_pane.add(central_pane, BorderLayout.CENTER);
|
---|
140 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
141 |
|
---|
142 | // Display
|
---|
143 | if (parent != null) {
|
---|
144 | Rectangle frame_bounds = parent.getBounds();
|
---|
145 | setLocation(frame_bounds.x + (frame_bounds.width - SIZE.width) / 2, frame_bounds.y + (frame_bounds.height - SIZE.height) / 2);
|
---|
146 | }
|
---|
147 | else {
|
---|
148 | Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
149 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public int getChoice() {
|
---|
154 | setVisible(true);
|
---|
155 | return choice;
|
---|
156 | }
|
---|
157 |
|
---|
158 | private String getValue(String name) {
|
---|
159 | String value = "";
|
---|
160 | if(document != null) {
|
---|
161 | try {
|
---|
162 | Element value_element = (Element) MSMUtils.getNodeFromNamed(document.getDocumentElement(), name);
|
---|
163 | value = MSMUtils.getValue(value_element);
|
---|
164 | }
|
---|
165 | catch (Exception error) {
|
---|
166 | }
|
---|
167 | }
|
---|
168 | else {
|
---|
169 | value = Dictionary.newget("LockFileDialog.Error");
|
---|
170 | }
|
---|
171 | return value;
|
---|
172 | }
|
---|
173 |
|
---|
174 | private class MyActionListener
|
---|
175 | implements ActionListener {
|
---|
176 | public void actionPerformed(ActionEvent event) {
|
---|
177 | if(event.getSource() == ok_button) {
|
---|
178 | choice = YES_OPTION;
|
---|
179 | }
|
---|
180 | else {
|
---|
181 | choice = NO_OPTION;
|
---|
182 | }
|
---|
183 | self.dispose();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|