[4293] | 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;
|
---|
[4308] | 9 | import org.greenstone.gatherer.gui.TextFieldLabel;
|
---|
[4293] | 10 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
| 11 | import org.greenstone.gatherer.util.Utility;
|
---|
[4427] | 12 | import org.greenstone.gatherer.gui.SimpleMenuBar;
|
---|
| 13 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
[4293] | 14 | import org.w3c.dom.*;
|
---|
| 15 |
|
---|
[4459] | 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. */
|
---|
[4293] | 17 | public class LockFileDialog
|
---|
[4459] | 18 | extends ModalDialog {
|
---|
[4293] | 19 |
|
---|
[4367] | 20 | private int choice;
|
---|
| 21 | private Document document;
|
---|
| 22 | private JButton cancel_button;
|
---|
| 23 | private JButton ok_button;
|
---|
| 24 | private LockFileDialog self;
|
---|
[4293] | 25 |
|
---|
[4367] | 26 | static final public int NO_OPTION = 0;
|
---|
| 27 | static final public int YES_OPTION = 1;
|
---|
[4293] | 28 |
|
---|
[4367] | 29 | static final private Dimension LABEL_SIZE = new Dimension(100, 25);
|
---|
[4427] | 30 | static final private Dimension SIZE = new Dimension(500, 250);
|
---|
[4367] | 31 | static final private int ICON_SIZE = 30;
|
---|
[4293] | 32 |
|
---|
[4367] | 33 | public LockFileDialog(JFrame parent, String name, File lock_file) {
|
---|
| 34 | super(parent, get("Title"), true);
|
---|
| 35 | setSize(SIZE);
|
---|
[4427] | 36 | setJMenuBar(new SimpleMenuBar("2.3"));
|
---|
[4367] | 37 | this.self = this;
|
---|
| 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 | // Creation
|
---|
| 41 | JPanel content_pane = (JPanel) getContentPane();
|
---|
| 42 | JPanel upper_pane = new JPanel();
|
---|
| 43 | ImageIcon icon = Utility.getImage("lcolicn.gif");
|
---|
| 44 | ImageIcon scaled_icon = new ImageIcon(icon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_DEFAULT));
|
---|
| 45 | JLabel icon_label = new JLabel(scaled_icon);
|
---|
| 46 | JPanel title_pane = new JPanel();
|
---|
| 47 | JLabel title_one_label = new JLabel(get("General.Warning"));
|
---|
| 48 | JLabel title_two_label = new JLabel(get("Lockfile_Message_One"));
|
---|
| 49 | JPanel central_pane = new JPanel();
|
---|
| 50 | JPanel name_pane = new JPanel();
|
---|
| 51 | JLabel name_label = new JLabel(get("Name"));
|
---|
| 52 | name_label.setPreferredSize(LABEL_SIZE);
|
---|
| 53 | TextFieldLabel name_field = new TextFieldLabel(name);
|
---|
| 54 | JPanel person_pane = new JPanel();
|
---|
| 55 | JLabel person_label = new JLabel(get("User"));
|
---|
| 56 | person_label.setPreferredSize(LABEL_SIZE);
|
---|
| 57 | TextFieldLabel person_field = new TextFieldLabel(getValue("User"));
|
---|
| 58 | JPanel machine_pane = new JPanel();
|
---|
| 59 | JLabel machine_label = new JLabel(get("Machine"));
|
---|
| 60 | machine_label.setPreferredSize(LABEL_SIZE);
|
---|
| 61 | TextFieldLabel machine_field = new TextFieldLabel(getValue("Machine"));
|
---|
| 62 | JPanel created_pane = new JPanel();
|
---|
| 63 | JLabel created_label = new JLabel(get("Date"));
|
---|
| 64 | created_label.setPreferredSize(LABEL_SIZE);
|
---|
| 65 | TextFieldLabel created_field = new TextFieldLabel(getValue("Date"));
|
---|
| 66 | JLabel central_label = new JLabel(get("Lockfile_Message_Two"));
|
---|
| 67 | JPanel button_pane = new JPanel();
|
---|
| 68 | ok_button = new JButton(get("General.OK"));
|
---|
| 69 | cancel_button = new JButton(get("General.Cancel"));
|
---|
| 70 | // Connection
|
---|
| 71 | ok_button.addActionListener(new MyActionListener());
|
---|
| 72 | cancel_button.addActionListener(new MyActionListener());
|
---|
| 73 | // Layout
|
---|
| 74 | icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,10));
|
---|
[4293] | 75 |
|
---|
[4367] | 76 | title_pane.setLayout(new GridLayout(2,1,0,5));
|
---|
| 77 | title_pane.add(title_one_label);
|
---|
| 78 | title_pane.add(title_two_label);
|
---|
[4293] | 79 |
|
---|
[4367] | 80 | upper_pane.setLayout(new BorderLayout());
|
---|
| 81 | upper_pane.add(icon_label, BorderLayout.WEST);
|
---|
| 82 | upper_pane.add(title_pane, BorderLayout.CENTER);
|
---|
[4293] | 83 |
|
---|
[4367] | 84 | name_pane.setLayout(new BorderLayout());
|
---|
| 85 | name_pane.add(name_label, BorderLayout.WEST);
|
---|
| 86 | name_pane.add(name_field, BorderLayout.CENTER);
|
---|
[4293] | 87 |
|
---|
[4367] | 88 | person_pane.setLayout(new BorderLayout());
|
---|
| 89 | person_pane.add(person_label, BorderLayout.WEST);
|
---|
| 90 | person_pane.add(person_field, BorderLayout.CENTER);
|
---|
[4293] | 91 |
|
---|
[4367] | 92 | machine_pane.setLayout(new BorderLayout());
|
---|
| 93 | machine_pane.add(machine_label, BorderLayout.WEST);
|
---|
| 94 | machine_pane.add(machine_field, BorderLayout.CENTER);
|
---|
[4293] | 95 |
|
---|
[4367] | 96 | created_pane.setLayout(new BorderLayout());
|
---|
| 97 | created_pane.add(created_label, BorderLayout.WEST);
|
---|
| 98 | created_pane.add(created_field, BorderLayout.CENTER);
|
---|
[4293] | 99 |
|
---|
[4367] | 100 | central_pane.setLayout(new GridLayout(5,1,0,5));
|
---|
| 101 | central_pane.add(name_pane);
|
---|
| 102 | central_pane.add(person_pane);
|
---|
| 103 | central_pane.add(machine_pane);
|
---|
| 104 | central_pane.add(created_pane);
|
---|
| 105 | central_pane.add(central_label);
|
---|
[4293] | 106 |
|
---|
[4367] | 107 | button_pane.setLayout(new GridLayout(1,2,5,0));
|
---|
| 108 | button_pane.add(ok_button);
|
---|
| 109 | button_pane.add(cancel_button);
|
---|
[4293] | 110 |
|
---|
[4367] | 111 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
| 112 | content_pane.setLayout(new BorderLayout());
|
---|
| 113 | content_pane.add(upper_pane, BorderLayout.NORTH);
|
---|
| 114 | content_pane.add(central_pane, BorderLayout.CENTER);
|
---|
| 115 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
| 116 | // Display
|
---|
| 117 | if(parent != null) {
|
---|
| 118 | Rectangle frame_bounds = parent.getBounds();
|
---|
| 119 | setLocation(frame_bounds.x + (frame_bounds.width - SIZE.width) / 2, frame_bounds.y + (frame_bounds.height - SIZE.height) / 2);
|
---|
| 120 | }
|
---|
| 121 | else {
|
---|
| 122 | Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
| 123 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[4293] | 126 |
|
---|
[4367] | 127 | public int getChoice() {
|
---|
[4459] | 128 | setVisible(true);
|
---|
[4367] | 129 | return choice;
|
---|
| 130 | }
|
---|
[4293] | 131 |
|
---|
[4367] | 132 | private String getValue(String name) {
|
---|
| 133 | String value = "";
|
---|
| 134 | if(document != null) {
|
---|
| 135 | try {
|
---|
| 136 | Element value_element = (Element) MSMUtils.getNodeFromNamed(document.getDocumentElement(), name);
|
---|
| 137 | value = MSMUtils.getValue(value_element);
|
---|
| 138 | }
|
---|
| 139 | catch (Exception error) {
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | else {
|
---|
| 143 | value = get("Error");
|
---|
| 144 | }
|
---|
| 145 | return value;
|
---|
| 146 | }
|
---|
[4293] | 147 |
|
---|
[4367] | 148 | private class MyActionListener
|
---|
| 149 | implements ActionListener {
|
---|
| 150 | public void actionPerformed(ActionEvent event) {
|
---|
| 151 | if(event.getSource() == ok_button) {
|
---|
| 152 | choice = YES_OPTION;
|
---|
| 153 | }
|
---|
| 154 | else {
|
---|
| 155 | choice = NO_OPTION;
|
---|
| 156 | }
|
---|
| 157 | self.dispose();
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
[4293] | 160 |
|
---|
[4367] | 161 | static public void main(String[] args) {
|
---|
| 162 | JFrame frame = new JFrame();
|
---|
| 163 | frame.setSize(640,480);
|
---|
| 164 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
| 165 | frame.show();
|
---|
| 166 | LockFileDialog dialog = new LockFileDialog(frame, "Test", new File(args[0]));
|
---|
| 167 | int choice = dialog.getChoice();
|
---|
| 168 | }
|
---|
[4293] | 169 |
|
---|
[4367] | 170 | static private String get(String key) {
|
---|
| 171 | if(key.indexOf(".") == -1) {
|
---|
| 172 | key = "LockFileDialog." + key;
|
---|
| 173 | }
|
---|
| 174 | return Gatherer.dictionary.get(key);
|
---|
| 175 | }
|
---|
[4293] | 176 | }
|
---|